Skip to content

Commit

Permalink
BAH-3773 | Add. link patient abha identifier after successful linking
Browse files Browse the repository at this point in the history
  • Loading branch information
SanoferSameera committed Apr 25, 2024
1 parent e5757fb commit 47a93ae
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/In.ProjectEKA.HipService/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static class Constants
public static readonly string PATH_OPENMRS_HITYPE = "ws/rest/v1/hip/";
public static readonly string CONFIG_KEY = "OpenMrs";
public static readonly string PATH_OPENMRS_LGD_CODE = "ws/rest/v1/hip/lgdCode";
public static readonly string PATH_OPENMRS_UPDATE_IDENTIFIER = "ws/rest/v1/hip/existingPatients/update";

public const string CORRELATION_ID = "CORRELATION-ID";
public const string PATH_PATIENT_PROFILE_ON_SHARE = "/" + CURRENT_VERSION + "/patients/profile/on-share";
Expand Down
14 changes: 14 additions & 0 deletions src/In.ProjectEKA.HipService/Discovery/DiscoveryReqMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using In.ProjectEKA.HipLibrary.Patient.Model;
using In.ProjectEKA.HipService.Common.Model;
using In.ProjectEKA.HipService.Link.Model;
using In.ProjectEKA.HipService.UserAuth.Model;
using Microsoft.AspNetCore.Http;

namespace In.ProjectEKA.HipService.Discovery
{
public static class DiscoveryReqMap {
public static Dictionary<string, string> AbhaIdentifierMap = new Dictionary<string, string>();
}
}
7 changes: 6 additions & 1 deletion src/In.ProjectEKA.HipService/Discovery/PatientController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace In.ProjectEKA.HipService.Discovery
using System.Linq;

namespace In.ProjectEKA.HipService.Discovery
{
using System;
using System.Net;
Expand Down Expand Up @@ -71,6 +73,9 @@ public async Task GetPatientCareContext(DiscoveryRequest request, string correla
new DiscoveryResponse(request.RequestId,
error == null ? HttpStatusCode.OK : HttpStatusCode.NotFound,
error == null ? SuccessMessage : ErrorMessage));
var abhaNumberIdentifier = request.Patient?.VerifiedIdentifiers.FirstOrDefault(id => id.Type == IdentifierType.NDHM_HEALTH_NUMBER) ;
DiscoveryReqMap.AbhaIdentifierMap.Add(patientId, abhaNumberIdentifier?.Value);

Log.Information("new GatewayDiscoveryRepresentation" + gatewayDiscoveryRepresentation);
Log.Information("Sending data to gateway");
Log.Information($"Response about to be send for {request.RequestId} with {@response?.Patient}");
Expand Down
35 changes: 33 additions & 2 deletions src/In.ProjectEKA.HipService/Link/LinkPatient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using In.ProjectEKA.HipService.OpenMrs;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace In.ProjectEKA.HipService.Link
{
using System;
Expand All @@ -11,7 +15,8 @@ namespace In.ProjectEKA.HipService.Link
using Logger;
using Microsoft.Extensions.Options;
using Model;

using static In.ProjectEKA.HipService.Discovery.DiscoveryReqMap;

public class LinkPatient
{
private readonly IDiscoveryRequestRepository discoveryRequestRepository;
Expand All @@ -20,21 +25,24 @@ public class LinkPatient
private readonly IPatientRepository patientRepository;
private readonly IPatientVerification patientVerification;
private readonly ReferenceNumberGenerator referenceNumberGenerator;
private readonly OpenMrsClient openMrsClient;

public LinkPatient(
ILinkPatientRepository linkPatientRepository,
IPatientRepository patientRepository,
IPatientVerification patientVerification,
ReferenceNumberGenerator referenceNumberGenerator,
IDiscoveryRequestRepository discoveryRequestRepository,
IOptions<OtpServiceConfiguration> otpService)
IOptions<OtpServiceConfiguration> otpService,
OpenMrsClient openMrsClient)
{
this.linkPatientRepository = linkPatientRepository;
this.patientRepository = patientRepository;
this.patientVerification = patientVerification;
this.referenceNumberGenerator = referenceNumberGenerator;
this.discoveryRequestRepository = discoveryRequestRepository;
this.otpService = otpService;
this.openMrsClient = openMrsClient;
}

public virtual async Task<ValueTuple<PatientLinkEnquiryRepresentation, ErrorRepresentation>> LinkPatients(
Expand Down Expand Up @@ -178,7 +186,30 @@ private async Task<bool> SaveLinkedAccounts(LinkEnquires linkEnquires,string pat
(patientUuid!=null?Guid.Parse(patientUuid): Guid.Empty)
)
.ConfigureAwait(false);
LinkAbhaIdentifier(patientUuid, linkEnquires.ConsentManagerUserId);
return linkedAccount.HasValue;

}

private async void LinkAbhaIdentifier(string patientUuid, string abhaAddress)
{
var abhaNumber = AbhaIdentifierMap[abhaAddress];

var json = JsonConvert.SerializeObject(new PatientAbhaIdentifier(abhaNumber, abhaAddress), new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
});
var resp = await openMrsClient.PostAsync(
$"{Constants.PATH_OPENMRS_UPDATE_IDENTIFIER}?patientUuid={patientUuid}",
json
)
.ConfigureAwait(false);
if (!resp.IsSuccessStatusCode)
Log.Error("Errored in linking the abha identifier to the patient");
}

public async Task<bool> SaveInitiatedLinkRequest(string requestId, string transactionId,
Expand Down
14 changes: 14 additions & 0 deletions src/In.ProjectEKA.HipService/Link/Model/PatientAbhaIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace In.ProjectEKA.HipService.Link.Model
{
public class PatientAbhaIdentifier
{
public string abhaNumber { get; }
public string abhaAddress { get; }

public PatientAbhaIdentifier(string abhaNumber, string abhaAddress)
{
this.abhaNumber = abhaNumber;
this.abhaAddress = abhaAddress;
}
}
}
19 changes: 19 additions & 0 deletions src/In.ProjectEKA.HipService/OpenMrs/OpenMrsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ public async Task<HttpResponseMessage> GetAsync(string openMrsUrl)

return responseMessage;
}

public async Task<HttpResponseMessage> PostAsync(string openMrsUrl, string jsonContent)
{
var httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");


var responseMessage = await httpClient.PostAsync(Path.Join(configuration.Url, openMrsUrl), httpContent);

if (!responseMessage.IsSuccessStatusCode)
{
var error = await responseMessage.Content.ReadAsStringAsync();
Log.Error(
$"Failure in getting the data from OpenMrs with status code {responseMessage.StatusCode}" +
$" {error}");
throw new OpenMrsConnectionException();
}

return responseMessage;
}

private void SettingUpHeaderAuthorization()
{
Expand Down

0 comments on commit 47a93ae

Please sign in to comment.