From 691035c394b70b7517c997dbd72bba813b270fca Mon Sep 17 00:00:00 2001 From: ilham Date: Mon, 20 Dec 2021 12:19:10 +0700 Subject: [PATCH] add: add validation if saldo not enough --- src/transaction/transaction.service.ts | 32 ++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 5204d80..1d6ae06 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -144,6 +144,21 @@ export class TransactionService { } // GET COA + const coaBudget = await this.coaService.findByName( + `${coaType[coaType.BUDGET]}-${supplier.code}`, + ); + if (coaBudget.amount < distributeTransactionDto.amount) { + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because saldo not enough`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + const coaContraBudget = await this.coaService.findByName( + `${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`, + ); const coaAR = await this.coaService.findByTwoUser( distributeTransactionDto.destination, currentUser.userId, @@ -153,13 +168,6 @@ export class TransactionService { distributeTransactionDto.destination, coaType.WALLET, ); - const coaBudget = await this.coaService.findByName( - `${coaType[coaType.BUDGET]}-${supplier.code}`, - ); - - const coaContraBudget = await this.coaService.findByName( - `${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`, - ); await this.connection.transaction(async (manager) => { //INSERT TRANSACTION @@ -238,6 +246,16 @@ export class TransactionService { coaType.ACCOUNT_RECEIVABLE, ); + if (coaSenderWallet.amount < distributeTransactionDto.amount) { + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because saldo not enough`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + await this.connection.transaction(async (manager) => { const transactionData = new Transactions();