feat: Implement Approve and Reject Withdrawal commands with handlers
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.CommissionCQ.Commands.ApproveWithdrawal;
|
||||
|
||||
public class ApproveWithdrawalCommand : IRequest<Unit>
|
||||
{
|
||||
public long PayoutId { get; set; }
|
||||
public string? Notes { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using CMSMicroservice.Application.Common.Exceptions;
|
||||
|
||||
namespace CMSMicroservice.Application.CommissionCQ.Commands.ApproveWithdrawal;
|
||||
|
||||
public class ApproveWithdrawalCommandHandler : IRequestHandler<ApproveWithdrawalCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public ApproveWithdrawalCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(ApproveWithdrawalCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var payout = await _context.UserCommissionPayouts
|
||||
.FirstOrDefaultAsync(x => x.Id == request.PayoutId, cancellationToken);
|
||||
|
||||
if (payout == null)
|
||||
{
|
||||
throw new NotFoundException($"Payout با شناسه {request.PayoutId} یافت نشد");
|
||||
}
|
||||
|
||||
if (payout.Status != CommissionPayoutStatus.WithdrawRequested)
|
||||
{
|
||||
throw new BadRequestException($"فقط درخواستهای در وضعیت WithdrawRequested قابل تایید هستند");
|
||||
}
|
||||
|
||||
// Update status to Withdrawn (approved)
|
||||
payout.Status = CommissionPayoutStatus.Withdrawn;
|
||||
payout.WithdrawnAt = DateTime.UtcNow;
|
||||
payout.LastModified = DateTime.UtcNow;
|
||||
|
||||
// TODO: Add PayoutHistory record
|
||||
// var history = new CommissionPayoutHistory
|
||||
// {
|
||||
// PayoutId = payout.Id,
|
||||
// UserId = payout.UserId,
|
||||
// WeekNumber = payout.WeekNumber,
|
||||
// AmountBefore = payout.TotalAmount,
|
||||
// AmountAfter = payout.TotalAmount,
|
||||
// OldStatus = (int)CommissionPayoutStatus.Pending,
|
||||
// NewStatus = (int)CommissionPayoutStatus.Approved,
|
||||
// Action = (int)CommissionPayoutAction.Approved,
|
||||
// PerformedBy = "Admin", // TODO: Get from authenticated user
|
||||
// Reason = request.Notes,
|
||||
// Created = DateTime.UtcNow
|
||||
// };
|
||||
// _context.CommissionPayoutHistories.Add(history);
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user