using AmpDAL.CustomCode.ApiExposurePolicy; using AmpDAL.DTO.ApiExposurePolicy; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.GB5Constant; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Microsoft.Extensions.Logging; namespace AMPBLL.ApiExposurePolicy; public class ApiExposurePolicyBLL( IApiExposurePolicyDAL _dal, IValidation _validation, AutoNumber _autoNumber, KeyInvalidate _keyInvalidate, EventLogPublish _eventLog, ILogger _logger ) : IApiExposurePolicyBLL { public async Task GetExposurePolicyAsync(long apiId, LoginDTO login, CancellationToken ct) => await _dal.GetExposurePolicyAsync(apiId, login, ct).ConfigureAwait(false); public async Task> GetExposurePolicySelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetExposurePolicySelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetExposurePolicyList(LoginDTO login, CancellationToken ct) => await _dal.GetExposurePolicyListAsync(login, ct).ConfigureAwait(false); public async Task SaveExposurePolicyAsync(ApiExposurePolicySaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ApiId == 0) throw new ArgumentException("ApiId must be greater than zero.", nameof(dto.ApiId)); bool isNew = dto.ApiExposurePolicyId == 0; long exposurePolicyId; var now = DateTime.UtcNow; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIEXPOSUREPOLICY", login); dto.ApiExposurePolicyId = autoNumberDTO.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = now; dto.ModifiedById = login.UserId; dto.ModifiedOn = now; exposurePolicyId = await _dal.SaveExposurePolicyAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedById = login.UserId; dto.ModifiedOn = now; exposurePolicyId = await _dal.UpdateExposurePolicyAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPEXPOSUREPOLICY, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for ExposurePolicy {ExposurePolicyId}", exposurePolicyId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP Exposure Policy Created" : "AMP Exposure Policy Updated", new { dto.ApiId, dto.AllowPartnerAccess, dto.AllowPublicAccess, ExposurePolicyId = exposurePolicyId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for ExposurePolicy {ExposurePolicyId}", exposurePolicyId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {exposurePolicyId}" : $"{SuccessResponse.UpdateSuccessMessage} {exposurePolicyId}"; } public async Task DeleteById(int apiExposurePolicyId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiExposurePolicyId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }