mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 08:23:12 +00:00
Reuse field-lock DB checks during staged audits, defer dispatch creation to the final save, and return 404 for missing work orders.
117 lines
4.9 KiB
C#
117 lines
4.9 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Data.SeaHavenIndustries.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
namespace SeaHaven.Services.Implementation
|
|
{
|
|
public class WorkOrderAuditService : IWorkOrderAuditService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly IWorkOrderFieldLockService _fieldLocks;
|
|
|
|
public WorkOrderAuditService(ApplicationDbContext context, IWorkOrderFieldLockService fieldLocks)
|
|
{
|
|
_context = context;
|
|
_fieldLocks = fieldLocks;
|
|
}
|
|
|
|
public Task LogStatusChangedAsync(int workOrderId, string? oldStatus, string? newStatus, string? actorId)
|
|
=> LogAsync(workOrderId, AuditActionType.StatusChanged, AuditEventType.Manual,
|
|
AuditActorType.Dispatcher, "LifecycleStatus", oldStatus, newStatus, actorId);
|
|
|
|
public Task LogAssignmentChangedAsync(int workOrderId, string? oldValue, string? newValue, string? actorId)
|
|
=> LogAsync(workOrderId, AuditActionType.AssignmentChanged, AuditEventType.Manual,
|
|
AuditActorType.Dispatcher, "AssignTo", oldValue, newValue, actorId);
|
|
|
|
public Task LogSyncRejectedAsync(int workOrderId, string fieldName, string? oldValue, string? newValue)
|
|
=> LogAsync(workOrderId, AuditActionType.SyncRejected, AuditEventType.Sync,
|
|
AuditActorType.Sync, fieldName, oldValue, newValue);
|
|
|
|
public Task StageFieldChangedAsync(int workOrderId, string fieldName, string? oldValue, string? newValue, string? actorId, int? dispatchId = null)
|
|
=> StageAuditAsync(workOrderId, AuditActionType.FieldChanged, fieldName, oldValue, newValue, actorId, dispatchId);
|
|
|
|
public Task StageStatusChangedAsync(int workOrderId, string? oldStatus, string? newStatus, string? actorId)
|
|
=> StageAuditAsync(workOrderId, AuditActionType.StatusChanged, "LifecycleStatus", oldStatus, newStatus, actorId);
|
|
|
|
public Task StageAssignmentChangedAsync(int workOrderId, string? oldValue, string? newValue, string? actorId)
|
|
=> StageAuditAsync(workOrderId, AuditActionType.AssignmentChanged, "AssignTo", oldValue, newValue, actorId);
|
|
|
|
public Task StageCreatedAsync(int workOrderId, string? actorId, string? woNumber)
|
|
=> StageAuditAsync(workOrderId, AuditActionType.Create, "WorkOrder", null, woNumber, actorId);
|
|
|
|
public async Task LogAsync(
|
|
int workOrderId,
|
|
AuditActionType action,
|
|
AuditEventType eventType,
|
|
AuditActorType actorType,
|
|
string fieldName,
|
|
string? oldValue,
|
|
string? newValue,
|
|
string? actorId = null,
|
|
int? dispatchId = null,
|
|
string? correlationId = null)
|
|
{
|
|
AddAuditEntry(workOrderId, action, eventType, actorType, fieldName, oldValue, newValue, actorId, dispatchId, correlationId);
|
|
|
|
if (eventType == AuditEventType.Manual &&
|
|
(action == AuditActionType.FieldChanged ||
|
|
action == AuditActionType.StatusChanged ||
|
|
action == AuditActionType.AssignmentChanged))
|
|
{
|
|
await _fieldLocks.LockFieldAsync(workOrderId, fieldName, actorId);
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
private async Task StageAuditAsync(
|
|
int workOrderId,
|
|
AuditActionType action,
|
|
string fieldName,
|
|
string? oldValue,
|
|
string? newValue,
|
|
string? actorId,
|
|
int? dispatchId = null)
|
|
{
|
|
AddAuditEntry(workOrderId, action, AuditEventType.Manual, AuditActorType.Dispatcher,
|
|
fieldName, oldValue, newValue, actorId, dispatchId);
|
|
|
|
if (action == AuditActionType.FieldChanged ||
|
|
action == AuditActionType.StatusChanged ||
|
|
action == AuditActionType.AssignmentChanged)
|
|
{
|
|
await _fieldLocks.LockFieldAsync(workOrderId, fieldName, actorId);
|
|
}
|
|
}
|
|
|
|
private void AddAuditEntry(
|
|
int workOrderId,
|
|
AuditActionType action,
|
|
AuditEventType eventType,
|
|
AuditActorType actorType,
|
|
string fieldName,
|
|
string? oldValue,
|
|
string? newValue,
|
|
string? actorId,
|
|
int? dispatchId = null,
|
|
string? correlationId = null)
|
|
{
|
|
_context.WorkOrderAuditLogs.Add(new WorkOrderAuditLog
|
|
{
|
|
WorkOrderId = workOrderId,
|
|
UserId = actorId,
|
|
FieldName = fieldName,
|
|
OldValue = oldValue ?? "",
|
|
NewValue = newValue ?? "",
|
|
Action = action.ToString(),
|
|
EventType = eventType.ToString(),
|
|
ActorType = actorType.ToString(),
|
|
DispatchId = dispatchId,
|
|
CorrelationId = correlationId,
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
}
|
|
|
|
}
|
|
}
|