From 84c35636d3191ee74293a9127765a7ecf2db083d Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Fri, 22 Nov 2024 07:00:59 +0530 Subject: [PATCH 1/6] BAH-4120 | Refactor. Use v3 sessions API for Gateway session and authentication --- .../Common/Constants.cs | 2 +- .../Common/HttpRequestHelper.cs | 2 +- .../Common/Model/GatewayAuthHttpHandler.cs | 28 +++++++++++++++++++ .../Gateway/GatewayClient.cs | 6 +++- src/In.ProjectEKA.HipService/Startup.cs | 3 +- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/In.ProjectEKA.HipService/Common/Model/GatewayAuthHttpHandler.cs diff --git a/src/In.ProjectEKA.HipService/Common/Constants.cs b/src/In.ProjectEKA.HipService/Common/Constants.cs index bd02cd65..67c157f9 100644 --- a/src/In.ProjectEKA.HipService/Common/Constants.cs +++ b/src/In.ProjectEKA.HipService/Common/Constants.cs @@ -17,7 +17,7 @@ public static class Constants public const string APP_PATH_CREATE_ABHA_ADDRESS = "/" + VERSION_V3 + "/hip/createAbhaAddress"; public const string APP_PATH_GET_ABHA_CARD = "/" + VERSION_V3 + "/hip/getAbhaCard"; - public const string PATH_SESSIONS = CURRENT_VERSION + "/sessions"; + public const string PATH_SESSIONS = "api/hiecm/gateway/"+VERSION_V3+"/sessions"; public const string PATH_CARE_CONTEXTS_DISCOVER = CURRENT_VERSION + "/care-contexts/discover"; public const string PATH_CONSENTS_HIP = CURRENT_VERSION + "/consents/hip/notify"; public const string PATH_LINKS_LINK_INIT = CURRENT_VERSION + "/links/link/init"; diff --git a/src/In.ProjectEKA.HipService/Common/HttpRequestHelper.cs b/src/In.ProjectEKA.HipService/Common/HttpRequestHelper.cs index e3bc8afc..9367fe74 100644 --- a/src/In.ProjectEKA.HipService/Common/HttpRequestHelper.cs +++ b/src/In.ProjectEKA.HipService/Common/HttpRequestHelper.cs @@ -50,7 +50,7 @@ public static HttpRequestMessage CreateHttpRequest( if (transactionId != null) httpRequestMessage.Headers.Add("Transaction_Id", transactionId); httpRequestMessage.Headers.Add("REQUEST-ID", Guid.NewGuid().ToString()); - httpRequestMessage.Headers.Add("TIMESTAMP", DateTime.Now.ToString(TIMESTAMP_FORMAT)); + httpRequestMessage.Headers.Add("TIMESTAMP", DateTime.UtcNow.ToString(TIMESTAMP_FORMAT)); return httpRequestMessage; } diff --git a/src/In.ProjectEKA.HipService/Common/Model/GatewayAuthHttpHandler.cs b/src/In.ProjectEKA.HipService/Common/Model/GatewayAuthHttpHandler.cs new file mode 100644 index 00000000..51f9ac5b --- /dev/null +++ b/src/In.ProjectEKA.HipService/Common/Model/GatewayAuthHttpHandler.cs @@ -0,0 +1,28 @@ +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Log = Serilog.Log; + +namespace In.ProjectEKA.HipService.Common.Model; + +public class GatewayAuthHttpHandler : HttpClientHandler +{ + public GatewayAuthHttpHandler(IConfiguration configuration) + { + Configuration = configuration; + } + + private IConfiguration Configuration; + + protected override async Task SendAsync(HttpRequestMessage request, + CancellationToken cancellationToken) + { + request.Headers.Add("X-CM-ID", Configuration.GetValue("Gateway:cmSuffix")); + request.Headers.Add("REQUEST-ID", Guid.NewGuid().ToString()); + request.Headers.Add("TIMESTAMP", DateTime.UtcNow.ToString(Constants.TIMESTAMP_FORMAT)); + HttpResponseMessage responseMessage = await base.SendAsync(request, cancellationToken); + return responseMessage; + } +} \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs b/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs index 2979e350..5f273859 100644 --- a/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs +++ b/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs @@ -39,7 +39,8 @@ public virtual async Task> Authenticate(String correlationId) var json = JsonConvert.SerializeObject(new { clientId = configuration.ClientId, - clientSecret = configuration.ClientSecret + clientSecret = configuration.ClientSecret, + grantType = "client_credentials" }, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, @@ -56,6 +57,9 @@ public virtual async Task> Authenticate(String correlationId) }; if (correlationId != null) message.Headers.Add(Constants.CORRELATION_ID, correlationId); + message.Headers.Add("REQUEST-ID", Guid.NewGuid().ToString()); + message.Headers.Add("TIMESTAMP", DateTime.Now.ToString(Constants.TIMESTAMP_FORMAT)); + message.Headers.Add("X-CM-ID", configuration.CmSuffix); var responseMessage = await httpClient.SendAsync(message).ConfigureAwait(false); var response = await responseMessage.Content.ReadAsStringAsync(); diff --git a/src/In.ProjectEKA.HipService/Startup.cs b/src/In.ProjectEKA.HipService/Startup.cs index 123702c9..6c055c85 100644 --- a/src/In.ProjectEKA.HipService/Startup.cs +++ b/src/In.ProjectEKA.HipService/Startup.cs @@ -214,7 +214,8 @@ public void ConfigureServices(IServiceCollection services) .AddJwtBearer(Constants.GATEWAY_AUTH, options => { // Need to validate Audience and Issuer properly - options.Authority = $"{Configuration.GetValue("Gateway:url")}/{Constants.CURRENT_VERSION}"; + options.Authority = $"{Configuration.GetValue("Gateway:url")}/api/hiecm/gateway/v3/"; + options.BackchannelHttpHandler = new GatewayAuthHttpHandler(Configuration); options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, From 5abc34a39dd732836c59ba298205f57d88bcd8a2 Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Fri, 22 Nov 2024 07:03:17 +0530 Subject: [PATCH 2/6] BAH-4120 | Refactor. Update the request payload and APIs for v3 scan and share feature --- .../Common/Constants.cs | 6 ++-- .../Patient/IPatientProfileService.cs | 2 +- .../Patient/Model/PatientDemographics.cs | 17 +++++----- .../Patient/Model/Profile.cs | 8 ++--- .../Model/ProfileShareAcknowledgement.cs | 23 ++++++++++--- .../Patient/Model/ProfileShareConfirmation.cs | 11 ++---- .../Patient/Model/ShareProfileRequest.cs | 34 ++++++++++++++----- .../Patient/PatientController.cs | 16 ++++----- .../Patient/PatientProfileService.cs | 18 +++++----- .../appsettings.Development.json | 4 +-- src/In.ProjectEKA.HipService/appsettings.json | 2 +- 11 files changed, 82 insertions(+), 59 deletions(-) diff --git a/src/In.ProjectEKA.HipService/Common/Constants.cs b/src/In.ProjectEKA.HipService/Common/Constants.cs index 67c157f9..da566624 100644 --- a/src/In.ProjectEKA.HipService/Common/Constants.cs +++ b/src/In.ProjectEKA.HipService/Common/Constants.cs @@ -86,9 +86,9 @@ public static class Constants public const string DEEPLINK_URL = "https://link.to.health.records"; public const string PATH_PATIENT_NOTIFY = "/" + CURRENT_VERSION + "/patients/status/notify"; public const string PATH_PATIENT_ON_NOTIFY = "/" + CURRENT_VERSION + "/patients/status/on-notify"; - public const string PATH_PROFILE_SHARE = "/" + UPDATED_VERSION + "/patients/profile/share"; - public const string PATH_PROFILE_ON_SHARE = "/" + UPDATED_VERSION + "/patients/profile/on-share"; - public const string PATH_PROFILE_FETCH = "/" + CURRENT_VERSION + "/patients/profile/fetch"; + public const string PATH_PROFILE_SHARE = "/api/" + VERSION_V3 + "/hip/patient/share"; + public const string PATH_PROFILE_ON_SHARE = "/api/hiecm/patient-share/"+VERSION_V3 +"/on-share"; + public const string GET_PATIENT_QUEUE = "/" + VERSION_V3 + "/hip/getPatientQueue"; public const string ABHA_SERVICE_CERT_URL = "/" + VERSION_V3 + "/profile/public/certificate"; public const string ENROLLMENT_REQUEST_OTP = "/" + VERSION_V3 + "/enrollment/request/otp"; diff --git a/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs b/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs index e04aa664..874b9eda 100644 --- a/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs +++ b/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs @@ -6,7 +6,7 @@ namespace In.ProjectEKA.HipService.Patient { public interface IPatientProfileService { - Task SavePatient(ShareProfileRequest shareProfileRequest); + Task SavePatient(ShareProfileRequest shareProfileRequest, string requestId, string timestamp); bool IsValidRequest(ShareProfileRequest shareProfileRequest); Task> GetPatientQueue(); Task linkToken(PatientDemographics patientDemographics); diff --git a/src/In.ProjectEKA.HipService/Patient/Model/PatientDemographics.cs b/src/In.ProjectEKA.HipService/Patient/Model/PatientDemographics.cs index d9060cf2..31b0675f 100644 --- a/src/In.ProjectEKA.HipService/Patient/Model/PatientDemographics.cs +++ b/src/In.ProjectEKA.HipService/Patient/Model/PatientDemographics.cs @@ -5,34 +5,35 @@ namespace In.ProjectEKA.HipService.Patient.Model { public class PatientDemographics { - public string HealthId { get; } - public string HealthIdNumber { get; } + public string AbhaAddress { get; } + public string AbhaNumber { get; } public string Name { get; } public string Gender { get; } public Address Address { get; } public int YearOfBirth { get; } public int? DayOfBirth { get; } public int? MonthOfBirth { get; } - public List Identifiers { get; } + public string PhoneNumber { get; } public PatientDemographics(string name, string gender, - string healthId, + string abhaAddress, Address address, int yearOfBirth, int? dayOfBirth, int? monthOfBirth, - List identifiers, string healthIdNumber) + string abhaNumber, + string phoneNumber) { Name = name; Gender = gender; - HealthId = healthId; + AbhaAddress = abhaAddress; Address = address; YearOfBirth = yearOfBirth; DayOfBirth = dayOfBirth; MonthOfBirth = monthOfBirth; - Identifiers = identifiers; - HealthIdNumber = healthIdNumber; + PhoneNumber = phoneNumber; + AbhaNumber = abhaNumber; } } } \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Patient/Model/Profile.cs b/src/In.ProjectEKA.HipService/Patient/Model/Profile.cs index 35d82964..172f98e1 100644 --- a/src/In.ProjectEKA.HipService/Patient/Model/Profile.cs +++ b/src/In.ProjectEKA.HipService/Patient/Model/Profile.cs @@ -2,12 +2,10 @@ namespace In.ProjectEKA.HipService.Patient.Model { public class Profile { - public string HipCode { get; } - public PatientDemographics PatientDemographics { get; } - public Profile(string hipCode, PatientDemographics patient) + public PatientDemographics Patient { get; } + public Profile(PatientDemographics patient) { - HipCode = hipCode; - PatientDemographics = patient; + Patient = patient; } } } \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareAcknowledgement.cs b/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareAcknowledgement.cs index 5d06be08..d5d16162 100644 --- a/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareAcknowledgement.cs +++ b/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareAcknowledgement.cs @@ -2,14 +2,29 @@ namespace In.ProjectEKA.HipService.Patient.Model { public class ProfileShareAcknowledgement { - public ProfileShareAcknowledgement(string status,string healthId, string tokenNumber) + public ProfileShareAcknowledgement(string status, string abhaAddress, ProfileShareAckProfile profile) { Status = status; - HealthId = healthId; - TokenNumber = tokenNumber; + AbhaAddress = abhaAddress; + Profile = profile; } + public string Status { get; } - public string HealthId { get; } + public string AbhaAddress { get; } + public ProfileShareAckProfile Profile { get; } + } + + public class ProfileShareAckProfile + { + public string Context { get; } public string TokenNumber { get; } + public string Expiry { get; } + + public ProfileShareAckProfile(string context, string tokenNumber, string expiry) + { + Context = context; + TokenNumber = tokenNumber; + Expiry = expiry; + } } } \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareConfirmation.cs b/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareConfirmation.cs index 2c2948cc..347eb9f1 100644 --- a/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareConfirmation.cs +++ b/src/In.ProjectEKA.HipService/Patient/Model/ProfileShareConfirmation.cs @@ -5,19 +5,14 @@ namespace In.ProjectEKA.HipService.Patient.Model { public class ProfileShareConfirmation { - public ProfileShareConfirmation(string requestId, string timestamp, - ProfileShareAcknowledgement acknowledgement, Error error, Resp resp) + public ProfileShareConfirmation(ProfileShareAcknowledgement acknowledgement, Error error, Resp resp) { - RequestId = requestId; - Timestamp = timestamp; Acknowledgement = acknowledgement; Error = error; - Resp = resp; + Response = resp; } - public string RequestId { get; } - public string Timestamp { get; } public Error Error { get; } - public Resp Resp { get; } + public Resp Response { get; } public ProfileShareAcknowledgement Acknowledgement { get; } } } \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Patient/Model/ShareProfileRequest.cs b/src/In.ProjectEKA.HipService/Patient/Model/ShareProfileRequest.cs index 14fa1cb8..4790fddf 100644 --- a/src/In.ProjectEKA.HipService/Patient/Model/ShareProfileRequest.cs +++ b/src/In.ProjectEKA.HipService/Patient/Model/ShareProfileRequest.cs @@ -6,16 +6,34 @@ namespace In.ProjectEKA.HipService.Patient.Model { public class ShareProfileRequest { - [Required] - public string RequestId { get;} - [Required] - public DateTime? Timestamp { get;} - public Profile Profile { get; } - public ShareProfileRequest(string requestId,DateTime? timestamp, Profile profile) + public string Intent { get; } + + public ShareProfileMetadata Metadata { get; } + [Required] public Profile Profile { get; } + + public ShareProfileRequest(string intent, ShareProfileMetadata metadata, Profile profile) { - RequestId = requestId; - Timestamp = timestamp; + Intent = intent; + Metadata = metadata; Profile = profile; } } + + public class ShareProfileMetadata + { + public string HipId { get; } + public string Context { get; } + public string HprId { get; } + public string Latitude { get; } + public string Longitude { get; } + + public ShareProfileMetadata(string hipId, string context, string hprId, string latitude, string longitude) + { + HipId = hipId; + Context = context; + HprId = hprId; + Latitude = latitude; + Longitude = longitude; + } + } } \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Patient/PatientController.cs b/src/In.ProjectEKA.HipService/Patient/PatientController.cs index 3b57b514..a2d9ff3e 100644 --- a/src/In.ProjectEKA.HipService/Patient/PatientController.cs +++ b/src/In.ProjectEKA.HipService/Patient/PatientController.cs @@ -1,4 +1,5 @@ using In.ProjectEKA.HipService.Common; +using Microsoft.AspNetCore.Authorization; namespace In.ProjectEKA.HipService.Patient { @@ -48,7 +49,7 @@ await _gatewayClient.SendDataToGateway(PATH_PATIENT_ON_NOTIFY, [Route(PATH_PROFILE_SHARE)] public async Task StoreDetails([FromHeader(Name = CORRELATION_ID)] string correlationId, - [FromBody] ShareProfileRequest shareProfileRequest) + [FromBody] ShareProfileRequest shareProfileRequest, [FromHeader(Name = "request-id")] string requestId, [FromHeader(Name = "timestamp")] string timestamp) { var cmSuffix = _gatewayConfiguration.CmSuffix; var status = Status.SUCCESS; @@ -62,14 +63,12 @@ public async Task StoreDetails([FromHeader(Name = CORRELATION_ID)] int token = 0; if(error == null) { - token = await _patientProfileService.SavePatient(shareProfileRequest); + token = await _patientProfileService.SavePatient(shareProfileRequest,requestId, timestamp); } var gatewayResponse = new ProfileShareConfirmation( - Guid.NewGuid().ToString(), - DateTime.Now.ToUniversalTime().ToString(DateTimeFormat), - new ProfileShareAcknowledgement(status.ToString(),shareProfileRequest.Profile.PatientDemographics.HealthId,token.ToString()), error, - new Resp(shareProfileRequest.RequestId)); + new ProfileShareAcknowledgement(status.ToString(),shareProfileRequest.Profile.Patient.AbhaAddress,new ProfileShareAckProfile(shareProfileRequest.Metadata.Context,token.ToString(),"1800")), error, + new Resp(requestId)); Task.Run(async () => { await Task.Delay(500); @@ -77,8 +76,6 @@ await _gatewayClient.SendDataToGateway(PATH_PROFILE_ON_SHARE, gatewayResponse, cmSuffix, correlationId); - if(error == null) - await _patientProfileService.linkToken(shareProfileRequest.Profile.PatientDemographics); }); if (error == null) { @@ -87,7 +84,8 @@ await _gatewayClient.SendDataToGateway(PATH_PROFILE_ON_SHARE, return BadRequest(); } - [Route(PATH_PROFILE_FETCH)] + [Authorize(AuthenticationSchemes = BAHMNI_AUTH)] + [Route(GET_PATIENT_QUEUE)] public async Task GetDetails() { var patientQueueResult = await _patientProfileService.GetPatientQueue(); diff --git a/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs b/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs index 2e009611..3622e617 100644 --- a/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs +++ b/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs @@ -36,13 +36,11 @@ public PatientProfileService(OpenMrsConfiguration openMrsConfiguration, PatientC this.hipConfiguration = hipConfiguration; } - public async Task SavePatient(ShareProfileRequest shareProfileRequest) + public async Task SavePatient(ShareProfileRequest shareProfileRequest, string requestId, string timestamp) { - var requesId = shareProfileRequest.RequestId; - var timeStamp = shareProfileRequest.Timestamp.ToString(); - var hipCode = shareProfileRequest.Profile.HipCode; - var patient = shareProfileRequest.Profile.PatientDemographics; - var response = await Save(new PatientQueue(requesId, timeStamp, patient, hipCode)); + var hipCode = shareProfileRequest.Metadata.HipId; + var patient = shareProfileRequest.Profile.Patient; + var response = await Save(new PatientQueue(requestId, timestamp, patient, hipCode)); if(response.HasValue) Log.Information("Patient saved to queue"); return response.ValueOrDefault(); @@ -65,8 +63,8 @@ private async Task> Save(PatientQueue patientQueue) public bool IsValidRequest(ShareProfileRequest shareProfileRequest) { var profile = shareProfileRequest?.Profile; - var demographics = profile?.PatientDemographics; - return demographics is {HealthId: { }, Identifiers: { }, Name: { },Gender:{}} && Enum.IsDefined(typeof(Gender), demographics.Gender) && demographics.YearOfBirth != 0; + var demographics = profile?.Patient; + return demographics is {AbhaAddress: { }, Name: { },Gender:{}} && Enum.IsDefined(typeof(Gender), demographics.Gender) && demographics.YearOfBirth != 0; } public async Task> GetPatientQueue() { @@ -89,9 +87,9 @@ public async Task linkToken(PatientDemographics patientDemographics) { var dob = new Date(patientDemographics.YearOfBirth, patientDemographics.MonthOfBirth ?? 1, patientDemographics.DayOfBirth ?? 1).ToString(); - var ndhmDemograhics = new NdhmDemographics(patientDemographics.HealthId, patientDemographics.Name, + var ndhmDemograhics = new NdhmDemographics(patientDemographics.AbhaAddress, patientDemographics.Name, patientDemographics.Gender, - dob, patientDemographics.Identifiers[0].value); + dob, patientDemographics.PhoneNumber); var request = new HttpRequestMessage(HttpMethod.Post, hipConfiguration.Value.Url + PATH_DEMOGRAPHICS); request.Content = new StringContent(JsonConvert.SerializeObject(ndhmDemograhics), Encoding.UTF8, "application/json"); diff --git a/src/In.ProjectEKA.HipService/appsettings.Development.json b/src/In.ProjectEKA.HipService/appsettings.Development.json index 33bdb5b8..07005d15 100644 --- a/src/In.ProjectEKA.HipService/appsettings.Development.json +++ b/src/In.ProjectEKA.HipService/appsettings.Development.json @@ -47,7 +47,7 @@ "Uri": "http://localhost:9200" }, "Gateway": { - "url": "https://dev.abdm.gov.in/gateway", + "url": "https://dev.abdm.gov.in", "timeout": 2000, "counter": 5, "clientId": "", @@ -62,7 +62,7 @@ "username": "superman", "password": "Admin123", "phoneNumber": "phoneNumber", - "patientQueueTimeLimit": 30 //in_minutes + "patientQueueTimeLimit": 60 //in_minutes }, "BackgroundJobs": { "cancellationCheckInterval": 5 diff --git a/src/In.ProjectEKA.HipService/appsettings.json b/src/In.ProjectEKA.HipService/appsettings.json index 54ce3401..7926ee78 100644 --- a/src/In.ProjectEKA.HipService/appsettings.json +++ b/src/In.ProjectEKA.HipService/appsettings.json @@ -63,7 +63,7 @@ "username": "superman", "password": "$OPENMRS_PASSWORD", "phoneNumber": "phoneNumber", - "patientQueueTimeLimit": 30 //in_minutes + "patientQueueTimeLimit": 60 //in_minutes }, "BackgroundJobs": { "cancellationCheckInterval": 5 From cff157bc223cfbcfe26b42336f397b50594b2a0c Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Mon, 25 Nov 2024 22:02:12 +0530 Subject: [PATCH 3/6] BAH-4120 | Add. Scheduled job for clean up of Patient Queue --- .../Patient/jobs/CleanPatientQueueJob.cs | 33 +++++++++++++++++++ src/In.ProjectEKA.HipService/Startup.cs | 8 +++++ 2 files changed, 41 insertions(+) create mode 100644 src/In.ProjectEKA.HipService/Patient/jobs/CleanPatientQueueJob.cs diff --git a/src/In.ProjectEKA.HipService/Patient/jobs/CleanPatientQueueJob.cs b/src/In.ProjectEKA.HipService/Patient/jobs/CleanPatientQueueJob.cs new file mode 100644 index 00000000..418844fb --- /dev/null +++ b/src/In.ProjectEKA.HipService/Patient/jobs/CleanPatientQueueJob.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using In.ProjectEKA.HipService.Logger; +using In.ProjectEKA.HipService.OpenMrs; +using In.ProjectEKA.HipService.Patient.Database; + +namespace In.ProjectEKA.HipService.Patient.jobs; + +public class CleanPatientQueueJob +{ + private readonly PatientContext patientContext; + private readonly OpenMrsConfiguration openMrsConfiguration; + + public CleanPatientQueueJob(PatientContext patientContext, OpenMrsConfiguration openMrsConfiguration) + { + this.patientContext = patientContext; + this.openMrsConfiguration = openMrsConfiguration; + } + + public void CleanPatientQueue() + { + List oldEntries = patientContext.PatientQueue.ToList().FindAll( + patient => DateTime.Now.Subtract(DateTime.Parse(patient.DateTimeStamp)).TotalMinutes > + openMrsConfiguration.PatientQueueTimeLimit + ); + + patientContext.PatientQueue.RemoveRange(oldEntries); + patientContext.SaveChanges(); + + Log.Information($"Deleted {oldEntries.Count} old patient queue at {DateTime.UtcNow}"); + } +} \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Startup.cs b/src/In.ProjectEKA.HipService/Startup.cs index 6c055c85..839aba62 100644 --- a/src/In.ProjectEKA.HipService/Startup.cs +++ b/src/In.ProjectEKA.HipService/Startup.cs @@ -2,6 +2,7 @@ using In.ProjectEKA.HipService.Creation; using In.ProjectEKA.HipService.Patient; using In.ProjectEKA.HipService.Patient.Database; +using In.ProjectEKA.HipService.Patient.jobs; using In.ProjectEKA.HipService.SmsNotification; using In.ProjectEKA.HipService.UserAuth; using In.ProjectEKA.HipService.UserAuth.Database; @@ -167,6 +168,7 @@ public void ConfigureServices(IServiceCollection services) .AddSingleton() .AddScoped() .AddScoped() + .AddScoped() .AddSingleton() .AddTransient() .AddRouting(options => options.LowercaseUrls = true) @@ -274,6 +276,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) CancellationCheckInterval = TimeSpan.FromMinutes( Configuration.GetSection("BackgroundJobs:cancellationCheckInterval").Get()) }); + RecurringJob.AddOrUpdate( + "cleanup-patient-queue", + job => job.CleanPatientQueue(), + Cron.Hourly + ); + using var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope(); var linkContext = serviceScope.ServiceProvider.GetService(); From 20498208674e10844ffe9c6ef251d353734afe02 Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Mon, 25 Nov 2024 22:13:28 +0530 Subject: [PATCH 4/6] BAH-4120 | Refactor. Remove unused code for generating link token --- .../Patient/IPatientProfileService.cs | 1 - .../Patient/PatientProfileService.cs | 13 ------------- 2 files changed, 14 deletions(-) diff --git a/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs b/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs index 874b9eda..bdf7723d 100644 --- a/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs +++ b/src/In.ProjectEKA.HipService/Patient/IPatientProfileService.cs @@ -9,6 +9,5 @@ public interface IPatientProfileService Task SavePatient(ShareProfileRequest shareProfileRequest, string requestId, string timestamp); bool IsValidRequest(ShareProfileRequest shareProfileRequest); Task> GetPatientQueue(); - Task linkToken(PatientDemographics patientDemographics); } } \ No newline at end of file diff --git a/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs b/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs index 3622e617..3560bb45 100644 --- a/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs +++ b/src/In.ProjectEKA.HipService/Patient/PatientProfileService.cs @@ -83,19 +83,6 @@ public async Task> GetPatientQueue() } } - public async Task linkToken(PatientDemographics patientDemographics) - { - var dob = new Date(patientDemographics.YearOfBirth, patientDemographics.MonthOfBirth ?? 1, - patientDemographics.DayOfBirth ?? 1).ToString(); - var ndhmDemograhics = new NdhmDemographics(patientDemographics.AbhaAddress, patientDemographics.Name, - patientDemographics.Gender, - dob, patientDemographics.PhoneNumber); - var request = new HttpRequestMessage(HttpMethod.Post, hipConfiguration.Value.Url + PATH_DEMOGRAPHICS); - request.Content = new StringContent(JsonConvert.SerializeObject(ndhmDemograhics), Encoding.UTF8, - "application/json"); - await httpClient.SendAsync(request).ConfigureAwait(false); - } - } } \ No newline at end of file From c7b029c42a8953dcddc90f02436dd80f1fb48517 Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Mon, 25 Nov 2024 22:14:00 +0530 Subject: [PATCH 5/6] BAH-4120 | Fix. Pass UTC timestamp in session API header --- src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs b/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs index 5f273859..a45dc619 100644 --- a/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs +++ b/src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs @@ -58,7 +58,7 @@ public virtual async Task> Authenticate(String correlationId) if (correlationId != null) message.Headers.Add(Constants.CORRELATION_ID, correlationId); message.Headers.Add("REQUEST-ID", Guid.NewGuid().ToString()); - message.Headers.Add("TIMESTAMP", DateTime.Now.ToString(Constants.TIMESTAMP_FORMAT)); + message.Headers.Add("TIMESTAMP", DateTime.UtcNow.ToString(Constants.TIMESTAMP_FORMAT)); message.Headers.Add("X-CM-ID", configuration.CmSuffix); var responseMessage = await httpClient.SendAsync(message).ConfigureAwait(false); var response = await responseMessage.Content.ReadAsStringAsync(); From caa222cac4ee38bb3e6ee30033444db6029592f7 Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Mon, 25 Nov 2024 22:43:43 +0530 Subject: [PATCH 6/6] BAH-4120 | Fix. Tests for Patient Controller --- .../Patient/PatientControllerTest.cs | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/test/In.ProjectEKA.HipServiceTest/Patient/PatientControllerTest.cs b/test/In.ProjectEKA.HipServiceTest/Patient/PatientControllerTest.cs index 0afc001c..85796a25 100644 --- a/test/In.ProjectEKA.HipServiceTest/Patient/PatientControllerTest.cs +++ b/test/In.ProjectEKA.HipServiceTest/Patient/PatientControllerTest.cs @@ -68,22 +68,18 @@ private void ShouldNotifyHip() private void ShouldSaveAPatient() { var requestId = Guid.NewGuid().ToString(); - var timestamp = DateTime.Now.ToUniversalTime(); + var timestamp = DateTime.Now.ToUniversalTime().ToString(); var identifier = new Identifier("MOBILE", "9999999999"); var address = new Address("string", "string", "string", "string"); - var patient = new PatientDemographics("test t", "M", "test@sbx", address, 2000, 0, 0, - new List() {identifier}, "1234-5678"); - var profile = new Profile("12345", patient); - var shareProfileRequest = new ShareProfileRequest(requestId, timestamp, profile); + var patient = new PatientDemographics("test t", "M", "test@sbx", address, 2000, 0, 0,"91-1184-2524-4233","9123456789"); + var profile = new Profile( patient); + var shareProfileMetadata = new ShareProfileMetadata("12345", "1","test@hpr.abdm","71.254","78.325"); + var shareProfileRequest = new ShareProfileRequest("PROFILE_SHARE", shareProfileMetadata, profile); var correlationId = Uuid.Generate().ToString(); var cmSuffix = "ncg"; _patientProfileService.Setup(d => d.IsValidRequest(shareProfileRequest)).Returns(true); - var profileShareConfirmation = new ProfileShareConfirmation( - Guid.NewGuid().ToString(), - DateTime.Now.ToUniversalTime().ToString(DateTimeFormat), - new ProfileShareAcknowledgement("test@sbx", - Status.SUCCESS.ToString(),"1"), null, - new Resp(requestId)); + var profileShareConfirmation = new ProfileShareConfirmation(new ProfileShareAcknowledgement( + Status.SUCCESS.ToString(),"test@sbx",new ProfileShareAckProfile(shareProfileMetadata.Context,"1","1800")),null,new Resp(Guid.NewGuid().ToString())); _gatewayClient.Setup( client => client.SendDataToGateway(PATH_PROFILE_ON_SHARE, @@ -91,7 +87,7 @@ private void ShouldSaveAPatient() .Returns(Task.FromResult("")); Assert.Equal( ((Microsoft.AspNetCore.Mvc.AcceptedResult) _patientController - .StoreDetails(correlationId, shareProfileRequest).Result).StatusCode, + .StoreDetails(correlationId, shareProfileRequest,requestId,timestamp).Result).StatusCode, StatusCodes.Status202Accepted); } @@ -99,22 +95,20 @@ private void ShouldSaveAPatient() private void ShouldThrowBadRequest() { var requestId = Guid.NewGuid().ToString(); - var timestamp = DateTime.Now.ToUniversalTime(); + var timestamp = DateTime.Now.ToUniversalTime().ToString(); var identifier = new Identifier("MOBILE", "9999999999"); var address = new Address("string", "string", "string", "string"); var patient = new PatientDemographics(null, "M", "test@sbx", address, 2000, 0, 0, - new List() {identifier}, "1234-5678"); - var profile = new Profile("12345", patient); - var shareProfileRequest = new ShareProfileRequest(requestId, timestamp, profile); + "91-1184-2524-4233","9123456789"); + var shareProfileMetadata = new ShareProfileMetadata("12345", "1","test@hpr.abdm","71.254","78.325"); + var profile = new Profile(patient); + var shareProfileRequest = new ShareProfileRequest("PROFILE_SHARE", shareProfileMetadata, profile); var correlationId = Uuid.Generate().ToString(); var cmSuffix = "ncg"; _patientProfileService.Setup(d => d.IsValidRequest(shareProfileRequest)).Returns(false); var profileShareConfirmation = new ProfileShareConfirmation( - Guid.NewGuid().ToString(), - DateTime.Now.ToUniversalTime().ToString(DateTimeFormat), - new ProfileShareAcknowledgement("test@sbx", - Status.SUCCESS.ToString(),"1"), null, - new Resp(requestId)); + new ProfileShareAcknowledgement( + Status.SUCCESS.ToString(),"test@sbx",new ProfileShareAckProfile(shareProfileMetadata.Context,"1","1800")),null, new Resp(Guid.NewGuid().ToString())); _gatewayClient.Setup( client => client.SendDataToGateway(PATH_PROFILE_ON_SHARE, @@ -122,7 +116,7 @@ private void ShouldThrowBadRequest() .Returns(Task.FromResult("")); Assert.Equal( ((Microsoft.AspNetCore.Mvc.BadRequestResult) _patientController - .StoreDetails(correlationId, shareProfileRequest).Result).StatusCode, + .StoreDetails(correlationId, shareProfileRequest,requestId,timestamp).Result).StatusCode, StatusCodes.Status400BadRequest); } }