94 lines
4.5 KiB
C#
94 lines
4.5 KiB
C#
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<PagedResult<BlacklistGridRowDto>> SearchAsync(BlacklistQuery query, PageRequest page, CancellationToken ct);
|
|
Task<BlacklistAddedDto> AddAsync(AddBlacklistRequest request, CancellationToken ct);
|
|
Task<RegistryResult> CancelAsync(long id, CancelBlacklistRequest request, CancellationToken ct);
|
|
/// <summary>jobs\changeBlacklistStatus.cfm: expires OPEN entries whose expiry date has passed.</summary>
|
|
Task<int> ExpireDueAsync(CancellationToken ct);
|
|
}
|
|
|
|
/// <summary>Port of grains.cfc getBlackListGrid / BL_addToBlacklist / deleteFromBlackList and the expiry job.</summary>
|
|
public sealed class BlacklistService(
|
|
IBlacklistRepository repository,
|
|
IRegistrySettingsService settings,
|
|
IUnitOfWork unitOfWork,
|
|
ICurrentUser user) : IBlacklistService
|
|
{
|
|
private static readonly string[] CancelRoles = ["GNSU", "GNCO", "GTO"];
|
|
|
|
public async Task<PagedResult<BlacklistGridRowDto>> 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<BlacklistAddedDto> 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<RegistryResult> 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<int> ExpireDueAsync(CancellationToken ct) => repository.ExpireDueAsync(ct);
|
|
}
|