using AccountsBLL.Warehouse; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; namespace AccountsSL.EndPoints.WarehouseFact { // Drains TWAREHOUSECHANGEQUEUE for a specific fact and posts the delta. // Called manually (admin trigger) or by a Quartz job on a short schedule. // The BLL uses UPDATE+OUTPUT for atomic claim — concurrent calls are safe. public class PostWarehouseDelta : BaseEndpoint> { private readonly IWarehouseChangeQueueBLL _queueBll; public PostWarehouseDelta(IWarehouseChangeQueueBLL queueBll) => _queueBll = queueBll; public override void Configure() { Post("/Warehouse/PostWarehouseDelta"); AllowAnonymous(); } public record PostWarehouseDeltaParameters( [property: FromHeader] string Login, [property: QueryParam] int FactId ); protected override string? GetCacheKey(PostWarehouseDeltaParameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync( PostWarehouseDeltaParameters req, LoginDTO login, CancellationToken ct) { try { var result = await _queueBll.ProcessDeltaForFactAsync(req.FactId, login, ct); var summary = new { SucceededOuCount = result.SucceededOuIds.Count, FailedOuCount = result.FailedOuIds.Count, result.FailedOuIds }; return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( System.Text.Json.JsonSerializer.Serialize(summary), GB5Shared.GB5Constant.Constant.CacheKeyLevel.NOT_REQUIRED, login); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, GB5Shared.GB5Constant.Constant.CacheKeyLevel.NOT_REQUIRED, login, ex.Message, 500); } } } }