using BizProcessBLL.Interfaces; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using static GB5Shared.GB5Constant.Constant; namespace BizProcessSL.Endpoints.Reports { // Phase 9a -- "Process Control Copy" PDF. Plain BaseEndpoint + HttpContext.Response // .SendStreamAsync + return null! pattern for streaming a generated file back (mirrors // WorkInstruction's PrintBatch.cs / GenerateScanCode.cs) -- NOT a BaseReportEndpoint, since // that class's PDF export path is a grid renderer bound to MREPORTVIEW metadata and can't // produce this bespoke letterheaded document layout. public class GetProcessControlCopy : BaseEndpoint> { private readonly IBizProcessBLL _bll; public GetProcessControlCopy(IBizProcessBLL bll) => _bll = bll; public override void Configure() { Get("/BizProcess/Reports/GetProcessControlCopy"); AllowAnonymous(); } public record Parameters( [property: FromHeader] string Login, [property: QueryParam] int BizProcessVariantId ); protected override string? GetCacheKey(Parameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync(Parameters req, LoginDTO login, CancellationToken ct) { try { var pdfBytes = await _bll.GetProcessControlCopyPdf(req.BizProcessVariantId, login, ct).ConfigureAwait(false); using var ms = new MemoryStream(pdfBytes); await HttpContext.Response.SendStreamAsync(ms, $"ProcessControlCopy_{req.BizProcessVariantId}.pdf", ms.Length, "application/pdf", cancellation: ct); return null!; } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, login, ex.Message, 500); } } } }