fix(uplifts): attribute audit to requested work order

Use the operation work order when staging uplift audit logs so a dispatch linked through DispatchWorkOrders cannot write the event to its primary work order. Add coverage for the cross-work-order fallback case.
This commit is contained in:
Alexandre Brandizzi 2026-09-22 21:29:35 -03:00
parent adb192cad7
commit 5c5c01b2e8
2 changed files with 32 additions and 1 deletions

View file

@ -437,7 +437,7 @@ namespace SeaHaven.Services.Implementation
{
await _dispatchData.StageAuditLogAsync(new WorkOrderAuditLog
{
WorkOrderId = dispatch.WorkOrderId ?? workOrderId,
WorkOrderId = workOrderId,
UserId = userId,
FieldName = $"Dispatch {dispatch.DispatchNumber} Uplift",
OldValue = isStatusTransition ? oldValue.ToString() : $"${Convert.ToDecimal(oldValue):F2}",

View file

@ -143,6 +143,37 @@ public sealed class WorkOrderUpliftServiceTests
Assert.Equal(1, await context.DispatchUpliftRequests.CountAsync());
}
[Fact]
public async Task CreateAsync_WithoutPrimaryDispatch_AuditsRequestedWorkOrderWhenDispatchBelongsToAnother()
{
await using var context = CreateContext();
var (workOrder, dispatch) = await SeedWorkOrderAsync(context, primaryDispatchId: null);
context.workOrders.Add(new WorkOrder
{
Id = 2,
InternalWONumber = "10000000002",
AccountId = 1,
WorkOrderType = WorkOrderType.PM,
});
dispatch.WorkOrderId = 2;
context.DispatchWorkOrders.Add(new DispatchWorkOrder
{
DispatchId = dispatch.Id,
WorkOrderId = workOrder.Id,
});
await context.SaveChangesAsync();
var service = NewService(context);
await service.CreateAsync(
workOrder.Id,
new CreateWorkOrderUpliftRequestDto { Amount = 400m, Notes = "Associated dispatch" },
Dispatcher(),
CancellationToken.None);
var audit = Assert.Single(context.WorkOrderAuditLogs);
Assert.Equal(workOrder.Id, audit.WorkOrderId);
}
[Fact]
public async Task CreateAsync_PmAmountAboveCap_CreatesPendingRequest()
{