|
| 1 | +namespace NHS.CohortManager.ParticipantManagementService; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Globalization; |
| 5 | +using System.Text.Json; |
| 6 | +using Common; |
| 7 | +using DataServices.Client; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | +using Model; |
| 11 | + |
| 12 | +public class BlockParticipantHandler : IBlockParticipantHandler |
| 13 | +{ |
| 14 | + private readonly ILogger<BlockParticipantHandler> _logger; |
| 15 | + private readonly IDataServiceClient<ParticipantManagement> _participantManagementDataService; |
| 16 | + private readonly IDataServiceClient<ParticipantDemographic> _participantDemographicDataService; |
| 17 | + private readonly IHttpClientFunction _httpClient; |
| 18 | + private readonly UpdateBlockedFlagConfig _config; |
| 19 | + public BlockParticipantHandler(ILogger<BlockParticipantHandler> logger, |
| 20 | + IDataServiceClient<ParticipantManagement> participantManagementDataService, |
| 21 | + IDataServiceClient<ParticipantDemographic> participantDemographicDataService, |
| 22 | + IHttpClientFunction httpClient, |
| 23 | + IOptions<UpdateBlockedFlagConfig> config |
| 24 | + ) |
| 25 | + { |
| 26 | + _logger = logger; |
| 27 | + _participantManagementDataService = participantManagementDataService; |
| 28 | + _participantDemographicDataService = participantDemographicDataService; |
| 29 | + _httpClient = httpClient; |
| 30 | + _config = config.Value; |
| 31 | + } |
| 32 | + |
| 33 | + public async Task<BlockParticipantResult> BlockParticipant(BlockParticipantDto blockParticipantRequest) |
| 34 | + { |
| 35 | + if (!ValidationHelper.ValidateNHSNumber(blockParticipantRequest.NhsNumber.ToString())) |
| 36 | + { |
| 37 | + _logger.LogWarning("Participant had an invalid NHS Number and cannot be blocked"); |
| 38 | + return new BlockParticipantResult(false, "Invalid NHS Number"); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + var participantManagementRecord = await _participantManagementDataService.GetSingleByFilter(x => x.NHSNumber == blockParticipantRequest.NhsNumber); |
| 44 | + |
| 45 | + if (participantManagementRecord == null) |
| 46 | + { |
| 47 | + return await BlockNewParticipant(blockParticipantRequest); |
| 48 | + } |
| 49 | + |
| 50 | + if (participantManagementRecord.BlockedFlag == 1) |
| 51 | + { |
| 52 | + _logger.LogWarning("Participant already blocked and cannot be blocked"); |
| 53 | + return new BlockParticipantResult(false, "Participant Already Blocked"); |
| 54 | + } |
| 55 | + |
| 56 | + var participantDemographic = await _participantDemographicDataService.GetSingleByFilter(x => x.NhsNumber == blockParticipantRequest.NhsNumber); |
| 57 | + |
| 58 | + if (!ValidateRecordsMatch(participantDemographic, blockParticipantRequest)) |
| 59 | + { |
| 60 | + _logger.LogWarning("Participant didn't pass three point check and cannot be blocked"); |
| 61 | + return new BlockParticipantResult(false, "Participant Didn't pass three point check"); |
| 62 | + } |
| 63 | + |
| 64 | + _logger.LogInformation("Participant has been blocked"); |
| 65 | + return await BlockExistingParticipant(participantManagementRecord); |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public async Task<BlockParticipantResult> UnblockParticipant(long nhsNumber) |
| 72 | + { |
| 73 | + |
| 74 | + var participantManagementRecord = await _participantManagementDataService.GetSingleByFilter(x => x.NHSNumber == nhsNumber); |
| 75 | + |
| 76 | + if (participantManagementRecord == null) |
| 77 | + { |
| 78 | + return new BlockParticipantResult(false, "Participant Couldn't be found"); |
| 79 | + } |
| 80 | + |
| 81 | + if (participantManagementRecord.BlockedFlag != 1) |
| 82 | + { |
| 83 | + _logger.LogInformation("Participant couldn't be unblocked as they are not currently blocked"); |
| 84 | + return new BlockParticipantResult(false, "Participant is not blocked"); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + var blockedFlagSet = await SetBlockedFlag(participantManagementRecord, false); |
| 89 | + if (!blockedFlagSet) |
| 90 | + { |
| 91 | + return new BlockParticipantResult(false, "Failed to unset blocked flag"); |
| 92 | + } |
| 93 | + |
| 94 | + if (participantManagementRecord.EligibilityFlag == 0) |
| 95 | + { |
| 96 | + return new BlockParticipantResult(true, "Participant was unblocked but not resubscribed to Nems as they are ineligible"); |
| 97 | + } |
| 98 | + |
| 99 | + var nemsSubscribed = await SubscribeParticipantToNEMS(nhsNumber); |
| 100 | + if (!nemsSubscribed) |
| 101 | + { |
| 102 | + return new BlockParticipantResult(false, "Participant couldn't be subscribed in Nems"); |
| 103 | + } |
| 104 | + |
| 105 | + _logger.LogInformation("Participant has been unblocked"); |
| 106 | + return new BlockParticipantResult(true, "Participant Unblocked"); |
| 107 | + |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + public async Task<BlockParticipantResult> GetParticipant(BlockParticipantDto blockParticipantRequest) |
| 112 | + { |
| 113 | + |
| 114 | + if (!ValidationHelper.ValidateNHSNumber(blockParticipantRequest.NhsNumber.ToString())) |
| 115 | + { |
| 116 | + _logger.LogWarning("Participant had an invalid NHS Number and cannot be blocked"); |
| 117 | + return new BlockParticipantResult(false, "Invalid NHS Number"); |
| 118 | + } |
| 119 | + |
| 120 | + var participantDemographic = await _participantDemographicDataService.GetSingleByFilter(x => x.NhsNumber == blockParticipantRequest.NhsNumber); |
| 121 | + |
| 122 | + if (participantDemographic != null) |
| 123 | + { |
| 124 | + var recordsMatch = ValidateRecordsMatch(participantDemographic, blockParticipantRequest); |
| 125 | + var responseBody = JsonSerializer.Serialize(new BlockParticipantDto |
| 126 | + { |
| 127 | + NhsNumber = participantDemographic.NhsNumber, |
| 128 | + FamilyName = participantDemographic.FamilyName!, |
| 129 | + DateOfBirth = participantDemographic.DateOfBirth!, |
| 130 | + }); |
| 131 | + return new BlockParticipantResult(recordsMatch, responseBody); |
| 132 | + } |
| 133 | + |
| 134 | + var pdsParticipant = await GetPDSParticipant(blockParticipantRequest.NhsNumber); |
| 135 | + |
| 136 | + if (pdsParticipant == null) |
| 137 | + { |
| 138 | + return new BlockParticipantResult(false, "Participant Couldn't be found"); |
| 139 | + } |
| 140 | + |
| 141 | + var pdsRecordsMatch = ValidateRecordsMatch(pdsParticipant, blockParticipantRequest); |
| 142 | + var pdsResponseBody = JsonSerializer.Serialize(new BlockParticipantDto |
| 143 | + { |
| 144 | + NhsNumber = pdsParticipant.NhsNumber, |
| 145 | + FamilyName = pdsParticipant.FamilyName!, |
| 146 | + DateOfBirth = pdsParticipant.DateOfBirth! |
| 147 | + }); |
| 148 | + |
| 149 | + return new BlockParticipantResult(pdsRecordsMatch, pdsResponseBody); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + private async Task<BlockParticipantResult> BlockNewParticipant(BlockParticipantDto blockParticipantRequest) |
| 154 | + { |
| 155 | + var pdsParticipant = await GetPDSParticipant(blockParticipantRequest.NhsNumber); |
| 156 | + |
| 157 | + if (pdsParticipant == null || !ValidateRecordsMatch(pdsParticipant, blockParticipantRequest)) |
| 158 | + { |
| 159 | + return new BlockParticipantResult(false, "Participant details do not match a records in Cohort Manager or PDS"); |
| 160 | + } |
| 161 | + |
| 162 | + var participantManagementRecord = new ParticipantManagement |
| 163 | + { |
| 164 | + NHSNumber = pdsParticipant.NhsNumber, |
| 165 | + BlockedFlag = 1, |
| 166 | + EligibilityFlag = 0, |
| 167 | + }; |
| 168 | + |
| 169 | + var participantManagementAdded = await _participantManagementDataService.Add(participantManagementRecord); |
| 170 | + |
| 171 | + if (!participantManagementAdded) |
| 172 | + { |
| 173 | + return new BlockParticipantResult(false, "Unable to add participant to Cohort Manager to be blocked"); |
| 174 | + } |
| 175 | + |
| 176 | + return new BlockParticipantResult(true, "Participant Has been blocked"); |
| 177 | + |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + private async Task<BlockParticipantResult> BlockExistingParticipant(ParticipantManagement participant) |
| 182 | + { |
| 183 | + var blockFlagUpdated = await SetBlockedFlag(participant, true); |
| 184 | + |
| 185 | + if (!blockFlagUpdated) |
| 186 | + { |
| 187 | + return new BlockParticipantResult(false, "Failed to Update participant in Cohort Manager"); |
| 188 | + } |
| 189 | + |
| 190 | + var unsubscribeFromNems = await UnsubscribeParticipantFromNEMS(participant.NHSNumber); |
| 191 | + |
| 192 | + if (!unsubscribeFromNems) |
| 193 | + { |
| 194 | + return new BlockParticipantResult(false, "Failed to unsubscribe Participant From NEMS"); |
| 195 | + } |
| 196 | + |
| 197 | + return new BlockParticipantResult(true, "Participant Has been blocked"); |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + private async Task<bool> UnsubscribeParticipantFromNEMS(long nhsNumber) |
| 202 | + { |
| 203 | + var nemsUnsubscribeResponse = await _httpClient.SendPost(_config.ManageNemsSubscriptionUnsubscribeURL, CreateNhsNumberQueryParams(nhsNumber)); |
| 204 | + |
| 205 | + return nemsUnsubscribeResponse.IsSuccessStatusCode; |
| 206 | + } |
| 207 | + |
| 208 | + private async Task<bool> SubscribeParticipantToNEMS(long nhsNumber) |
| 209 | + { |
| 210 | + var nemsSubscribeResponse = await _httpClient.SendPost(_config.ManageNemsSubscriptionSubscribeURL, CreateNhsNumberQueryParams(nhsNumber)); |
| 211 | + |
| 212 | + return nemsSubscribeResponse.IsSuccessStatusCode; |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + private async Task<bool> SetBlockedFlag(ParticipantManagement participant, bool blocked) |
| 217 | + { |
| 218 | + participant.BlockedFlag = blocked ? (short)1 : (short)0; |
| 219 | + return await _participantManagementDataService.Update(participant); |
| 220 | + } |
| 221 | + |
| 222 | + private async Task<ParticipantDemographic> GetPDSParticipant(long nhsNumber) |
| 223 | + { |
| 224 | + var pdsResponse = await _httpClient.SendGet(_config.RetrievePdsDemographicURL, CreateNhsNumberQueryParams(nhsNumber)); |
| 225 | + if (string.IsNullOrEmpty(pdsResponse)) |
| 226 | + { |
| 227 | + _logger.LogWarning("RetrievePDSDemographic Didn't return a valid response"); |
| 228 | + return null!; |
| 229 | + } |
| 230 | + |
| 231 | + var pdsDemographic = JsonSerializer.Deserialize<ParticipantDemographic>(pdsResponse); |
| 232 | + |
| 233 | + return pdsDemographic!; |
| 234 | + } |
| 235 | + |
| 236 | + private static bool ValidateRecordsMatch(ParticipantDemographic participant, BlockParticipantDto dto) |
| 237 | + { |
| 238 | + |
| 239 | + if (!DateOnly.TryParseExact(dto.DateOfBirth, "yyyy-MM-dd",new CultureInfo("en-GB"),DateTimeStyles.None, out var dtoDateOfBirth )) |
| 240 | + { |
| 241 | + throw new FormatException("Date of Birth not in the correct format"); |
| 242 | + } |
| 243 | + |
| 244 | + if (!DateOnly.TryParseExact(participant.DateOfBirth, "yyyyMMdd",new CultureInfo("en-GB"),DateTimeStyles.None, out var parsedDob)) |
| 245 | + { |
| 246 | + return false; |
| 247 | + } |
| 248 | + return string.Equals(participant.FamilyName, dto.FamilyName, StringComparison.InvariantCultureIgnoreCase) |
| 249 | + && participant.NhsNumber == dto.NhsNumber |
| 250 | + && parsedDob == dtoDateOfBirth; |
| 251 | + } |
| 252 | + |
| 253 | + private static Dictionary<string, string> CreateNhsNumberQueryParams(long nhsNumber) => |
| 254 | + new Dictionary<string, string> |
| 255 | + { |
| 256 | + {"nhsNumber",nhsNumber.ToString()} |
| 257 | + }; |
| 258 | + |
| 259 | +} |
0 commit comments