Skip to content

Commit

Permalink
Check that MQs have actually been added/edited (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 2, 2024
1 parent bf0ba2b commit 78fe30e
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ public static bool TryMapFromDqtMqEstablishment(
return false;
}

switch (mqestablishment.dfeta_Value)
return TryMapFromDqtMqEstablishmentValue(mqestablishment.dfeta_Value, out provider);
}

public static bool TryMapFromDqtMqEstablishmentValue(
string mqestablishmentValue,
[NotNullWhen(true)] out MandatoryQualificationProvider? provider)
{
switch (mqestablishmentValue)
{
case "963": // University of Oxford/Oxford Polytechnic
provider = All.Single(p => p.Name == "University of Oxford/Oxford Polytechnic");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.Dqt.Models;
using TeachingRecordSystem.Core.Dqt.Queries;
using TeachingRecordSystem.Core.Jobs.Scheduling;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.SupportUi.Pages.Mqs.AddMq;

[Journey(JourneyNames.AddMq), RequireJourneyInstance]
public class CheckAnswersModel : PageModel
public class CheckAnswersModel(
ICrmQueryDispatcher crmQueryDispatcher,
ReferenceDataCache referenceDataCache,
TrsLinkGenerator linkGenerator,
IBackgroundJobScheduler backgroundJobScheduler) : PageModel
{
private readonly ICrmQueryDispatcher _crmQueryDispatcher;
private readonly ReferenceDataCache _referenceDataCache;
private readonly TrsLinkGenerator _linkGenerator;

public CheckAnswersModel(
ICrmQueryDispatcher crmQueryDispatcher,
ReferenceDataCache referenceDataCache,
TrsLinkGenerator linkGenerator)
{
_crmQueryDispatcher = crmQueryDispatcher;
_referenceDataCache = referenceDataCache;
_linkGenerator = linkGenerator;
}

public JourneyInstance<AddMqState>? JourneyInstance { get; set; }

[FromQuery]
Expand All @@ -42,9 +34,9 @@ public CheckAnswersModel(

public async Task<IActionResult> OnPost()
{
var mqSpecialism = await _referenceDataCache.GetMqSpecialismByValue(Specialism!.Value.GetDqtValue());
var mqSpecialism = await referenceDataCache.GetMqSpecialismByValue(Specialism!.Value.GetDqtValue());

await _crmQueryDispatcher.ExecuteQuery(
var qualificationId = await crmQueryDispatcher.ExecuteQuery(
new CreateMandatoryQualificationQuery()
{
ContactId = PersonId,
Expand All @@ -57,16 +49,18 @@ await _crmQueryDispatcher.ExecuteQuery(
: null
});

await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncMandatoryQualification(qualificationId, CancellationToken.None));

await JourneyInstance!.CompleteAsync();
TempData.SetFlashSuccess("Mandatory qualification added");

return Redirect(_linkGenerator.PersonQualifications(PersonId));
return Redirect(linkGenerator.PersonQualifications(PersonId));
}

public async Task<IActionResult> OnPostCancel()
{
await JourneyInstance!.DeleteAsync();
return Redirect(_linkGenerator.PersonDetail(PersonId));
return Redirect(linkGenerator.PersonDetail(PersonId));
}

public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
Expand All @@ -75,11 +69,11 @@ public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingConte

if (!JourneyInstance!.State.IsComplete)
{
context.Result = Redirect(_linkGenerator.MqAddProvider(PersonId, JourneyInstance.InstanceId));
context.Result = Redirect(linkGenerator.MqAddProvider(PersonId, JourneyInstance.InstanceId));
}

PersonName = personDetail!.Contact.ResolveFullName(includeMiddleName: false);
MqEstablishment = await _referenceDataCache.GetMqEstablishmentByValue(JourneyInstance!.State.MqEstablishmentValue!);
MqEstablishment = await referenceDataCache.GetMqEstablishmentByValue(JourneyInstance!.State.MqEstablishmentValue!);
Specialism = JourneyInstance.State.Specialism;
StartDate = JourneyInstance.State.StartDate;
Status = JourneyInstance.State.Status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.Dqt.Models;
using TeachingRecordSystem.Core.Dqt.Queries;
using TeachingRecordSystem.Core.Jobs.Scheduling;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.SupportUi.Pages.Mqs.EditMq.Provider;

[Journey(JourneyNames.EditMqProvider), RequireJourneyInstance]
public class ConfirmModel : PageModel
public class ConfirmModel(
ICrmQueryDispatcher crmQueryDispatcher,
ReferenceDataCache referenceDataCache,
TrsLinkGenerator linkGenerator,
IBackgroundJobScheduler backgroundJobScheduler) : PageModel
{
private readonly ICrmQueryDispatcher _crmQueryDispatcher;
private readonly ReferenceDataCache _referenceDataCache;
private readonly TrsLinkGenerator _linkGenerator;

public ConfirmModel(
ICrmQueryDispatcher crmQueryDispatcher,
ReferenceDataCache referenceDataCache,
TrsLinkGenerator linkGenerator)
{
_crmQueryDispatcher = crmQueryDispatcher;
_referenceDataCache = referenceDataCache;
_linkGenerator = linkGenerator;
}

public JourneyInstance<EditMqProviderState>? JourneyInstance { get; set; }

[FromRoute]
Expand All @@ -38,35 +30,37 @@ public ConfirmModel(

public async Task<IActionResult> OnPost()
{
await _crmQueryDispatcher.ExecuteQuery(
await crmQueryDispatcher.ExecuteQuery(
new UpdateMandatoryQualificationEstablishmentQuery(
QualificationId,
NewMqEstablishment!.Id));

await JourneyInstance!.CompleteAsync();
TempData.SetFlashSuccess("Mandatory qualification changed");

return Redirect(_linkGenerator.PersonQualifications(PersonId!.Value));
await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncMandatoryQualification(QualificationId, CancellationToken.None));

return Redirect(linkGenerator.PersonQualifications(PersonId!.Value));
}

public async Task<IActionResult> OnPostCancel()
{
await JourneyInstance!.DeleteAsync();
return Redirect(_linkGenerator.PersonQualifications(PersonId!.Value));
return Redirect(linkGenerator.PersonQualifications(PersonId!.Value));
}

public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
{
if (!JourneyInstance!.State.IsComplete)
{
context.Result = Redirect(_linkGenerator.MqEditProvider(QualificationId, JourneyInstance.InstanceId));
context.Result = Redirect(linkGenerator.MqEditProvider(QualificationId, JourneyInstance.InstanceId));
return;
}

PersonId = JourneyInstance!.State.PersonId;
PersonName = JourneyInstance!.State.PersonName;
CurrentMqEstablishmentName = JourneyInstance!.State.CurrentMqEstablishmentName;
NewMqEstablishment = await _referenceDataCache.GetMqEstablishmentByValue(JourneyInstance!.State.MqEstablishmentValue!);
NewMqEstablishment = await referenceDataCache.GetMqEstablishmentByValue(JourneyInstance!.State.MqEstablishmentValue!);

await next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.Dqt.Queries;
using TeachingRecordSystem.Core.Jobs.Scheduling;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.SupportUi.Pages.Mqs.EditMq.Specialism;

[Journey(JourneyNames.EditMqSpecialism), RequireJourneyInstance]
public class ConfirmModel(
ICrmQueryDispatcher crmQueryDispatcher,
ReferenceDataCache referenceDataCache,
TrsLinkGenerator linkGenerator) : PageModel
TrsLinkGenerator linkGenerator,
IBackgroundJobScheduler backgroundJobScheduler) : PageModel
{
public JourneyInstance<EditMqSpecialismState>? JourneyInstance { get; set; }

Expand All @@ -36,6 +39,8 @@ await crmQueryDispatcher.ExecuteQuery(
await JourneyInstance!.CompleteAsync();
TempData.SetFlashSuccess("Mandatory qualification changed");

await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncMandatoryQualification(QualificationId, CancellationToken.None));

return Redirect(linkGenerator.PersonQualifications(PersonId!.Value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.Dqt.Queries;
using TeachingRecordSystem.Core.Jobs.Scheduling;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.SupportUi.Pages.Mqs.EditMq.StartDate;

[Journey(JourneyNames.EditMqStartDate), RequireJourneyInstance]
public class ConfirmModel : PageModel
public class ConfirmModel(
ICrmQueryDispatcher crmQueryDispatcher,
TrsLinkGenerator linkGenerator,
IBackgroundJobScheduler backgroundJobScheduler) : PageModel
{
private readonly ICrmQueryDispatcher _crmQueryDispatcher;
private readonly TrsLinkGenerator _linkGenerator;

public ConfirmModel(
ICrmQueryDispatcher crmQueryDispatcher,
TrsLinkGenerator linkGenerator)
{
_crmQueryDispatcher = crmQueryDispatcher;
_linkGenerator = linkGenerator;
}

public JourneyInstance<EditMqStartDateState>? JourneyInstance { get; set; }

[FromRoute]
Expand All @@ -34,28 +28,30 @@ public ConfirmModel(

public async Task<IActionResult> OnPost()
{
await _crmQueryDispatcher.ExecuteQuery(
await crmQueryDispatcher.ExecuteQuery(
new UpdateMandatoryQualificationStartDateQuery(
QualificationId,
NewStartDate!.Value));

await JourneyInstance!.CompleteAsync();
TempData.SetFlashSuccess("Mandatory qualification changed");

return Redirect(_linkGenerator.PersonQualifications(PersonId!.Value));
await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncMandatoryQualification(QualificationId, CancellationToken.None));

return Redirect(linkGenerator.PersonQualifications(PersonId!.Value));
}

public async Task<IActionResult> OnPostCancel()
{
await JourneyInstance!.DeleteAsync();
return Redirect(_linkGenerator.PersonQualifications(PersonId!.Value));
return Redirect(linkGenerator.PersonQualifications(PersonId!.Value));
}

public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
{
if (!JourneyInstance!.State.IsComplete)
{
context.Result = Redirect(_linkGenerator.MqEditStartDate(QualificationId, JourneyInstance.InstanceId));
context.Result = Redirect(linkGenerator.MqEditStartDate(QualificationId, JourneyInstance.InstanceId));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.Dqt.Queries;
using TeachingRecordSystem.Core.Jobs.Scheduling;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.SupportUi.Pages.Mqs.EditMq.Status;

[Journey(JourneyNames.EditMqResult), RequireJourneyInstance]
public class ConfirmModel(
ICrmQueryDispatcher crmQueryDispatcher,
TrsLinkGenerator linkGenerator) : PageModel
TrsLinkGenerator linkGenerator,
IBackgroundJobScheduler backgroundJobScheduler) : PageModel
{
public JourneyInstance<EditMqResultState>? JourneyInstance { get; set; }

Expand Down Expand Up @@ -38,6 +41,8 @@ await crmQueryDispatcher.ExecuteQuery(
await JourneyInstance!.CompleteAsync();
TempData.SetFlashSuccess("Mandatory qualification changed");

await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncMandatoryQualification(QualificationId, CancellationToken.None));

return Redirect(linkGenerator.PersonQualifications(PersonId!.Value));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt.Models;

namespace TeachingRecordSystem.Core.Tests.DataStore.Postgres.Models;

Expand Down Expand Up @@ -49,13 +48,9 @@ public class MandatoryQualificationProviderTests
public void TryMapFromDqtMqEstablishment_ReturnsExpectedResult(string mqestablishmentValue, bool expectedResult)
{
// Arrange
var mqestablishment = new dfeta_mqestablishment()
{
dfeta_Value = mqestablishmentValue
};

// Act
var result = MandatoryQualificationProvider.TryMapFromDqtMqEstablishment(mqestablishment, out var provider);
var result = MandatoryQualificationProvider.TryMapFromDqtMqEstablishmentValue(mqestablishmentValue, out var provider);

// Assert
Assert.Equal(expectedResult, result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using FormFlow;
using Microsoft.EntityFrameworkCore;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.SupportUi.Pages.Mqs.AddMq;

namespace TeachingRecordSystem.SupportUi.Tests.PageTests.Mqs.AddMq;
Expand Down Expand Up @@ -123,11 +125,12 @@ public async Task Post_WithPersonIdForNonExistentPerson_ReturnsNotFound()
[InlineData(MandatoryQualificationStatus.InProgress)]
[InlineData(MandatoryQualificationStatus.Failed)]
[InlineData(MandatoryQualificationStatus.Passed)]
public async Task Post_Confirm_CompletesJourneyAndRedirectsWithFlashMessage(MandatoryQualificationStatus status)
public async Task Post_Confirm_CompletesJourneyRedirectsWithFlashMessageAndCreatesMq(MandatoryQualificationStatus status)
{
// Arrange
var person = await TestData.CreatePerson(b => b.WithQts(qtsDate: new DateOnly(2021, 10, 5)));
var mqEstablishment = await TestData.ReferenceDataCache.GetMqEstablishmentByValue("959"); // University of Leeds
MandatoryQualificationProvider.TryMapFromDqtMqEstablishment(mqEstablishment, out var provider);
var specialism = MandatoryQualificationSpecialism.Hearing;
var startDate = new DateOnly(2021, 3, 1);
DateOnly? endDate = status == MandatoryQualificationStatus.Passed ? new DateOnly(2021, 11, 5) : null;
Expand Down Expand Up @@ -159,10 +162,21 @@ public async Task Post_Confirm_CompletesJourneyAndRedirectsWithFlashMessage(Mand

journeyInstance = await ReloadJourneyInstance(journeyInstance);
Assert.True(journeyInstance.Completed);

await WithDbContext(async dbContext =>
{
var qualification = await dbContext.MandatoryQualifications.SingleOrDefaultAsync(q => q.PersonId == person.PersonId);
Assert.NotNull(qualification);
Assert.Equal(provider!.MandatoryQualificationProviderId, qualification.ProviderId);
Assert.Equal(specialism, qualification.Specialism);
Assert.Equal(status, qualification.Status);
Assert.Equal(startDate, qualification.StartDate);
Assert.Equal(endDate, qualification.EndDate);
});
}

[Fact]
public async Task Post_Cancel_DeletesJourneyAndRedirects()
public async Task Post_Cancel_DeletesJourneyRedirectsAndDoesNotCreateMq()
{
// Arrange
var person = await TestData.CreatePerson(b => b.WithQts(qtsDate: new DateOnly(2021, 10, 5)));
Expand Down Expand Up @@ -195,6 +209,12 @@ public async Task Post_Cancel_DeletesJourneyAndRedirects()

journeyInstance = await ReloadJourneyInstance(journeyInstance);
Assert.Null(journeyInstance);

await WithDbContext(async dbContext =>
{
var qualifications = await dbContext.MandatoryQualifications.Where(q => q.PersonId == person.PersonId).ToArrayAsync();
Assert.Empty(qualifications);
});
}

private async Task<JourneyInstance<AddMqState>> CreateJourneyInstance(Guid personId, AddMqState? state = null) =>
Expand Down
Loading

0 comments on commit 78fe30e

Please sign in to comment.