Skip to content

Commit

Permalink
Create edit an alert end date journey in trs console (#1538)
Browse files Browse the repository at this point in the history
  • Loading branch information
hortha authored Sep 30, 2024
1 parent 620c5ef commit 84f8938
Show file tree
Hide file tree
Showing 21 changed files with 1,547 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public static class JourneyNames
public const string DeleteMq = nameof(DeleteMq);
public const string AddAlert = nameof(AddAlert);
public const string EditAlertStartDate = nameof(EditAlertStartDate);
public const string EditAlertEndDate = nameof(EditAlertEndDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.DataStore.Postgres;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Services.Files;

namespace TeachingRecordSystem.SupportUi.Pages.Alerts.AddAlert;
Expand Down Expand Up @@ -48,7 +47,7 @@ public async Task<IActionResult> OnPost()
{
var now = clock.UtcNow;

var alert = new Alert()
var alert = new Core.DataStore.Postgres.Models.Alert()
{
AlertId = Guid.NewGuid(),
CreatedOn = now,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page "/alerts/{alertId}/{handler?}"
@model TeachingRecordSystem.SupportUi.Pages.Alerts.Alert.IndexModel
@{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace TeachingRecordSystem.SupportUi.Pages.Alerts.Alert;

public class IndexModel : PageModel
{
[FromRoute]
public Guid AlertId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace TeachingRecordSystem.SupportUi.Pages.Alerts.EditAlert.EndDate;

public enum AlertChangeEndDateReasonOption
{
[Display(Name = "Incorrect end date")]
IncorrectEndDate,
[Display(Name = "Change of end date")]
ChangeOfEndDate,
[Display(Name = "Another reason")]
AnotherReason
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@page "/alerts/{alertId}/end-date/check-answers/{handler?}"
@model TeachingRecordSystem.SupportUi.Pages.Alerts.EditAlert.EndDate.CheckAnswersModel
@{
ViewBag.Title = "Check details and confirm change";
}

@section BeforeContent {
<govuk-back-link href="@LinkGenerator.AlertEditEndDateReason(Model.AlertId, Model.JourneyInstance!.InstanceId)">Back</govuk-back-link>
}

<div class="govuk-grid-row">
<div class="govuk-grid-column-full-from-desktop">
<form action="@LinkGenerator.AlertEditEndDateCheckAnswers(Model.AlertId, Model.JourneyInstance!.InstanceId)" method="post">
<span class="govuk-caption-l">Change previous alert - @Model.PersonName</span>
<h1 class="govuk-heading-l" data-testid="title">@ViewBag.Title</h1>

<govuk-summary-list data-testid="change-summary">
<govuk-summary-list-row>
<govuk-summary-list-row-key>New end date</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="new-end-date">@Model.NewEndDate!.Value.ToString("d MMMM yyyy")</govuk-summary-list-row-value>
<govuk-summary-list-row-actions>
<govuk-summary-list-row-action href="@LinkGenerator.AlertEditEndDate(Model.AlertId, Model.JourneyInstance!.InstanceId, fromCheckAnswers: true)">Change</govuk-summary-list-row-action>
</govuk-summary-list-row-actions>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Current end date</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="current-end-date" use-empty-fallback>@Model.CurrentEndDate?.ToString("d MMMM yyyy")</govuk-summary-list-row-value>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Reason for change</govuk-summary-list-row-key>
<govuk-summary-list-row-value><multi-line-text text="@Model.ChangeReason" data-testid="change-reason" /></govuk-summary-list-row-value>
<govuk-summary-list-row-actions>
<govuk-summary-list-row-action href="@LinkGenerator.AlertEditEndDateReason(Model.AlertId, Model.JourneyInstance!.InstanceId, fromCheckAnswers: true)">Change</govuk-summary-list-row-action>
</govuk-summary-list-row-actions>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Evidence</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="evidence">
@if (Model.UploadedEvidenceFileUrl is not null)
{
<a href="@Model.UploadedEvidenceFileUrl" class="govuk-link" rel="noreferrer noopener" target="_blank" data-testid="uploaded-evidence-link">@($"{Model.EvidenceFileName} (opens in new tab)")</a>
}
else
{
<span data-testid="uploaded-evidence-link" use-empty-fallback></span>
}
</govuk-summary-list-row-value>
<govuk-summary-list-row-actions>
<govuk-summary-list-row-action href="@LinkGenerator.AlertEditEndDateReason(Model.AlertId, Model.JourneyInstance!.InstanceId, fromCheckAnswers: true)">Change</govuk-summary-list-row-action>
</govuk-summary-list-row-actions>
</govuk-summary-list-row>
</govuk-summary-list>

<div class="govuk-button-group">
<govuk-button type="submit">Confirm change</govuk-button>
<govuk-button formaction="@LinkGenerator.AlertEditEndDateCheckAnswersCancel(Model.AlertId, Model.JourneyInstance!.InstanceId)" class="govuk-button--secondary" type="submit">Cancel and return to record</govuk-button>
</div>
</form>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.Core.DataStore.Postgres;
using TeachingRecordSystem.Core.Services.Files;

namespace TeachingRecordSystem.SupportUi.Pages.Alerts.EditAlert.EndDate;

[Journey(JourneyNames.EditAlertEndDate), RequireJourneyInstance]
public class CheckAnswersModel(
TrsDbContext dbContext,
TrsLinkGenerator linkGenerator,
IFileService fileService,
IClock clock) : PageModel
{
private static readonly TimeSpan _fileUrlExpiresAfter = TimeSpan.FromMinutes(15);

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

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

[FromQuery]
public bool FromCheckAnswers { get; set; }

public Guid PersonId { get; set; }

public string? PersonName { get; set; }

public DateOnly? NewEndDate { get; set; }

public DateOnly? CurrentEndDate { get; set; }

public string? ChangeReason { get; set; }

public string? EvidenceFileName { get; set; }

public string? EvidenceFileSizeDescription { get; set; }

public string? UploadedEvidenceFileUrl { get; set; }

public async Task<IActionResult> OnPost()
{
var now = clock.UtcNow;

var alert = await dbContext.Alerts
.SingleAsync(a => a.AlertId == AlertId);

var changes = NewEndDate != alert.EndDate ?
AlertUpdatedEventChanges.EndDate :
AlertUpdatedEventChanges.None;

if (changes != AlertUpdatedEventChanges.None)
{
var oldAlertEventModel = EventModels.Alert.FromModel(alert);

alert.EndDate = NewEndDate;
alert.UpdatedOn = now;

var updatedEvent = new AlertUpdatedEvent()
{
EventId = Guid.NewGuid(),
CreatedUtc = now,
RaisedBy = User.GetUserId(),
PersonId = PersonId,
Alert = EventModels.Alert.FromModel(alert),
OldAlert = oldAlertEventModel,
ChangeReason = ChangeReason,
EvidenceFile = JourneyInstance!.State.EvidenceFileId is Guid fileId ?
new EventModels.File()
{
FileId = fileId,
Name = JourneyInstance.State.EvidenceFileName!
} :
null,
Changes = changes
};

dbContext.AddEvent(updatedEvent);

await dbContext.SaveChangesAsync();
}

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

return Redirect(linkGenerator.PersonAlerts(PersonId));
}

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

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

var personInfo = context.HttpContext.GetCurrentPersonFeature();
var alertInfo = context.HttpContext.GetCurrentAlertFeature();

PersonId = personInfo.PersonId;
PersonName = personInfo.Name;
NewEndDate = JourneyInstance!.State.EndDate;
CurrentEndDate = alertInfo.Alert.EndDate;
ChangeReason = JourneyInstance.State.ChangeReason != AlertChangeEndDateReasonOption.AnotherReason ?
JourneyInstance.State.ChangeReason!.GetDisplayName() :
JourneyInstance!.State.ChangeReasonDetail;
EvidenceFileName = JourneyInstance.State.EvidenceFileName;
UploadedEvidenceFileUrl = JourneyInstance!.State.EvidenceFileId is not null ?
await fileService.GetFileUrl(JourneyInstance!.State.EvidenceFileId!.Value, _fileUrlExpiresAfter) :
null;

await next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace TeachingRecordSystem.SupportUi.Pages.Alerts.EditAlert.EndDate;

public class EditAlertEndDateState
{
public bool Initialized { get; set; }

public DateOnly? CurrentEndDate { get; set; }

public DateOnly? EndDate { get; set; }

public AlertChangeEndDateReasonOption? ChangeReason { get; set; }

public string? ChangeReasonDetail { get; set; }

public bool? UploadEvidence { get; set; }

public Guid? EvidenceFileId { get; set; }

public string? EvidenceFileName { get; set; }

public string? EvidenceFileSizeDescription { get; set; }

[JsonIgnore]
[MemberNotNullWhen(true, nameof(EndDate), nameof(ChangeReason), nameof(UploadEvidence), nameof(EvidenceFileId))]
public bool IsComplete => EndDate is not null &&
ChangeReason.HasValue &&
(ChangeReason.Value == AlertChangeEndDateReasonOption.AnotherReason ? !string.IsNullOrWhiteSpace(ChangeReasonDetail) : true) &&
UploadEvidence.HasValue &&
(!UploadEvidence.Value || (UploadEvidence.Value && EvidenceFileId.HasValue));

public void EnsureInitialized(CurrentAlertFeature alertInfo)
{
if (Initialized)
{
return;
}

EndDate = CurrentEndDate = alertInfo.Alert.EndDate;
Initialized = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@page "/alerts/{alertId}/end-date/{handler?}"
@model TeachingRecordSystem.SupportUi.Pages.Alerts.EditAlert.EndDate.IndexModel
@{
ViewBag.Title = "Enter a new end date";
}

@section BeforeContent {
<govuk-back-link href="@(Model.FromCheckAnswers == true ? LinkGenerator.AlertEditEndDateCheckAnswers(Model.AlertId, Model.JourneyInstance!.InstanceId) : LinkGenerator.PersonAlerts(Model.PersonId))">Back</govuk-back-link>
}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<form action="@LinkGenerator.AlertEditEndDate(Model.AlertId, Model.JourneyInstance!.InstanceId, Model.FromCheckAnswers)" method="post">
<span class="govuk-caption-l">Change previous alert - @Model.PersonName</span>

<govuk-date-input asp-for="EndDate">
<govuk-date-input-fieldset>
<govuk-date-input-fieldset-legend is-page-heading="true" class="govuk-fieldset__legend--l" />
</govuk-date-input-fieldset>
</govuk-date-input>

<div class="govuk-button-group">
<govuk-button type="submit">Continue</govuk-button>
<govuk-button formaction="@LinkGenerator.AlertEditEndDateCancel(Model.AlertId, Model.JourneyInstance!.InstanceId)" class="govuk-button--secondary" type="submit">Cancel and return to record</govuk-button>
</div>
</form>
</<div>
</div>
Loading

0 comments on commit 84f8938

Please sign in to comment.