-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUpdateBlockedFlag.cs
More file actions
188 lines (159 loc) · 8.22 KB
/
UpdateBlockedFlag.cs
File metadata and controls
188 lines (159 loc) · 8.22 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
namespace NHS.CohortManager.ParticipantManagementService;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Common;
using System.Net;
using System.Text.Json;
using Azure.Messaging.EventGrid.SystemEvents;
public class UpdateBlockedFlag
{
private readonly ILogger<UpdateBlockedFlag> _logger;
private readonly ICreateResponse _createResponse;
private readonly IBlockParticipantHandler _blockParticipantHandler;
private const string cannotBeDeserializedMessage = "Participant Block Dto couldn't be deserialized";
public UpdateBlockedFlag(ILogger<UpdateBlockedFlag> logger, ICreateResponse createResponse, IBlockParticipantHandler blockParticipantHandler)
{
_logger = logger;
_createResponse = createResponse;
_blockParticipantHandler = blockParticipantHandler;
}
/// <summary>
/// Takes in a http request that if the input is valid then it updates the blocked flag within participant management table to 1.
/// Otherwise, adds an exception to the exception table.
/// </summary>
/// <param name="req">
/// A Http request that should contain an NHS number, family name, date of birth and a screening ID.
/// </param>
/// <returns>
/// A http status code. (200 - Ok / 404 - Not Found / 500 - Internal server error)
/// </returns>
[Function("BlockParticipant")]
public async Task<HttpResponseData> BlockParticipant([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
_logger.LogInformation("Block Participant Called");
try
{
var blockParticipantDTOJson = await req.ReadAsStringAsync();
if (string.IsNullOrWhiteSpace(blockParticipantDTOJson))
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "Unable to parse request");
}
var blockParticipantDTO = JsonSerializer.Deserialize<BlockParticipantDto>(blockParticipantDTOJson);
if (blockParticipantDTO == null)
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "Unable to parse request");
}
var blockParticipantResult = await _blockParticipantHandler.BlockParticipant(blockParticipantDTO);
if (blockParticipantResult.Success)
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.OK, req, blockParticipantResult.ResponseMessage!);
}
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, blockParticipantResult.ResponseMessage!);
}
catch (JsonException jex)
{
_logger.LogError(jex, cannotBeDeserializedMessage);
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req);
}
catch (FormatException fex)
{
_logger.LogError(fex, cannotBeDeserializedMessage);
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while blocking a participant");
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
}
}
/// <summary>
/// Get Participant details will look up a participant in cohort manager if they do not exist in cohort manager then they
/// will be looked up in PDS. Once they are found a three point check will be carried out to ensure thier details matched, the found
/// details will be returned for manual assurance that the correct individual has been found
/// </summary>
/// <param name="req"></param>
/// <returns>BlockParticipantDto or Error message</returns>
[Function("GetParticipant")]
public async Task<HttpResponseData> GetParticipantDetails([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
_logger.LogInformation("Get Participant Details Called");
try
{
var blockParticipantDTOJson = await req.ReadAsStringAsync();
if (string.IsNullOrWhiteSpace(blockParticipantDTOJson))
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "Unable to parse request");
}
var blockParticipantDTO = JsonSerializer.Deserialize<BlockParticipantDto>(blockParticipantDTOJson);
if (blockParticipantDTO == null)
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "Unable to parse request");
}
var getParticipantResult = await _blockParticipantHandler.GetParticipant(blockParticipantDTO);
if (getParticipantResult.Success)
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.OK, req, getParticipantResult.ResponseMessage!);
}
if (getParticipantResult.ResponseMessage == "Participant Couldn't be found")
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.NotFound, req, "Participant Not found");
}
if (getParticipantResult.ResponseMessage == "Invalid NHS Number")
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, getParticipantResult.ResponseMessage);
}
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.InternalServerError, req, getParticipantResult.ResponseMessage!);
}
catch (JsonException jex)
{
_logger.LogError(jex, cannotBeDeserializedMessage);
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req);
}
catch (FormatException fex)
{
_logger.LogError(fex, cannotBeDeserializedMessage);
return _createResponse.CreateHttpResponse(HttpStatusCode.BadRequest, req);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while blocking a participant");
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
}
}
/// <summary>
/// Takes in a http request that if the input is valid then it updates the blocked flag within participant management table to 0.
/// Otherwise, adds an exception to the exception table.
/// </summary>
/// <param name="req">
/// A Http request that should contain an NHS number, family name, date of birth and a screening ID.
/// </param>
/// <returns>
/// A http status code. (200 - Ok / 404 - Not Found / 500 - Internal server error)
/// </returns>
[Function("UnblockParticipant")]
public async Task<HttpResponseData> UnblockParticipant([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
_logger.LogInformation("Unblock Participant Called");
var nhsNumber = req.Query["nhsNumber"];
if (string.IsNullOrWhiteSpace(nhsNumber))
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "No NHS Number provided");
}
if (!ValidationHelper.ValidateNHSNumber(nhsNumber))
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, "Invalid NHS Number provided");
}
var nhsNumberParsed = long.Parse(nhsNumber);
var unBlockParticipantResult = await _blockParticipantHandler.UnblockParticipant(nhsNumberParsed);
if (unBlockParticipantResult.Success)
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.OK, req, "Participant successfully unblocked");
}
if (unBlockParticipantResult.ResponseMessage == "Participant Couldn't be found")
{
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.NotFound, req, "Participant Not found");
}
return await _createResponse.CreateHttpResponseWithBodyAsync(HttpStatusCode.BadRequest, req, unBlockParticipantResult.ResponseMessage!);
}
}