Skip to content

Commit 440e548

Browse files
feat: RetrievePdsDemographic function adds demographic data to database (#1179)
* refactor: Updated namespace for consistency * feat: Implemented insert behaviour for when participant demographic doesn't exist * test: Updated unit tests * test: Fixed broken unit test
1 parent e6794b3 commit 440e548

File tree

7 files changed

+220
-157
lines changed

7 files changed

+220
-157
lines changed

application/CohortManager/src/Functions/DemographicServices/RetrievePDSDemographic/HealthCheckFunction.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ await response.WriteAsJsonAsync(new
3030
return response;
3131
}
3232
}
33-

application/CohortManager/src/Functions/DemographicServices/RetrievePDSDemographic/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
66
using DataServices.Client;
7-
using NHS.Screening.RetrievePDSDemographic;
87
using Model;
8+
using NHS.CohortManager.DemographicServices;
99

1010
var host = new HostBuilder()
1111
.AddConfiguration<RetrievePDSDemographicConfig>(out RetrievePDSDemographicConfig config)

application/CohortManager/src/Functions/DemographicServices/RetrievePDSDemographic/RetrievePDSDemographic.cs

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace NHS.CohortManager.DemographicServices;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.Extensions.Options;
1212
using Model;
13-
using Model.Enums;
14-
using NHS.Screening.RetrievePDSDemographic;
1513

1614
public class RetrievePdsDemographic
1715
{
@@ -56,71 +54,59 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
5654

5755
var response = await _httpClientFunction.SendPdsGet(url);
5856

59-
if (response.StatusCode == HttpStatusCode.OK)
60-
{
61-
var jsonResponse = await _httpClientFunction.GetResponseText(response);
62-
var demographic = _fhirPatientDemographicMapper.ParseFhirJson(jsonResponse);
63-
var updatedParticipantDemographic = demographic.ToParticipantDemographic();
64-
var updateResult = await UpdateDemographicRecordFromPDS(updatedParticipantDemographic);
65-
return updateResult switch
66-
{
67-
UpdateResult.Success => CreateSuccessResponse(req, demographic),
68-
UpdateResult.NotFound => HandleParticipantNotFound(req),
69-
UpdateResult.UpdateFailed => HandleUpdateFailure(req),
70-
_ => HandleUnexpectedUpdateResult(req)
71-
};
72-
}
73-
7457
if (response.StatusCode == HttpStatusCode.NotFound)
7558
{
7659
return _createResponse.CreateHttpResponse(HttpStatusCode.NotFound, req);
7760
}
7861

79-
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
62+
response.EnsureSuccessStatusCode();
63+
64+
var jsonResponse = await _httpClientFunction.GetResponseText(response);
65+
var pdsDemographic = _fhirPatientDemographicMapper.ParseFhirJson(jsonResponse);
66+
var participantDemographic = pdsDemographic.ToParticipantDemographic();
67+
var upsertResult = await UpsertDemographicRecordFromPDS(participantDemographic);
8068

69+
return upsertResult ?
70+
_createResponse.CreateHttpResponse(HttpStatusCode.OK, req, JsonSerializer.Serialize(participantDemographic)) :
71+
_createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
8172
}
8273
catch (Exception ex)
8374
{
84-
_logger.LogError(ex, "There has been an error fetching PDS participant data: {Message}", ex.Message);
75+
_logger.LogError(ex, "There has been an error retrieving PDS participant data.");
8576
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
8677
}
8778
}
8879

89-
private async Task<UpdateResult> UpdateDemographicRecordFromPDS(ParticipantDemographic updatedParticipantDemographic)
80+
private async Task<bool> UpsertDemographicRecordFromPDS(ParticipantDemographic participantDemographic)
9081
{
91-
// Check participant exists in Participant Demographic table.
92-
ParticipantDemographic oldParticipantDemographic = await _participantDemographicClient.GetSingleByFilter(i => i.NhsNumber == updatedParticipantDemographic.NhsNumber);
82+
ParticipantDemographic oldParticipantDemographic = await _participantDemographicClient.GetSingleByFilter(i => i.NhsNumber == participantDemographic.NhsNumber);
83+
9384
if (oldParticipantDemographic == null)
9485
{
95-
_logger.LogWarning("The participant could not be found, when trying to update old Participant from PDS.");
96-
return UpdateResult.NotFound;
97-
}
98-
updatedParticipantDemographic.ParticipantId = oldParticipantDemographic.ParticipantId;
99-
bool updateSuccess = await _participantDemographicClient.Update(updatedParticipantDemographic);
100-
return updateSuccess ? UpdateResult.Success : UpdateResult.UpdateFailed;
101-
}
86+
_logger.LogInformation("Participant Demographic record not found, attemping to add Participant Demographic.");
87+
bool addSuccess = await _participantDemographicClient.Add(participantDemographic);
10288

103-
private HttpResponseData CreateSuccessResponse(HttpRequestData req, PdsDemographic demographic)
104-
{
105-
return _createResponse.CreateHttpResponse(HttpStatusCode.OK,req,JsonSerializer.Serialize(demographic));
106-
}
89+
if (addSuccess)
90+
{
91+
_logger.LogInformation("Successfully added Participant Demographic.");
92+
return true;
93+
}
10794

108-
private HttpResponseData HandleParticipantNotFound(HttpRequestData req)
109-
{
110-
_logger.LogWarning("Participant not found when updating from PDS for NHS number");
111-
return _createResponse.CreateHttpResponse(HttpStatusCode.NotFound, req);
112-
}
95+
_logger.LogError("Failed to add Participant Demographic.");
96+
return false;
97+
}
11398

114-
private HttpResponseData HandleUpdateFailure(HttpRequestData req)
115-
{
116-
_logger.LogError("Failed to update Demographic record from PDS.");
117-
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
118-
}
99+
_logger.LogInformation("Participant Demographic record found, attempting to update Participant Demographic.");
100+
participantDemographic.ParticipantId = oldParticipantDemographic.ParticipantId;
101+
bool updateSuccess = await _participantDemographicClient.Update(participantDemographic);
119102

120-
private HttpResponseData HandleUnexpectedUpdateResult(HttpRequestData req)
121-
{
122-
_logger.LogError("Unexpected result when updating participant demographic record from PDS.");
123-
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
124-
}
103+
if (updateSuccess)
104+
{
105+
_logger.LogInformation("Successfully updated Participant Demographic.");
106+
return true;
107+
}
125108

109+
_logger.LogError("Failed to update Participant Demographic.");
110+
return false;
111+
}
126112
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
namespace NHS.Screening.RetrievePDSDemographic;
1+
namespace NHS.CohortManager.DemographicServices;
22

33
using System.ComponentModel.DataAnnotations;
44

55
public class RetrievePDSDemographicConfig
66
{
77
[Required]
8-
public string RetrievePdsParticipantURL { get; set; }
8+
public required string RetrievePdsParticipantURL { get; set; }
99

1010
[Required]
11-
public string DemographicDataServiceURL { get; set; }
11+
public required string DemographicDataServiceURL { get; set; }
1212
}

application/CohortManager/src/Functions/Shared/Model/Enums/UpdateResult.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)