Skip to content

Commit 15003bf

Browse files
fix: Nhs Number Search (#1801)
* feat: search by NHS Number * fix: search fixes * chore: removed sort, functionality in another branch * test: updated existing and added new tests * test: fixed tests
1 parent a9adbec commit 15003bf

4 files changed

Lines changed: 137 additions & 76 deletions

File tree

application/CohortManager/src/Functions/Shared/Data/Database/ValidationExceptionData.cs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public async Task<List<ExceptionManagement>> GetByFilter(Expression<Func<Excepti
140140

141141
public async Task<ValidationExceptionsByNhsNumberResponse> GetExceptionsWithReportsByNhsNumber(string nhsNumber)
142142
{
143-
var validationExceptions = await GetValidationExceptionsByNhsNumber(nhsNumber);
143+
var allValidationExceptions = await GetValidationExceptionsByNhsNumber(nhsNumber);
144144

145-
if (validationExceptions.Count == 0)
145+
if (allValidationExceptions.Count == 0)
146146
{
147147
return new ValidationExceptionsByNhsNumberResponse
148148
{
@@ -152,15 +152,46 @@ public async Task<ValidationExceptionsByNhsNumberResponse> GetExceptionsWithRepo
152152
};
153153
}
154154

155-
var reports = GenerateExceptionReports(validationExceptions);
155+
var exceptions = allValidationExceptions
156+
.Where(x => x.Category == (int)ExceptionCategory.NBO)
157+
.ToList();
158+
159+
var reportExceptions = allValidationExceptions
160+
.Where(x => x.Category == (int)ExceptionCategory.Confusion ||
161+
x.Category == (int)ExceptionCategory.Superseded)
162+
.ToList();
163+
164+
var reports = GenerateExceptionReports(reportExceptions);
165+
156166
return new ValidationExceptionsByNhsNumberResponse
157167
{
158-
Exceptions = validationExceptions,
168+
Exceptions = exceptions,
159169
Reports = reports,
160170
NhsNumber = nhsNumber
161171
};
162172
}
163173

174+
private async Task<List<ValidationException>> GetValidationExceptionsByNhsNumber(string nhsNumber)
175+
{
176+
var exceptions = await _validationExceptionDataServiceClient.GetByFilter(x =>
177+
x.NhsNumber == nhsNumber &&
178+
x.Category.HasValue &&
179+
(x.Category.Value == (int)ExceptionCategory.NBO ||
180+
x.Category.Value == (int)ExceptionCategory.Confusion ||
181+
x.Category.Value == (int)ExceptionCategory.Superseded));
182+
183+
if (exceptions == null || !exceptions.Any())
184+
{
185+
return [];
186+
}
187+
188+
return [.. exceptions
189+
.Select(GetValidationExceptionWithDetails)
190+
.Where(x => x != null)
191+
.Cast<ValidationException>()
192+
.OrderByDescending(x => x.DateCreated)];
193+
}
194+
164195
private List<ValidationException> MapToValidationExceptions(IEnumerable<ExceptionManagement> exceptions)
165196
{
166197
return exceptions.Select(GetValidationExceptionWithDetails).Where(x => x != null).ToList()!;
@@ -252,7 +283,6 @@ private ServiceResponseModel CreateResponse(bool success, HttpStatusCode statusC
252283
};
253284
}
254285

255-
256286
private static string? ValidateServiceNowId(string serviceNowId)
257287
{
258288
if (string.IsNullOrWhiteSpace(serviceNowId))
@@ -343,27 +373,9 @@ private static List<ValidationException> SortExceptions(SortOrder? sortOrder, IE
343373
: [.. filteredList.OrderByDescending(dateProperty)];
344374
}
345375

346-
347-
348-
private async Task<List<ValidationException>> GetValidationExceptionsByNhsNumber(string nhsNumber)
349-
{
350-
var exceptions = await _validationExceptionDataServiceClient.GetByFilter(x => x.NhsNumber == nhsNumber && x.Category.HasValue && x.Category.Value == (int)ExceptionCategory.NBO);
351-
if (exceptions == null || !exceptions.Any())
352-
{
353-
return [];
354-
}
355-
356-
return [.. exceptions
357-
.Select(GetValidationExceptionWithDetails)
358-
.Where(x => x != null)
359-
.Cast<ValidationException>()
360-
.OrderByDescending(x => x.DateCreated)];
361-
}
362-
363376
private static List<ValidationExceptionReport> GenerateExceptionReports(List<ValidationException> validationExceptions)
364377
{
365378
return [.. validationExceptions
366-
.Where(x => x.Category.HasValue && (x.Category.Value == 12 || x.Category.Value == 13))
367379
.GroupBy(x => new
368380
{
369381
Date = x.DateCreated?.Date ?? DateTime.Now.Date,

application/CohortManager/src/Functions/screeningDataServices/GetValidationExceptions/GetValidationExceptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public async Task<HttpResponseData> GetValidationExceptionsByNhsNumber([HttpTrig
157157
{
158158
var result = await _validationData.GetExceptionsWithReportsByNhsNumber(cleanedNhsNumber);
159159

160-
if (result.Exceptions.Count == 0)
160+
if (result.Exceptions.Count == 0 && result.Reports.Count == 0)
161161
{
162162
return _createResponse.CreateHttpResponse(HttpStatusCode.NoContent, req);
163163
}

application/CohortManager/src/Web/app/exceptions/search/page.tsx

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ function ResultsHeader({ startItem, endItem, totalCount }: {
146146
);
147147
}
148148

149+
// Component: No Exceptions State
150+
function NoExceptionsState({ nhsNumber }: { readonly nhsNumber: string }) {
151+
return (
152+
<>
153+
<h2 className="nhsuk-heading-m nhsuk-u-margin-bottom-5">Exceptions</h2>
154+
<div className="nhsuk-card nhsuk-u-margin-bottom-5">
155+
<div className="nhsuk-card__content">
156+
<p>No exceptions found for NHS Number {nhsNumber}</p>
157+
</div>
158+
</div>
159+
</>
160+
);
161+
}
162+
149163
// Component: Reports Table Row
150164
function ReportsTableRow({ report, nhsNumber, index }: {
151165
readonly report: ValidationExceptionReport;
@@ -243,10 +257,10 @@ export default async function Page({
243257
});
244258

245259
const totalCount = response.data.PaginatedExceptions.TotalItems || 0;
260+
const reportsCount = response.data.Reports?.length || 0;
246261

247-
// Redirect to No Results page if no results found
248-
if (totalCount === 0) {
249-
redirect(`/exceptions/noResults`);
262+
if (totalCount === 0 && reportsCount === 0) {
263+
redirect(`/exceptions/noResults`);
250264
}
251265

252266
try {
@@ -271,16 +285,22 @@ export default async function Page({
271285
Search results for {nhsNumber}
272286
</h1>
273287

274-
<ResultsHeader
275-
startItem={startItem}
276-
endItem={endItem}
277-
totalCount={totalCount}
278-
/>
279-
<div className="nhsuk-card nhsuk-u-margin-bottom-5">
280-
<div className="nhsuk-card__content">
281-
<ExceptionsTable exceptions={exceptionDetails} />
282-
</div>
283-
</div>
288+
{exceptionDetails.length > 0 ? (
289+
<>
290+
<ResultsHeader
291+
startItem={startItem}
292+
endItem={endItem}
293+
totalCount={totalCount}
294+
/>
295+
<div className="nhsuk-card nhsuk-u-margin-bottom-5">
296+
<div className="nhsuk-card__content">
297+
<ExceptionsTable exceptions={exceptionDetails} />
298+
</div>
299+
</div>
300+
</>
301+
) : (
302+
<NoExceptionsState nhsNumber={nhsNumber} />
303+
)}
284304

285305
{totalPages > 1 && (
286306
<Pagination

0 commit comments

Comments
 (0)