fix bug in wallet transaction

This commit is contained in:
2025-12-29 11:07:31 +03:30
parent b9ef611a76
commit f6e52d4522
2 changed files with 18 additions and 8 deletions
@@ -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<void> {
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(