diff --git a/src/Application/Features/Assessments/Commands/BeginAssessment.cs b/src/Application/Features/Assessments/Commands/BeginAssessment.cs index 8fbb8213..670359da 100644 --- a/src/Application/Features/Assessments/Commands/BeginAssessment.cs +++ b/src/Application/Features/Assessments/Commands/BeginAssessment.cs @@ -1,5 +1,6 @@ using System.Text.Json.Serialization; using Cfo.Cats.Application.Common.Security; +using Cfo.Cats.Application.Common.Validators; using Cfo.Cats.Application.Features.Assessments.Caching; using Cfo.Cats.Application.Features.Assessments.DTOs; using Cfo.Cats.Application.Features.Assessments.DTOs.V1.Pathways.Education; @@ -78,12 +79,30 @@ public async Task> Handle(Command request, CancellationToken cancel public class Validator : AbstractValidator { - public Validator() + private readonly IUnitOfWork _unitOfWork; + + public Validator(IUnitOfWork unitOfWork) { + _unitOfWork = unitOfWork; + RuleFor(c => c.ParticipantId) .MinimumLength(9) - .MaximumLength(9); + .MaximumLength(9) + .Matches(ValidationConstants.AlphaNumeric) + .WithMessage(string.Format(ValidationConstants.AlphaNumericMessage, "Participant Id")); + + RuleFor(c => c.ParticipantId) + .MustAsync(Exist) + .WithMessage("Participant not found") + .MustAsync(HaveEnrolmentLocation) + .WithMessage("Participant must have an enrolment location"); } + + private async Task Exist(string participantId, CancellationToken cancellationToken) + => await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId, cancellationToken); + + private async Task HaveEnrolmentLocation(string participantId, CancellationToken cancellationToken) + => await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId && e.EnrolmentLocation != null, cancellationToken); } } diff --git a/src/Application/Features/Assessments/Commands/SaveAssessment.cs b/src/Application/Features/Assessments/Commands/SaveAssessment.cs index 95fc78ba..b347e28f 100644 --- a/src/Application/Features/Assessments/Commands/SaveAssessment.cs +++ b/src/Application/Features/Assessments/Commands/SaveAssessment.cs @@ -1,4 +1,5 @@ using Cfo.Cats.Application.Common.Security; +using Cfo.Cats.Application.Common.Validators; using Cfo.Cats.Application.Features.Assessments.Caching; using Cfo.Cats.Application.Features.Assessments.DTOs; using Cfo.Cats.Application.SecurityConstants; @@ -75,4 +76,33 @@ public async Task Handle(Command request, CancellationToken cancellation } } + public class Validator : AbstractValidator + { + private readonly IUnitOfWork _unitOfWork; + + public Validator(IUnitOfWork unitOfWork) + { + _unitOfWork = unitOfWork; + + RuleFor(c => c.Assessment.Id) + .MustAsync(Exist) + .WithMessage("Assessment not found"); + + RuleFor(c => c.Assessment.ParticipantId) + .MustAsync(Exist) + .WithMessage("Participant not found") + .MustAsync(HaveEnrolmentLocation) + .WithMessage("Participant must have an enrolment location"); + } + + private async Task Exist(Guid assessmentId, CancellationToken cancellationToken) + => await _unitOfWork.DbContext.ParticipantAssessments.AnyAsync(e => e.Id == assessmentId, cancellationToken); + + private async Task Exist(string participantId, CancellationToken cancellationToken) + => await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId, cancellationToken); + + private async Task HaveEnrolmentLocation(string participantId, CancellationToken cancellationToken) + => await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId && e.EnrolmentLocation != null, cancellationToken); + + } } diff --git a/src/Server.UI/Pages/Participants/Components/CaseSummary.razor.cs b/src/Server.UI/Pages/Participants/Components/CaseSummary.razor.cs index 2214a7a0..11335d14 100644 --- a/src/Server.UI/Pages/Participants/Components/CaseSummary.razor.cs +++ b/src/Server.UI/Pages/Participants/Components/CaseSummary.razor.cs @@ -35,12 +35,17 @@ public async Task BeginAssessment() { ParticipantId = ParticipantSummaryDto.Id }; + var result = await GetNewMediator().Send(command); if (result.Succeeded) { Navigation.NavigateTo($"/pages/participants/{ParticipantSummaryDto.Id}/assessment/{result.Data}"); } + else + { + Snackbar.Add(result.ErrorMessage, Severity.Error); + } } public void ContinueAssessment()