This commit is contained in:
2026-02-01 16:19:13 +03:30
parent f1ff3250a9
commit d5d31d528d
2 changed files with 8 additions and 9 deletions
@@ -18,6 +18,7 @@ import { Payment } from '../entities/payment.entity';
import { FindPaymentsDto } from '../dto/find-payments.dto'; import { FindPaymentsDto } from '../dto/find-payments.dto';
import { Admin } from 'src/modules/admin/entities/admin.entity'; import { Admin } from 'src/modules/admin/entities/admin.entity';
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository'; 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) const order = await this.orderService.findOrderOrFail(orderId)
if (!order) {
throw new NotFoundException(OrderMessage.NOT_FOUND);
}
if (!order.balance) { if (!order.balance) {
throw new NotFoundException('Balance is not set'); throw new NotFoundException('Balance is not set');
} }
@@ -128,7 +125,7 @@ export class PaymentService {
const newBalance = remainedCredit - amount; const newBalance = remainedCredit - amount;
// 2. Create payment record // 2. Create payment record
const payment = this.paymentRepository.create({ const payment = em.create(Payment, {
creator: admin, creator: admin,
amount: ctx.amount, amount: ctx.amount,
method: PaymentMethodEnum.Credit, method: PaymentMethodEnum.Credit,
@@ -138,7 +135,7 @@ export class PaymentService {
paidAt: new Date(), paidAt: new Date(),
}); });
// 3. Create Credit transaction record // 3. Create Credit transaction record
const newCreditTransaction = this.creditRepository.create({ const newCreditTransaction = em.create(CreditTransaction, {
user: order.user, user: order.user,
amount, amount,
type: CreditTransactionType.WITHDRAW, type: CreditTransactionType.WITHDRAW,
@@ -146,8 +143,7 @@ export class PaymentService {
orderId: order.id, orderId: order.id,
}); });
em.persist([payment, newCreditTransaction]); await em.persistAndFlush([payment, newCreditTransaction]);
await em.flush();
}); });
this.eventEmitter.emit( this.eventEmitter.emit(
@@ -17,10 +17,13 @@ export class CreditService {
) { } ) { }
async getCurrentCredit(userId: string,): Promise<number> { async getCurrentCredit(userId: string,): Promise<number> {
const user = await this.userRepository.findOne(userId) const user = await this.userRepository.findOne(userId)
if (!user) { if (!user) {
throw new BadRequestException("User not found") throw new BadRequestException("User not found")
} }
const maxCredit = user.maxCredit const maxCredit = user.maxCredit
const latestTransaction = await this.creditRepository.findOne( const latestTransaction = await this.creditRepository.findOne(