diff --git a/src/modules/payment/services/payments.service.ts b/src/modules/payment/services/payments.service.ts index 4df5b2e..d59c951 100644 --- a/src/modules/payment/services/payments.service.ts +++ b/src/modules/payment/services/payments.service.ts @@ -18,6 +18,7 @@ import { Payment } from '../entities/payment.entity'; import { FindPaymentsDto } from '../dto/find-payments.dto'; import { Admin } from 'src/modules/admin/entities/admin.entity'; import { AdminRepository } from 'src/modules/admin/repositories/admin.repository'; +import { CreditTransaction } from 'src/modules/user/entities/credit-transaction.entity'; @@ -63,10 +64,6 @@ export class PaymentService { const order = await this.orderService.findOrderOrFail(orderId) - if (!order) { - throw new NotFoundException(OrderMessage.NOT_FOUND); - } - if (!order.balance) { throw new NotFoundException('Balance is not set'); } @@ -128,7 +125,7 @@ export class PaymentService { const newBalance = remainedCredit - amount; // 2. Create payment record - const payment = this.paymentRepository.create({ + const payment = em.create(Payment, { creator: admin, amount: ctx.amount, method: PaymentMethodEnum.Credit, @@ -138,7 +135,7 @@ export class PaymentService { paidAt: new Date(), }); // 3. Create Credit transaction record - const newCreditTransaction = this.creditRepository.create({ + const newCreditTransaction = em.create(CreditTransaction, { user: order.user, amount, type: CreditTransactionType.WITHDRAW, @@ -146,8 +143,7 @@ export class PaymentService { orderId: order.id, }); - em.persist([payment, newCreditTransaction]); - await em.flush(); + await em.persistAndFlush([payment, newCreditTransaction]); }); this.eventEmitter.emit( diff --git a/src/modules/user/providers/credit.service.ts b/src/modules/user/providers/credit.service.ts index 7c86599..f1d39dd 100644 --- a/src/modules/user/providers/credit.service.ts +++ b/src/modules/user/providers/credit.service.ts @@ -17,10 +17,13 @@ export class CreditService { ) { } async getCurrentCredit(userId: string,): Promise { + const user = await this.userRepository.findOne(userId) + if (!user) { throw new BadRequestException("User not found") } + const maxCredit = user.maxCredit const latestTransaction = await this.creditRepository.findOne( @@ -32,7 +35,7 @@ export class CreditService { const balance = latestTransaction?.balance || 0 const remained = maxCredit - balance - + return remained } // async getUserWalletTransactions(