Skip to content

Commit e7f399e

Browse files
Merge branch 'main' into feat/DTOSS-7351-adding-new-ReceiveServiceNowMessage-function
2 parents 4fe0c8a + f720e05 commit e7f399e

26 files changed

Lines changed: 512 additions & 358 deletions

File tree

application/CohortManager/compose.core.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ services:
376376
- DemographicDataServiceURI=http://localhost:7072/api/DemographicDataService
377377
- ExceptionFunctionURL=http://localhost:7070/api/CreateException
378378
- ParticipantDemographicDataServiceURL=http://localhost:7993/api/ParticipantDemographicDataService
379+
retrieve-pds-demographic:
380+
container_name: retrieve-pds-demographic
381+
network_mode: host
382+
build:
383+
context: ./src/Functions/
384+
dockerfile: DemographicServices/RetrievePDSDemographic/Dockerfile
385+
environment:
386+
- ASPNETCORE_URLS=http://*:8082
387+
- ExceptionFunctionURL=http://localhost:7070/api/CreateException
388+
- ParticipantDemographicDataServiceURL=http://localhost:7993/api/ParticipantDemographicDataService
379389

380390
durable-demographic-function:
381391
container_name: durable-demographic-function

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/ITransformDataLookupFacade.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ public interface ITransformDataLookupFacade
33
{
44
bool ValidateOutcode(string postcode);
55
string GetBsoCode(string postcode);
6+
public string GetBsoCodeUsingPCP(string primaryCareProvider);
67
}

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
.ConfigureServices(services =>
2222
{
2323
services.AddSingleton<ICreateResponse, CreateResponse>();
24-
services.AddScoped<IBsTransformationLookups, BsTransformationLookups>();
2524
services.AddSingleton<ITransformDataLookupFacade, TransformDataLookupFacade>();
2625
services.AddSingleton<ITransformReasonForRemoval, TransformReasonForRemoval>();
2726
})

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/TransformDataLookupFacade.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ public class TransformDataLookupFacade : ITransformDataLookupFacade
88
{
99
private readonly ILogger<TransformDataLookupFacade> _logger;
1010
private readonly IDataServiceClient<BsSelectOutCode> _outcodeClient;
11-
public TransformDataLookupFacade(ILogger<TransformDataLookupFacade> logger, IDataServiceClient<BsSelectOutCode> outcodeClient)
11+
private readonly IDataServiceClient<BsSelectGpPractice> _bsSelectGPPracticeClient;
12+
public TransformDataLookupFacade(ILogger<TransformDataLookupFacade> logger,
13+
IDataServiceClient<BsSelectOutCode> outcodeClient,
14+
IDataServiceClient<BsSelectGpPractice> bsSelectGPPracticeClient)
1215
{
1316
_logger = logger;
1417
_outcodeClient = outcodeClient;
18+
_bsSelectGPPracticeClient = bsSelectGPPracticeClient;
1519
}
1620

1721
public bool ValidateOutcode(string postcode)
@@ -30,4 +34,20 @@ public string GetBsoCode(string postcode){
3034
}
3135

3236

37+
/// <summary>
38+
/// Used in the 4 chained ParticipantNotRegisteredToGPWithReasonForRemoval rules.
39+
/// Gets the participant's BSO code using their existing primary care provider.
40+
/// </summary>
41+
/// <param name="primaryCareProvider">The participant's existing primary care provider.</param>
42+
/// <returns>string, the participant's BSO code.<returns>
43+
public string GetBsoCodeUsingPCP(string primaryCareProvider)
44+
{
45+
var gpPractice = _bsSelectGPPracticeClient.GetSingle(primaryCareProvider).Result;
46+
47+
if (gpPractice == null)
48+
{
49+
return string.Empty;
50+
}
51+
return gpPractice.BsoCode;
52+
}
3353
}

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/TransformDataService.cs

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,21 @@ public class TransformDataService
2626
private readonly ILogger<TransformDataService> _logger;
2727
private readonly ICreateResponse _createResponse;
2828
private readonly IExceptionHandler _exceptionHandler;
29-
private readonly IBsTransformationLookups _transformationLookups;
3029
private readonly ITransformReasonForRemoval _transformReasonForRemoval;
31-
private readonly IDataServiceClient<CohortDistribution> _CohortDistributionClient;
30+
private readonly IDataServiceClient<CohortDistribution> _cohortDistributionClient;
3231
public TransformDataService(
3332
ICreateResponse createResponse,
3433
IExceptionHandler exceptionHandler,
3534
ILogger<TransformDataService> logger,
36-
IBsTransformationLookups transformationLookups,
3735
ITransformReasonForRemoval transformReasonForRemoval,
3836
IDataServiceClient<CohortDistribution> cohortDistributionClient
3937
)
4038
{
4139
_createResponse = createResponse;
4240
_exceptionHandler = exceptionHandler;
4341
_logger = logger;
44-
_transformationLookups = transformationLookups;
4542
_transformReasonForRemoval = transformReasonForRemoval;
46-
_CohortDistributionClient = cohortDistributionClient;
43+
_cohortDistributionClient = cohortDistributionClient;
4744
}
4845

4946
[Function("TransformDataService")]
@@ -75,25 +72,24 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Ano
7572
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req, "NHS Number couldn't be parsed to long");
7673
}
7774

78-
var existingParticipantsList = await _CohortDistributionClient.GetByFilter(x => x.NHSNumber == nhsNumberLong);
79-
75+
var existingParticipantsList = await _cohortDistributionClient.GetByFilter(x => x.NHSNumber == nhsNumberLong);
8076
var lastParticipant = existingParticipantsList.OrderByDescending(x => x.CohortDistributionId).FirstOrDefault();
8177

8278
// Character transformation
8379
var transformString = new TransformString();
8480
participant = await transformString.TransformStringFields(participant);
8581

86-
// Database lookup transformations
87-
participant = await LookupTransformations(participant);
82+
// Address transformation
83+
participant = TransformAddress(lastParticipant, participant);
8884

8985
// Other transformation rules
90-
participant = await TransformParticipantAsync(participant);
86+
participant = await TransformParticipantAsync(participant, lastParticipant);
9187

9288
// Name prefix transformation
9389
if (participant.NamePrefix != null)
9490
participant.NamePrefix = await TransformNamePrefixAsync(participant.NamePrefix);
9591

96-
participant = await _transformReasonForRemoval.ReasonForRemovalTransformations(participant,lastParticipant);
92+
participant = await _transformReasonForRemoval.ReasonForRemovalTransformations(participant, lastParticipant);
9793

9894
var response = JsonSerializer.Serialize(participant);
9995

@@ -112,7 +108,8 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Ano
112108
}
113109
}
114110

115-
public async Task<CohortDistributionParticipant> TransformParticipantAsync(CohortDistributionParticipant participant)
111+
public async Task<CohortDistributionParticipant> TransformParticipantAsync(CohortDistributionParticipant participant,
112+
CohortDistribution databaseParticipant)
116113
{
117114
string json = await File.ReadAllTextAsync("transformRules.json");
118115
var rules = JsonSerializer.Deserialize<Workflow[]>(json);
@@ -126,8 +123,8 @@ public async Task<CohortDistributionParticipant> TransformParticipantAsync(Cohor
126123
var re = new RulesEngine.RulesEngine(rules, reSettings);
127124

128125
var ruleParameters = new[] {
126+
new RuleParameter("databaseParticipant", databaseParticipant),
129127
new RuleParameter("participant", participant),
130-
new RuleParameter("transformLookups", _transformationLookups)
131128
};
132129

133130
var resultList = await re.ExecuteAllRulesAsync("TransformData", ruleParameters);
@@ -170,60 +167,34 @@ private static async Task<string> TransformNamePrefixAsync(string namePrefix)
170167
}
171168

172169
/// <summary>
173-
/// Performs transformations that require database lookups using the BsTransformationLookups class
170+
/// If the request participant's address is missing, this method updates it with the address from
171+
/// the existing record in the database.
174172
/// </summary>
175-
/// <param name="participant">The CohortDistributionParticipant to be transformed.</param>
176-
/// <returns>The transformed participant</returns>
177-
public async Task<CohortDistributionParticipant> LookupTransformations(CohortDistributionParticipant participant)
173+
/// <exception cref="ArgumentException">Throws an error if the record's postcodes do not match</exception>
174+
private static CohortDistributionParticipant TransformAddress(CohortDistribution databaseParticipant,
175+
CohortDistributionParticipant requestParticipant)
178176
{
179-
// Set up rules engine
180-
string json = await File.ReadAllTextAsync("lookupTransformationRules.json");
181-
var rules = JsonSerializer.Deserialize<Workflow[]>(json);
182-
var reSettings = new ReSettings { CustomTypes = [typeof(Actions)] };
183-
var re = new RulesEngine.RulesEngine(rules, reSettings);
184-
185-
var ruleParameters = new[] {
186-
new RuleParameter("participant", participant),
187-
new RuleParameter("transformationLookups", _transformationLookups)
188-
};
189-
190-
// Execute rules
191-
var rulesList = await re.ExecuteAllRulesAsync("LookupTransformations", ruleParameters);
192-
193-
await HandleExceptions(rulesList, participant);
194-
195-
participant.FirstName = GetTransformedData<string>(rulesList, "FirstName", participant.FirstName);
196-
participant.FamilyName = GetTransformedData<string>(rulesList, "FamilyName", participant.FamilyName);
197-
198-
// address transformation
199-
if (!string.IsNullOrEmpty(participant.Postcode) &&
200-
string.IsNullOrEmpty(participant.AddressLine1) &&
201-
string.IsNullOrEmpty(participant.AddressLine2) &&
202-
string.IsNullOrEmpty(participant.AddressLine3) &&
203-
string.IsNullOrEmpty(participant.AddressLine4) &&
204-
string.IsNullOrEmpty(participant.AddressLine5))
177+
if (requestParticipant.RecordType != Actions.Amended)
178+
return requestParticipant;
179+
180+
if (!string.IsNullOrEmpty(requestParticipant.Postcode) &&
181+
string.IsNullOrEmpty(requestParticipant.AddressLine1) &&
182+
string.IsNullOrEmpty(requestParticipant.AddressLine2) &&
183+
string.IsNullOrEmpty(requestParticipant.AddressLine3) &&
184+
string.IsNullOrEmpty(requestParticipant.AddressLine4) &&
185+
string.IsNullOrEmpty(requestParticipant.AddressLine5))
205186
{
206-
participant = _transformationLookups.GetAddress(participant);
187+
if (requestParticipant.Postcode != databaseParticipant.PostCode)
188+
throw new ArgumentException("Participant has an empty address and postcode does not match existing record");
189+
190+
requestParticipant.AddressLine1 = databaseParticipant.AddressLine1;
191+
requestParticipant.AddressLine2 = databaseParticipant.AddressLine2;
192+
requestParticipant.AddressLine3 = databaseParticipant.AddressLine3;
193+
requestParticipant.AddressLine4 = databaseParticipant.AddressLine4;
194+
requestParticipant.AddressLine5 = databaseParticipant.AddressLine5;
207195
}
208196

209-
return participant;
210-
}
211-
212-
/// <summary>
213-
/// Gets the result of the transformation from the rule output and assigns it to the relevant field.
214-
/// Only being used for the rules that require database lookup as the other assignment method does not work.
215-
/// </summary>
216-
/// <param name="results">The rule result tree produced from the rule execution</param>
217-
/// <param name="field">The name of the field</param>
218-
/// <param name="currentValue">The current value of the field in the participant</param>
219-
/// <returns>The transformed value, or the current value if null</returns>
220-
private static T GetTransformedData<T>(List<RuleResultTree> results, string field, T currentValue)
221-
{
222-
// The field is the 3rd part of the rule
223-
var result = results.Find(x => x.Rule.RuleName.Split('.')[2] == field);
224-
if (result == null) return currentValue;
225-
226-
return result.ActionResult.Output == null ? currentValue : (T)result.ActionResult.Output;
197+
return requestParticipant;
227198
}
228199

229200
private async Task HandleExceptions(List<RuleResultTree> exceptions, CohortDistributionParticipant participant)

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/TransformReasonForRemoval.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ namespace NHS.CohortManager.CohortDistribution;
88
public class TransformReasonForRemoval : ITransformReasonForRemoval
99
{
1010
private readonly IExceptionHandler _exceptionHandler;
11-
private readonly IBsTransformationLookups _transformationLookups;
1211
private readonly ITransformDataLookupFacade _dataLookup;
13-
public TransformReasonForRemoval(IExceptionHandler exceptionHandler, IBsTransformationLookups transformationLookups, ITransformDataLookupFacade dataLookup)
12+
public TransformReasonForRemoval(IExceptionHandler exceptionHandler, ITransformDataLookupFacade dataLookup)
1413
{
1514
_exceptionHandler = exceptionHandler;
16-
_transformationLookups = transformationLookups;
1715
_dataLookup = dataLookup;
1816
}
1917

@@ -75,7 +73,7 @@ private string GetDummyPrimaryCareProvider(string postcode, string existingPrima
7573
}
7674
else
7775
{
78-
return dummyPrimaryCareProvider + _transformationLookups.GetBsoCodeUsingPCP(existingPrimaryCareProvider);
76+
return dummyPrimaryCareProvider + _dataLookup.GetBsoCodeUsingPCP(existingPrimaryCareProvider);
7977
}
8078
}
8179
}

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/lookupTransformationRules.json

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

application/CohortManager/src/Functions/CohortDistributionServices/TransformDataService/namePrefixRules.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"GlobalParams": [
55
{
66
"Name":"ValidNamePrefixes",
7-
"Expression": "new string[] {\"A.ML\",\"ADM\",\"BRIG\",\"BRO\",\"CAN\",\"CAPT\",\"CMDR\",\"COL\",\"DAME\",\"DEAN\",\"DR\",\"EARL\",\"FR\",\"G.C\",\"GEN\",\"HON\",\"HRH\",\"LADY\",\"LORD\",\"LT.C\",\"MJR\",\"MISS\",\"MR\",\"MRS\",\"MS\",\"MX\",\"PROF\",\"R.RN\",\"R.HV\",\"REV\",\"SIR\",\"SQLD\",\"SR\",\"VISC\",\"WCOM\"}"
7+
"Expression": "new string[] {\"A.ML\",\"ADM\",\"BRIG\",\"BRO\",\"CAN\",\"CAPT\",\"CMDR\",\"COL\",\"DAME\",\"DEAN\",\"DR\",\"EARL\",\"FR\",\"G.C\",\"GEN\",\"HON\",\"HRH\",\"LADY\",\"LORD\",\"LT.C\",\"MAJ\",\"MISS\",\"MR\",\"MRS\",\"MS\",\"MX\",\"PROF\",\"R.RN\",\"R.HV\",\"REV\",\"SIR\",\"SQLD\",\"SR\",\"VISC\",\"WCOM\"}"
88
}
99
],
1010
"Rules": [

0 commit comments

Comments
 (0)