mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 10:43:13 +00:00
Reconcile migration/docs deletions from phase 2, adopt async audit staging and ApptTime lock contracts, and keep phase 3 create/cancel transactional behavior.
57 lines
2.5 KiB
C#
57 lines
2.5 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Data.SeaHavenIndustries.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Exceptions;
|
|
using SeaHaven.Services.Helpers;
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
namespace SeaHaven.Services.Implementation
|
|
{
|
|
public class WorkOrderBoardCancelService : IWorkOrderBoardCancelService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly IWorkOrderBoardService _boardService;
|
|
private readonly IWorkOrderAuditService _auditService;
|
|
|
|
public WorkOrderBoardCancelService(
|
|
ApplicationDbContext context,
|
|
IWorkOrderBoardService boardService,
|
|
IWorkOrderAuditService auditService)
|
|
{
|
|
_context = context;
|
|
_boardService = boardService;
|
|
_auditService = auditService;
|
|
}
|
|
|
|
public async Task<WorkOrderBoardRowDto> CancelAsync(int workOrderId, string? actorId)
|
|
{
|
|
var workOrder = await _context.workOrders
|
|
.FirstOrDefaultAsync(w => w.Id == workOrderId && w.istemplate != true && (w.IsDeleted != true || w.IsDeleted == null));
|
|
|
|
if (workOrder == null)
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
if (workOrder.LifecycleStatus == LifecycleStatus.Canceled)
|
|
{
|
|
var existing = await _boardService.GetBoardRowAsync(workOrderId);
|
|
return existing ?? throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
}
|
|
|
|
if (workOrder.LifecycleStatus == LifecycleStatus.Completed)
|
|
throw new WorkOrderBoardValidationException("CancelNotAllowed", "Work order cannot be canceled in its current status.");
|
|
|
|
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;
|
|
|
|
await _auditService.StageStatusChangedAsync(workOrderId, oldStatus, LifecycleStatus.Canceled.ToString(), actorId);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var row = await _boardService.GetBoardRowAsync(workOrderId);
|
|
return row ?? throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
}
|
|
}
|
|
}
|