chore: add direct payment for invoice

This commit is contained in:
Mahyargdz
2025-05-14 16:31:05 +03:30
parent eae5981ea1
commit 2ac29d1a91
9 changed files with 82 additions and 19 deletions
@@ -481,6 +481,33 @@ export class InvoicesService {
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
let shouldCharge = false;
let remainingToCharge = 0;
if (!isAdmin) {
if (invoice.status === InvoiceStatus.WAIT_PAYMENT) {
const queryRunner = this.dataSource.createQueryRunner();
try {
await queryRunner.connect();
const userWallet = await this.walletsService.getWalletByUserId(userId, queryRunner);
const invoicePrice = new Decimal(invoice.totalPrice);
const walletBalance = new Decimal(userWallet.balance);
if (walletBalance.lessThan(invoicePrice)) {
remainingToCharge = invoicePrice.sub(walletBalance).round().toNumber();
shouldCharge = true;
} else {
remainingToCharge = 0;
}
} finally {
await queryRunner.release();
}
}
return {
invoice,
shouldCharge,
remainingToCharge,
};
}
return {
invoice,
};