2026-08-11 10:45:37 -03:00
|
|
|
using System.Security.Claims;
|
2026-07-08 10:17:31 -03:00
|
|
|
using Data.SeaHavenIndustries.Enums;
|
2026-07-24 17:35:34 -03:00
|
|
|
using SeaHaven.DataServices.Interfaces;
|
2026-07-08 10:17:31 -03:00
|
|
|
using SeaHaven.Services.DTOs;
|
|
|
|
|
using SeaHaven.Services.Exceptions;
|
|
|
|
|
using SeaHaven.Services.Helpers;
|
|
|
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace SeaHaven.Services.Implementation
|
|
|
|
|
{
|
|
|
|
|
public class WorkOrderBoardCancelService : IWorkOrderBoardCancelService
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
private readonly IWorkOrderBoardMutationDataService _mutationData;
|
2026-07-08 10:17:31 -03:00
|
|
|
private readonly IWorkOrderBoardService _boardService;
|
|
|
|
|
private readonly IWorkOrderAuditService _auditService;
|
2026-08-14 10:36:35 -03:00
|
|
|
private readonly IWorkOrderUpliftService _upliftService;
|
2026-08-18 17:41:14 -03:00
|
|
|
private readonly IUpliftDataService _upliftData;
|
2026-07-08 10:17:31 -03:00
|
|
|
|
|
|
|
|
public WorkOrderBoardCancelService(
|
2026-07-24 17:35:34 -03:00
|
|
|
IWorkOrderBoardMutationDataService mutationData,
|
2026-07-08 10:17:31 -03:00
|
|
|
IWorkOrderBoardService boardService,
|
2026-08-14 10:36:35 -03:00
|
|
|
IWorkOrderAuditService auditService,
|
2026-08-18 17:41:14 -03:00
|
|
|
IWorkOrderUpliftService upliftService,
|
|
|
|
|
IUpliftDataService upliftData)
|
2026-07-08 10:17:31 -03:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
_mutationData = mutationData;
|
2026-07-08 10:17:31 -03:00
|
|
|
_boardService = boardService;
|
|
|
|
|
_auditService = auditService;
|
2026-08-14 10:36:35 -03:00
|
|
|
_upliftService = upliftService;
|
2026-08-18 17:41:14 -03:00
|
|
|
_upliftData = upliftData;
|
2026-07-08 10:17:31 -03:00
|
|
|
}
|
|
|
|
|
|
2026-08-11 10:45:37 -03:00
|
|
|
public async Task<WorkOrderBoardRowDto> CancelAsync(
|
|
|
|
|
int workOrderId,
|
|
|
|
|
ClaimsPrincipal user,
|
|
|
|
|
string? actorId)
|
2026-07-08 10:17:31 -03:00
|
|
|
{
|
2026-08-18 17:41:14 -03:00
|
|
|
// SH-196: run the whole cancel under the same per-work-order gate that uplift
|
|
|
|
|
// create uses. Previously the pending-uplift read happened outside any gate, so
|
|
|
|
|
// an in-flight create could commit after that read and leave a pending uplift on
|
|
|
|
|
// a Canceled work order, breaking "cancelling a WO with a pending uplift cancels
|
|
|
|
|
// that uplift in the same action".
|
|
|
|
|
await _upliftData.ExecuteWorkOrderMutationAsync(
|
|
|
|
|
workOrderId,
|
|
|
|
|
async ct =>
|
|
|
|
|
{
|
|
|
|
|
var workOrder = await _mutationData.GetTrackedWorkOrderAsync(workOrderId, ct);
|
2026-07-08 10:17:31 -03:00
|
|
|
|
2026-08-18 17:41:14 -03:00
|
|
|
if (workOrder == null)
|
|
|
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
2026-07-08 10:17:31 -03:00
|
|
|
|
2026-08-18 17:41:14 -03:00
|
|
|
// Already canceled: nothing to mutate, the row is returned below.
|
|
|
|
|
if (workOrder.LifecycleStatus == LifecycleStatus.Canceled)
|
|
|
|
|
return false;
|
2026-07-08 10:17:31 -03:00
|
|
|
|
2026-08-18 17:41:14 -03:00
|
|
|
if (workOrder.LifecycleStatus == LifecycleStatus.Completed)
|
|
|
|
|
throw new WorkOrderBoardValidationException("CancelNotAllowed", "Work order cannot be canceled in its current status.");
|
2026-07-08 10:17:31 -03:00
|
|
|
|
2026-08-18 17:41:14 -03:00
|
|
|
var oldStatus = workOrder.LifecycleStatus?.ToString() ?? workOrder.Status ?? "";
|
|
|
|
|
workOrder.LifecycleStatus = LifecycleStatus.Canceled;
|
|
|
|
|
workOrder.Status = WorkOrderDerivedFields.GetLifecycleStatusLabel(LifecycleStatus.Canceled);
|
|
|
|
|
if (workOrder.LegacyStatus == null && workOrder.Status != null)
|
|
|
|
|
workOrder.LegacyStatus = workOrder.Status;
|
2026-07-08 10:17:31 -03:00
|
|
|
|
2026-08-18 17:41:14 -03:00
|
|
|
await _upliftService.WithdrawPendingForWorkOrderAsync(workOrderId, actorId, ct);
|
|
|
|
|
await _auditService.StageStatusChangedAsync(workOrderId, oldStatus, LifecycleStatus.Canceled.ToString(), actorId);
|
|
|
|
|
await _mutationData.SaveAsync(ct);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
CancellationToken.None);
|
2026-07-08 10:17:31 -03:00
|
|
|
|
2026-08-11 10:45:37 -03:00
|
|
|
var row = await _boardService.GetBoardRowAsync(workOrderId, user);
|
2026-07-08 10:17:31 -03:00
|
|
|
return row ?? throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|