chore: add direct payment for invoice
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user