-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidationExceptionData.cs
More file actions
244 lines (205 loc) · 10.2 KB
/
ValidationExceptionData.cs
File metadata and controls
244 lines (205 loc) · 10.2 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
namespace Data.Database;
using System;
using System.Data;
using System.Net;
using System.Threading.Tasks;
using Common;
using DataServices.Client;
using Microsoft.Extensions.Logging;
using Model;
using Model.Enums;
public class ValidationExceptionData : IValidationExceptionData
{
private readonly ILogger<ValidationExceptionData> _logger;
private readonly IDataServiceClient<ExceptionManagement> _validationExceptionDataServiceClient;
private readonly IDataServiceClient<ParticipantDemographic> _demographicDataServiceClient;
public ValidationExceptionData(
ILogger<ValidationExceptionData> logger,
IDataServiceClient<ExceptionManagement> validationExceptionDataServiceClient,
IDataServiceClient<ParticipantDemographic> demographicDataServiceClient
)
{
_logger = logger;
_validationExceptionDataServiceClient = validationExceptionDataServiceClient;
_demographicDataServiceClient = demographicDataServiceClient;
}
public async Task<List<ValidationException>?> GetAllFilteredExceptions(ExceptionStatus? exceptionStatus, SortOrder? sortOrder, ExceptionCategory exceptionCategory)
{
var category = (int)exceptionCategory;
var exceptions = await _validationExceptionDataServiceClient.GetByFilter(x => x.Category != null && x.Category.Value == category);
var exceptionList = exceptions.Select(s => s.ToValidationException());
return SortExceptions(sortOrder, exceptionList, exceptionStatus);
}
public async Task<ValidationException?> GetExceptionById(int exceptionId)
{
var exception = await _validationExceptionDataServiceClient.GetSingle(exceptionId.ToString());
if (exception == null)
{
_logger.LogInformation("Exception not found");
return null;
}
if (!long.TryParse(exception.NhsNumber, out long nhsNumber))
{
throw new FormatException("Unable to parse NHS Number");
}
var participantDemographic = await _demographicDataServiceClient.GetSingleByFilter(x => x.NhsNumber == nhsNumber);
return GetExceptionDetails(exception.ToValidationException(), participantDemographic);
}
public async Task<bool> Create(ValidationException exception)
{
var exceptionToUpdate = new ExceptionManagement().FromValidationException(exception);
return await _validationExceptionDataServiceClient.Add(exceptionToUpdate);
}
public async Task<bool> RemoveOldException(string nhsNumber, string screeningName)
{
var exceptions = await GetExceptionRecords(nhsNumber, screeningName);
if (exceptions == null)
{
return false;
}
// we only need to get the last unresolved exception for the nhs number and screening service
var validationExceptionToUpdate = exceptions.Where(x => DateToString(x.DateResolved) == "9999-12-31")
.OrderByDescending(x => x.DateCreated).FirstOrDefault();
if (validationExceptionToUpdate != null)
{
validationExceptionToUpdate.DateResolved = DateTime.UtcNow.Date;
validationExceptionToUpdate.RecordUpdatedDate = DateTime.UtcNow;
return await _validationExceptionDataServiceClient.Update(validationExceptionToUpdate);
}
return false;
}
public async Task<ServiceResponseModel> UpdateExceptionServiceNowId(int exceptionId, string serviceNowId)
{
try
{
serviceNowId = serviceNowId?.Trim() ?? string.Empty;
var validationError = ValidateServiceNowId(serviceNowId);
if (validationError != null)
{
return CreateErrorResponse(validationError, HttpStatusCode.BadRequest);
}
var exception = await _validationExceptionDataServiceClient.GetSingle(exceptionId.ToString());
if (exception == null)
{
return CreateErrorResponse($"Exception with ID {exceptionId} not found", HttpStatusCode.NotFound);
}
bool serviceNowIdChanged = serviceNowId != exception.ServiceNowId;
exception.ServiceNowId = serviceNowId;
exception.RecordUpdatedDate = DateTime.UtcNow;
var updateResult = await _validationExceptionDataServiceClient.Update(exception);
if (!updateResult)
{
return CreateErrorResponse($"Failed to update exception {exceptionId} in data service", HttpStatusCode.InternalServerError);
}
string successMessage = serviceNowIdChanged ? "ServiceNowId updated successfully" : "ServiceNowId unchanged, but record updated date has been updated";
return CreateSuccessResponse(successMessage);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating ServiceNowId for exception {ExceptionId}", exceptionId);
return CreateErrorResponse($"Error updating ServiceNowId for exception {exceptionId}", HttpStatusCode.InternalServerError);
}
}
public async Task<List<ValidationException>?> GetReportExceptions(DateTime? reportDate, ExceptionCategory exceptionCategory)
{
var isSpecificReportCategory = exceptionCategory == ExceptionCategory.Confusion || exceptionCategory == ExceptionCategory.Superseded;
var filteredExceptions = (await _validationExceptionDataServiceClient.GetByFilter(x =>
x.Category.HasValue && (x.Category.Value == (int)ExceptionCategory.Confusion || x.Category.Value == (int)ExceptionCategory.Superseded)))?.AsEnumerable();
if (isSpecificReportCategory)
{
filteredExceptions = filteredExceptions?.Where(x => x.Category.HasValue && x.Category.Value == (int)exceptionCategory);
}
if (reportDate.HasValue)
{
var startDate = reportDate.Value.Date;
var endDate = startDate.AddDays(1);
filteredExceptions = filteredExceptions?.Where(x => x.DateCreated >= startDate && x.DateCreated < endDate);
}
return filteredExceptions?.Select(s => s.ToValidationException()).ToList();
}
private ServiceResponseModel CreateResponse(bool success, HttpStatusCode statusCode, string message)
{
if (!success)
{
_logger.LogWarning("Service error occurred: {ErrorMessage}", message);
}
return new ServiceResponseModel
{
Success = success,
StatusCode = statusCode,
Message = message,
};
}
private ServiceResponseModel CreateSuccessResponse(string message) => CreateResponse(true, HttpStatusCode.OK, message);
private ServiceResponseModel CreateErrorResponse(string message, HttpStatusCode statusCode) => CreateResponse(false, statusCode, message);
private static string? ValidateServiceNowId(string serviceNowId)
{
if (serviceNowId.Contains(' '))
return "ServiceNowId cannot contain spaces.";
if (serviceNowId.Length < 9)
return "ServiceNowId must be at least 9 characters long.";
if (!serviceNowId.All(char.IsLetterOrDigit))
return "ServiceNowId must contain only alphanumeric characters.";
return null;
}
private ValidationException? GetExceptionDetails(ValidationException? exception, ParticipantDemographic? participantDemographic)
{
if (exception == null)
{
_logger.LogInformation("Exception not found");
return null;
}
exception.ExceptionDetails = new ExceptionDetails
{
GivenName = participantDemographic?.GivenName,
FamilyName = participantDemographic?.FamilyName,
DateOfBirth = participantDemographic?.DateOfBirth,
Gender = Enum.TryParse(participantDemographic?.Gender.ToString(), out Gender gender) ? gender : Gender.NotKnown,
ParticipantAddressLine1 = participantDemographic?.AddressLine1,
ParticipantAddressLine2 = participantDemographic?.AddressLine2,
ParticipantAddressLine3 = participantDemographic?.AddressLine3,
ParticipantAddressLine4 = participantDemographic?.AddressLine4,
ParticipantAddressLine5 = participantDemographic?.AddressLine5,
ParticipantPostCode = participantDemographic?.PostCode,
TelephoneNumberHome = participantDemographic?.TelephoneNumberHome,
EmailAddressHome = participantDemographic?.EmailAddressHome,
PrimaryCareProvider = participantDemographic?.PrimaryCareProvider,
SupersededByNhsNumber = participantDemographic?.SupersededByNhsNumber,
};
if (participantDemographic == null)
{
_logger.LogWarning("Missing data: ParticipantDemographic: {ParticipantDemographic}", participantDemographic != null);
}
return exception;
}
private async Task<List<ExceptionManagement>?> GetExceptionRecords(string nhsNumber, string screeningName)
{
var exceptions = await _validationExceptionDataServiceClient.GetByFilter(x => x.NhsNumber == nhsNumber && x.ScreeningName == screeningName);
return exceptions != null ? exceptions.ToList() : null;
}
private static string DateToString(DateTime? datetime)
{
if (datetime != null)
{
DateTime NonNullableDateTime = datetime.Value;
return NonNullableDateTime.ToString("yyyy-MM-dd");
}
// we throw here to stop processing as the date should never be null
throw new ArgumentNullException(nameof(datetime), "Failed to parse null datetime");
}
private static List<ValidationException> SortExceptions(SortOrder? sortOrder, IEnumerable<ValidationException> list, ExceptionStatus? status)
{
var filteredList = status switch
{
ExceptionStatus.Raised => list.Where(x => !string.IsNullOrEmpty(x.ServiceNowId)),
ExceptionStatus.NotRaised => list.Where(x => string.IsNullOrEmpty(x.ServiceNowId)),
_ => list
};
Func<ValidationException, DateTime?> dateProperty = status == ExceptionStatus.Raised
? x => x.ServiceNowCreatedDate
: x => x.DateCreated;
return sortOrder == SortOrder.Ascending
? [.. filteredList.OrderBy(dateProperty)]
: [.. filteredList.OrderByDescending(dateProperty)];
}
}