From 150a0ad81da3f8a61dae5ae44a2f37a687cb0685 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Thu, 19 Oct 2023 14:19:23 +0100 Subject: [PATCH] Add Prohibition Alerts to v3 teacher response --- .../V3/ApiModels/AlertInfo.cs | 7 +++ .../V3/ApiModels/AlertType.cs | 7 +++ .../TeachingRecordSystem.Api/V3/Constants.cs | 23 ++++++++++ .../V3/Handlers/GetTeacherHandler.cs | 29 ++++++++----- .../V3/Requests/GetTeacherRequest.cs | 3 +- .../V3/Responses/GetTeacherResponse.cs | 1 + .../V3/GetTeacherByTrnTests.cs | 9 ++++ .../V3/GetTeacherTestBase.cs | 43 +++++++++++++++++++ .../V3/GetTeacherTests.cs | 10 +++++ .../SeedCrmReferenceData.cs | 6 +++ 10 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertInfo.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertType.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertInfo.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertInfo.cs new file mode 100644 index 000000000..ea99231f9 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertInfo.cs @@ -0,0 +1,7 @@ +namespace TeachingRecordSystem.Api.V3.ApiModels; + +public record AlertInfo +{ + public required AlertType AlertType { get; init; } + public required string DqtSanctionCode { get; init; } +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertType.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertType.cs new file mode 100644 index 000000000..02c2d4032 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/ApiModels/AlertType.cs @@ -0,0 +1,7 @@ +namespace TeachingRecordSystem.Api.V3.ApiModels; + +public enum AlertType +{ + Prohibition, + // Only exposing Prohibitions for now +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Constants.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Constants.cs index 7848b3c80..ad10e5d80 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Constants.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Constants.cs @@ -38,4 +38,27 @@ public static class Constants "A24", "A23", }.ToImmutableArray(); + + public static ImmutableArray ProhibitionSanctionCodes { get; } = new[] + { + "G1", + "B1", + "G2", + "B6", + "T2", + "B3", + "B5", + "T3", + "T5", + "T4", + "T1", + "A25B", + "A25A", + "A21B", + "A21A", + "A5B", + "A5A", + "A1B", + "A1A", + }.ToImmutableArray(); } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Handlers/GetTeacherHandler.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Handlers/GetTeacherHandler.cs index 9645996e2..1206d7b1a 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Handlers/GetTeacherHandler.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Handlers/GetTeacherHandler.cs @@ -187,22 +187,16 @@ public GetTeacherHandler(IDataverseAdapter dataverseAdapter, ICrmQueryDispatcher pendingDateOfBirthChange = incidents.Any(i => i.SubjectId.Id == dateOfBirthChangeSubject.Id); } - IEnumerable? sanctions = null; + SanctionResult[]? sanctions = null; - if (request.Include.HasFlag(GetTeacherRequestIncludes.Sanctions)) + if (request.Include.HasFlag(GetTeacherRequestIncludes.Sanctions) || request.Include.HasFlag(GetTeacherRequestIncludes.Alerts)) { var getSanctionsQuery = new GetSanctionsByContactIdsQuery( new[] { teacher.Id }, ActiveOnly: true, ColumnSet: new(dfeta_sanction.Fields.dfeta_StartDate)); - sanctions = (await _crmQueryDispatcher.ExecuteQuery(getSanctionsQuery))[teacher.Id] - .Where(s => Constants.ExposableSanctionCodes.Contains(s.SanctionCode)) - .Select(s => new SanctionInfo() - { - Code = s.SanctionCode, - StartDate = s.Sanction.dfeta_StartDate?.ToDateOnlyWithDqtBstFix(isLocalTime: true) - }); + sanctions = (await _crmQueryDispatcher.ExecuteQuery(getSanctionsQuery))[teacher.Id]; } var firstName = teacher.ResolveFirstName(); @@ -260,7 +254,22 @@ public GetTeacherHandler(IDataverseAdapter dataverseAdapter, ICrmQueryDispatcher Option.Some(MapHigherEducationQualifications(qualifications!)) : default, Sanctions = request.Include.HasFlag(GetTeacherRequestIncludes.Sanctions) ? - Option.Some(sanctions!) : + Option.Some(sanctions! + .Where(s => Constants.ExposableSanctionCodes.Contains(s.SanctionCode)) + .Select(s => new SanctionInfo() + { + Code = s.SanctionCode, + StartDate = s.Sanction.dfeta_StartDate?.ToDateOnlyWithDqtBstFix(isLocalTime: true) + })) : + default, + Alerts = request.Include.HasFlag(GetTeacherRequestIncludes.Alerts) ? + Option.Some(sanctions! + .Where(s => Constants.ProhibitionSanctionCodes.Contains(s.SanctionCode)) + .Select(s => new AlertInfo() + { + AlertType = AlertType.Prohibition, + DqtSanctionCode = s.SanctionCode + })) : default }; } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Requests/GetTeacherRequest.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Requests/GetTeacherRequest.cs index b6f38acfa..7bdd943d0 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Requests/GetTeacherRequest.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Requests/GetTeacherRequest.cs @@ -24,6 +24,7 @@ public enum GetTeacherRequestIncludes PendingDetailChanges = 1 << 4, HigherEducationQualifications = 1 << 5, Sanctions = 1 << 6, + Alerts = 1 << 7, - All = Induction | InitialTeacherTraining | NpqQualifications | MandatoryQualifications | PendingDetailChanges | HigherEducationQualifications | Sanctions + All = Induction | InitialTeacherTraining | NpqQualifications | MandatoryQualifications | PendingDetailChanges | HigherEducationQualifications | Sanctions | Alerts } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Responses/GetTeacherResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Responses/GetTeacherResponse.cs index 32c32f8f2..b817e12e5 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Responses/GetTeacherResponse.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Responses/GetTeacherResponse.cs @@ -23,6 +23,7 @@ public record GetTeacherResponse public required Option> MandatoryQualifications { get; init; } public required Option> HigherEducationQualifications { get; init; } public required Option> Sanctions { get; init; } + public required Option> Alerts { get; init; } } public record GetTeacherResponseQts diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherByTrnTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherByTrnTests.cs index 0748df409..bec3f401b 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherByTrnTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherByTrnTests.cs @@ -135,4 +135,13 @@ public Task Get_ValidRequestWithSanctions_ReturnsExpectedSanctionsContent() return ValidRequestWithSanctions_ReturnsExpectedSanctionsContent(HttpClientWithApiKey, baseUrl, trn); } + + [Fact] + public Task Get_ValidRequestWithAlerts_ReturnsExpectedSanctionsContent() + { + var trn = "1234567"; + var baseUrl = $"/v3/teachers/{trn}"; + + return ValidRequestWithAlerts_ReturnsExpectedSanctionsContent(HttpClientWithApiKey, baseUrl, trn); + } } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTestBase.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTestBase.cs index 682eb4ca3..ffd0b19bb 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTestBase.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTestBase.cs @@ -568,6 +568,49 @@ protected async Task ValidRequestWithSanctions_ReturnsExpectedSanctionsContent( responseSanctions); } + protected async Task ValidRequestWithAlerts_ReturnsExpectedSanctionsContent( + HttpClient httpClient, + string baseUrl, + string trn) + { + // Arrange + var contact = await CreateContact(trn); + + var sanctions = new (string SanctionCode, DateOnly? StartDate)[] + { + new("B1", null), + new("G1", new DateOnly(2022, 4, 1)), + }; + Debug.Assert(sanctions.Select(s => s.SanctionCode).All(TeachingRecordSystem.Api.V3.Constants.ProhibitionSanctionCodes.Contains)); + + await ConfigureMocks(trn, contact, sanctions: sanctions); + + var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}?include=Alerts"); + + // Act + var response = await httpClient.SendAsync(request); + + // Assert + var jsonResponse = await AssertEx.JsonResponse(response); + var responseAlerts = jsonResponse.RootElement.GetProperty("alerts"); + + AssertEx.JsonObjectEquals( + new[] + { + new + { + alertType = "Prohibition", + dqtSanctionCode = sanctions[0].SanctionCode + }, + new + { + alertType = "Prohibition", + dqtSanctionCode = sanctions[1].SanctionCode + } + }, + responseAlerts); + } + private async Task ConfigureMocks( string trn, Contact contact, diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTests.cs index 4da8e1193..580cb38b1 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/V3/GetTeacherTests.cs @@ -132,4 +132,14 @@ public Task Get_ValidRequestWithSanctions_ReturnsExpectedSanctionsContent() return ValidRequestWithSanctions_ReturnsExpectedSanctionsContent(httpClient, baseUrl, trn); } + + [Fact] + public Task Get_ValidRequestWithAlerts_ReturnsExpectedSanctionsContent() + { + var trn = "1234567"; + var httpClient = GetHttpClientWithIdentityAccessToken(trn); + var baseUrl = "v3/teacher"; + + return ValidRequestWithAlerts_ReturnsExpectedSanctionsContent(httpClient, baseUrl, trn); + } } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.TestCommon/SeedCrmReferenceData.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.TestCommon/SeedCrmReferenceData.cs index 3e6bb0d31..3d34a256b 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.TestCommon/SeedCrmReferenceData.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.TestCommon/SeedCrmReferenceData.cs @@ -59,5 +59,11 @@ private void AddSanctionCodes() dfeta_Value = "A18", dfeta_name = "A18 Description" }); + + _xrmFakedContext.CreateEntity(new dfeta_sanctioncode() + { + dfeta_Value = "B1", + dfeta_name = "B1 Description" + }); } }