Skip to content

Commit

Permalink
Add Prohibition Alerts to v3 teacher response (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Oct 19, 2023
1 parent e022417 commit ab35c7a
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TeachingRecordSystem.Api.V3.ApiModels;

public record AlertInfo
{
public required AlertType AlertType { get; init; }
public required string DqtSanctionCode { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TeachingRecordSystem.Api.V3.ApiModels;

public enum AlertType
{
Prohibition,
// Only exposing Prohibitions for now
}
23 changes: 23 additions & 0 deletions TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,27 @@ public static class Constants
"A24",
"A23",
}.ToImmutableArray();

public static ImmutableArray<string> ProhibitionSanctionCodes { get; } = new[]
{
"G1",
"B1",
"G2",
"B6",
"T2",
"B3",
"B5",
"T3",
"T5",
"T4",
"T1",
"A25B",
"A25A",
"A21B",
"A21A",
"A5B",
"A5A",
"A1B",
"A1A",
}.ToImmutableArray();
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,16 @@ public GetTeacherHandler(IDataverseAdapter dataverseAdapter, ICrmQueryDispatcher
pendingDateOfBirthChange = incidents.Any(i => i.SubjectId.Id == dateOfBirthChangeSubject.Id);
}

IEnumerable<SanctionInfo>? 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();
Expand Down Expand Up @@ -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
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public record GetTeacherResponse
public required Option<IEnumerable<GetTeacherResponseMandatoryQualificationsQualification>> MandatoryQualifications { get; init; }
public required Option<IEnumerable<GetTeacherResponseHigherEducationQualificationsQualification>> HigherEducationQualifications { get; init; }
public required Option<IEnumerable<SanctionInfo>> Sanctions { get; init; }
public required Option<IEnumerable<AlertInfo>> Alerts { get; init; }
}

public record GetTeacherResponseQts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
}
}

0 comments on commit ab35c7a

Please sign in to comment.