Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a support task when automatic matching fails #1297

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -0,0 +1,21 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;

namespace TeachingRecordSystem.Core.DataStore.Postgres.Mappings;

public class SupportTaskMapping : IEntityTypeConfiguration<SupportTask>
{
public void Configure(EntityTypeBuilder<SupportTask> builder)
{
builder.ToTable("support_tasks");
builder.HasKey(p => p.SupportTaskReference);
builder.Property(p => p.SupportTaskReference).HasMaxLength(16);
builder.HasOne<OneLoginUser>().WithMany().HasForeignKey(o => o.OneLoginUserSubject).HasConstraintName("fk_support_tasks_one_login_user");
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
Loading