diff --git a/PeakLims/src/PeakLims/Domain/Accessions/Dtos/AccessionPageViewDto.cs b/PeakLims/src/PeakLims/Domain/Accessions/Dtos/AccessionPageViewDto.cs index bb318b6..4e850d9 100644 --- a/PeakLims/src/PeakLims/Domain/Accessions/Dtos/AccessionPageViewDto.cs +++ b/PeakLims/src/PeakLims/Domain/Accessions/Dtos/AccessionPageViewDto.cs @@ -74,6 +74,9 @@ public sealed record Panel public string PanelName { get; set; } public string Type { get; set; } public int? Version { get; set; } + + public string CancellationReason { get; set; } + public string CancellationComments { get; set; } public string Status { get; set; } public Guid? PanelOrderId { get; set; } } diff --git a/PeakLims/src/PeakLims/Domain/Accessions/Mappings/AccessionMapper.cs b/PeakLims/src/PeakLims/Domain/Accessions/Mappings/AccessionMapper.cs index e80bf45..de8429c 100644 --- a/PeakLims/src/PeakLims/Domain/Accessions/Mappings/AccessionMapper.cs +++ b/PeakLims/src/PeakLims/Domain/Accessions/Mappings/AccessionMapper.cs @@ -65,6 +65,8 @@ public static AccessionPageViewDto ToEditableAccessionDto(this Accession accessi Type = x.PanelOrder?.Panel?.Type, Version = x.PanelOrder?.Panel?.Version, Status = x?.PanelOrder?.Status()?.Value, + CancellationReason = x?.PanelOrder?.CancellationReason, + CancellationComments = x?.PanelOrder?.CancellationComments }, Priority = x?.Priority?.Value, TestCode = x.Test.TestCode, diff --git a/PeakLims/src/PeakLims/Domain/PanelOrders/Features/CancelPanelOrder.cs b/PeakLims/src/PeakLims/Domain/PanelOrders/Features/CancelPanelOrder.cs index 454bda5..39fb6f3 100644 --- a/PeakLims/src/PeakLims/Domain/PanelOrders/Features/CancelPanelOrder.cs +++ b/PeakLims/src/PeakLims/Domain/PanelOrders/Features/CancelPanelOrder.cs @@ -1,9 +1,8 @@ namespace PeakLims.Domain.PanelOrders.Features; -using PeakLims.Domain.PanelOrders.Services; -using PeakLims.Domain; -using HeimGuard; +using Databases; using MediatR; +using Microsoft.EntityFrameworkCore; using PeakLims.Services; using TestOrderCancellationReasons; @@ -11,18 +10,37 @@ public static class CancelPanelOrder { public sealed record Command(Guid PanelOrderId, string Reason, string Comments) : IRequest; - public sealed class Handler( - IPanelOrderRepository panelOrderRepository, - IUnitOfWork unitOfWork) + public sealed class Handler(PeakLimsDbContext dbContext) : IRequestHandler { public async Task Handle(Command request, CancellationToken cancellationToken) { - var panelOrder = await panelOrderRepository.GetById(request.PanelOrderId, true, cancellationToken); + var accessionId = dbContext.PanelOrders + .Include(x => x.TestOrders) + .ThenInclude(x => x.Accession) + .Where(x => x.Id == request.PanelOrderId) + .AsNoTracking() + .Select(x => x.TestOrders) + .FirstOrDefault() + ?.FirstOrDefault() + ?.Accession + ?.MustBeFoundOrThrow() + .Id; + + var accession = await dbContext.GetAccessionAggregate() + .GetById((Guid)accessionId!, cancellationToken: cancellationToken); + accession.MustBeFoundOrThrow(); + + var panelOrder = accession + .TestOrders + .FirstOrDefault(x => x.PanelOrder.Id == request.PanelOrderId) + .MustBeFoundOrThrow() + .PanelOrder; + panelOrder.Cancel(TestOrderCancellationReason.Of(request.Reason), request.Comments); - panelOrderRepository.Update(panelOrder); + dbContext.PanelOrders.Update(panelOrder); - await unitOfWork.CommitChanges(cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); } } }