From 1fcc7ca8ad27ae06791f54e1d1622c855ae8ce77 Mon Sep 17 00:00:00 2001 From: Fadli Date: Wed, 29 Jun 2022 18:10:05 +0700 Subject: [PATCH] -add condition for ppob_callback can't hit 2 times if the transaction is already success or failed (only pending) --- src/transaction/ppob_callback.controller.ts | 18 +--- src/transaction/transaction.service.ts | 106 +++++++++++++------- 2 files changed, 73 insertions(+), 51 deletions(-) diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index a196727..5da6003 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -19,31 +19,17 @@ export class PpobCallbackController { if (response['statuscode'] == 2) { //TODO: UPDATE GAGAL - const updateTransaction = - await this.transactionService.callbackOrderFailed( + await this.transactionService.callbackOrderFailed( response['clientid'], response, ); - - return { - updateTransaction, - statusCode: HttpStatus.BAD_REQUEST, - message: 'failed to proccess', - }; } //TODO: UPDATE BERHASIL - const updateTransaction = - await this.transactionService.callbackOrderSuccess( + await this.transactionService.callbackOrderSuccess( response['clientid'], response, ); - - return { - updateTransaction, - statusCode: HttpStatus.OK, - message: 'success', - }; } @Public() diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 2c3abb4..a28fbfb 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -1064,6 +1064,17 @@ export class TransactionService { relations: ['product_price'], }); + if (dataTransaction.status == statusTransaction.FAILED) { + return { + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to update, the transaction already failed', + }; + } else if (dataTransaction.status == statusTransaction.SUCCESS) { + return { + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to update, the transaction already success', + }; + } else { const dataMsg = callback.msg; const failedReason = dataMsg.split('.'); @@ -1074,29 +1085,29 @@ export class TransactionService { const userData = await this.userService.findExist(dataTransaction.user); const product_price = await this.productHistoryPriceService.findById( - dataTransaction.product_price.id, + dataTransaction.product_price.id, ); const product = await this.productService.findOneById( - product_price.product.id, + product_price.product.id, ); //GET COA const coaAccount = await this.coaService.findByUser( - userData.id, - coaType.WALLET, + userData.id, + coaType.WALLET, ); const coaInventory = await this.coaService.findByName( - `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, + `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, ); const coaCostOfSales = await this.coaService.findByName( - `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, + `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, ); const coaSales = await this.coaService.findByName( - `${coaType[coaType.SALES]}-SYSTEM`, + `${coaType[coaType.SALES]}-SYSTEM`, ); try { @@ -1135,16 +1146,23 @@ export class TransactionService { if (userData.partner) { const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah gagal.`; this.callbackToPartner( - userData.id, - message, - dataTransaction.partner_trx_id, - dataTransaction.amount, - product.code, - dataTransaction.destination, - '-', - 'gagal', + userData.id, + message, + dataTransaction.partner_trx_id, + dataTransaction.amount, + product.code, + dataTransaction.destination, + '-', + 'gagal', ); } + + return { + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to proccess', + }; + + } } async callbackOrderSuccess(supplier_trx_id: string, callback: any) { @@ -1155,6 +1173,19 @@ export class TransactionService { relations: ['product_price'], }); + + if (dataTransaction.status == statusTransaction.FAILED) { + return { + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to update, the transaction already failed', + }; + } else if (dataTransaction.status == statusTransaction.SUCCESS) { + return { + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to update, the transaction already success', + }; + } else { + dataTransaction.status = statusTransaction.SUCCESS; dataTransaction.seri_number = callback['sn']; dataTransaction.callback_json = callback; @@ -1164,33 +1195,33 @@ export class TransactionService { let supervisorData = []; const product_price = await this.productHistoryPriceService.findById( - dataTransaction.product_price.id, + dataTransaction.product_price.id, ); const product = await this.productService.findOneById( - product_price.product.id, + product_price.product.id, ); let profit = product_price.mark_up_price; //GET COA const coaExpense = await this.coaService.findByName( - `${coaType[coaType.EXPENSE]}-SYSTEM`, + `${coaType[coaType.EXPENSE]}-SYSTEM`, ); if (userData.partner != null) { //GET SALES supervisorData = await this.calculateCommission( - supervisorData, - profit, - userData, + supervisorData, + profit, + userData, ); profit = supervisorData - .map((item) => { - return item.credit; - }) - .reduce((prev, curr) => { - return prev + curr; - }, 0); + .map((item) => { + return item.credit; + }) + .reduce((prev, curr) => { + return prev + curr; + }, 0); supervisorData = supervisorData.concat([ { @@ -1219,16 +1250,21 @@ export class TransactionService { if (userData.partner) { const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah berhasil.`; this.callbackToPartner( - userData.id, - message, - dataTransaction.partner_trx_id, - dataTransaction.amount, - product.code, - dataTransaction.destination, - dataTransaction.seri_number, - 'berhasil', + userData.id, + message, + dataTransaction.partner_trx_id, + dataTransaction.amount, + product.code, + dataTransaction.destination, + dataTransaction.seri_number, + 'berhasil', ); } + return { + statusCode: HttpStatus.OK, + message: 'success', + }; + } } async callbackToPartner(