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>
|
/// </summary>
|
||||||
public string? Description { get; init; }
|
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.Exceptions;
|
||||||
using CMSMicroservice.Application.Common.Interfaces;
|
using CMSMicroservice.Application.Common.Interfaces;
|
||||||
using CMSMicroservice.Domain.Entities;
|
using CMSMicroservice.Domain.Entities;
|
||||||
|
using CMSMicroservice.Domain.Entities.Payment;
|
||||||
using CMSMicroservice.Domain.Enums;
|
using CMSMicroservice.Domain.Enums;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
@@ -9,13 +10,16 @@ namespace CMSMicroservice.Application.ManualPaymentCQ.Commands.ProcessManualMemb
|
|||||||
public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<ProcessManualMembershipPaymentCommand, ProcessManualMembershipPaymentResponseDto>
|
public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<ProcessManualMembershipPaymentCommand, ProcessManualMembershipPaymentResponseDto>
|
||||||
{
|
{
|
||||||
private readonly IApplicationDbContext _context;
|
private readonly IApplicationDbContext _context;
|
||||||
|
private readonly ICurrentUserService _currentUser;
|
||||||
private readonly ILogger<ProcessManualMembershipPaymentCommandHandler> _logger;
|
private readonly ILogger<ProcessManualMembershipPaymentCommandHandler> _logger;
|
||||||
|
|
||||||
public ProcessManualMembershipPaymentCommandHandler(
|
public ProcessManualMembershipPaymentCommandHandler(
|
||||||
IApplicationDbContext context,
|
IApplicationDbContext context,
|
||||||
|
ICurrentUserService currentUser,
|
||||||
ILogger<ProcessManualMembershipPaymentCommandHandler> logger)
|
ILogger<ProcessManualMembershipPaymentCommandHandler> logger)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
_currentUser = currentUser;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +33,19 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
|||||||
"Processing manual membership payment for UserId: {UserId}, Amount: {Amount}",
|
"Processing manual membership payment for UserId: {UserId}, Amount: {Amount}",
|
||||||
request.UserId, request.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
|
var user = await _context.Users
|
||||||
.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
|
.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
|
||||||
|
|
||||||
@@ -39,45 +55,38 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
|||||||
throw new NotFoundException($"کاربر با شناسه {request.UserId} یافت نشد");
|
throw new NotFoundException($"کاربر با شناسه {request.UserId} یافت نشد");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. بررسی وجود ادمین
|
// 3. ایجاد ManualPayment با وضعیت Approved
|
||||||
var admin = await _context.Users
|
var manualPayment = new ManualPayment
|
||||||
.FirstOrDefaultAsync(u => u.Id == request.AdminUserId, cancellationToken);
|
|
||||||
|
|
||||||
if (admin == null)
|
|
||||||
{
|
{
|
||||||
_logger.LogError("Admin user not found: {AdminUserId}", request.AdminUserId);
|
UserId = request.UserId,
|
||||||
throw new NotFoundException($"ادمین با شناسه {request.AdminUserId} یافت نشد");
|
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
|
var wallet = await _context.UserWallets
|
||||||
.FirstOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken);
|
.FirstOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken);
|
||||||
|
|
||||||
if (wallet == null)
|
if (wallet == null)
|
||||||
{
|
{
|
||||||
wallet = new UserWallet
|
_logger.LogError("Wallet not found for UserId: {UserId}", request.UserId);
|
||||||
{
|
throw new NotFoundException($"کیف پول کاربر {request.UserId} یافت نشد");
|
||||||
UserId = request.UserId,
|
|
||||||
Balance = 0,
|
|
||||||
NetworkBalance = 0,
|
|
||||||
DiscountBalance = 0
|
|
||||||
};
|
|
||||||
await _context.UserWallets.AddAsync(wallet, cancellationToken);
|
|
||||||
await _context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. افزودن به Balance و DiscountBalance
|
// 6. ثبت تراکنش
|
||||||
var oldBalance = wallet.Balance;
|
|
||||||
var oldDiscountBalance = wallet.DiscountBalance;
|
|
||||||
|
|
||||||
wallet.Balance += request.Amount;
|
|
||||||
wallet.DiscountBalance += request.Amount;
|
|
||||||
|
|
||||||
// 5. ثبت تراکنش
|
|
||||||
var transaction = new Transaction
|
var transaction = new Transaction
|
||||||
{
|
{
|
||||||
Amount = request.Amount,
|
Amount = request.Amount,
|
||||||
Description = $"پرداخت دستی عضویت - {request.Description ?? "بدون توضیحات"} - مرجع: {request.ReferenceNumber}",
|
Description = $"پرداخت دستی عضویت - {manualPayment.Description} - مرجع: {request.ReferenceNumber}",
|
||||||
PaymentStatus = PaymentStatus.Success,
|
PaymentStatus = PaymentStatus.Success,
|
||||||
PaymentDate = DateTime.Now,
|
PaymentDate = DateTime.Now,
|
||||||
RefId = request.ReferenceNumber,
|
RefId = request.ReferenceNumber,
|
||||||
@@ -87,7 +96,14 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
|||||||
_context.Transactions.Add(transaction);
|
_context.Transactions.Add(transaction);
|
||||||
await _context.SaveChangesAsync(cancellationToken);
|
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
|
var balanceLog = new UserWalletChangeLog
|
||||||
{
|
{
|
||||||
WalletId = wallet.Id,
|
WalletId = wallet.Id,
|
||||||
@@ -95,29 +111,19 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
|||||||
ChangeValue = request.Amount,
|
ChangeValue = request.Amount,
|
||||||
CurrentNetworkBalance = wallet.NetworkBalance,
|
CurrentNetworkBalance = wallet.NetworkBalance,
|
||||||
ChangeNerworkValue = 0,
|
ChangeNerworkValue = 0,
|
||||||
CurrentDiscountBalance = oldDiscountBalance,
|
CurrentDiscountBalance = wallet.DiscountBalance,
|
||||||
ChangeDiscountValue = 0,
|
ChangeDiscountValue = request.Amount,
|
||||||
IsIncrease = true,
|
IsIncrease = true,
|
||||||
RefrenceId = transaction.Id
|
RefrenceId = transaction.Id
|
||||||
};
|
};
|
||||||
await _context.UserWalletChangeLogs.AddAsync(balanceLog, cancellationToken);
|
await _context.UserWalletChangeLogs.AddAsync(balanceLog, cancellationToken);
|
||||||
|
|
||||||
// 7. ثبت لاگ DiscountBalance
|
|
||||||
// var discountLog = new UserWalletChangeLog
|
// 10. بهروزرسانی ManualPayment با TransactionId
|
||||||
// {
|
manualPayment.TransactionId = transaction.Id;
|
||||||
// WalletId = wallet.Id,
|
await _context.SaveChangesAsync(cancellationToken);
|
||||||
// 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);
|
|
||||||
|
|
||||||
// 8. پیدا کردن یا ایجاد آدرس پیشفرض کاربر
|
// 11. پیدا کردن یا ایجاد آدرس پیشفرض کاربر
|
||||||
var userAddress = await _context.UserAddresses
|
var userAddress = await _context.UserAddresses
|
||||||
.Where(a => a.UserId == request.UserId)
|
.Where(a => a.UserId == request.UserId)
|
||||||
.OrderByDescending(a => a.IsDefault)
|
.OrderByDescending(a => a.IsDefault)
|
||||||
@@ -139,7 +145,7 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
|||||||
await _context.SaveChangesAsync(cancellationToken);
|
await _context.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. ثبت سفارش
|
// 12. ثبت سفارش
|
||||||
var order = new UserOrder
|
var order = new UserOrder
|
||||||
{
|
{
|
||||||
UserId = request.UserId,
|
UserId = request.UserId,
|
||||||
@@ -147,19 +153,18 @@ public class ProcessManualMembershipPaymentCommandHandler : IRequestHandler<Proc
|
|||||||
TransactionId = transaction.Id,
|
TransactionId = transaction.Id,
|
||||||
PaymentStatus = PaymentStatus.Success,
|
PaymentStatus = PaymentStatus.Success,
|
||||||
PaymentDate = DateTime.Now,
|
PaymentDate = DateTime.Now,
|
||||||
PaymentMethod = PaymentMethod.IPG,
|
PaymentMethod = PaymentMethod.Deposit,
|
||||||
DeliveryStatus = DeliveryStatus.None,
|
DeliveryStatus = DeliveryStatus.None,
|
||||||
PackageId = 3,
|
|
||||||
UserAddressId = userAddress.Id,
|
UserAddressId = userAddress.Id,
|
||||||
DeliveryDescription = $"پرداخت دستی عضویت توسط ادمین {admin.FirstName} {admin.LastName} - مرجع: {request.ReferenceNumber}"
|
DeliveryDescription = $"پرداخت دستی عضویت - مرجع: {request.ReferenceNumber}"
|
||||||
};
|
};
|
||||||
|
|
||||||
_context.UserOrders.Add(order);
|
_context.UserOrders.Add(order);
|
||||||
await _context.SaveChangesAsync(cancellationToken);
|
await _context.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"Manual membership payment processed successfully. UserId: {UserId}, Amount: {Amount}, TransactionId: {TransactionId}, OrderId: {OrderId}, AdminUserId: {AdminUserId}",
|
"Manual membership payment processed successfully. UserId: {UserId}, Amount: {Amount}, ManualPaymentId: {ManualPaymentId}, TransactionId: {TransactionId}, OrderId: {OrderId}, AdminUserId: {AdminUserId}",
|
||||||
request.UserId, request.Amount, transaction.Id, order.Id, request.AdminUserId);
|
request.UserId, request.Amount, manualPayment.Id, transaction.Id, order.Id, adminUserId);
|
||||||
|
|
||||||
return new ProcessManualMembershipPaymentResponseDto
|
return new ProcessManualMembershipPaymentResponseDto
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,9 +17,5 @@ public class ProcessManualMembershipPaymentCommandValidator : AbstractValidator<
|
|||||||
.WithMessage("شماره مرجع الزامی است")
|
.WithMessage("شماره مرجع الزامی است")
|
||||||
.MaximumLength(50)
|
.MaximumLength(50)
|
||||||
.WithMessage("شماره مرجع نباید بیشتر از 50 کاراکتر باشد");
|
.WithMessage("شماره مرجع نباید بیشتر از 50 کاراکتر باشد");
|
||||||
|
|
||||||
RuleFor(x => x.AdminUserId)
|
|
||||||
.GreaterThan(0)
|
|
||||||
.WithMessage("شناسه ادمین معتبر نیست");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,7 +141,6 @@ message ProcessManualMembershipPaymentRequest
|
|||||||
int64 amount = 2;
|
int64 amount = 2;
|
||||||
string reference_number = 3;
|
string reference_number = 3;
|
||||||
google.protobuf.StringValue description = 4;
|
google.protobuf.StringValue description = 4;
|
||||||
int64 admin_user_id = 5;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message ProcessManualMembershipPaymentResponse
|
message ProcessManualMembershipPaymentResponse
|
||||||
|
|||||||
@@ -67,8 +67,7 @@ public class ManualPaymentService : ManualPaymentContract.ManualPaymentContractB
|
|||||||
UserId = request.UserId,
|
UserId = request.UserId,
|
||||||
Amount = request.Amount,
|
Amount = request.Amount,
|
||||||
ReferenceNumber = request.ReferenceNumber,
|
ReferenceNumber = request.ReferenceNumber,
|
||||||
Description = request.Description,
|
Description = request.Description
|
||||||
AdminUserId = request.AdminUserId
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = await _sender.Send(command, context.CancellationToken);
|
var result = await _sender.Send(command, context.CancellationToken);
|
||||||
|
|||||||
Reference in New Issue
Block a user