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

Added stricter validation to assessment creating/saving #155

Merged
merged 1 commit into from
Aug 20, 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
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
Loading