Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this functionality probably needs to be in the DistributeParticipant function, I'm currently adding a call to that function from ManageServiceNowParticipant and should have a PR up soon.

The problem is that the ManageServiceNowParticipant function won't know if the participant has been distributed yet, since the DistributeParticipant function has a service bus trigger. The ManageServiceNowParticipant function might be prematurely attempting to update the CohortDistribution table.

Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ public class ManageServiceNowParticipantFunction
private readonly IHttpClientFunction _httpClientFunction;
private readonly IExceptionHandler _exceptionHandler;
private readonly IDataServiceClient<ParticipantManagement> _participantManagementClient;
private readonly IDataServiceClient<CohortDistribution> _cohortDistributionClient;

public ManageServiceNowParticipantFunction(ILogger<ManageServiceNowParticipantFunction> logger, IOptions<ManageServiceNowParticipantConfig> config,
IHttpClientFunction httpClientFunction, IExceptionHandler handleException, IDataServiceClient<ParticipantManagement> participantManagementClient)
IHttpClientFunction httpClientFunction, IExceptionHandler handleException, IDataServiceClient<ParticipantManagement> participantManagementClient,
IDataServiceClient<CohortDistribution> cohortDistributionClient)
{
_logger = logger;
_config = config.Value;
_httpClientFunction = httpClientFunction;
_exceptionHandler = handleException;
_participantManagementClient = participantManagementClient;
_cohortDistributionClient = cohortDistributionClient;
}

/// <summary>
Expand All @@ -44,7 +47,7 @@ public async Task Run([ServiceBusTrigger(topicName: "%ServiceNowParticipantManag
var participantManagement = await _participantManagementClient.GetSingleByFilter(
x => x.NHSNumber == serviceNowParticipant.NhsNumber && x.ScreeningId == serviceNowParticipant.ScreeningId);

var success = await ProcessParticipantRecord(serviceNowParticipant, participantManagement);
var success = await ProcessParticipantRecord(serviceNowParticipant, participantManagement, participantDemographic);
if (!success)
{
await HandleException(new Exception("Participant Management Data Service request failed"), serviceNowParticipant, ServiceNowMessageType.AddRequestInProgress);
Expand Down Expand Up @@ -111,7 +114,7 @@ private async Task<bool> ValidateParticipantData(ServiceNowParticipant serviceNo
return true;
}

private async Task<bool> ProcessParticipantRecord(ServiceNowParticipant serviceNowParticipant, ParticipantManagement? participantManagement)
private async Task<bool> ProcessParticipantRecord(ServiceNowParticipant serviceNowParticipant, ParticipantManagement? participantManagement, ParticipantDemographic participantDemographic)
{
if (participantManagement is null)
{
Expand All @@ -124,7 +127,7 @@ private async Task<bool> ProcessParticipantRecord(ServiceNowParticipant serviceN
return true;
}

return await UpdateExistingParticipant(serviceNowParticipant, participantManagement);
return await UpdateExistingParticipant(serviceNowParticipant, participantManagement, participantDemographic);
}

private async Task<bool> AddNewParticipant(ServiceNowParticipant serviceNowParticipant)
Expand All @@ -149,10 +152,18 @@ private async Task<bool> AddNewParticipant(ServiceNowParticipant serviceNowParti
_logger.LogInformation("Participant with NHS Number: {NhsNumber} set as High Risk", serviceNowParticipant.NhsNumber);
}

return await _participantManagementClient.Add(participantToAdd);
var participantManagementSuccess = await _participantManagementClient.Add(participantToAdd);
if (!participantManagementSuccess)
{
return false;
}

await HandleGpCodeForAddParticipant(serviceNowParticipant);

return true;
}

private async Task<bool> UpdateExistingParticipant(ServiceNowParticipant serviceNowParticipant, ParticipantManagement participantManagement)
private async Task<bool> UpdateExistingParticipant(ServiceNowParticipant serviceNowParticipant, ParticipantManagement participantManagement, ParticipantDemographic participantDemographic)
{
_logger.LogInformation("Existing participant management record found, updating record {ParticipantId}", participantManagement.ParticipantId);

Expand All @@ -163,7 +174,15 @@ private async Task<bool> UpdateExistingParticipant(ServiceNowParticipant service

HandleVhrFlagForExistingParticipant(serviceNowParticipant, participantManagement);

return await _participantManagementClient.Update(participantManagement);
var participantManagementSuccess = await _participantManagementClient.Update(participantManagement);
if (!participantManagementSuccess)
{
return false;
}

await HandleGpCodeForAmendParticipant(serviceNowParticipant, participantDemographic);

return true;
}

private void HandleVhrFlagForExistingParticipant(ServiceNowParticipant serviceNowParticipant, ParticipantManagement participantManagement)
Expand All @@ -182,6 +201,68 @@ private void HandleVhrFlagForExistingParticipant(ServiceNowParticipant serviceNo
}
}

private async Task HandleGpCodeForAddParticipant(ServiceNowParticipant serviceNowParticipant)
{
var hasDummyGpCode = CheckIfHasDummyGpCode(serviceNowParticipant);
if (!hasDummyGpCode)
{
return;
}

_logger.LogInformation("ADD participant with NHS Number: {NhsNumber} has dummy GP code: {GpCode}, updating Cohort Distribution table",
serviceNowParticipant.NhsNumber, serviceNowParticipant.RequiredGpCode);

await UpdateCohortDistributionGpCode(serviceNowParticipant, serviceNowParticipant.RequiredGpCode!, false);
}

private async Task HandleGpCodeForAmendParticipant(ServiceNowParticipant serviceNowParticipant, ParticipantDemographic participantDemographic)
{
if (string.IsNullOrEmpty(participantDemographic.PrimaryCareProvider)) return;

_logger.LogInformation("AMEND participant with NHS Number: {NhsNumber}, overwriting Primary_Care_Provider with PDS data: {UpdatedGpCode}",
serviceNowParticipant.NhsNumber, participantDemographic.PrimaryCareProvider);

await UpdateCohortDistributionGpCode(serviceNowParticipant, participantDemographic.PrimaryCareProvider, true);
}

private async Task UpdateCohortDistributionGpCode(ServiceNowParticipant serviceNowParticipant, string gpCode, bool isAmendParticipant)
{
try
{
var cohortDistribution = await _cohortDistributionClient.GetSingleByFilter(x => x.NHSNumber == serviceNowParticipant.NhsNumber);

if (cohortDistribution == null)
{
_logger.LogError("No Cohort Distribution record found for NHS Number: {NhsNumber}", serviceNowParticipant.NhsNumber);
return;
}

if (isAmendParticipant && cohortDistribution.PrimaryCareProvider == gpCode)
{
_logger.LogInformation("Primary_Care_Provider for NHS Number: {NhsNumber} is already up to date: {GpCode}", serviceNowParticipant.NhsNumber, gpCode);
return;
}

cohortDistribution.PrimaryCareProvider = gpCode;
cohortDistribution.RecordUpdateDateTime = DateTime.UtcNow;

var success = await _cohortDistributionClient.Update(cohortDistribution);
if (success)
{
_logger.LogInformation("Successfully updated Primary Care Provider in Cohort Distribution for NHS Number: {NhsNumber}", serviceNowParticipant.NhsNumber);
}

if (!success)
{
_logger.LogError("Failed to update Primary Care Provider in Cohort Distribution for NHS Number: {NhsNumber}", serviceNowParticipant.NhsNumber);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating Cohort Distribution GP code for NHS Number: {NhsNumber}", serviceNowParticipant.NhsNumber);
}
}

private async Task HandleException(Exception exception, ServiceNowParticipant serviceNowParticipant, ServiceNowMessageType serviceNowMessageType)
{
_logger.LogError(exception, "Exception occurred whilst attempting to add participant from ServiceNow");
Expand Down Expand Up @@ -214,4 +295,10 @@ private static bool CheckIfVhrParticipant(ServiceNowParticipant serviceNowPartic
{
return serviceNowParticipant.ReasonForAdding == ServiceNowReasonsForAdding.VeryHighRisk;
}

private static bool CheckIfHasDummyGpCode(ServiceNowParticipant serviceNowParticipant)
{
return !string.IsNullOrEmpty(serviceNowParticipant.RequiredGpCode) &&
serviceNowParticipant.RequiredGpCode.StartsWith("ZZZ", StringComparison.OrdinalIgnoreCase);
}
}
Loading
Loading