-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidationExceptionData.cs
More file actions
176 lines (149 loc) · 7.14 KB
/
ValidationExceptionData.cs
File metadata and controls
176 lines (149 loc) · 7.14 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
namespace Data.Database;
using System;
using System.Data;
using System.Threading.Tasks;
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<bool> UpdateExceptionServiceNowId(int exceptionId, string serviceNowId)
{
try
{
var exception = await _validationExceptionDataServiceClient.GetSingle(exceptionId.ToString());
if (exception == null)
{
_logger.LogWarning("Exception with ID {ExceptionId} not found", exceptionId);
return false;
}
exception.ServiceNowId = serviceNowId;
exception.RecordUpdatedDate = DateTime.UtcNow;
return await _validationExceptionDataServiceClient.Update(exception);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating ServiceNowID for exception {ExceptionId}", exceptionId);
return false;
}
}
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)];
}
}