rerfactor user wallet ans user points
This commit is contained in:
@@ -5,14 +5,13 @@ import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { Order } from '../../orders/entities/order.entity';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { GatewayManager } from '../gateways/gateway.manager';
|
||||
import { UserWallet } from 'src/modules/users/entities/user-wallet.entity';
|
||||
import { WalletTransaction } from 'src/modules/users/entities/wallet-transaction.entity';
|
||||
import { OrderPaymentContext } from '../interface/payment';
|
||||
import { OrderStatus } from 'src/modules/orders/interface/order.interface';
|
||||
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()
|
||||
@@ -104,20 +103,23 @@ export class PaymentsService {
|
||||
return;
|
||||
}
|
||||
|
||||
const wallet = await em.findOne(UserWallet, {
|
||||
const walletTransaction = await em.findOne(WalletTransaction, {
|
||||
user: { id: order.user.id },
|
||||
restaurant: { id: order.restaurant.id },
|
||||
}, {
|
||||
orderBy: { createdAt: 'DESC' }
|
||||
});
|
||||
|
||||
if (!wallet) {
|
||||
if (!walletTransaction) {
|
||||
throw new NotFoundException('User wallet not found');
|
||||
}
|
||||
|
||||
if (wallet.wallet < ctx.amount) {
|
||||
if (walletTransaction.balance < ctx.amount) {
|
||||
throw new BadRequestException('Insufficient wallet balance');
|
||||
}
|
||||
|
||||
wallet.wallet -= ctx.amount;
|
||||
const newBalance = walletTransaction.balance - ctx.amount;
|
||||
|
||||
|
||||
const payment = await this.getOrCreateLatestPendingPayment(order.id, {
|
||||
em,
|
||||
@@ -126,19 +128,20 @@ export class PaymentsService {
|
||||
gateway: null,
|
||||
});
|
||||
|
||||
const walletTransaction = this.em.create(WalletTransaction, {
|
||||
userWallet: wallet,
|
||||
const newWalletTransaction = em.create(WalletTransaction, {
|
||||
user: order.user,
|
||||
restaurant: order.restaurant,
|
||||
amount: ctx.amount,
|
||||
type: WalletTransactionType.DEBIT,
|
||||
reason: WalletTransactionReason.ORDER_PAYMENT,
|
||||
balance: wallet.wallet - ctx.amount,
|
||||
balance: newBalance,
|
||||
});
|
||||
|
||||
payment.status = PaymentStatusEnum.Paid;
|
||||
payment.paidAt = new Date();
|
||||
order.status = OrderStatus.PAID;
|
||||
|
||||
em.persist([wallet, payment, order, walletTransaction]);
|
||||
em.persist([ payment, order, newWalletTransaction]);
|
||||
await em.flush();
|
||||
});
|
||||
this.eventEmitter.emit(
|
||||
|
||||
Reference in New Issue
Block a user