Skip to content

Commit

Permalink
Merge pull request #155 from ministryofjustice/CFODEV-618
Browse files Browse the repository at this point in the history
Added stricter validation to assessment creating/saving
  • Loading branch information
carlsixsmith-moj authored Aug 20, 2024
2 parents bd4d440 + b18fa34 commit faf3863
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Application/Features/Assessments/Commands/BeginAssessment.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -78,12 +79,30 @@ public async Task<Result<Guid>> Handle(Command request, CancellationToken cancel

public class Validator : AbstractValidator<Command>
{
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<bool> Exist(string participantId, CancellationToken cancellationToken)
=> await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId, cancellationToken);

private async Task<bool> HaveEnrolmentLocation(string participantId, CancellationToken cancellationToken)
=> await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId && e.EnrolmentLocation != null, cancellationToken);
}

}
30 changes: 30 additions & 0 deletions src/Application/Features/Assessments/Commands/SaveAssessment.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -75,4 +76,33 @@ public async Task<Result> Handle(Command request, CancellationToken cancellation
}
}

public class Validator : AbstractValidator<Command>
{
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<bool> Exist(Guid assessmentId, CancellationToken cancellationToken)
=> await _unitOfWork.DbContext.ParticipantAssessments.AnyAsync(e => e.Id == assessmentId, cancellationToken);

private async Task<bool> Exist(string participantId, CancellationToken cancellationToken)
=> await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId, cancellationToken);

private async Task<bool> HaveEnrolmentLocation(string participantId, CancellationToken cancellationToken)
=> await _unitOfWork.DbContext.Participants.AnyAsync(e => e.Id == participantId && e.EnrolmentLocation != null, cancellationToken);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit faf3863

Please sign in to comment.