Skip to content

Commit

Permalink
Merge pull request #149 from ministryofjustice/hotfix/enrol-validation
Browse files Browse the repository at this point in the history
Fix enrolment validation for locations
  • Loading branch information
PaulCooperWorkJustice authored Aug 19, 2024
2 parents 74dc1e8 + a30691e commit dc9be25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class Command(string identifier, LocationDto currentLocation, LocationDto
/// <summary>
/// The location to assign the enrolment to
/// </summary>
public LocationDto? EnrolmentLocation { get; set; } = enrolmentLocation;
[Description("Alternative Location")]
public LocationDto? AlternativeLocation { get; set; } = enrolmentLocation;

/// <summary>
/// A justification for enrolling a participant in a location
Expand All @@ -37,7 +38,7 @@ public class Command(string identifier, LocationDto currentLocation, LocationDto
public string? JustificationReason { get; set; } = justificationReason;

[Description("Enrol at an alternative location enrolment")]
public bool EnrolFromOtherLocation { get; set; }
public bool EnrolFromAlternativeLocation { get; set; }

public string[] CacheKeys => [ParticipantCacheKey.GetCacheKey($"Id:{this.Identifier}")];
public CancellationTokenSource? SharedExpiryTokenSource
Expand All @@ -60,7 +61,7 @@ public async Task<Result<string>> Handle(Command request, CancellationToken canc
throw new ConflictException($"Participant {request.Identifier} is already enrolled");
}

participant.SetEnrolmentLocation(request.EnrolmentLocation?.Id ?? request.CurrentLocation.Id, request.JustificationReason);
participant.SetEnrolmentLocation(request.AlternativeLocation?.Id ?? request.CurrentLocation.Id, request.JustificationReason);
return participant.Id;
}
}
Expand All @@ -70,21 +71,20 @@ public class Validator : AbstractValidator<Command>
public Validator()
{
RuleFor(x => x.CurrentLocation)
.NotNull();
.NotNull()
.WithMessage("Current location is unknown or missing");

RuleFor(x => x.EnrolmentLocation)
.NotNull();
When(x => x.EnrolFromAlternativeLocation, () => {
RuleFor(x => x.AlternativeLocation)
.NotNull()
.WithMessage("You must provide an alternative location");

RuleFor(x => x.AlternativeLocation)
.Must((x, alternativeLocation) => x.CurrentLocation.Id != alternativeLocation!.Id)
.When(x => x.AlternativeLocation is not null)
.WithMessage("You must provide a different alternative location to the current location");

When(x => x.EnrolFromOtherLocation, () => {
RuleFor(x => x.EnrolmentLocation)
.Must((model, enrolmentLocation) => model.CurrentLocation != enrolmentLocation)
.WithMessage("Enrolment location must be different when Enrol from another location is selected");
});

When(x => x.CurrentLocation != x.EnrolmentLocation, () => {
RuleFor(x => x.JustificationReason)
.NotNull()
.WithMessage("Justification reason is mandatory when enrolling in a different location")
.NotEmpty()
.WithMessage("Justification reason is mandatory when enrolling in a different location");
});
Expand Down
15 changes: 7 additions & 8 deletions src/Server.UI/Pages/Enrolments/Components/Location.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@
You must select a different enrolment location
</MudAlert>
}
<MudCheckBox T="bool" @bind-Value="Model.EnrolFromOtherLocation"
<MudCheckBox T="bool" @bind-Value="Model.EnrolFromAlternativeLocation"
Disabled="_mustEnrolInOtherLocation"
Label="@L[Model.GetMemberDescription(x => x.EnrolFromOtherLocation)]">
Label="@L[Model.GetMemberDescription(x => x.EnrolFromAlternativeLocation)]">
</MudCheckBox>
</MudItem>
@if (Model.EnrolFromOtherLocation)
@if (Model.EnrolFromAlternativeLocation)
{
<MudItem xs="12">
<MudSelect T="LocationDto"
Label="@L["Alternative Location"]"
Required="true"
@bind-Value="@Model.EnrolmentLocation"
For="@(()=>Model.EnrolmentLocation!)">
Label="@Model.GetMemberDescription(x => x.AlternativeLocation)"
@bind-Value="@Model.AlternativeLocation"
For="@(()=>Model.AlternativeLocation!)">
@foreach (var item in Locations!)
{
<MudSelectItem T="LocationDto" Value="@item">
Expand Down Expand Up @@ -78,7 +77,7 @@
{
if (Model is not null && Locations is not null && Locations.Any(l => l.Id == Model!.CurrentLocation.Id) == false)
{
Model!.EnrolFromOtherLocation = true;
Model!.EnrolFromAlternativeLocation = true;
_mustEnrolInOtherLocation = true;
}
}
Expand Down

0 comments on commit dc9be25

Please sign in to comment.