@@ -10,6 +10,8 @@ namespace NHS.CohortManager.CohortDistributionServices;
1010using Model . Enums ;
1111using DataServices . Client ;
1212using Microsoft . Extensions . Options ;
13+ using Newtonsoft . Json . Linq ;
14+ using RulesEngine . Models ;
1315
1416public class ValidateParticipant
1517{
@@ -36,7 +38,7 @@ public ValidateParticipant(IDataServiceClient<CohortDistribution> cohortDistribu
3638 }
3739
3840 /// <summary>
39- /// Orchestration for the validation and trasnformation process
41+ /// Orchestrator for the validation and trasnformation process
4042 /// </summary>
4143 /// <param name="context">Context containing a validation record</param>
4244 /// <returns>The transformed <see cref="CohortDistributionParticipant"/>, or null if the validation/ transformation failed</returns>
@@ -52,21 +54,28 @@ public ValidateParticipant(IDataServiceClient<CohortDistribution> cohortDistribu
5254 var previousRecord = await context . CallActivityAsync < CohortDistributionParticipant > ( nameof ( GetCohortDistributionRecord ) , validationRecord . Participant . ParticipantId ) ;
5355 validationRecord . PreviousParticipantRecord = previousRecord ;
5456
57+ // Remove Previous Validation Errors from DB
58+ await context . CallActivityAsync ( nameof ( RemoveOldValidationExceptions ) , new OldExceptionRecord ( )
59+ {
60+ NhsNumber = validationRecord . Participant . NhsNumber ,
61+ ScreeningName = validationRecord . Participant . ScreeningName
62+ } ) ;
63+
5564 // Lookup & Static Validation
5665 _logger . LogInformation ( "Validating participant" ) ;
57- ValidationExceptionLog [ ] validationResults = await Task . WhenAll (
58- context . CallActivityAsync < ValidationExceptionLog > ( nameof ( StaticValidation ) , validationRecord ) ,
59- context . CallActivityAsync < ValidationExceptionLog > ( nameof ( LookupValidation ) , validationRecord )
60- ) ;
66+ var staticTask = context . CallActivityAsync < List < ValidationRuleResult > > ( nameof ( StaticValidation ) , validationRecord ) ;
67+ var lookupTask = context . CallActivityAsync < List < ValidationRuleResult > > ( nameof ( LookupValidation ) , validationRecord ) ;
68+
69+ await Task . WhenAll ( staticTask , lookupTask ) ;
6170
62- var validationResult = new ValidationExceptionLog ( validationResults [ 0 ] , validationResults [ 1 ] ) ;
71+ var validationResult = staticTask . Result . Concat ( lookupTask . Result ) . ToList ( ) ;
6372
6473 // Update exception flag and return
65- if ( validationResult . CreatedException )
74+ if ( validationResult . Any ( ) )
6675 {
67- _logger . LogError ( "Participant {ParticipantId} triggered a validation rule" , validationRecord . Participant . ParticipantId ) ;
76+ _logger . LogWarning ( "Participant {ParticipantId} triggered a validation rule" , validationRecord . Participant . ParticipantId ) ;
6877
69- await context . CallActivityAsync ( nameof ( UpdateExceptionFlag ) , validationRecord . Participant . ParticipantId ) ;
78+ await context . CallActivityAsync ( nameof ( HandleValidationExceptions ) , new ValidationExceptionRecord ( validationRecord , validationResult ) ) ;
7079
7180 if ( ! _config . IgnoreParticipantExceptions )
7281 {
@@ -115,13 +124,23 @@ public async Task<CohortDistributionParticipant> GetCohortDistributionRecord([Ac
115124 }
116125 }
117126
127+ /// <summary>
128+ /// Calls the remove old validation exception function
129+ /// </summary>
130+ [ Function ( nameof ( RemoveOldValidationExceptions ) ) ]
131+ public async Task RemoveOldValidationExceptions ( [ ActivityTrigger ] OldExceptionRecord request )
132+ {
133+ string json = JsonSerializer . Serialize ( request ) ;
134+ await _httpClient . SendPost ( _config . RemoveOldValidationRecordUrl , json ) ;
135+ }
136+
118137 /// <summary>
119138 /// Calls static validation
120139 /// </summary>
121140 /// <param name="validationRecord"></param>
122141 /// <returns>A <see cref="ValidationExceptionLog"/> representing if the participant has triggered a rule</returns>
123142 [ Function ( nameof ( StaticValidation ) ) ]
124- public async Task < ValidationExceptionLog > StaticValidation ( [ ActivityTrigger ] ValidationRecord validationRecord )
143+ public async Task < List < ValidationRuleResult > ? > StaticValidation ( [ ActivityTrigger ] ValidationRecord validationRecord )
125144 {
126145 var request = new ParticipantCsvRecord
127146 {
@@ -133,9 +152,14 @@ public async Task<ValidationExceptionLog> StaticValidation([ActivityTrigger] Val
133152
134153 var response = await _httpClient . SendPost ( _config . StaticValidationURL , json ) ;
135154 response . EnsureSuccessStatusCode ( ) ;
155+
156+ if ( response . StatusCode == HttpStatusCode . NoContent )
157+ {
158+ return new List < ValidationRuleResult > ( ) ;
159+ }
136160 string body = await _httpClient . GetResponseText ( response ) ;
137161
138- var exceptionLog = JsonSerializer . Deserialize < ValidationExceptionLog > ( body ) ;
162+ var exceptionLog = JsonSerializer . Deserialize < List < ValidationRuleResult > > ( body ) ;
139163
140164 return exceptionLog ;
141165 }
@@ -146,7 +170,7 @@ public async Task<ValidationExceptionLog> StaticValidation([ActivityTrigger] Val
146170 /// <param name="validationRecord"></param>
147171 /// <returns>A <see cref="ValidationExceptionLog"/> representing if the participant has triggered a rule</returns>
148172 [ Function ( nameof ( LookupValidation ) ) ]
149- public async Task < ValidationExceptionLog > LookupValidation ( [ ActivityTrigger ] ValidationRecord validationRecord )
173+ public async Task < List < ValidationRuleResult > > LookupValidation ( [ ActivityTrigger ] ValidationRecord validationRecord )
150174 {
151175 var request = new LookupValidationRequestBody
152176 {
@@ -159,9 +183,14 @@ public async Task<ValidationExceptionLog> LookupValidation([ActivityTrigger] Val
159183
160184 var response = await _httpClient . SendPost ( _config . LookupValidationURL , json ) ;
161185 response . EnsureSuccessStatusCode ( ) ;
186+ if ( response . StatusCode == HttpStatusCode . NoContent )
187+ {
188+ return new List < ValidationRuleResult > ( ) ;
189+ }
190+
162191 string body = await _httpClient . GetResponseText ( response ) ;
163192
164- var exceptionLog = JsonSerializer . Deserialize < ValidationExceptionLog > ( body ) ;
193+ var exceptionLog = JsonSerializer . Deserialize < List < ValidationRuleResult > > ( body ) ;
165194 return exceptionLog ;
166195 }
167196
@@ -194,20 +223,30 @@ public async Task<ValidationExceptionLog> LookupValidation([ActivityTrigger] Val
194223 }
195224
196225 /// <summary>
197- /// Updates the exception flag in participant management
226+ /// Creates a validation excpetion and updates the exception flag
227+ /// in participant management
198228 /// </summary>
199- /// <param name="participantId"></param>
200229 /// <exception cref="IOException">Thrown if the update fails</exception>
201- [ Function ( nameof ( UpdateExceptionFlag ) ) ]
202- public async Task UpdateExceptionFlag ( [ ActivityTrigger ] string participantId )
230+ [ Function ( nameof ( HandleValidationExceptions ) ) ]
231+ public async Task HandleValidationExceptions ( [ ActivityTrigger ] ValidationExceptionRecord validationExceptionRecord )
203232 {
204- var participantManagement = await _participantManagementClient . GetSingle ( participantId ) ;
233+ // Send exceptions to DB
234+ ParticipantCsvRecord participantRecord = new ( )
235+ {
236+ Participant = new Participant ( validationExceptionRecord . ValidationRecord . Participant ) ,
237+ FileName = validationExceptionRecord . ValidationRecord . FileName
238+ } ;
239+
240+ var exceptionCreated = await _exceptionHandler . CreateValidationExceptionLog ( validationExceptionRecord . ValidationExceptions , participantRecord ) ;
241+
242+ var participantManagement = await _participantManagementClient . GetSingle ( participantRecord . Participant . ParticipantId ) ;
205243 participantManagement . ExceptionFlag = 1 ;
206244
207245 var exceptionFlagUpdated = await _participantManagementClient . Update ( participantManagement ) ;
208246 if ( ! exceptionFlagUpdated )
209247 {
210248 throw new IOException ( "Failed to update exception flag" ) ;
211249 }
250+ _logger . LogInformation ( "Created validation exception and set exception flag to 1 for participant {ParticipantId}" , participantRecord . Participant . ParticipantId ) ;
212251 }
213252}
0 commit comments