Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAH-4157 | Migrate HIP Initiated Linking Flow to use ABDM v3 APIs #181

Merged
merged 8 commits into from
Feb 4, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ namespace In.ProjectEKA.HipLibrary.Patient.Model
{
public class CareContextRepresentation
{
[JsonConstructor]

public CareContextRepresentation(string referenceNumber, string display)
{
ReferenceNumber = referenceNumber;
Display = display;
}


[JsonConstructor]
public CareContextRepresentation(string referenceNumber, string display, string type,
IEnumerable<HiType> hiTypes)
{
Expand Down
18 changes: 9 additions & 9 deletions src/In.ProjectEKA.HipService/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static class Constants
public const string APP_PATH_VERIFICATION_ABHAADDRESS_PROFILE = "/" + VERSION_V3 + "/hip/verification/abhaAddress/getProfile";
public const string APP_PATH_VERIFICATION_ABHAADDRESS_CARD = "/" + VERSION_V3 + "/hip/verification/abhaAddress/getCard";

public const string PATH_GENERATE_TOKEN = "/api/hiecm/"+ VERSION_V3 +"/token/generate-token";
public const string PATH_ON_GENERATE_TOKEN = "/api/"+ VERSION_V3 +"/hip/token/on-generate-token";

public const string PATH_SESSIONS = "api/hiecm/gateway/"+VERSION_V3+"/sessions";
public const string PATH_CARE_CONTEXTS_DISCOVER = "/api/"+VERSION_V3 + "/hip/patient/care-context/discover";
public const string PATH_CONSENTS_HIP = "/api/" + VERSION_V3 + "/consent/request/hip/notify";
Expand All @@ -39,8 +42,8 @@ public static class Constants
public const string PATH_ON_AUTH_CONFIRM = CURRENT_VERSION + "/users/auth/on-confirm";
public const string PATH_ON_AUTH_INIT = "/" + CURRENT_VERSION + "/users/auth/on-init";
public const string PATH_ON_FETCH_AUTH_MODES = "/" + CURRENT_VERSION + "/users/auth/on-fetch-modes";
public const string PATH_ON_ADD_CONTEXTS = "/" + CURRENT_VERSION + "/links/link/on-add-contexts";
public const string PATH_ON_NOTIFY_CONTEXTS = "/" + CURRENT_VERSION + "/links/context/on-notify";
public const string PATH_ON_ADD_CONTEXTS = "/api/"+ VERSION_V3 +"/link/on_carecontext";
public const string PATH_ON_NOTIFY_CONTEXTS = "/api/"+ VERSION_V3 +"/links/context/on-notify";
public static readonly string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
public static readonly string PATH_ON_DISCOVER = "/api/hiecm/user-initiated-linking/"+VERSION_V3+"/patient/care-context/on-discover";
public static readonly string PATH_ON_LINK_INIT = "/api/hiecm/user-initiated-linking/"+VERSION_V3+"/link/care-context/on-init";
Expand All @@ -65,26 +68,23 @@ public static class Constants
public const string TIMESTAMP = "TIMESTAMP";
public const string PATH_PATIENT_PROFILE_ON_SHARE = "/" + CURRENT_VERSION + "/patients/profile/on-share";
public const string PATH_FETCH_AUTH_MODES = "/" + CURRENT_VERSION + "/users/auth/fetch-modes";
public const string PATH_ADD_PATIENT_CONTEXTS = "/" + CURRENT_VERSION + "/links/link/add-contexts";
public const string PATH_NOTIFY_PATIENT_CONTEXTS = "/" + CURRENT_VERSION + "/links/context/notify";
public const string PATH_ADD_PATIENT_CONTEXTS = "/api/hiecm/hip/" + VERSION_V3 + "/link/carecontext";
public const string PATH_NOTIFY_PATIENT_CONTEXTS = "/api/hiecm/hip/"+ VERSION_V3 +"/link/context/notify";
public const string PATH_FETCH_MODES = "/" + CURRENT_VERSION + "/hip/fetch-modes";
public const string PATH_NEW_CARECONTEXT = "/" + CURRENT_VERSION + "/hip/new-carecontext";
public const string PATH_ADD_CONTEXTS = "/" + CURRENT_VERSION + "/hip/add-contexts";
public const string PATH_NOTIFY_CONTEXTS = "/" + CURRENT_VERSION + "/hip/notify";
public const string KYC_AND_LINK = "KYC_AND_LINK";
public const string HIP = "HIP";
public const string PATH_AUTH_INIT = "/" + CURRENT_VERSION + "/users/auth/init";
public const string PATH_HIP_AUTH_INIT = "/" + CURRENT_VERSION + "/hip/auth/init";
public const string PATH_HIP_AUTH_CONFIRM = "/" + CURRENT_VERSION + "/hip/auth/confirm";
public const string PATH_ADD_NDHM_DEMOGRAPHICS = "/" + CURRENT_VERSION + "/hip/ndhm-demographics";
public const string PATH_DEMOGRAPHICS = "/" + CURRENT_VERSION + "/hip/auth/demographics";
public const string PATH_HIP_DIRECT_AUTH = "/" + CURRENT_VERSION + "/hip/auth/direct";
public const string PATH_AUTH_NOTIFY = "/" + CURRENT_VERSION + "/users/auth/notify";
public const string PATH_AUTH_ON_NOTIFY = "/" + CURRENT_VERSION + "/users/auth/on-notify";

public const string PATH_HIP_SMS_NOTIFY = "/" + CURRENT_VERSION + "/hip/patients/sms/notify";
public const string PATH_SMS_NOTIFY = "/" + CURRENT_VERSION + "/patients/sms/notify2";
public const string PATH_SMS_ON_NOTIFY = "/" + CURRENT_VERSION + "/patients/sms/on-notify";
public const string PATH_SMS_NOTIFY = "/api/hiecm/hip/"+ VERSION_V3 +"/link/patient/links/sms/notify2";
public const string PATH_SMS_ON_NOTIFY = "/api/"+ VERSION_V3 +"/patients/sms/on-notify";

public const string REPORTING_SESSION = "reporting_session";
public const string OPENMRS_SESSION_ID_COOKIE_NAME = "JSESSIONID";
Expand Down
12 changes: 10 additions & 2 deletions src/In.ProjectEKA.HipService/Common/HttpRequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public static HttpRequestMessage CreateHttpRequest<T>(
string correlationId,
string xtoken = null,
string tToken = null,
string transactionId = null)
string transactionId = null,
string hipId = null,
string requestId = null,
string linkToken = null
)
{
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, new Uri($"{url}"));
;
Expand Down Expand Up @@ -56,7 +60,11 @@ public static HttpRequestMessage CreateHttpRequest<T>(
httpRequestMessage.Headers.Add(CORRELATION_ID, correlationId);
if (transactionId != null)
httpRequestMessage.Headers.Add("Transaction_Id", transactionId);
httpRequestMessage.Headers.Add("REQUEST-ID", Guid.NewGuid().ToString());
if(hipId != null)
httpRequestMessage.Headers.Add("X-HIP-ID", hipId);
if(linkToken !=null)
httpRequestMessage.Headers.Add("X-LINK-TOKEN", linkToken);
httpRequestMessage.Headers.Add("REQUEST-ID", requestId ?? Guid.NewGuid().ToString());
httpRequestMessage.Headers.Add("TIMESTAMP", DateTime.UtcNow.ToString(TIMESTAMP_FORMAT));
return httpRequestMessage;
}
Expand Down
10 changes: 5 additions & 5 deletions src/In.ProjectEKA.HipService/Gateway/GatewayClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace In.ProjectEKA.HipService.Gateway

public interface IGatewayClient
{
Task SendDataToGateway<T>(string urlPath, T response, string cmSuffix,string correlationId);
Task SendDataToGateway<T>(string urlPath, T response, string cmSuffix,string correlationId, string hipId = null, string requestId = null, string linkToken = null);
Task<HttpResponseMessage> CallABHAService<T>(HttpMethod method, string baseUrl, string urlPath, T representation,
string correlationId, string xtoken = null, string tToken = null, string transactionId = null);
}
Expand Down Expand Up @@ -83,9 +83,9 @@ public virtual async Task<Option<string>> Authenticate(String correlationId)
}
}

public virtual async Task SendDataToGateway<T>(string urlPath, T response, string cmSuffix, string correlationId)
public virtual async Task SendDataToGateway<T>(string urlPath, T response, string cmSuffix, string correlationId, string hipId = null,string requestId=null, string linkToken = null)
{
await PostTo(configuration.Url + urlPath, response, cmSuffix, correlationId).ConfigureAwait(false);
await PostTo(configuration.Url + urlPath, response, cmSuffix, correlationId, hipId,requestId,linkToken).ConfigureAwait(false);
}

public virtual async Task<HttpResponseMessage> CallABHAService<T>(HttpMethod method, string baseUrl,string urlPath,
Expand Down Expand Up @@ -114,7 +114,7 @@ public virtual async Task<HttpResponseMessage> CallABHAService<T>(HttpMethod met
return response;
}

private async Task PostTo<T>(string gatewayUrl, T representation, string cmSuffix, string correlationId)
private async Task PostTo<T>(string gatewayUrl, T representation, string cmSuffix, string correlationId, string hipId, string requestId, string linkToken = null)
{
try
{
Expand All @@ -127,7 +127,7 @@ private async Task PostTo<T>(string gatewayUrl, T representation, string cmSuffi
Log.Debug("Request Payload {@payload}", representation);
var responseMessage = await httpClient
.SendAsync(CreateHttpRequest(HttpMethod.Post, gatewayUrl, representation, accessToken,
cmSuffix, correlationId))
cmSuffix, correlationId, hipId: hipId,requestId:requestId,linkToken:linkToken))
.ConfigureAwait(false);
Log.Information("Response Status from Gateway for URI {@uri} is {@status}", gatewayUrl,
responseMessage.StatusCode);
Expand Down
95 changes: 8 additions & 87 deletions src/In.ProjectEKA.HipService/Link/CareContextController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Threading.Tasks;
using In.ProjectEKA.HipLibrary.Patient.Model;
using In.ProjectEKA.HipService.Common;
using In.ProjectEKA.HipService.Gateway;
using In.ProjectEKA.HipService.Link.Model;
using In.ProjectEKA.HipService.Logger;
using In.ProjectEKA.HipService.UserAuth.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace In.ProjectEKA.HipService.Link
{
Expand All @@ -17,106 +15,29 @@ namespace In.ProjectEKA.HipService.Link
[ApiController]
public class CareContextController : Controller
{
private readonly GatewayConfiguration gatewayConfiguration;
private readonly IGatewayClient gatewayClient;
private readonly ILogger<CareContextController> logger;
private readonly ICareContextService careContextService;
private readonly ILinkPatientRepository linkPatientRepository;

public CareContextController(IGatewayClient gatewayClient,
ILogger<CareContextController> logger,
GatewayConfiguration gatewayConfiguration,
public CareContextController(
ICareContextService careContextService,
ILinkPatientRepository linkPatientRepository
)
{
this.gatewayClient = gatewayClient;
this.logger = logger;
this.gatewayConfiguration = gatewayConfiguration;
this.careContextService = careContextService;
this.linkPatientRepository = linkPatientRepository;

}

[Route(PATH_ADD_CONTEXTS)]
public async Task<ActionResult> AddContexts(
[FromHeader(Name = CORRELATION_ID)] string correlationId, [FromBody] AddContextsRequest addContextsRequest)
{
await careContextService.SetAccessToken(addContextsRequest.ConsentManagerUserId);
var cmSuffix = gatewayConfiguration.CmSuffix;
var (gatewayAddContextsRequestRepresentation, error) =
await careContextService.AddContextsResponse(addContextsRequest,cmSuffix);
if (error != null)
return StatusCode(StatusCodes.Status400BadRequest, error);
Guid requestId = gatewayAddContextsRequestRepresentation.RequestId;
try
{
logger.Log(LogLevel.Information,
LogEvents.AddContext,
"Request for add-context to gateway: {@GatewayResponse}",
gatewayAddContextsRequestRepresentation.dump(gatewayAddContextsRequestRepresentation));
logger.Log(LogLevel.Information, LogEvents.AddContext, $"cmSuffix: {{cmSuffix}}," +
$" correlationId: {{correlationId}}, " +
$"requestId: {{requestId}}",
cmSuffix, correlationId, requestId);
await gatewayClient.SendDataToGateway(PATH_ADD_PATIENT_CONTEXTS,
gatewayAddContextsRequestRepresentation,
cmSuffix, correlationId);
return Accepted();
}
catch (Exception exception)
{
logger.LogError(LogEvents.AddContext, exception, "Error happened for requestId: {RequestId} for" +
" add-care context request", requestId);
}

return StatusCode(StatusCodes.Status504GatewayTimeout,
new ErrorRepresentation(new Error(ErrorCode.GatewayTimedOut, "Gateway timed out")));
}

[Route(PATH_NOTIFY_CONTEXTS)]
public async Task<ActionResult> NotificationContext(
[FromHeader(Name = CORRELATION_ID)] string correlationId,
[FromBody] NotifyContextRequest notifyContextRequest)
{
var (gatewayNotificationContextRepresentation, error) =
careContextService.NotificationContextResponse(notifyContextRequest);
if (error != null)
return StatusCode(StatusCodes.Status400BadRequest, error);
Guid requestId = gatewayNotificationContextRepresentation.requestId;
var cmSuffix = gatewayConfiguration.CmSuffix;
try
{
logger.Log(LogLevel.Information,
LogEvents.AddContext,
"Request for notification-contexts to gateway: {@GatewayResponse}",
gatewayNotificationContextRepresentation.dump(gatewayNotificationContextRepresentation));
await gatewayClient.SendDataToGateway(PATH_NOTIFY_PATIENT_CONTEXTS,
gatewayNotificationContextRepresentation,
cmSuffix, correlationId);
return Accepted();
}
catch (Exception exception)
{
logger.LogError(LogEvents.AddContext, exception, "Error happened for requestId: {RequestId} for" +
" notification-care context request", requestId);
}

return StatusCode(StatusCodes.Status504GatewayTimeout,
new ErrorRepresentation(new Error(ErrorCode.GatewayTimedOut, "Gateway timed out")));
}


[Authorize]
[HttpPost(PATH_ON_NOTIFY_CONTEXTS)]
public AcceptedResult HipLinkOnNotifyContexts(HipLinkContextConfirmation confirmation)
public AcceptedResult HipLinkOnNotifyContexts(HipLinkOnNotifyConfirmation confirmation)
{
Log.Information("Link on-notify context received." +
$" RequestId:{confirmation.RequestId}, " +
$" Timestamp:{confirmation.Timestamp}");
Log.Information("Link on-notify context received.");
if (confirmation.Error != null)
Log.Information($" Error Code:{confirmation.Error.Code}," +
$" Error Message:{confirmation.Error.Message}");
else if (confirmation.Acknowledgement != null)
Log.Information($" Acknowledgment Status:{confirmation.Acknowledgement.Status}");
Log.Information($" Resp RequestId:{confirmation.Resp.RequestId}");
Log.Information($" Resp RequestId:{confirmation.Response.RequestId}");
return Accepted();
}

Expand Down
Loading
Loading