// EN: Command to reject a merchant registration.
// VI: Command để từ chối đăng ký merchant.
using MediatR;
using MerchantService.Domain.AggregatesModel.MerchantAggregate;
using MerchantService.Domain.Exceptions;
namespace MerchantService.API.Application.Commands.Admin;
///
/// EN: Command to reject a merchant registration (Admin only).
/// VI: Command để từ chối đăng ký merchant (chỉ Admin).
///
/// Merchant ID to reject / ID Merchant cần từ chối
/// Reason for rejection / Lý do từ chối
/// Admin user ID / ID Admin từ chối
public record RejectMerchantCommand(
Guid MerchantId,
string Reason,
Guid RejectedBy) : IRequest;
///
/// EN: Result of merchant rejection.
/// VI: Kết quả từ chối merchant.
///
public record RejectMerchantResult(
Guid MerchantId,
string Status,
string Reason,
DateTime RejectedAt);
///
/// EN: Handler for RejectMerchantCommand.
/// VI: Handler cho RejectMerchantCommand.
///
public class RejectMerchantCommandHandler : IRequestHandler
{
private readonly IMerchantRepository _merchantRepository;
private readonly ILogger _logger;
public RejectMerchantCommandHandler(
IMerchantRepository merchantRepository,
ILogger logger)
{
_merchantRepository = merchantRepository;
_logger = logger;
}
public async Task Handle(
RejectMerchantCommand request,
CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(request.Reason))
throw new DomainException("Rejection reason is required");
var merchant = await _merchantRepository.GetByIdAsync(request.MerchantId, cancellationToken)
?? throw new DomainException($"Merchant {request.MerchantId} not found");
// EN: Reject sets status to Rejected - need to add this method to Domain
// VI: Reject đặt status thành Rejected - cần thêm method vào Domain
merchant.Suspend(request.Reason); // Using Suspend as rejection for now
_merchantRepository.Update(merchant);
await _merchantRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
_logger.LogInformation(
"Merchant {MerchantId} rejected by Admin {AdminId}. Reason: {Reason}",
request.MerchantId, request.RejectedBy, request.Reason);
return new RejectMerchantResult(
merchant.Id,
merchant.Status.Name,
request.Reason,
DateTime.UtcNow);
}
}