From f6e52d4522d1e5ba6f26049ad20ac8718c6ac5f2 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Mon, 29 Dec 2025 11:07:31 +0330 Subject: [PATCH] fix bug in wallet transaction --- src/modules/payments/services/payments.service.ts | 13 ++++++++++++- src/modules/users/providers/user.service.ts | 13 ++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/modules/payments/services/payments.service.ts b/src/modules/payments/services/payments.service.ts index d5f0e06..35673a9 100644 --- a/src/modules/payments/services/payments.service.ts +++ b/src/modules/payments/services/payments.service.ts @@ -12,6 +12,8 @@ import { ChartPeriodEnum, PaymentChartDto } from '../dto/payment-chart.dto'; import { PaymentMessage, OrderMessage } from 'src/common/enums/message.enum'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { onlinePaymentSucceedEvent, paymentSucceedEvent } from '../events/payment.events'; +import { WalletTransaction } from 'src/modules/users/entities/wallet-transaction.entity'; +import { WalletTransactionReason, WalletTransactionType } from 'src/modules/users/interface/wallet'; @Injectable() export class PaymentsService { @@ -92,6 +94,7 @@ export class PaymentsService { }); } + // TODO clean this code and refactor it private async handleWalletPayment(ctx: OrderPaymentContext): Promise { await this.em.transactional(async em => { const order = await em.findOne(Order, { id: ctx.order.id }, { populate: ['user', 'restaurant'] }); @@ -123,11 +126,19 @@ export class PaymentsService { gateway: null, }); + const walletTransaction = this.em.create(WalletTransaction, { + userWallet: wallet, + amount: ctx.amount, + type: WalletTransactionType.DEBIT, + reason: WalletTransactionReason.ORDER_PAYMENT, + balance: wallet.wallet - ctx.amount, + }); + payment.status = PaymentStatusEnum.Paid; payment.paidAt = new Date(); order.status = OrderStatus.PAID; - em.persist([wallet, payment, order]); + em.persist([wallet, payment, order, walletTransaction]); await em.flush(); }); this.eventEmitter.emit( diff --git a/src/modules/users/providers/user.service.ts b/src/modules/users/providers/user.service.ts index 4662cc8..bb8509c 100644 --- a/src/modules/users/providers/user.service.ts +++ b/src/modules/users/providers/user.service.ts @@ -18,6 +18,7 @@ import { UserWallet } from '../entities/user-wallet.entity'; import { UserWalletRepository } from '../repositories/user-wallet.repository'; import { WalletService } from './wallet.service'; import { WalletTransactionReason, WalletTransactionType } from '../interface/wallet'; +import { WalletTransaction } from '../entities/wallet-transaction.entity'; @Injectable() export class UserService { @@ -322,12 +323,6 @@ export class UserService { throw new BadRequestException('Points to convert must be greater than 0.'); } - if (userWallet.points < pointsToConvert) { - throw new BadRequestException( - `Insufficient points. Available: ${userWallet.points}, Requested: ${pointsToConvert}.`, - ); - } - if (!restaurant.score) { throw new BadRequestException('Restaurant score not found.'); } @@ -341,15 +336,19 @@ export class UserService { (Number(pointsToConvert) * Number(restaurant.score.purchaseAmount)) / Number(restaurant.score.purchaseScore); // Convert points to wallet (1:1 ratio - can be made configurable) - this.walletService.createTransaction(em, userId, restId, { + const walletTransaction = em.create(WalletTransaction, { + userWallet: userWallet, amount: walletIncreaseAmount, type: WalletTransactionType.CREDIT, reason: WalletTransactionReason.CONVERT_SCORE_TO_WALLET, + balance: userWallet.wallet + walletIncreaseAmount, }); // Update user's wallet and points userWallet.wallet = (userWallet.wallet || 0) + walletIncreaseAmount; userWallet.points = (userWallet.points || 0) - pointsToConvert; + em.persist([userWallet, walletTransaction]); + await em.flush(); return user;