Skip to content

Commit

Permalink
status managed by CPD logic
Browse files Browse the repository at this point in the history
  • Loading branch information
CathLass committed Dec 6, 2024
1 parent ce5d136 commit b09d180
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.DataStore.Postgres;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt.Models;
using TeachingRecordSystem.Core.Dqt.Queries;

namespace TeachingRecordSystem.SupportUi.Pages.Persons.PersonDetail;

public class InductionModel(TrsDbContext dbContext) : PageModel
public class InductionModel(TrsDbContext dbContext, ICrmQueryDispatcher crmQueryDispatcher, IClock clock) : PageModel
{
private static readonly string NoQualifiedTeacherStatusWarning = "This teacher doesn\u2019t have QTS and, therefore, is ineligible for induction.";
private static readonly string InductionManagedByCpdWarning = "To change a teacher\u2019s induction status to passed, failed, or in progress, use the Record inductions as an appropriate body service.";
private bool StatusManagedByCPD;
private bool TeacherHoldsQualifiedTeacherStatus;

[FromRoute]
public Guid PersonId { get; set; }

Expand Down Expand Up @@ -34,19 +40,22 @@ public string? ExemptionReasonsText
get => string.Join(", ", ExemptionReasons.SplitFlags());
}

public string StatusWarningMessage // CML TODO change logic to match test spec (criteria based on cpd fields)
public string? StatusWarningMessage
{
get
{
var message = "Changing status to In Progress, Passed or Failed will be handled in CPD";
return Status switch
if (StatusManagedByCPD)
{
return InductionManagedByCpdWarning;
}
else if (TeacherHoldsQualifiedTeacherStatus)
{
return NoQualifiedTeacherStatusWarning;
}
else
{
InductionStatus.RequiredToComplete => message,
InductionStatus.InProgress => message,
InductionStatus.Passed => message,
InductionStatus.Failed => message,
_ => string.Empty
};
return null;
}
}
}

Expand All @@ -55,9 +64,21 @@ public async Task OnGetAsync()
var person = await dbContext.Persons
.SingleAsync(q => q.PersonId == PersonId);

GetActiveContactDetailByIdQuery query = new(
person.PersonId,
ColumnSet: new ColumnSet(Contact.Fields.dfeta_QTSDate));

var result = await crmQueryDispatcher.ExecuteQueryAsync(query);

TeacherHoldsQualifiedTeacherStatus = result?.Contact.dfeta_QTSDate is null;

Status = person!.InductionStatus;
StartDate = person!.InductionStartDate;
CompletionDate = person!.InductionCompletedDate;
ExemptionReasons = person!.InductionExemptionReasons;
var sevenYearsAgo = clock.Today.AddYears(-7);
StatusManagedByCPD = person!.CpdInductionStatus is not null
&& person.CpdInductionCompletedDate is not null
&& person.CpdInductionCompletedDate < sevenYearsAgo;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using TeachingRecordSystem.Core.DataStore.Postgres.Models;

namespace TeachingRecordSystem.SupportUi.Tests.PageTests.Persons.PersonDetail;

public class InductionTests(HostFixture hostFixture) : TestBase(hostFixture)
Expand Down Expand Up @@ -144,16 +146,15 @@ public async Task Get_WithPersonIdForPersonWithInductionStatusRequiringStartDate
Assert.NotNull(doc.GetAllElementsByTestId("induction-backlink"));
}

[Theory]
[InlineData(InductionStatus.Passed)]
public async Task Get_WithPersonIdForPersonWithInductionStatusRequiringStartDateButStartDateIsNull_DisplaysExpectedContent(InductionStatus setInductionStatus)
[Fact(Skip ="TestData setup doesn't allow null start date")]
public async Task Get_WithPersonIdForPersonWithInductionStatusRequiringStartDateButStartDateIsNull_DisplaysExpectedContent()
{
// Arrange
//var expectedWarning = "To change a teacher's induction status ";
var person = await TestData.CreatePersonAsync(
x => x
.WithQts()
.WithInductionStatus(setInductionStatus)
.WithInductionStatus(InductionStatus.InProgress)
);

var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.ContactId}/induction");
Expand All @@ -163,20 +164,14 @@ public async Task Get_WithPersonIdForPersonWithInductionStatusRequiringStartDate

// Assert
var doc = await AssertEx.HtmlResponseAsync(response);
//Assert.Contains(expectedWarning, doc.GetElementByTestId("induction-status-warning")!.TextContent); // to be covered by other test (below)
var inductionStatus = doc.GetElementByTestId("induction-status");
Assert.Contains(StatusStrings[setInductionStatus], inductionStatus!.TextContent);
var startDate = doc.GetElementByTestId("induction-start-date")!.Children[1].TextContent;
Assert.True(String.IsNullOrWhiteSpace(startDate.Trim()));
Assert.Null(doc.GetElementByTestId("induction-exemption-reasons"));
Assert.NotNull(doc.GetAllElementsByTestId("induction-backlink"));
}

[Theory]
[InlineData(InductionStatus.Passed)]
[InlineData(InductionStatus.Failed)]
[InlineData(InductionStatus.FailedInWales)]
// CML TODO - method name - completed vs completion -when the decision comes in
public async Task Get_WithPersonIdForPersonWithInductionStatusRequiringCompletionDate_DisplaysExpectedCompletionDate(InductionStatus setInductionStatus)
{
// Arrange
Expand Down Expand Up @@ -204,57 +199,67 @@ public async Task Get_WithPersonIdForPersonWithInductionStatusRequiringCompletio
Assert.Contains(setCompletionDate.ToString("d MMMM yyyy"), completionDate);
}

//[Theory]
//[InlineData(InductionStatus.InProgress)]
//[InlineData(InductionStatus.Passed)]
//[InlineData(InductionStatus.Failed)]
// CL - what I had inferred from page designs
//public async Task Get_WithPersonIdForPersonWithInductionStatusMangedByCPD_DisplaysWarning(InductionStatus setInductionStatus)
//{
// // Arrange
// var expectedWarning = "To change a teacher's induction status ";
// var setStartDate = DateOnly.FromDateTime(DateTime.Now);
// var person = await TestData.CreatePersonAsync(
// x => x
// .WithQts()
// .WithInductionStatus(builder => builder
// .WithStatus(setInductionStatus)
// .WithStartDate(setStartDate)
// ));

// var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.ContactId}/induction");

// // Act
// var response = await HttpClient.SendAsync(request);

// // Assert
// var doc = await AssertEx.HtmlResponseAsync(response);
// Assert.Contains(expectedWarning, doc.GetElementByTestId("induction-status-warning")!.TextContent);
//}

//[Fact]
//// replacement for above, when TestData builder methods are there - plus add tests for these conditions not met
//public async Task Get_WithPersonIdForPersonWithInductionStatusMangedByCPD_DisplaysWarning()
//{
// //Arrange
// var expectedWarning = "To change a teacher's induction status ";
// var sevenYearsAgo = Clock.Today.AddYears(-7).AddDays(-1); // more than 7 years ago
// var setCompletionDate = DateOnly.FromDateTime(sevenYearsAgo);
// var person = await TestData.CreatePersonAsync(
// x => x
// .WithQts()
// .WithCpdInductionStatus(builder => builder
// .WithStatus(It.IsAny<CpdInductionStatus>)
// .WithCompletionDate(setCompletionDate)
// ));

// var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.ContactId}/induction");

// // Act
// var response = await HttpClient.SendAsync(request);

// // Assert
// var doc = await AssertEx.HtmlResponseAsync(response);
// Assert.Contains(expectedWarning, doc.GetElementByTestId("induction-status-warning")!.TextContent);
//}
[Fact]
public async Task Get_WithPersonIdForPersonWithInductionStatusManagedByCPD_ShowsWarning()
{
//Arrange
var expectedWarning = "To change a teacher’s induction status ";
var overSevenYearsAgo = Clock.Today.AddYears(-7).AddDays(-1);

var person = await TestData.CreatePersonAsync();
await WithDbContext(async dbContext =>
{
dbContext.Attach(person.Person);
person.Person.SetCpdInductionStatus(
InductionStatus.Passed,
startDate: Clock.Today.AddYears(-7).AddMonths(-6),
completedDate: overSevenYearsAgo,
cpdModifiedOn: Clock.UtcNow,
updatedBy: SystemUser.SystemUserId,
now: Clock.UtcNow,
out _);
await dbContext.SaveChangesAsync();
});

var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.ContactId}/induction");

// Act
var response = await HttpClient.SendAsync(request);

// Assert
var doc = await AssertEx.HtmlResponseAsync(response);
Assert.Contains(expectedWarning, doc.GetElementByTestId("induction-status-warning")!.Children[1].TextContent);
}

[Fact]
public async Task Get_WithPersonIdForPersonWithInductionStatusNotManagedByCPD_NoWarning()
{
//Arrange
var underSevenYearsAgo = Clock.Today.AddYears(-6);

var person = await TestData.CreatePersonAsync();

await WithDbContext(async dbContext =>
{
dbContext.Attach(person.Person);
person.Person.SetCpdInductionStatus(
InductionStatus.Passed,
startDate: underSevenYearsAgo.AddYears(-1),
completedDate: underSevenYearsAgo,
cpdModifiedOn: Clock.UtcNow,
updatedBy: SystemUser.SystemUserId,
now: Clock.UtcNow,
out _);
await dbContext.SaveChangesAsync();
});

var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.ContactId}/induction");

// Act
var response = await HttpClient.SendAsync(request);

// Assert
var doc = await AssertEx.HtmlResponseAsync(response);
Assert.Null(doc.GetElementByTestId("induction-status-warning"));
}
}

0 comments on commit b09d180

Please sign in to comment.