bug
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -17,10 +17,13 @@ export class CreditService {
|
||||
) { }
|
||||
|
||||
async getCurrentCredit(userId: string,): Promise<number> {
|
||||
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user