using FastEndpoints; using GB5Shared.DTO.ECM; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using MaintenanceBLL.Claim; using static GB5Shared.GB5Constant.Constant; namespace MaintenanceSL.EndPoints.Claim { // ObjectTypeId is hardcoded to OBJECTCLAIM server-side (IClaimBLL.UploadClaimDocumentAsync) — // never client-supplied — so this endpoint can't tag an attachment against an unrelated entity. public class UploadClaimDocument : BaseEndpoint> { private readonly IClaimBLL _ClaimBLL; public UploadClaimDocument(IClaimBLL claimBLL) { _ClaimBLL = claimBLL; } public record UploadClaimDocumentParameters( [property: FromHeader] string Login, [property: QueryParam] int ClaimId, [property: QueryParam] int DocumentSetDetailId, [property: QueryParam] string? Remarks, [property: QueryParam] string? Tags ); public override void Configure() { Post("/Claim/UploadClaimDocument"); AllowFileUploads(); AllowAnonymous(); } protected override async Task> ExecuteAsync( UploadClaimDocumentParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { var file = HttpContext.Request.Form.Files.FirstOrDefault(); if (file is null || file.Length == 0) throw new ArgumentException("No file provided or file is empty."); await using var stream = file.OpenReadStream(); var uploadRequest = new AttachmentUploadRequest { DocumentSetDetailId = req.DocumentSetDetailId, Remarks = req.Remarks, Tags = req.Tags ?? string.Empty }; var result = await _ClaimBLL.UploadClaimDocumentAsync( req.ClaimId, uploadRequest, stream, file.FileName, file.ContentType, LoginDTO!, ct); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( result, CacheKeyLevel.NOT_REQUIRED, LoginDTO); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, ex.Message, 500); } } } }