Skip to content

Commit

Permalink
BAH-3773 | Add. store patient demographics after linking
Browse files Browse the repository at this point in the history
  • Loading branch information
SanoferSameera committed May 6, 2024
1 parent 7afa480 commit d446f92
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
7 changes: 1 addition & 6 deletions src/In.ProjectEKA.HipService/Discovery/DiscoveryReqMap.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
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>();
public static Dictionary<string, PatientEnquiry> PatientInfoMap = new Dictionary<string, PatientEnquiry>();
}
}
5 changes: 2 additions & 3 deletions src/In.ProjectEKA.HipService/Discovery/PatientController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,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);
if (!AbhaIdentifierMap.ContainsKey(patientId))
if (!PatientInfoMap.ContainsKey(patientId))
{
AbhaIdentifierMap.Add(patientId, abhaNumberIdentifier?.Value);
PatientInfoMap.Add(patientId, request.Patient);
}

Log.Information("new GatewayDiscoveryRepresentation" + gatewayDiscoveryRepresentation);
Expand Down
32 changes: 25 additions & 7 deletions src/In.ProjectEKA.HipService/Link/LinkPatient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using In.ProjectEKA.HipService.OpenMrs;
using In.ProjectEKA.HipService.UserAuth;
using In.ProjectEKA.HipService.UserAuth.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -26,6 +28,7 @@ public class LinkPatient
private readonly IPatientVerification patientVerification;
private readonly ReferenceNumberGenerator referenceNumberGenerator;
private readonly IOpenMrsClient openMrsClient;
private readonly IUserAuthService userAuthService;

public LinkPatient(
ILinkPatientRepository linkPatientRepository,
Expand All @@ -34,7 +37,7 @@ public LinkPatient(
ReferenceNumberGenerator referenceNumberGenerator,
IDiscoveryRequestRepository discoveryRequestRepository,
IOptions<OtpServiceConfiguration> otpService,
IOpenMrsClient openMrsClient)
IOpenMrsClient openMrsClient, IUserAuthService userAuthService)
{
this.linkPatientRepository = linkPatientRepository;
this.patientRepository = patientRepository;
Expand All @@ -43,6 +46,7 @@ public LinkPatient(
this.discoveryRequestRepository = discoveryRequestRepository;
this.otpService = otpService;
this.openMrsClient = openMrsClient;
this.userAuthService = userAuthService;
}

public virtual async Task<ValueTuple<PatientLinkEnquiryRepresentation, ErrorRepresentation>> LinkPatients(
Expand Down Expand Up @@ -165,9 +169,13 @@ public virtual async Task<ValueTuple<PatientLinkConfirmationRepresentation, stri
linkEnquires.PatientReferenceNumber,
$"{patient.Name}",
representations));
return await SaveLinkedAccounts(linkEnquires,patient.Uuid)
? (patientLinkResponse,cmId, (ErrorRepresentation) null)
: (null,cmId,
var resp = await SaveLinkedAccounts(linkEnquires, patient.Uuid);
if (resp)
{
LinkAbhaIdentifier(patient.Uuid, linkEnquires.ConsentManagerUserId);
return (patientLinkResponse, cmId, (ErrorRepresentation) null);
}
return (null,cmId,
new ErrorRepresentation(new Error(ErrorCode.NoPatientFound,
ErrorMessage.NoPatientFound)));
}).ValueOr(
Expand All @@ -186,15 +194,16 @@ 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
var patient = PatientInfoMap[abhaAddress];
var abhaNumberIdentifier = patient?.VerifiedIdentifiers.FirstOrDefault(id => id.Type == IdentifierType.NDHM_HEALTH_NUMBER);
var json = JsonConvert.SerializeObject(new PatientAbhaIdentifier(abhaNumberIdentifier?.Value, abhaAddress), new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver
Expand All @@ -207,6 +216,15 @@ private async void LinkAbhaIdentifier(string patientUuid, string abhaAddress)
json
)
.ConfigureAwait(false);
string content = await resp.Content.ReadAsStringAsync();
if (content.Equals("true"))
{
if (patient != null)
{
var ndhmDemographics = new NdhmDemographics(abhaAddress, patient.Name, patient.Gender.ToString(), patient.YearOfBirth.ToString(), patient.VerifiedIdentifiers.FirstOrDefault(id => id.Type == IdentifierType.MOBILE)?.Value);
await userAuthService.Dump(ndhmDemographics);
}
}
if (!resp.IsSuccessStatusCode)
Log.Error("Errored in linking the abha identifier to the patient");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class LinkControllerTest

public LinkControllerTest()
{
link = new Mock<LinkPatient>(MockBehavior.Strict, null, null, null, null, null, null, null);
link = new Mock<LinkPatient>(MockBehavior.Strict, null, null, null, null, null, null, null, null);
discoveryRequestRepository = new Mock<IDiscoveryRequestRepository>();
var gatewayClient = new Mock<GatewayClient>(MockBehavior.Strict, null, null);

Expand Down
15 changes: 13 additions & 2 deletions test/In.ProjectEKA.HipServiceTest/Link/LinkPatientTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net;
using System.Net.Http;
using In.ProjectEKA.HipService.OpenMrs;
using In.ProjectEKA.HipService.UserAuth;

namespace In.ProjectEKA.HipServiceTest.Link
{
Expand Down Expand Up @@ -49,6 +50,7 @@ public class LinkPatientTest
private readonly Mock<IPatientVerification> patientVerification = new Mock<IPatientVerification>();
private readonly Mock<ReferenceNumberGenerator> guidGenerator = new Mock<ReferenceNumberGenerator>();
private readonly Mock<IOpenMrsClient> openmrsClient = new Mock<IOpenMrsClient>();
private readonly Mock<IUserAuthService> userAuthService = new Mock<IUserAuthService>();

public LinkPatientTest()
{
Expand All @@ -60,7 +62,8 @@ public LinkPatientTest()
guidGenerator.Object,
discoveryRequestRepository.Object,
otpServiceConfigurations,
openmrsClient.Object);
openmrsClient.Object,
userAuthService.Object);
}

[Fact]
Expand Down Expand Up @@ -217,7 +220,15 @@ private async void SuccessLinkPatientForValidOtp()
var testLinkedAccounts = new LinkedAccounts(testLinkRequest.PatientReferenceNumber,
testLinkRequest.LinkReferenceNumber,
testLinkRequest.ConsentManagerUserId, It.IsAny<string>(), new[] {programRefNo}.ToList(),It.IsAny<Guid>());
DiscoveryReqMap.AbhaIdentifierMap.Add(testLinkRequest.ConsentManagerUserId, "1234567891011" );
var patientEnquiry =
new PatientEnquiry(
"id", verifiedIdentifiers: new List<Identifier>()
{
new Identifier(IdentifierType.MOBILE, "9999999999"),
new Identifier(IdentifierType.NDHM_HEALTH_NUMBER, "123456718910")
}, unverifiedIdentifiers: null,
"name", HipLibrary.Patient.Model.Gender.M, 2000);
DiscoveryReqMap.PatientInfoMap.Add(testLinkRequest.ConsentManagerUserId, patientEnquiry);
patientVerification.Setup(e => e.Verify(sessionId, otpToken))
.ReturnsAsync((OtpMessage) null);
linkRepository.Setup(e => e.GetPatientFor(sessionId))
Expand Down

0 comments on commit d446f92

Please sign in to comment.