using EntitlementBLL.Auth; using EntitlementBLL.Interfaces; using EntitlementSL.Common; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using ErrorResponse = GB5Shared.Resource.Response.ErrorResponse; using static GB5Shared.GB5Constant.Constant; namespace EntitlementSL.Endpoints.License; // Client-facing equivalent of GetUtilisation — ILicenseBLL.GetUtilisationAsync(clientId,...) is // already scoped correctly by its clientId argument, so this just sources that argument from // verified JWT claims instead of a caller-supplied query parameter. public class GetMyUtilisation : BaseEndpoint> { private const string ClientJwtSchemeName = "ClientJwtBearer"; private readonly ILicenseBLL _LicenseBLL; public GetMyUtilisation(ILicenseBLL licenseBLL) => _LicenseBLL = licenseBLL; public override void Configure() { Get("/lic/License.svc/GetMyUtilisation"); AuthSchemes(ClientJwtSchemeName); Roles(ClientRoleCodes.ClientAdmin, ClientRoleCodes.ClientUser); } public record Params([property: FromHeader] string Login); protected override string? GetCacheKey(Params req, LoginDTO loginDTO) { var caller = ClientCallerContext.FromClaims(HttpContext.User.Claims); return KeyGenerator.KeyGeneration(caller.ClientId, EntityConstant.OBJECTENTITLEMENTLICENSE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); } protected override async Task> ExecuteAsync(Params req, LoginDTO loginDTO, CancellationToken ct) { var caller = ClientCallerContext.FromClaims(HttpContext.User.Claims); if (caller.ClientId <= 0) return await GB5Shared.ResponseStandard.Response.CreateErrorResponse( ErrorResponse.AccessDeniedMessage, CacheKeyLevel.NOT_REQUIRED, loginDTO, statusCode: 403); var result = await _LicenseBLL.GetUtilisationAsync(caller.ClientId, loginDTO, ct); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponseEnumerable(result, CacheKeyLevel.CLIENT_LEVEL, loginDTO); } }