fix bug in wallet transaction
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user