using FastEndpoints; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using MMBLL.Register; using static GB5Shared.GB5Constant.Constant; namespace MMSL.EndPoints.Register { // Route matches the legacy GB4 Register.svc/DocumentTraceability endpoint exactly — // MWEBSERVICE -1399991650 is repointed at this route (see the MM migration script). // Not paged — a partial trace isn't meaningful, same reasoning as BOM Tree. public class GetDocumentTraceability : BaseEndpoint> { private readonly IRegisterBLL _RegisterBLL; public GetDocumentTraceability(IRegisterBLL registerBLL) { _RegisterBLL = registerBLL; } public override void Configure() { Post("/Register/DocumentTraceability"); AllowAnonymous(); } public record GetDocumentTraceabilityParameters( [property: FromHeader] string Login, [property: FromBody] CriteriaDTO CriteriaDTO, [property: QueryParam] int FirstNumber, [property: QueryParam] int MaxResult ); protected override string? GetCacheKey(GetDocumentTraceabilityParameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync(GetDocumentTraceabilityParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { int pivotAllocationId = ParsePivotAllocationId(req.CriteriaDTO); var trace = await _RegisterBLL.GetDocumentTraceability(pivotAllocationId, LoginDTO!, ct); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(trace, CacheKeyLevel.NOT_REQUIRED, LoginDTO); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response .CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, ex.Message, 500); } } private static int ParsePivotAllocationId(CriteriaDTO? criteriaDTO) { if (criteriaDTO?.SectionCriteriaList == null) return -1; foreach (var sec in criteriaDTO.SectionCriteriaList) { foreach (var attr in sec.AttributesCriteriaList) { if (attr.FieldValue is null) continue; if (string.Equals(attr.FieldName, "AllocationId", StringComparison.OrdinalIgnoreCase) && int.TryParse(attr.FieldValue.ToString(), out var id)) return id; } } return -1; } } }