Skip to content

Commit 3c2e6d6

Browse files
feat: manage participant (#1167)
* feat: migrate to service bus * feat: service bus migration * feat: new functions * feat: distribute participant * feat: queue name switch * revert changes * fix: update name * Update Program.cs * fix: tests * fix: tests * feat: manage participant function * feat: manage participant * feat: queue extension overload * fix: update consolidated tests csproj * chore: responding to comments * chore: responding to comments
1 parent 82c6485 commit 3c2e6d6

File tree

15 files changed

+362
-22
lines changed

15 files changed

+362
-22
lines changed

application/CohortManager/compose.core.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ services:
102102
- ServiceBusConnectionString=Endpoint=sb://service-bus;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;
103103
- CohortQueueName=cohort-distribution-queue
104104
- ParticipantManagementQueueName=participant-management-queue
105-
105+
- ParticipantManagementUrl=http://participant-management-data-service:7994/api/ParticipantManagementDataService
106+
- ExceptionFunctionURL=http://create-exception:7070/api/CreateException
107+
106108
add-participant:
107109
container_name: add-participant
108110
image: cohort-manager-add-participant

application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
})
4545
.AddTelemetry()
4646
.AddHttpClient()
47-
.AddAzureQueues()
47+
.AddAzureQueues(config.UseNewFunctions, config.ServiceBusConnectionString)
4848
.AddExceptionHandler()
4949
.AddDatabaseConnection()
5050
.Build();

application/CohortManager/src/Functions/Functions.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManageParticipant", "Partic
261261
EndProject
262262
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DistributeParticipant", "CohortDistributionServices\DistributeParticipant\DistributeParticipant.csproj", "{87F24206-BDE4-471F-B031-791308443CEE}"
263263
EndProject
264+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManageParticipantTests", "..\..\..\..\tests\UnitTests\ParticipantManagementServicesTests\ManageParticipantTests\ManageParticipantTests.csproj", "{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}"
264265
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ServiceNowIntegration", "ServiceNowIntegration", "{707D3294-6E53-492E-8AA8-1B2F0D358AC4}"
265266
EndProject
266267
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceNowMessageHandler", "ServiceNowIntegration\ServiceNowMessageHandler\ServiceNowMessageHandler.csproj", "{EFAEEEFA-5970-4905-8F81-2471BA5DB8ED}"
@@ -1571,6 +1572,18 @@ Global
15711572
{87F24206-BDE4-471F-B031-791308443CEE}.Release|x64.Build.0 = Release|Any CPU
15721573
{87F24206-BDE4-471F-B031-791308443CEE}.Release|x86.ActiveCfg = Release|Any CPU
15731574
{87F24206-BDE4-471F-B031-791308443CEE}.Release|x86.Build.0 = Release|Any CPU
1575+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1576+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
1577+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Debug|x64.ActiveCfg = Debug|Any CPU
1578+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Debug|x64.Build.0 = Debug|Any CPU
1579+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Debug|x86.ActiveCfg = Debug|Any CPU
1580+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Debug|x86.Build.0 = Debug|Any CPU
1581+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
1582+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Release|Any CPU.Build.0 = Release|Any CPU
1583+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Release|x64.ActiveCfg = Release|Any CPU
1584+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Release|x64.Build.0 = Release|Any CPU
1585+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Release|x86.ActiveCfg = Release|Any CPU
1586+
{F915CDB3-7C80-229D-8E41-4B6A8E8038F2}.Release|x86.Build.0 = Release|Any CPU
15741587
{EFAEEEFA-5970-4905-8F81-2471BA5DB8ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15751588
{EFAEEEFA-5970-4905-8F81-2471BA5DB8ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
15761589
{EFAEEEFA-5970-4905-8F81-2471BA5DB8ED}.Debug|x64.ActiveCfg = Debug|Any CPU
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace NHS.CohortManager.ParticipantManagementServices;
2+
3+
using Microsoft.Azure.Functions.Worker;
4+
using Microsoft.Azure.Functions.Worker.Http;
5+
using Microsoft.Extensions.Diagnostics.HealthChecks;
6+
using System.Threading.Tasks;
7+
using HealthChecks.Extensions;
8+
9+
public class HealthCheckFunction
10+
{
11+
private readonly HealthCheckService _healthCheckService;
12+
13+
public HealthCheckFunction(HealthCheckService healthCheckService)
14+
{
15+
_healthCheckService = healthCheckService;
16+
}
17+
18+
[Function("health")]
19+
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
20+
{
21+
return await HealthCheckServiceExtensions.CreateHealthCheckResponseAsync(req, _healthCheckService);
22+
}
23+
}

application/CohortManager/src/Functions/ParticipantManagementServices/ManageParticipant/ManageParticipant.cs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,86 @@ namespace NHS.CohortManager.ParticipantManagementServices;
1010
using Common;
1111
using System.Text.Json;
1212
using Model;
13+
using DataServices.Client;
1314

1415
public class ManageParticipant
1516
{
1617
private readonly ILogger<ManageParticipant> _logger;
1718
private readonly ManageParticipantConfig _config;
1819
private readonly IQueueClient _queueClient;
20+
private readonly IDataServiceClient<ParticipantManagement> _participantManagementClient;
21+
private readonly IExceptionHandler _handleException;
1922

20-
public ManageParticipant(ILogger<ManageParticipant> logger, IOptions<ManageParticipantConfig> config, IQueueClient queueClient)
23+
public ManageParticipant(ILogger<ManageParticipant> logger,
24+
IOptions<ManageParticipantConfig> config,
25+
IQueueClient queueClient,
26+
IDataServiceClient<ParticipantManagement> participantManagementClient,
27+
IExceptionHandler handleException)
2128
{
2229
_logger = logger;
2330
_config = config.Value;
2431
_queueClient = queueClient;
32+
_participantManagementClient = participantManagementClient;
33+
_handleException = handleException;
2534
}
2635

36+
/// <summary>
37+
/// Reads messages from the participant management queue, adds/ updates the record in participant management,
38+
/// and sends the record to cohort distribution
39+
/// </summary>
40+
/// <param name="message">json string containing the participant record</param>
2741
[Function(nameof(ManageParticipant))]
28-
public async Task Run([ServiceBusTrigger("%ParticipantManagementQueueName%", Connection = "ServiceBusConnectionString")] string messageBody)
42+
public async Task Run([ServiceBusTrigger("%ParticipantManagementQueueName%", Connection = "ServiceBusConnectionString")] string message)
2943
{
30-
_logger.LogInformation($"Received message: {messageBody}");
31-
BasicParticipantCsvRecord message = JsonSerializer.Deserialize<BasicParticipantCsvRecord>(messageBody);
32-
_logger.LogInformation(_config.CohortQueueName);
44+
var participantRecord = JsonSerializer.Deserialize<BasicParticipantCsvRecord>(message)!;
45+
Participant participant = participantRecord.participant;
46+
try
47+
{
48+
_logger.LogInformation("Recieved manage participant request");
49+
bool nhsNumberValid = ValidationHelper.ValidateNHSNumber(participant.NhsNumber);
50+
if (!nhsNumberValid)
51+
{
52+
await HandleException(new ArgumentException("NHS Number invalid"), participant, participantRecord.FileName);
53+
return;
54+
}
3355

34-
await _queueClient.AddAsync<BasicParticipantCsvRecord>(message, _config.CohortQueueName);
35-
}
56+
long nhsNumber = long.Parse(participant.NhsNumber);
57+
short screeningId = short.Parse(participant.ScreeningId);
58+
59+
var databaseParticipant = await _participantManagementClient.GetSingleByFilter(x => x.NHSNumber == nhsNumber && x.ScreeningId == screeningId);
60+
61+
bool dataServiceResponse;
62+
if (databaseParticipant is null)
63+
{
64+
dataServiceResponse = await _participantManagementClient.Add(participant.ToParticipantManagement());
65+
}
66+
else if (databaseParticipant.BlockedFlag == 1)
67+
{
68+
await HandleException(new InvalidOperationException("Participant is blocked"), participant, participantRecord.FileName);
69+
return;
70+
}
71+
else
72+
{
73+
dataServiceResponse = await _participantManagementClient.Update(participant.ToParticipantManagement());
74+
}
3675

76+
if (!dataServiceResponse)
77+
{
78+
await HandleException(new InvalidOperationException("Participant Management Data Service request failed"), participant, participantRecord.FileName);
79+
return;
80+
}
3781

82+
await _queueClient.AddAsync(participantRecord, _config.CohortQueueName);
83+
}
84+
catch (Exception ex)
85+
{
86+
await HandleException(ex, participant, participantRecord.FileName);
87+
}
88+
}
3889

90+
private async Task HandleException(Exception ex, Participant participant, string fileName)
91+
{
92+
_logger.LogError(ex, "Manage Exception failed");
93+
await _handleException.CreateSystemExceptionLog(ex, participant, fileName);
94+
}
3995
}

application/CohortManager/src/Functions/ParticipantManagementServices/ManageParticipant/ManageParticipant.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ItemGroup>
2424
<ProjectReference Include="../../Shared/Common/Common.csproj" />
2525
<ProjectReference Include="..\..\Shared\HealthChecks\HealthChecks.csproj" />
26+
<ProjectReference Include="../../Shared/DataServices.Client/DataServices.Client.csproj" />
2627
</ItemGroup>
2728
<ItemGroup>
2829
<None Update="host.json">

application/CohortManager/src/Functions/ParticipantManagementServices/ManageParticipant/ManageParticipantConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public class ManageParticipantConfig
1010
public string CohortQueueName { get; set; }
1111
[Required]
1212
public string ParticipantManagementQueueName { get; set; }
13+
[Required]
14+
public string ParticipantManagementUrl { get; set; }
1315
}

application/CohortManager/src/Functions/ParticipantManagementServices/ManageParticipant/Program.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
using Microsoft.Extensions.Hosting;
22
using Common;
3+
using Model;
34
using NHS.CohortManager.ParticipantManagementServices;
5+
using DataServices.Client;
6+
using HealthChecks.Extensions;
47

58
var host = new HostBuilder()
69
.AddConfiguration<ManageParticipantConfig>(out ManageParticipantConfig config)
710
.ConfigureFunctionsWebApplication()
11+
.AddDataServicesHandler()
12+
.AddDataService<ParticipantManagement>(config.ParticipantManagementUrl)
13+
.Build()
14+
.ConfigureServices(services =>
15+
{
16+
// Register health checks
17+
services.AddBasicHealthCheck("ManageParticipant");
18+
})
19+
.AddExceptionHandler()
20+
.AddAzureQueues(true, config.ServiceBusConnectionString)
821
.AddTelemetry()
9-
// .ConfigureServices(services => {
10-
// // Register health checks
11-
// services.AddBasicHealthCheck("CheckParticipantExists");
12-
// })
13-
.AddAzureQueues()
1422
.Build();
1523

1624

application/CohortManager/src/Functions/Shared/Common/AzureServiceBusClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public async Task<bool> AddAsync<T>(T message, string queueName)
2828
string jsonMessage = JsonSerializer.Serialize(message);
2929
ServiceBusMessage serviceBusMessage = new(jsonMessage);
3030

31-
_logger.LogInformation("sending message to service buss queue");
31+
_logger.LogInformation("sending message to service bus queue");
3232

3333
await sender.SendMessageAsync(serviceBusMessage);
3434
return true;
3535
}
3636
catch (Exception ex)
3737
{
38-
_logger.LogError(ex, "There was an error sending message to service buss queue {queueName} {errorMessage}", queueName, ex.Message);
38+
_logger.LogError(ex, "There was an error sending message to service bus queue {QueueName} {ErrorMessage}", queueName, ex.Message);
3939
return false;
4040
}
4141
finally

application/CohortManager/src/Functions/Shared/Common/Extensions/AzureQueueExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ public static IHostBuilder AddAzureQueues(this IHostBuilder hostBuilder)
3737
_.AddTransient<IQueueClientFactory, QueueClientFactory>();
3838
});
3939
}
40-
}
40+
}

0 commit comments

Comments
 (0)