@@ -12,8 +12,6 @@ namespace NHS.CohortManager.DemographicServices;
1212using Microsoft . Extensions . Options ;
1313using System . Threading . Tasks ;
1414using Model ;
15- using System . Net . Http . Json ;
16- using System . Collections . Concurrent ;
1715
1816public class RetrievePdsDemographic
1917{
@@ -22,10 +20,8 @@ public class RetrievePdsDemographic
2220 private readonly IHttpClientFunction _httpClientFunction ;
2321 private readonly RetrievePDSDemographicConfig _config ;
2422 private readonly IFhirPatientDemographicMapper _fhirPatientDemographicMapper ;
25- private readonly IDataServiceClient < ParticipantDemographic > _participantDemographicClient ;
2623 private readonly IBearerTokenService _bearerTokenService ;
27- private readonly ICreateBasicParticipantData _createBasicParticipantData ;
28- private readonly IAddBatchToQueue _addBatchToQueue ;
24+ private readonly IPdsProcessor _pdsProcessor ;
2925 private const string PdsParticipantUrlFormat = "{0}/{1}" ;
3026
3127
@@ -35,21 +31,17 @@ public RetrievePdsDemographic(
3531 IHttpClientFunction httpClientFunction ,
3632 IFhirPatientDemographicMapper fhirPatientDemographicMapper ,
3733 IOptions < RetrievePDSDemographicConfig > retrievePDSDemographicConfig ,
38- IDataServiceClient < ParticipantDemographic > participantDemographicClient ,
39- ICreateBasicParticipantData createBasicParticipantData ,
40- IAddBatchToQueue addBatchToQueue ,
41- IBearerTokenService bearerTokenService
34+ IBearerTokenService bearerTokenService ,
35+ IPdsProcessor pdsProcessor
4236 )
4337 {
4438 _logger = logger ;
4539 _createResponse = createResponse ;
4640 _httpClientFunction = httpClientFunction ;
4741 _fhirPatientDemographicMapper = fhirPatientDemographicMapper ;
4842 _config = retrievePDSDemographicConfig . Value ;
49- _participantDemographicClient = participantDemographicClient ;
50- _createBasicParticipantData = createBasicParticipantData ;
5143 _bearerTokenService = bearerTokenService ;
52- _addBatchToQueue = addBatchToQueue ;
44+ _pdsProcessor = pdsProcessor ;
5345 }
5446
5547 // TODO: Need to send an exception to the EXCEPTION_MANAGEMENT table whenever this function returns a non OK status.
@@ -79,7 +71,7 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
7971
8072 if ( response . StatusCode == HttpStatusCode . NotFound )
8173 {
82- await ProcessPdsNotFoundResponse ( response , nhsNumber ) ;
74+ await _pdsProcessor . ProcessPdsNotFoundResponse ( response , nhsNumber ) ;
8375 return _createResponse . CreateHttpResponse ( HttpStatusCode . NotFound , req , "PDS returned a 404 please database for details" ) ;
8476 }
8577
@@ -88,7 +80,7 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
8880 jsonResponse = await _httpClientFunction . GetResponseText ( response ) ;
8981 var pdsDemographic = _fhirPatientDemographicMapper . ParseFhirJson ( jsonResponse ) ;
9082 var participantDemographic = pdsDemographic . ToParticipantDemographic ( ) ;
91- var upsertResult = await UpsertDemographicRecordFromPDS ( participantDemographic ) ;
83+ var upsertResult = await _pdsProcessor . UpsertDemographicRecordFromPDS ( participantDemographic ) ;
9284
9385 return upsertResult ?
9486 _createResponse . CreateHttpResponse ( HttpStatusCode . OK , req , JsonSerializer . Serialize ( participantDemographic ) ) :
@@ -100,79 +92,4 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
10092 return _createResponse . CreateHttpResponse ( HttpStatusCode . InternalServerError , req ) ;
10193 }
10294 }
103-
104- private async Task ProcessPdsNotFoundResponse ( HttpResponseMessage pdsResponse , string nhsNumber )
105- {
106- var errorResponse = await pdsResponse ! . Content . ReadFromJsonAsync < PdsErrorResponse > ( ) ;
107- // we now create a record as an update record and send to the manage participant function. Reason for removal for date should be today and the reason for remove of ORR
108- if ( errorResponse ! . issue ! . FirstOrDefault ( ) ! . details ! . coding ! . FirstOrDefault ( ) ! . code == PdsConstants . InvalidatedResourceCode )
109- {
110- var pdsDemographic = new PdsDemographic ( )
111- {
112- NhsNumber = nhsNumber ,
113- PrimaryCareProvider = null ,
114- ReasonForRemoval = PdsConstants . OrrRemovalReason ,
115- RemovalEffectiveFromDate = DateTime . UtcNow . Date . ToString ( "yyyyMMdd" )
116- } ;
117- var participant = new Participant ( pdsDemographic ) ;
118- participant . RecordType = Actions . Removed ;
119- //sends record for an update
120- await ProcessRecord ( participant ) ;
121- return ;
122- }
123- _logger . LogError ( "the PDS function has returned a 404 error. function now stopping processing" ) ;
124- }
125-
126-
127- private async Task ProcessRecord ( Participant participant )
128- {
129- var updateRecord = new ConcurrentQueue < BasicParticipantCsvRecord > ( ) ;
130- participant . RecordType = participant . RecordType = Actions . Removed ;
131-
132- var basicParticipantCsvRecord = new BasicParticipantCsvRecord
133- {
134- BasicParticipantData = _createBasicParticipantData . BasicParticipantData ( participant ) ,
135- FileName = PdsConstants . DefaultFileName ,
136- Participant = participant
137- } ;
138-
139- updateRecord . Enqueue ( basicParticipantCsvRecord ) ;
140-
141- _logger . LogInformation ( "Sending record to the update queue." ) ;
142- await _addBatchToQueue . ProcessBatch ( updateRecord , _config . ParticipantManagementTopic ) ;
143- }
144-
145-
146- private async Task < bool > UpsertDemographicRecordFromPDS ( ParticipantDemographic participantDemographic )
147- {
148- ParticipantDemographic oldParticipantDemographic = await _participantDemographicClient . GetSingleByFilter ( i => i . NhsNumber == participantDemographic . NhsNumber ) ;
149-
150- if ( oldParticipantDemographic == null )
151- {
152- _logger . LogInformation ( "Participant Demographic record not found, attemping to add Participant Demographic." ) ;
153- bool addSuccess = await _participantDemographicClient . Add ( participantDemographic ) ;
154-
155- if ( addSuccess )
156- {
157- _logger . LogInformation ( "Successfully added Participant Demographic." ) ;
158- return true ;
159- }
160-
161- _logger . LogError ( "Failed to add Participant Demographic." ) ;
162- return false ;
163- }
164-
165- _logger . LogInformation ( "Participant Demographic record found, attempting to update Participant Demographic." ) ;
166- participantDemographic . ParticipantId = oldParticipantDemographic . ParticipantId ;
167- bool updateSuccess = await _participantDemographicClient . Update ( participantDemographic ) ;
168-
169- if ( updateSuccess )
170- {
171- _logger . LogInformation ( "Successfully updated Participant Demographic." ) ;
172- return true ;
173- }
174-
175- _logger . LogError ( "Failed to update Participant Demographic." ) ;
176- return false ;
177- }
17895}
0 commit comments