shoc-backend/SeaHaven.DataServices/Implementation/WorkOrderIngestDataService.cs
Alexandre Brandizzi 306ab159cc Cancel pending uplifts when the legacy ingest cancels a work order
The ingest writes only the status text, so the shared helper gains a
status-text form of the same rule and the ingest stages the same
sync-attributed cancellation in its batch save.
2026-09-25 19:01:37 -03:00

68 lines
2.7 KiB
C#

using Data.SeaHavenIndustries;
using Data.SeaHavenIndustries.Enums;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Helpers;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class WorkOrderIngestDataService : IWorkOrderIngestDataService
{
private readonly ApplicationDbContext _context;
public WorkOrderIngestDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<int> GetMaxWorkOrderIdAsync(CancellationToken cancellationToken)
=> await _context.workOrders.MaxAsync(w => (int?)w.Id, cancellationToken) ?? 0;
public Task<WorkOrder?> GetTrackedByExternalIdAsync(string externalWorkOrderId, CancellationToken cancellationToken)
=> _context.workOrders.FirstOrDefaultAsync(w => w.ExternalWorkOrderId == externalWorkOrderId, cancellationToken);
public void TrackWorkOrder(WorkOrder workOrder)
=> _context.workOrders.Add(workOrder);
public Task<Locations?> FindLocationAsync(string name, CancellationToken cancellationToken)
=> _context.Locations.FirstOrDefaultAsync(l => l.Name == name || l.Title == name, cancellationToken);
public async Task AddAndSaveLocationAsync(Locations location, CancellationToken cancellationToken)
{
_context.Locations.Add(location);
await _context.SaveChangesAsync(cancellationToken);
}
public Task<int> StageCancelPendingUpliftsAsync(int workOrderId, CancellationToken cancellationToken)
=> PendingUpliftCancellation.StageAsync(
_context,
workOrderId,
actorId: null,
actorType: AuditActorType.Sync.ToString(),
DateTime.UtcNow,
cancellationToken);
public async Task ExecuteTransactionalAsync(Func<CancellationToken, Task> work, CancellationToken cancellationToken)
{
await using var transaction = _context.Database.IsRelational()
? await _context.Database.BeginTransactionAsync(cancellationToken)
: null;
try
{
await work(cancellationToken);
if (transaction is not null)
await transaction.CommitAsync(cancellationToken);
}
catch
{
if (transaction is not null)
await transaction.RollbackAsync(cancellationToken);
throw;
}
}
public Task SaveAsync(CancellationToken cancellationToken)
=> _context.SaveChangesAsync(cancellationToken);
}
}