@@ -10,8 +10,6 @@ namespace NHS.CohortManager.DemographicServices;
1010using Microsoft . Extensions . Logging ;
1111using Microsoft . Extensions . Options ;
1212using Model ;
13- using Model . Enums ;
14- using NHS . Screening . RetrievePDSDemographic ;
1513
1614public 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}
0 commit comments