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 { 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(
+4 -1
View File
@@ -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(