refactor: remove admin user id from manual payment command
All checks were successful
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 2m13s
All checks were successful
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 2m13s
This commit is contained in:
@@ -21,9 +21,4 @@ public record ProcessManualMembershipPaymentCommand : IRequest<ProcessManualMemb
|
||||
/// توضیحات (اختیاری)
|
||||
/// </summary>
|
||||
public string? Description { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// شناسه ادمین ثبت کننده
|
||||
/// </summary>
|
||||
public long AdminUserId { get; init; }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using CMSMicroservice.Application.Common.Exceptions;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Entities;
|
||||
using CMSMicroservice.Domain.Entities.Payment;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -9,13 +10,16 @@ namespace CMSMicroservice.Application.ManualPaymentCQ.Commands.ProcessManualMemb
|
||||
public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<ProcessManualMembershipPaymentCommand, ProcessManualMembershipPaymentResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
private readonly ILogger<ProcessManualMembershipPaymentCommandHandler> _logger;
|
||||
|
||||
public ProcessManualMembershipPaymentCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUser,
|
||||
ILogger<ProcessManualMembershipPaymentCommandHandler> logger)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -29,7 +33,19 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
||||
"Processing manual membership payment for UserId: {UserId}, Amount: {Amount}",
|
||||
request.UserId, request.Amount);
|
||||
|
||||
// 1. بررسی وجود کاربر
|
||||
// 1. دریافت شناسه ادمین از کاربر جاری
|
||||
var currentUserId = _currentUser.UserId;
|
||||
if (string.IsNullOrEmpty(currentUserId))
|
||||
{
|
||||
throw new UnauthorizedAccessException("کاربر احراز هویت نشده است");
|
||||
}
|
||||
|
||||
if (!long.TryParse(currentUserId, out var adminUserId))
|
||||
{
|
||||
throw new UnauthorizedAccessException("شناسه کاربر نامعتبر است");
|
||||
}
|
||||
|
||||
// 2. بررسی وجود کاربر
|
||||
var user = await _context.Users
|
||||
.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
|
||||
|
||||
@@ -39,45 +55,38 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
||||
throw new NotFoundException($"کاربر با شناسه {request.UserId} یافت نشد");
|
||||
}
|
||||
|
||||
// 2. بررسی وجود ادمین
|
||||
var admin = await _context.Users
|
||||
.FirstOrDefaultAsync(u => u.Id == request.AdminUserId, cancellationToken);
|
||||
|
||||
if (admin == null)
|
||||
// 3. ایجاد ManualPayment با وضعیت Approved
|
||||
var manualPayment = new ManualPayment
|
||||
{
|
||||
_logger.LogError("Admin user not found: {AdminUserId}", request.AdminUserId);
|
||||
throw new NotFoundException($"ادمین با شناسه {request.AdminUserId} یافت نشد");
|
||||
}
|
||||
UserId = request.UserId,
|
||||
Amount = request.Amount,
|
||||
Type = ManualPaymentType.CashDeposit,
|
||||
Description = request.Description ?? "پرداخت دستی عضویت",
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Status = ManualPaymentStatus.Approved,
|
||||
RequestedBy = adminUserId,
|
||||
ApprovedBy = adminUserId,
|
||||
ApprovedAt = DateTime.Now
|
||||
};
|
||||
|
||||
// 3. پیدا کردن یا ایجاد کیف پول
|
||||
_context.ManualPayments.Add(manualPayment);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 4. پیدا کردن یا ایجاد کیف پول
|
||||
var wallet = await _context.UserWallets
|
||||
.FirstOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken);
|
||||
|
||||
if (wallet == null)
|
||||
{
|
||||
wallet = new UserWallet
|
||||
{
|
||||
UserId = request.UserId,
|
||||
Balance = 0,
|
||||
NetworkBalance = 0,
|
||||
DiscountBalance = 0
|
||||
};
|
||||
await _context.UserWallets.AddAsync(wallet, cancellationToken);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_logger.LogError("Wallet not found for UserId: {UserId}", request.UserId);
|
||||
throw new NotFoundException($"کیف پول کاربر {request.UserId} یافت نشد");
|
||||
}
|
||||
|
||||
// 4. افزودن به Balance و DiscountBalance
|
||||
var oldBalance = wallet.Balance;
|
||||
var oldDiscountBalance = wallet.DiscountBalance;
|
||||
|
||||
wallet.Balance += request.Amount;
|
||||
wallet.DiscountBalance += request.Amount;
|
||||
|
||||
// 5. ثبت تراکنش
|
||||
// 6. ثبت تراکنش
|
||||
var transaction = new Transaction
|
||||
{
|
||||
Amount = request.Amount,
|
||||
Description = $"پرداخت دستی عضویت - {request.Description ?? "بدون توضیحات"} - مرجع: {request.ReferenceNumber}",
|
||||
Description = $"پرداخت دستی عضویت - {manualPayment.Description} - مرجع: {request.ReferenceNumber}",
|
||||
PaymentStatus = PaymentStatus.Success,
|
||||
PaymentDate = DateTime.Now,
|
||||
RefId = request.ReferenceNumber,
|
||||
@@ -87,7 +96,14 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
||||
_context.Transactions.Add(transaction);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 6. ثبت لاگ Balance
|
||||
// 7. اعمال تغییرات بر کیف پول
|
||||
var oldBalance = wallet.Balance;
|
||||
var oldDiscountBalance = wallet.DiscountBalance;
|
||||
|
||||
wallet.Balance += request.Amount;
|
||||
wallet.DiscountBalance += request.Amount;
|
||||
|
||||
// 8. ثبت لاگ Balance
|
||||
var balanceLog = new UserWalletChangeLog
|
||||
{
|
||||
WalletId = wallet.Id,
|
||||
@@ -95,29 +111,19 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
||||
ChangeValue = request.Amount,
|
||||
CurrentNetworkBalance = wallet.NetworkBalance,
|
||||
ChangeNerworkValue = 0,
|
||||
CurrentDiscountBalance = oldDiscountBalance,
|
||||
ChangeDiscountValue = 0,
|
||||
CurrentDiscountBalance = wallet.DiscountBalance,
|
||||
ChangeDiscountValue = request.Amount,
|
||||
IsIncrease = true,
|
||||
RefrenceId = transaction.Id
|
||||
};
|
||||
await _context.UserWalletChangeLogs.AddAsync(balanceLog, cancellationToken);
|
||||
|
||||
// 7. ثبت لاگ DiscountBalance
|
||||
// var discountLog = new UserWalletChangeLog
|
||||
// {
|
||||
// WalletId = wallet.Id,
|
||||
// CurrentBalance = wallet.Balance,
|
||||
// ChangeValue = 0,
|
||||
// CurrentNetworkBalance = wallet.NetworkBalance,
|
||||
// ChangeNerworkValue = 0,
|
||||
// CurrentDiscountBalance = wallet.DiscountBalance,
|
||||
// ChangeDiscountValue = request.Amount,
|
||||
// IsIncrease = true,
|
||||
// RefrenceId = transaction.Id
|
||||
// };
|
||||
// await _context.UserWalletChangeLogs.AddAsync(discountLog, cancellationToken);
|
||||
|
||||
// 10. بهروزرسانی ManualPayment با TransactionId
|
||||
manualPayment.TransactionId = transaction.Id;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 8. پیدا کردن یا ایجاد آدرس پیشفرض کاربر
|
||||
// 11. پیدا کردن یا ایجاد آدرس پیشفرض کاربر
|
||||
var userAddress = await _context.UserAddresses
|
||||
.Where(a => a.UserId == request.UserId)
|
||||
.OrderByDescending(a => a.IsDefault)
|
||||
@@ -139,7 +145,7 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
// 9. ثبت سفارش
|
||||
// 12. ثبت سفارش
|
||||
var order = new UserOrder
|
||||
{
|
||||
UserId = request.UserId,
|
||||
@@ -147,19 +153,18 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
||||
TransactionId = transaction.Id,
|
||||
PaymentStatus = PaymentStatus.Success,
|
||||
PaymentDate = DateTime.Now,
|
||||
PaymentMethod = PaymentMethod.IPG,
|
||||
PaymentMethod = PaymentMethod.Deposit,
|
||||
DeliveryStatus = DeliveryStatus.None,
|
||||
PackageId = 3,
|
||||
UserAddressId = userAddress.Id,
|
||||
DeliveryDescription = $"پرداخت دستی عضویت توسط ادمین {admin.FirstName} {admin.LastName} - مرجع: {request.ReferenceNumber}"
|
||||
DeliveryDescription = $"پرداخت دستی عضویت - مرجع: {request.ReferenceNumber}"
|
||||
};
|
||||
|
||||
_context.UserOrders.Add(order);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Manual membership payment processed successfully. UserId: {UserId}, Amount: {Amount}, TransactionId: {TransactionId}, OrderId: {OrderId}, AdminUserId: {AdminUserId}",
|
||||
request.UserId, request.Amount, transaction.Id, order.Id, request.AdminUserId);
|
||||
"Manual membership payment processed successfully. UserId: {UserId}, Amount: {Amount}, ManualPaymentId: {ManualPaymentId}, TransactionId: {TransactionId}, OrderId: {OrderId}, AdminUserId: {AdminUserId}",
|
||||
request.UserId, request.Amount, manualPayment.Id, transaction.Id, order.Id, adminUserId);
|
||||
|
||||
return new ProcessManualMembershipPaymentResponseDto
|
||||
{
|
||||
|
||||
@@ -17,9 +17,5 @@ public class ProcessManualMembershipPaymentCommandValidator : AbstractValidator<
|
||||
.WithMessage("شماره مرجع الزامی است")
|
||||
.MaximumLength(50)
|
||||
.WithMessage("شماره مرجع نباید بیشتر از 50 کاراکتر باشد");
|
||||
|
||||
RuleFor(x => x.AdminUserId)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("شناسه ادمین معتبر نیست");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user