using FluentValidation;
using NTP.Application.Common;
using NTP.Application.Grains.Orders;
namespace NTP.Application.Refinery.Wallets;
public interface IWalletsRepository
{
/// Oldest first (legacy sort ID ASC).
Task> SearchAsync(WalletsQuery query, PageRequest page, CancellationToken ct = default);
Task GetAsync(long id, CancellationToken ct = default);
/// Ids of the ACTIVE records of a truck.
Task> GetActiveIdsByTruckAsync(long tn, CancellationToken ct = default);
/// Ids of the ACTIVE records of a driver (national number).
Task> GetActiveIdsByDriverAsync(long nnpn, CancellationToken ct = default);
Task InsertAsync(WalletWrite record, string createdBy, CancellationToken ct = default);
Task SetStatusAsync(long id, string status, string userId, CancellationToken ct = default);
Task InsertLogAsync(long entityId, string method, string userId, string? userIp, string oldData, string newData, CancellationToken ct = default);
Task> GetLogAsync(long entityId, PageRequest page, CancellationToken ct = default);
}
public interface IWalletsService
{
Task> SearchAsync(WalletsQuery query, PageRequest page, CancellationToken ct);
Task GetAsync(long id, CancellationToken ct);
Task> GetLogAsync(long id, PageRequest page, CancellationToken ct);
Task CreateAsync(SaveWalletRequest request, string? userIp, CancellationToken ct);
Task UpdateAsync(long id, SaveWalletRequest request, string? userIp, CancellationToken ct);
Task ActivateAsync(long id, string? userIp, CancellationToken ct);
Task DeactivateAsync(long id, string? userIp, CancellationToken ct);
Task ExportAsync(WalletsQuery query, CancellationToken ct);
}
///
/// The form of REFINERY_WALLET.js: required fields per payment method, mobile / bank account / digits formats. Stricter than legacy in two places
/// only: the truck number (WALLET, BANK) and the wallet number (WALLET, DRIVER_TIP) are required, because the duplicate rule and the payment
/// need them (every stored record has them).
///
public sealed class SaveWalletValidator : AbstractValidator
{
public SaveWalletValidator()
{
RuleFor(x => x.PaymentMethod).Must(m => m is WalletValues.MethodWallet or WalletValues.MethodBank or WalletValues.MethodDriverTip)
.WithMessage(WalletsMessages.Required);
When(x => x.PaymentMethod is WalletValues.MethodWallet or WalletValues.MethodBank, () =>
{
RuleFor(x => x.Tn).Must(v => WalletsRules.IsDigits(v?.Trim(), 10)).WithMessage(WalletsMessages.Required);
RuleFor(x => x.OwnerName).NotEmpty().MaximumLength(450).WithMessage(WalletsMessages.Required);
RuleFor(x => x.OwnerNnpn).Must(v => string.IsNullOrWhiteSpace(v) || WalletsRules.IsDigits(v.Trim(), 15)).WithMessage(WalletsMessages.NumbersOnly);
});
When(x => x.PaymentMethod == WalletValues.MethodWallet, () =>
{
RuleFor(x => x.WalletNumber).NotEmpty().WithMessage(WalletsMessages.Required)
.Must(v => WalletsRules.IsMobile(v?.Trim())).WithMessage(WalletsMessages.WrongMobile);
});
When(x => x.PaymentMethod == WalletValues.MethodBank, () =>
{
RuleFor(x => x.Iban).NotEmpty().MaximumLength(50).WithMessage(WalletsMessages.Required);
RuleFor(x => x.BankAccount).NotEmpty().MaximumLength(50).WithMessage(WalletsMessages.Required)
.Must(v => WalletsRules.IsBankAccount(v?.Trim())).WithMessage(WalletsMessages.WrongBankAccount);
RuleFor(x => x.BankName).MaximumLength(300);
RuleFor(x => x.BankBranch).MaximumLength(50);
RuleFor(x => x.OwnerNameEnglish).MaximumLength(150);
});
When(x => x.PaymentMethod == WalletValues.MethodDriverTip, () =>
{
RuleFor(x => x.DriverName).NotEmpty().MaximumLength(450).WithMessage(WalletsMessages.Required);
RuleFor(x => x.DriverNnpn).Must(v => WalletsRules.IsDigits(v?.Trim(), 15)).WithMessage(WalletsMessages.Required);
RuleFor(x => x.WalletNumber).NotEmpty().WithMessage(WalletsMessages.Required)
.Must(v => WalletsRules.IsMobile(v?.Trim())).WithMessage(WalletsMessages.WrongMobile);
});
}
}