using AccountsBLL.Voucher; using AccountsDAL.DTO.Voucher; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using System.Linq; using static GB5Shared.GB5Constant.Constant; namespace AccountsSL.EndPoints.Voucher { public class SaveVoucher : BaseEndpoint> { private readonly IVoucherBLL _VoucherBLL; public SaveVoucher(IVoucherBLL voucherBLL) { _VoucherBLL = voucherBLL; } public override void Configure() { Post("/Voucher/SaveVoucher"); AllowAnonymous(); } public record SaveVoucherParameters( [property: FromHeader] string Login, [property: FromBody] VoucherDTO VoucherDTO ); protected override async Task> ExecuteAsync(SaveVoucherParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { var Result = await _VoucherBLL.SaveVoucherAsync(req.VoucherDTO, LoginDTO!); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(Result, CacheKeyLevel.NOT_REQUIRED, LoginDTO); } catch (VoucherEndValidationException ex) { // GB4's FNVOUCHERVALIDATE failure path — surfaces the validation rows, not a flat message. string message = string.Join("; ", ex.ValidationRows.Select(r => r.ValidationMessage)); return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, message, 422); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, ex.Message, 500); } } } }