using AccountsBLL.Warehouse; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; namespace AccountsSL.EndPoints.WarehouseFact { // Full-batch warehouse posting: posts ALL registered fact tables for a given date range across // all active OUs. Intended for nightly jobs or manual admin backfills. // For per-fact delta drain, use POST /Warehouse/PostWarehouseDelta?FactId=N instead. public class PostAll : BaseEndpoint> { private readonly IWarehouseFactPostingBLL _postingBll; public PostAll(IWarehouseFactPostingBLL postingBll) => _postingBll = postingBll; public override void Configure() { Post("/Warehouse/PostAll"); AllowAnonymous(); } public record PostAllParameters( [property: FromHeader] string Login, [property: QueryParam] DateTime DateFrom, [property: QueryParam] DateTime DateTo ); protected override string? GetCacheKey(PostAllParameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync( PostAllParameters req, LoginDTO login, CancellationToken ct) { if (req.DateFrom > req.DateTo) return await GB5Shared.ResponseStandard.Response.CreateExceptionError( new ArgumentException("DateFrom must not be after DateTo."), GB5Shared.GB5Constant.Constant.CacheKeyLevel.NOT_REQUIRED, login, "DateFrom must not be after DateTo.", 400); try { var results = await _postingBll.PostAllFactsAsync(req.DateFrom, req.DateTo, login, ct); var summary = results.Select(kv => new { FactId = kv.Key, SucceededOuCount = kv.Value.SucceededOuIds.Count, FailedOuCount = kv.Value.FailedOuIds.Count, kv.Value.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); } } } }