Skip to content

Commit

Permalink
Create a support task when automatic matching fails
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Apr 26, 2024
1 parent 44eb59e commit 1df1a77
Show file tree
Hide file tree
Showing 19 changed files with 2,624 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public string Trn(JourneyInstanceId journeyInstanceId, bool? fromCheckAnswers =
public string CheckAnswers(JourneyInstanceId journeyInstanceId) =>
GetRequiredPathByPage("/CheckAnswers", journeyInstanceId: journeyInstanceId);

public string SupportRequestSubmitted(JourneyInstanceId journeyInstanceId) =>
GetRequiredPathByPage("/SupportRequestSubmitted", journeyInstanceId: journeyInstanceId);

public string Found(JourneyInstanceId journeyInstanceId) =>
GetRequiredPathByPage("/Found", journeyInstanceId: journeyInstanceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Npgsql;
using TeachingRecordSystem.Core.DataStore.Postgres;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Models.SupportTaskData;
using TeachingRecordSystem.FormFlow;

namespace TeachingRecordSystem.AuthorizeAccess.Pages;

[Journey(SignInJourneyState.JourneyName), RequireJourneyInstance]
public class CheckAnswersModel(SignInJourneyHelper helper) : PageModel
public class CheckAnswersModel(SignInJourneyHelper helper, TrsDbContext dbContext, IClock clock) : PageModel
{
public JourneyInstance<SignInJourneyState>? JourneyInstance { get; set; }

Expand All @@ -25,7 +29,58 @@ public void OnGet()
{
}

public IActionResult OnPost() => throw new NotImplementedException();
public async Task<IActionResult> OnPost()
{
var subject = JourneyInstance!.State.OneLoginAuthenticationTicket!.Principal.FindFirstValue("sub")!;
var email = JourneyInstance!.State.OneLoginAuthenticationTicket!.Principal.FindFirstValue("email")!;

var supportTask = new SupportTask()
{
SupportTaskReference = SupportTask.GenerateSupportTaskReference(),
CreatedOn = clock.UtcNow,
UpdatedOn = clock.UtcNow,
SupportTaskType = SupportTaskType.ConnectOneLoginUser,
Status = SupportTaskStatus.Open,
Data = new ConnectOneLoginUserData()
{
Verified = true,
OneLoginUserSubject = subject,
OneLoginUserEmail = email,
VerifiedNames = JourneyInstance.State.VerifiedNames,
VerifiedDatesOfBirth = JourneyInstance.State.VerifiedDatesOfBirth,
StatedNationalInsuranceNumber = JourneyInstance.State.NationalInsuranceNumber,
StatedTrn = JourneyInstance.State.Trn
},
OneLoginUserSubject = subject
};
dbContext.SupportTasks.Add(supportTask);

dbContext.AddEvent(new SupportTaskCreatedEvent()
{
EventId = Guid.NewGuid(),
CreatedUtc = clock.UtcNow,
RaisedBy = SystemUser.SystemUserId,
SupportTask = EventModels.SupportTask.FromModel(supportTask)
});

while (true)
{
try
{
await dbContext.SaveChangesAsync();
break;
}
catch (Exception ex) when (ex.InnerException is PostgresException postgresException && postgresException.SqlState == PostgresErrorCodes.UniqueViolation)
{
supportTask.SupportTaskReference = SupportTask.GenerateSupportTaskReference();
continue;
}
}

await JourneyInstance.UpdateStateAsync(state => state.HasPendingSupportRequest = true);

return Redirect(helper.LinkGenerator.SupportRequestSubmitted(JourneyInstance!.InstanceId));
}

public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
}

<form action="@LinkGenerator.NotFound(Model.JourneyInstance!.InstanceId)" method="post">
<div class="govuk-panel govuk-panel--interruption">
<h1 class="govuk-panel__title">@ViewBag.Title</h1>
<govuk-panel class="govuk-panel--interruption">
<govuk-panel-title>@ViewBag.Title</govuk-panel-title>

<div class="govuk-panel__body">
<govuk-panel-body class="govuk-panel__body">
<div class="trs-two-thirds">
<p class="govuk-body govuk-!-margin-bottom-0">
We’ve been unable to match your answers to a teaching record.
Expand All @@ -30,6 +30,6 @@

<govuk-button class="govuk-!-margin-bottom-0">Check your answers</govuk-button>
</div>
</div>
</div>
</govuk-panel-body>
</govuk-panel>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@page "/request-submitted"
@model TeachingRecordSystem.AuthorizeAccess.Pages.SupportRequestSubmittedModel
@{
ViewBag.Title = "Support request submitted";
}

<govuk-panel>
<govuk-panel-title>@ViewBag.Title</govuk-panel-title>
<govuk-panel-body>
We’ll contact you within<br />
<strong>5 days</strong>
</govuk-panel-body>
</govuk-panel>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeachingRecordSystem.FormFlow;

namespace TeachingRecordSystem.AuthorizeAccess.Pages;

[Journey(SignInJourneyState.JourneyName), RequireJourneyInstance]
public class SupportRequestSubmittedModel(SignInJourneyHelper helper) : PageModel
{
public JourneyInstance<SignInJourneyState>? JourneyInstance { get; set; }

public void OnGet()
{
}

public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
var state = JourneyInstance!.State;

if (state.OneLoginAuthenticationTicket is null || !state.IdentityVerified)
{
// Not authenticated/verified with One Login
context.Result = BadRequest();
}
else if (state.AuthenticationTicket is not null)
{
// Already matched to a Teaching Record
context.Result = Redirect(helper.GetSafeRedirectUri(JourneyInstance));
}
else if (!state.HaveNationalInsuranceNumber.HasValue)
{
// Not answered the NINO question
context.Result = Redirect(helper.LinkGenerator.NationalInsuranceNumber(JourneyInstance.InstanceId));
}
else if (!state.HaveTrn.HasValue)
{
// Not answered the TRN question
context.Result = Redirect(helper.LinkGenerator.Trn(JourneyInstance.InstanceId));
}
else if (!state.HasPendingSupportRequest)
{
// Not submitted a submit request
context.Result = Redirect(helper.LinkGenerator.CheckAnswers(JourneyInstance.InstanceId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class SignInJourneyState(

public bool AttemptedIdentityVerification { get; set; }

public bool HasPendingSupportRequest { get; set; }

[JsonInclude]
public bool IdentityVerified { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;

Expand All @@ -14,5 +15,7 @@ public void Configure(EntityTypeBuilder<SupportTask> builder)
builder.HasOne<Person>().WithMany().HasForeignKey(p => p.PersonId).HasConstraintName("fk_support_tasks_person");
builder.HasIndex(t => t.OneLoginUserSubject);
builder.HasIndex(t => t.PersonId);
builder.Property<JsonDocument>("_data").HasColumnName("data").IsRequired();
builder.Ignore(t => t.Data);
}
}
Loading

0 comments on commit 1df1a77

Please sign in to comment.