using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using PayRollBLL.SAP; using System; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace PayRollSL.EndPoints.SAP { /// /// GET /SAP/GetITDeclarationJson /// Returns SAP infotype JSON payloads as a dictionary (infotype → JSON string). /// Supports single-declaration or bulk (by EmployeeId / PeriodId) modes. /// Pass Infotype=0 to generate all infotypes 580–586. /// public class GetITDeclarationJson : BaseEndpoint> { private readonly IITDeclarationSapBLL _bll; public GetITDeclarationJson(IITDeclarationSapBLL bll) => _bll = bll; public override void Configure() { Get("/SAP/GetITDeclarationJson"); AllowAnonymous(); } public record GetITDeclarationJsonParameters( [property: FromHeader] string Login, [property: QueryParam] int DeclarationId, [property: QueryParam] int EmployeeId, [property: QueryParam] int PeriodId, [property: QueryParam] int Infotype, // 0 = all 580–586 [property: QueryParam] string? Date, // dd.MM.yyyy; defaults to today [property: QueryParam] string? Remarks ); // No cache — SAP payloads should always reflect the latest approved data protected override string? GetCacheKey(GetITDeclarationJsonParameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync( GetITDeclarationJsonParameters req, LoginDTO login, CancellationToken ct) { string date = string.IsNullOrWhiteSpace(req.Date) ? DateTime.Today.ToString("dd.MM.yyyy") : req.Date; var payloads = await _bll.GenerateSapPayloadsAsync( req.DeclarationId, req.EmployeeId, req.PeriodId, req.Infotype, date, req.Remarks ?? string.Empty, login, ct); // Parse each JSON string to JsonElement so the response contains // proper nested JSON objects, not escaped string values. var result = payloads.ToDictionary( kvp => kvp.Key, kvp => JsonDocument.Parse(kvp.Value).RootElement.Clone()); return await GB5Shared.ResponseStandard.Response .CreateSuccessResponse(result, CacheKeyLevel.NOT_REQUIRED, login); } } }