Skip to content

Commit

Permalink
Merge branch 'main' into hotfix/primary-colour-layout-service
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulCooperWorkJustice authored Aug 14, 2024
2 parents ed31113 + 0832e23 commit d5e1abd
Show file tree
Hide file tree
Showing 16 changed files with 1,664 additions and 1,596 deletions.
6 changes: 3 additions & 3 deletions ModifyDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ function Add-Migration {
}

function Remove-LastMigration {
$command = "dotnet ef migrations remove --project src\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj --startup-project src\Server.UI\Server.UI.csproj --context Cfo.Cats.Infrastructure.Persistence.ApplicationDbContext --configuration Debug"
$command = "dotnet ef migrations remove --project src\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj --startup-project src\Server.UI\Server.UI.csproj --no-build --context Cfo.Cats.Infrastructure.Persistence.ApplicationDbContext --configuration Debug"
Write-Host "Executing: $command"
Invoke-Expression $command
}

function Remove-Database {
$command = "dotnet ef database drop --project src\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj --startup-project src\Server.UI\Server.UI.csproj --context Cfo.Cats.Infrastructure.Persistence.ApplicationDbContext --configuration Debug --force"
$command = "dotnet ef database drop --project src\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj --startup-project src\Server.UI\Server.UI.csproj --no-build --context Cfo.Cats.Infrastructure.Persistence.ApplicationDbContext --configuration Debug --force"
Write-Host "Executing: $command"
Invoke-Expression $command
}

function Update-Database {
$command = "dotnet ef database update --project src\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj --startup-project src\Server.UI\Server.UI.csproj --context Cfo.Cats.Infrastructure.Persistence.ApplicationDbContext --configuration Debug"
$command = "dotnet ef database update --project src\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj --startup-project src\Server.UI\Server.UI.csproj --no-build --context Cfo.Cats.Infrastructure.Persistence.ApplicationDbContext --configuration Debug"
Write-Host "Executing: $command"
Invoke-Expression $command
}
Expand Down
2 changes: 2 additions & 0 deletions cats.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution
LICENSE = LICENSE
NuGet.config = NuGet.config
README.md = README.md
LocalPublish.ps1 = LocalPublish.ps1
ModifyDatabase.ps1 = ModifyDatabase.ps1
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{62ABFE2F-D4F3-4E44-AA07-20D679B04CF3}"
Expand Down
1 change: 1 addition & 0 deletions db/seed/001_development_seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ BEGIN TRY

SET IDENTITY_INSERT [Configuration].[Location] ON;

INSERT INTO Configuration.Location (Id, Name, ContractId, LifetimeStart, LifetimeEnd, ParentLocationId, GenderProvisionId, LocationTypeId, Created, CreatedBy, LastModified, LastModifiedBy) VALUES (0, N'Unmapped Location', null, @Created, N'2029-03-31 23:59:59.0000000', null, 2, 6, @Created, @CreatedUserId, null, null);
INSERT INTO Configuration.Location (Id, Name, ContractId, LifetimeStart, LifetimeEnd, ParentLocationId, GenderProvisionId, LocationTypeId, Created, CreatedBy, LastModified, LastModifiedBy) VALUES (1, N'HMP Risley', N'con_24036', @Created, N'2029-03-31 23:59:59.0000000', null, 0, 0, @Created, @CreatedUserId, null, null);
INSERT INTO Configuration.Location (Id, Name, ContractId, LifetimeStart, LifetimeEnd, ParentLocationId, GenderProvisionId, LocationTypeId, Created, CreatedBy, LastModified, LastModifiedBy) VALUES (2, N'HMP Lancaster Farms', N'con_24036', @Created, N'2029-03-31 23:59:59.0000000', null, 0, 0, @Created, @CreatedUserId, null, null);
INSERT INTO Configuration.Location (Id, Name, ContractId, LifetimeStart, LifetimeEnd, ParentLocationId, GenderProvisionId, LocationTypeId, Created, CreatedBy, LastModified, LastModifiedBy) VALUES (3, N'HMP Forest Bank', N'con_24036', @Created, N'2029-03-31 23:59:59.0000000', null, 0, 1, @Created, @CreatedUserId, null, null);
Expand Down
52 changes: 27 additions & 25 deletions src/Application/Common/Interfaces/IApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,42 @@ namespace Cfo.Cats.Application.Common.Interfaces;

public interface IApplicationDbContext
{
public DatabaseFacade Database { get; }
DatabaseFacade Database { get; }

DbSet<AuditTrail> AuditTrails { get; }
DbSet<Tenant> Tenants { get; }

DbSet<Contract> Contracts { get; }
DbSet<Contract> Contracts { get; }

DbSet<Location> Locations { get; }

public DbSet<Document> Documents { get; }

public DbSet<Participant> Participants { get; }

public DbSet<Risk> Risks { get; }

public DbSet<KeyValue> KeyValues { get; }

public DbSet<ParticipantAssessment> ParticipantAssessments { get; }

public DbSet<ParticipantEnrolmentHistory> ParticipantEnrolmentHistories { get; }

public DbSet<Timeline> Timelines { get; }

public DbSet<ApplicationUser> Users { get; }

public DbSet<EnrolmentPqaQueueEntry> EnrolmentPqaQueue { get; }
public DbSet<EnrolmentQa1QueueEntry> EnrolmentQa1Queue { get; }
public DbSet<EnrolmentQa2QueueEntry> EnrolmentQa2Queue { get; }
public DbSet<EnrolmentEscalationQueueEntry> EnrolmentEscalationQueue { get; }

DbSet<Document> Documents { get; }

DbSet<Participant> Participants { get; }

DbSet<Risk> Risks { get; }

DbSet<KeyValue> KeyValues { get; }

DbSet<ParticipantAssessment> ParticipantAssessments { get; }

DbSet<ParticipantEnrolmentHistory> ParticipantEnrolmentHistories { get; }

DbSet<Timeline> Timelines { get; }

DbSet<ApplicationUser> Users { get; }

DbSet<EnrolmentPqaQueueEntry> EnrolmentPqaQueue { get; }
DbSet<EnrolmentQa1QueueEntry> EnrolmentQa1Queue { get; }
DbSet<EnrolmentQa2QueueEntry> EnrolmentQa2Queue { get; }
DbSet<EnrolmentEscalationQueueEntry> EnrolmentEscalationQueue { get; }

DbSet<LocationMapping> LocationMappings { get; }

ChangeTracker ChangeTracker { get; }

DbSet<DataProtectionKey> DataProtectionKeys { get; }

DbSet<PasswordHistory> PasswordHistories { get; }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public class Handler(IUnitOfWork unitOfWork, ICurrentUserService currentUserServ
public async Task<Result<string>> Handle(Command request, CancellationToken cancellationToken)
{
var candidate = request.Candidate;

var location = await unitOfWork.DbContext.LocationMappings
.Include(l => l.Location)
.SingleAsync(l => l.Code == candidate.EstCode, cancellationToken);

Participant participant = Participant.CreateFrom(candidate.Identifier, candidate.FirstName, candidate.LastName, candidate.DateOfBirth,
request.ReferralSource!, request.ReferralComments);
request.ReferralSource!, request.ReferralComments, location.Location?.Id ?? 0);
participant.AssignTo(currentUserService.UserId);

await unitOfWork.DbContext.Participants.AddAsync(participant, cancellationToken);
Expand All @@ -64,10 +69,21 @@ public Validator(IUnitOfWork unitOfWork)
.MinimumLength(9)
.MaximumLength(9)
.WithMessage("Invalid Cats Identifier")
.Matches(ValidationConstants.AlphaNumeric).WithMessage(string.Format(ValidationConstants.AlphaNumericMessage, "Identifier"))
.MustAsync(NotAlreadyExist)
.WithMessage("Participant is already enrolled")
.Matches(ValidationConstants.AlphaNumeric).WithMessage(string.Format(ValidationConstants.AlphaNumericMessage, "Identifier"));

.WithMessage("Participant is already enrolled");

RuleFor(x => x.Candidate.EstCode)
.NotNull()
.MaximumLength(3)
.WithMessage("Invalid establishment code")
.MinimumLength(3)
.WithMessage("Invalid establishment code")
.Matches(ValidationConstants.AlphaNumeric)
.WithMessage("Invalid establishment code")
.MustAsync(MappedLocation)
.WithMessage("Unknown establishment location");

RuleFor(x => x.ReferralSource)
.NotNull()
.NotEmpty()
Expand All @@ -82,6 +98,9 @@ public Validator(IUnitOfWork unitOfWork)


}
private async Task<bool> MappedLocation(string estCode, CancellationToken cancellationToken)
=> await _unitOfWork.DbContext.LocationMappings.AnyAsync(l => l.Code == estCode, cancellationToken);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Cfo.Cats.Application.Features.Participants.Commands;
public static class SetEnrolmentLocation
{
[RequestAuthorize(Policy = SecurityPolicies.Enrol)]
public class Command(string identifier, LocationDto currentLocation, LocationDto enrolmentLocation, string? justificationReason)
public class Command(string identifier, LocationDto currentLocation, LocationDto? enrolmentLocation, string? justificationReason)
: ICacheInvalidatorRequest<Result<string>>
{

Expand All @@ -27,7 +27,7 @@ public class Command(string identifier, LocationDto currentLocation, LocationDto
/// <summary>
/// The location to assign the enrolment to
/// </summary>
public LocationDto EnrolmentLocation { get; set; } = enrolmentLocation;
public LocationDto? EnrolmentLocation { get; set; } = enrolmentLocation;

/// <summary>
/// A justification for enrolling a participant in a location
Expand All @@ -37,7 +37,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; } = enrolmentLocation.Id != currentLocation.Id;
public bool EnrolFromOtherLocation { get; set; }

public string[] CacheKeys => [ParticipantCacheKey.GetCacheKey($"Id:{this.Identifier}")];
public CancellationTokenSource? SharedExpiryTokenSource
Expand All @@ -60,7 +60,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.JustificationReason);
participant.SetEnrolmentLocation(request.EnrolmentLocation?.Id ?? request.CurrentLocation.Id, request.JustificationReason);
return participant.Id;
}
}
Expand All @@ -81,7 +81,6 @@ public Validator()
.WithMessage("Enrolment location must be different when Enrol from another location is selected");
});


When(x => x.CurrentLocation != x.EnrolmentLocation, () => {
RuleFor(x => x.JustificationReason)
.NotNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ParticipantSummaryDto
/// </summary>
public required string Location { get; set; }

public string? EnrolmentLocation { get; set; }

/// <summary>
/// The participant's date of birth
/// </summary>
Expand Down Expand Up @@ -48,6 +50,9 @@ public Mapping()
CreateMap<Participant, ParticipantSummaryDto>(MemberList.None)
.ForMember(target => target.Id, options => options.MapFrom(source => source.Id))
.ForMember(target => target.Location, options => options.MapFrom(source => source.CurrentLocation.Name))
#pragma warning disable CS8602 // Dereference of a possibly null reference.
.ForMember(target => target.EnrolmentLocation, options => options.MapFrom(source => source.EnrolmentLocation.Name))
#pragma warning restore CS8602 // Dereference of a possibly null reference.
.ForMember(target => target.OwnerName, options => options.MapFrom(source => source.Owner!.DisplayName))
.ForMember(target => target.ParticipantName, options => options.MapFrom(source => source.FirstName + ' ' + source.LastName));

Expand Down
1 change: 1 addition & 0 deletions src/Domain/Common/Enums/LocationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class LocationType : SmartEnum<LocationType>
public static readonly LocationType Community = new(nameof(Community), 4);
public static readonly LocationType Hub = new(nameof(Hub), 5);
public static readonly LocationType Satellite = new(nameof(Satellite), 6);
public static readonly LocationType Unknown = new(nameof(Unknown), 6, false);

private LocationType(string name, int value, bool isCustody = false) : base(name, value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Entities/Administration/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Location : BaseAuditableEntity<int>, ILifetime
{

private string _name;
private string _contractId;
private string? _contractId;
private int _genderProvisionId;
private int _locationTypeId;
private Lifetime _lifetime;
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Entities/Participants/Participant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private Participant()
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

public static Participant CreateFrom(string id, string firstName, string lastName, DateTime dateOfBirth, string referralSource, string? referralComments)
public static Participant CreateFrom(string id, string firstName, string lastName, DateTime dateOfBirth, string referralSource, string? referralComments, int locationId)
{
Participant p = new Participant
{
Expand All @@ -33,7 +33,7 @@ public static Participant CreateFrom(string id, string firstName, string lastNam
LastName = lastName,
ReferralSource = referralSource,
ReferralComments = referralComments,
_currentLocationId = 1
_currentLocationId = locationId
};

p.AddDomainEvent(new ParticipantCreatedDomainEvent(p));
Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/Persistence/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)

public DbSet<DataProtectionKey> DataProtectionKeys => Set<DataProtectionKey>();

public DbSet<LocationMapping> LocationMappings => Set<LocationMapping>();

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
Expand Down
Loading

0 comments on commit d5e1abd

Please sign in to comment.