From 38101466512f79c8c41b22c0af2cbd813dda550f Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Thu, 6 Aug 2026 23:59:36 +0330 Subject: [PATCH] feat(wallet): enforce club membership validation for wallet charge operations - Added club membership inclusion in user retrieval for ChargeCreditWallet and VerifyCreditWalletCharge command handlers. - Implemented validation to ensure users have an active club membership before allowing wallet charges, throwing a BadRequestException if not. - Updated UserWalletService to handle BadRequestException and return appropriate error messages in responses. --- .../ChargeCreditWalletCommandHandler.cs | 7 +++++++ .../VerifyCreditWalletChargeCommandHandler.cs | 7 +++++++ src/CMSMicroservice.WebApi/Services/UserWalletService.cs | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/src/CMSMicroservice.Application/WalletCQ/Commands/ChargeCreditWallet/ChargeCreditWalletCommandHandler.cs b/src/CMSMicroservice.Application/WalletCQ/Commands/ChargeCreditWallet/ChargeCreditWalletCommandHandler.cs index d983cb7..03c231a 100644 --- a/src/CMSMicroservice.Application/WalletCQ/Commands/ChargeCreditWallet/ChargeCreditWalletCommandHandler.cs +++ b/src/CMSMicroservice.Application/WalletCQ/Commands/ChargeCreditWallet/ChargeCreditWalletCommandHandler.cs @@ -55,9 +55,16 @@ public class ChargeCreditWalletCommandHandler request.Amount); var user = await _context.Users + .Include(u => u.ClubMembership) .FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken) ?? throw new NotFoundException(nameof(User), request.UserId); + if (user.ClubMembership?.IsActive != true) + { + throw new BadRequestException( + "برای شارژ کیف پول اصلی ابتدا باید پکیج را خریداری کرده و قرارداد باشگاه مشتریان را امضا کنید."); + } + var wallet = await _context.UserWallets .FirstOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken) ?? throw new NotFoundException("کیف پول کاربر یافت نشد"); diff --git a/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyCreditWalletCharge/VerifyCreditWalletChargeCommandHandler.cs b/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyCreditWalletCharge/VerifyCreditWalletChargeCommandHandler.cs index c3985a5..9986a5a 100644 --- a/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyCreditWalletCharge/VerifyCreditWalletChargeCommandHandler.cs +++ b/src/CMSMicroservice.Application/WalletCQ/Commands/VerifyCreditWalletCharge/VerifyCreditWalletChargeCommandHandler.cs @@ -51,9 +51,16 @@ public class VerifyCreditWalletChargeCommandHandler request.Authority); var user = await _context.Users + .Include(u => u.ClubMembership) .FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken) ?? throw new NotFoundException(nameof(User), request.UserId); + if (user.ClubMembership?.IsActive != true) + { + throw new BadRequestException( + "برای شارژ کیف پول اصلی ابتدا باید پکیج را خریداری کرده و قرارداد باشگاه مشتریان را امضا کنید."); + } + var paymentTx = await _context.PaymentTransactions .FirstOrDefaultAsync(pt => pt.Authority == request.Authority, cancellationToken); diff --git a/src/CMSMicroservice.WebApi/Services/UserWalletService.cs b/src/CMSMicroservice.WebApi/Services/UserWalletService.cs index e8b8ff4..fa60522 100644 --- a/src/CMSMicroservice.WebApi/Services/UserWalletService.cs +++ b/src/CMSMicroservice.WebApi/Services/UserWalletService.cs @@ -277,6 +277,14 @@ public class UserWalletService : UserWalletContract.UserWalletContractBase ErrorMessage = ex.Message }; } + catch (BadRequestException ex) + { + return new InitiateCreditChargeResponse + { + IsSuccess = false, + ErrorMessage = ex.Message + }; + } } // ============= Wallet Verify Methods =============