-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRetrieveParticipantData.cs
More file actions
102 lines (89 loc) · 4.74 KB
/
RetrieveParticipantData.cs
File metadata and controls
102 lines (89 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace NHS.CohortManager.CohortDistributionService;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Common;
using System.Net;
using Microsoft.Extensions.Logging;
using Model;
using NHS.CohortManager.CohortDistribution;
using System.Text;
using System.Text.Json;
using DataServices.Client;
using Microsoft.Extensions.Options;
using NHS.Screening.RetrieveParticipantData;
public class RetrieveParticipantData
{
private readonly ICreateResponse _createResponse;
private readonly ILogger<RetrieveParticipantData> _logger;
private readonly ICallFunction _callFunction;
private readonly ICreateParticipant _createParticipant;
private readonly IExceptionHandler _exceptionHandler;
private readonly IDataServiceClient<ParticipantManagement> _participantManagementClient;
private readonly RetrieveParticipantDataConfig _config;
public RetrieveParticipantData(ICreateResponse createResponse, ILogger<RetrieveParticipantData> logger,
IDataServiceClient<ParticipantManagement> participantManagementClient,
ICreateParticipant createParticipant, IExceptionHandler exceptionHandler,
ICallFunction callFunction, IOptions<RetrieveParticipantDataConfig> retrieveParticipantDataConfig)
{
_createResponse = createResponse;
_logger = logger;
_callFunction = callFunction;
_createParticipant = createParticipant;
_exceptionHandler = exceptionHandler;
_participantManagementClient = participantManagementClient;
_config = retrieveParticipantDataConfig.Value;
}
[Function("RetrieveParticipantData")]
public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
long screeningIdLong;
RetrieveParticipantRequestBody requestBody;
var participant = new CohortDistributionParticipant();
try
{
string requestBodyJson;
using (var reader = new StreamReader(req.Body, Encoding.UTF8))
{
requestBodyJson = await reader.ReadToEndAsync();
}
requestBody = JsonSerializer.Deserialize<RetrieveParticipantRequestBody>(requestBodyJson);
screeningIdLong = long.Parse(requestBody.ScreeningService);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req);
}
try
{
var longNhsNumber = long.Parse(requestBody.NhsNumber);
var participantData = await _participantManagementClient.GetSingleByFilter(p => p.NHSNumber == longNhsNumber &&
p.ScreeningId == screeningIdLong);
_logger.LogInformation("Got the participant. ScreeningId: {ScreeningServiceId}", participantData.ScreeningId);
var demographicFunctionParams = new Dictionary<string, string>()
{
{"Id", requestBody.NhsNumber }
};
var demographicDataJson = await _callFunction.SendGet(_config.DemographicDataFunctionURL, demographicFunctionParams);
var demographicData = JsonSerializer.Deserialize<Demographic>(demographicDataJson);
if (demographicData == null)
{
_logger.LogError("the demographicData was null the {RetrieveParticipantData} function", nameof(RetrieveParticipantData));
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req, $"the demographicData was null the {nameof(RetrieveParticipantData)} function");
}
participant = _createParticipant.CreateCohortDistributionParticipantModel(participantData, demographicData);
//TODO, This needs to happen elsewhere Hardcoded for now
participant.ScreeningName = "Breast Screening";
participant.ScreeningAcronym = "BSS";
var responseBody = JsonSerializer.Serialize(participant);
_logger.LogInformation("ParticipantScreeningID: {ScreeningServiceId}", participant.ScreeningServiceId);
return _createResponse.CreateHttpResponse(HttpStatusCode.OK, req, responseBody);
}
catch (Exception ex)
{
_logger.LogError(ex, "Retrieve participant data failed.\nMessage: {Message}\nStack Trace: {StackTrace}", ex.Message, ex.StackTrace);
await _exceptionHandler.CreateSystemExceptionLogFromNhsNumber(ex, requestBody.NhsNumber, "", "", JsonSerializer.Serialize(participant) ?? "N/A");
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
}
}
}