using System.Globalization; using NTP.Application.Abstractions; using NTP.Application.Common; using NTP.Domain.Grains.Registry; namespace NTP.Application.Grains.Registry; public interface IBlacklistService { Task> SearchAsync(BlacklistQuery query, PageRequest page, CancellationToken ct); Task AddAsync(AddBlacklistRequest request, CancellationToken ct); Task CancelAsync(long id, CancelBlacklistRequest request, CancellationToken ct); /// jobs\changeBlacklistStatus.cfm: expires OPEN entries whose expiry date has passed. Task ExpireDueAsync(CancellationToken ct); } /// Port of grains.cfc getBlackListGrid / BL_addToBlacklist / deleteFromBlackList and the expiry job. public sealed class BlacklistService( IBlacklistRepository repository, IRegistrySettingsService settings, IUnitOfWork unitOfWork, ICurrentUser user) : IBlacklistService { private static readonly string[] CancelRoles = ["GNSU", "GNCO", "GTO"]; public async Task> SearchAsync(BlacklistQuery query, PageRequest page, CancellationToken ct) { var result = await repository.SearchAsync(query, page, ct); foreach (var r in result.Items) { r.CategoryCaption = RegistryRules.BlacklistCategoryCaption(r.Category); r.EntityTypeCaption = RegistryRules.BlacklistEntityCaption(r.EntityType); r.StatusCaption = RegistryRules.BlacklistStatusCaption(r.StatusCode); } return result; } public async Task AddAsync(AddBlacklistRequest request, CancellationToken ct) { RegistryValidation.ThrowIfAny(RegistryValidation.ValidateBlacklist(request)); var entityType = string.IsNullOrWhiteSpace(request.EntityType) ? BlacklistValues.EntityPermit : request.EntityType.Trim().ToUpperInvariant(); var number = request.EntityNum!.Trim(); // The legacy function stores the permit id as ENTITYID: the number typed is a PN (looked up) unless ISID is set. var entityId = number; var shift = 1; if (entityType == BlacklistValues.EntityPermit) { long permitId; if (request.IsId) permitId = long.Parse(number, CultureInfo.InvariantCulture); else permitId = await repository.FindPermitIdByNumberAsync(number, ct) ?? throw new BusinessException(RegistryErrors.PermitNotFound, RegistryMessages.PermitNotFound(number)); entityId = permitId.ToString(CultureInfo.InvariantCulture); // shift of the tender order of the permit (default 1) shift = await repository.GetPermitShiftAsync(permitId, ct) ?? 1; } var (start, end) = settings.ShiftHours(shift); var expiry = RegistryRules.BlacklistExpiry(DateTime.Now, request.ExpiryDays, start, end); var remarks = RegistryRules.BlacklistRemarks(request.Remarks?.Trim(), expiry); var id = await repository.InsertAsync(request.Category!, entityType, entityId, user.UserId, expiry, remarks, ct); return new BlacklistAddedDto(RegistryMessages.BlacklistAdded, id, expiry); } public async Task CancelAsync(long id, CancelBlacklistRequest request, CancellationToken ct) { // the cancel icon of the grid is only offered to GNSU, GNCO, GTO if (!user.IsInAnyRole(CancelRoles)) throw new ForbiddenException(RegistryErrors.Forbidden, RegistryMessages.Forbidden); RegistryValidation.ThrowIfAny(RegistryValidation.ValidateCancelBlacklist(request)); var entry = await repository.GetAsync(id, ct) ?? throw new NotFoundException("Blacklist entry", id); // ...and only for OPEN entries if (!string.Equals(entry.StatusCode, BlacklistValues.Open, StringComparison.OrdinalIgnoreCase)) throw new BusinessException(RegistryErrors.BlacklistNotOpen, RegistryMessages.BlacklistNotOpen); var remarks = request.Remarks!.Trim(); await unitOfWork.ExecuteInTransactionAsync(async token => { await repository.CancelAsync(id, user.UserId, remarks, token); await repository.DeleteDistributionLogAsync(id, remarks, token); return true; }, ct); return new RegistryResult(RegistryMessages.Done, id); } public Task ExpireDueAsync(CancellationToken ct) => repository.ExpireDueAsync(ct); }