diff --git a/src/In.ProjectEKA.HipService/Common/Constants.cs b/src/In.ProjectEKA.HipService/Common/Constants.cs index 52c4a52ce..f791e5338 100644 --- a/src/In.ProjectEKA.HipService/Common/Constants.cs +++ b/src/In.ProjectEKA.HipService/Common/Constants.cs @@ -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"; diff --git a/src/In.ProjectEKA.HipService/Discovery/DiscoveryReqMap.cs b/src/In.ProjectEKA.HipService/Discovery/DiscoveryReqMap.cs new file mode 100644 index 000000000..14b1ba805 --- /dev/null +++ b/src/In.ProjectEKA.HipService/Discovery/DiscoveryReqMap.cs @@ -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 AbhaIdentifierMap = new Dictionary(); + } +} \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Discovery/PatientController.cs b/src/In.ProjectEKA.HipService/Discovery/PatientController.cs index f65819a07..9aa26f628 100644 --- a/src/In.ProjectEKA.HipService/Discovery/PatientController.cs +++ b/src/In.ProjectEKA.HipService/Discovery/PatientController.cs @@ -1,4 +1,6 @@ -namespace In.ProjectEKA.HipService.Discovery +using System.Linq; + +namespace In.ProjectEKA.HipService.Discovery { using System; using System.Net; @@ -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}"); diff --git a/src/In.ProjectEKA.HipService/Link/LinkPatient.cs b/src/In.ProjectEKA.HipService/Link/LinkPatient.cs index 54a64a4ca..ecf25848b 100644 --- a/src/In.ProjectEKA.HipService/Link/LinkPatient.cs +++ b/src/In.ProjectEKA.HipService/Link/LinkPatient.cs @@ -1,3 +1,7 @@ +using In.ProjectEKA.HipService.OpenMrs; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + namespace In.ProjectEKA.HipService.Link { using System; @@ -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; @@ -20,6 +25,7 @@ public class LinkPatient private readonly IPatientRepository patientRepository; private readonly IPatientVerification patientVerification; private readonly ReferenceNumberGenerator referenceNumberGenerator; + private readonly OpenMrsClient openMrsClient; public LinkPatient( ILinkPatientRepository linkPatientRepository, @@ -27,7 +33,8 @@ public LinkPatient( IPatientVerification patientVerification, ReferenceNumberGenerator referenceNumberGenerator, IDiscoveryRequestRepository discoveryRequestRepository, - IOptions otpService) + IOptions otpService, + OpenMrsClient openMrsClient) { this.linkPatientRepository = linkPatientRepository; this.patientRepository = patientRepository; @@ -35,6 +42,7 @@ public LinkPatient( this.referenceNumberGenerator = referenceNumberGenerator; this.discoveryRequestRepository = discoveryRequestRepository; this.otpService = otpService; + this.openMrsClient = openMrsClient; } public virtual async Task> LinkPatients( @@ -178,7 +186,30 @@ private async Task 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 SaveInitiatedLinkRequest(string requestId, string transactionId, diff --git a/src/In.ProjectEKA.HipService/Link/Model/PatientAbhaIdentifier.cs b/src/In.ProjectEKA.HipService/Link/Model/PatientAbhaIdentifier.cs new file mode 100644 index 000000000..c86705329 --- /dev/null +++ b/src/In.ProjectEKA.HipService/Link/Model/PatientAbhaIdentifier.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/OpenMrs/OpenMrsClient.cs b/src/In.ProjectEKA.HipService/OpenMrs/OpenMrsClient.cs index f6c22a9f6..31cd32856 100644 --- a/src/In.ProjectEKA.HipService/OpenMrs/OpenMrsClient.cs +++ b/src/In.ProjectEKA.HipService/OpenMrs/OpenMrsClient.cs @@ -35,6 +35,25 @@ public async Task GetAsync(string openMrsUrl) return responseMessage; } + + public async Task 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() {