From f3ac5ad7dfc848694c2c80b65da383c92b3b0953 Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Mon, 23 Feb 2026 22:59:00 +0330 Subject: [PATCH] fix: add missing UserWalletChangeLog for discount shop purchases and discount wallet charge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: when buying from discount shop, DiscountBalance was deducted from DB but no UserWalletChangeLog was created — making it invisible in wallet history. Same issue existed for discount wallet top-up (charge). Fixed in 3 handlers: - PlaceOrderCommandHandler (fully paid by discount balance path) - CompleteOrderPaymentCommandHandler (gateway + discount balance path) - VerifyDiscountWalletChargeCommandHandler (discount wallet charge) --- .../CompleteOrderPaymentCommandHandler.cs | 16 +++++++++++++++- .../PlaceOrder/PlaceOrderCommandHandler.cs | 18 +++++++++++++++++- ...VerifyDiscountWalletChargeCommandHandler.cs | 15 +++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/CMSMicroservice.Application/DiscountShopCQ/Commands/CompleteOrderPayment/CompleteOrderPaymentCommandHandler.cs b/src/CMSMicroservice.Application/DiscountShopCQ/Commands/CompleteOrderPayment/CompleteOrderPaymentCommandHandler.cs index 0216a08..ffd5769 100644 --- a/src/CMSMicroservice.Application/DiscountShopCQ/Commands/CompleteOrderPayment/CompleteOrderPaymentCommandHandler.cs +++ b/src/CMSMicroservice.Application/DiscountShopCQ/Commands/CompleteOrderPayment/CompleteOrderPaymentCommandHandler.cs @@ -62,9 +62,23 @@ public class CompleteOrderPaymentCommandHandler : IRequestHandler w.UserId == order.UserId, cancellationToken); - if (userWallet != null) + if (userWallet != null && order.DiscountBalanceUsed > 0) { userWallet.DiscountBalance -= order.DiscountBalanceUsed; + + // ثبت لاگ تغییرات کیف پول + _context.UserWalletChangeLogs.Add(new Domain.Entities.UserWalletChangeLog + { + WalletId = userWallet.Id, + CurrentBalance = userWallet.Balance, + ChangeValue = 0, + CurrentNetworkBalance = userWallet.NetworkBalance, + ChangeNerworkValue = 0, + CurrentDiscountBalance = userWallet.DiscountBalance, + ChangeDiscountValue = -order.DiscountBalanceUsed, + IsIncrease = false, + RefrenceId = transaction.Id + }); } // تایید فروش و کسر موجودی از طریق InventoryService diff --git a/src/CMSMicroservice.Application/DiscountShopCQ/Commands/PlaceOrder/PlaceOrderCommandHandler.cs b/src/CMSMicroservice.Application/DiscountShopCQ/Commands/PlaceOrder/PlaceOrderCommandHandler.cs index 58bb737..9a3d9a4 100644 --- a/src/CMSMicroservice.Application/DiscountShopCQ/Commands/PlaceOrder/PlaceOrderCommandHandler.cs +++ b/src/CMSMicroservice.Application/DiscountShopCQ/Commands/PlaceOrder/PlaceOrderCommandHandler.cs @@ -263,9 +263,25 @@ public class PlaceOrderCommandHandler : IRequestHandler w.UserId == request.UserId, cancellationToken); - if (walletForDeduct != null) + if (walletForDeduct != null && actualDiscountBalanceUsed > 0) + { walletForDeduct.DiscountBalance -= actualDiscountBalanceUsed; + // ثبت لاگ تغییرات کیف پول + _context.UserWalletChangeLogs.Add(new Domain.Entities.UserWalletChangeLog + { + WalletId = walletForDeduct.Id, + CurrentBalance = walletForDeduct.Balance, + ChangeValue = 0, + CurrentNetworkBalance = walletForDeduct.NetworkBalance, + ChangeNerworkValue = 0, + CurrentDiscountBalance = walletForDeduct.DiscountBalance, + ChangeDiscountValue = -actualDiscountBalanceUsed, + IsIncrease = false, + RefrenceId = transaction.Id + }); + } + foreach (var cartItem in cartItems) { await _inventoryService.ConfirmSaleAsync( diff --git a/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyDiscountWalletCharge/VerifyDiscountWalletChargeCommandHandler.cs b/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyDiscountWalletCharge/VerifyDiscountWalletChargeCommandHandler.cs index c1989b7..3dac1d8 100644 --- a/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyDiscountWalletCharge/VerifyDiscountWalletChargeCommandHandler.cs +++ b/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyDiscountWalletCharge/VerifyDiscountWalletChargeCommandHandler.cs @@ -114,6 +114,21 @@ public class VerifyDiscountWalletChargeCommandHandler _context.Transactions.Add(transaction); await _context.SaveChangesAsync(cancellationToken); + // ثبت لاگ تغییرات کیف پول (بعد از ایجاد Transaction برای داشتن TransactionId) + _context.UserWalletChangeLogs.Add(new Domain.Entities.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.SaveChangesAsync(cancellationToken); + // لینک PaymentTransaction به Transaction داخلی if (paymentTx != null) {