-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRetrievePDSDemographic.cs
More file actions
96 lines (82 loc) · 4.04 KB
/
RetrievePDSDemographic.cs
File metadata and controls
96 lines (82 loc) · 4.04 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
namespace NHS.CohortManager.DemographicServices;
using System;
using System.Net;
using System.Text.Json;
using Common;
using Common.Interfaces;
using DataServices.Client;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using Model;
public class RetrievePdsDemographic
{
private readonly ILogger<RetrievePdsDemographic> _logger;
private readonly ICreateResponse _createResponse;
private readonly IHttpClientFunction _httpClientFunction;
private readonly RetrievePDSDemographicConfig _config;
private readonly IFhirPatientDemographicMapper _fhirPatientDemographicMapper;
private readonly IBearerTokenService _bearerTokenService;
private readonly IPdsProcessor _pdsProcessor;
private const string PdsParticipantUrlFormat = "{0}/{1}";
public RetrievePdsDemographic(
ILogger<RetrievePdsDemographic> logger,
ICreateResponse createResponse,
IHttpClientFunction httpClientFunction,
IFhirPatientDemographicMapper fhirPatientDemographicMapper,
IOptions<RetrievePDSDemographicConfig> retrievePDSDemographicConfig,
IBearerTokenService bearerTokenService,
IPdsProcessor pdsProcessor
)
{
_logger = logger;
_createResponse = createResponse;
_httpClientFunction = httpClientFunction;
_fhirPatientDemographicMapper = fhirPatientDemographicMapper;
_config = retrievePDSDemographicConfig.Value;
_bearerTokenService = bearerTokenService;
_pdsProcessor = pdsProcessor;
}
// TODO: Need to send an exception to the EXCEPTION_MANAGEMENT table whenever this function returns a non OK status.
[Function("RetrievePdsDemographic")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
{
try
{
var nhsNumber = req.Query["nhsNumber"];
var bearerToken = await _bearerTokenService.GetBearerToken();
if (bearerToken == null)
{
_logger.LogError("the bearer token could not be found");
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req, "The bearer token could not be found");
}
if (string.IsNullOrEmpty(nhsNumber) || !ValidationHelper.ValidateNHSNumber(nhsNumber))
{
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req, "Invalid NHS number provided.");
}
var url = string.Format(PdsParticipantUrlFormat, _config.RetrievePdsParticipantURL, nhsNumber);
var response = await _httpClientFunction.SendPdsGet(url, bearerToken);
string jsonResponse = "";
jsonResponse = await _httpClientFunction.GetResponseText(response);
var pdsDemographic = _fhirPatientDemographicMapper.ParseFhirJson(jsonResponse);
if (response.StatusCode == HttpStatusCode.NotFound || pdsDemographic.ConfidentialityCode == "R")
{
await _pdsProcessor.ProcessPdsNotFoundResponse(response, nhsNumber);
return _createResponse.CreateHttpResponse(HttpStatusCode.NotFound, req, "PDS returned a 404 please database for details");
}
response.EnsureSuccessStatusCode();
var participantDemographic = pdsDemographic.ToParticipantDemographic();
var upsertResult = await _pdsProcessor.UpsertDemographicRecordFromPDS(participantDemographic);
return upsertResult ?
_createResponse.CreateHttpResponse(HttpStatusCode.OK, req, JsonSerializer.Serialize(participantDemographic)) :
_createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
}
catch (Exception ex)
{
_logger.LogError(ex, "There has been an error retrieving PDS participant data.");
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
}
}
}