From 7fe73c0dc42aa54a8cbef97560fd9d074671f79f Mon Sep 17 00:00:00 2001 From: Fadli Date: Wed, 29 Jun 2022 22:46:28 +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 | 53 +++---- src/transaction/transaction.service.ts | 158 ++++++++++++++++---- 2 files changed, 155 insertions(+), 56 deletions(-) diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index 5da6003..90c5eae 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -19,17 +19,18 @@ export class PpobCallbackController { if (response['statuscode'] == 2) { //TODO: UPDATE GAGAL - await this.transactionService.callbackOrderFailed( - response['clientid'], - response, - ); + await this.transactionService.checkCallbackOrderFailed( + response['clientid'], + response, + ); } //TODO: UPDATE BERHASIL - await this.transactionService.callbackOrderSuccess( - response['clientid'], - response, - ); + await this.transactionService.checkCallbackOrderSuccess( + response['clientid'], + response, + ); + } @Public() @@ -41,11 +42,11 @@ export class PpobCallbackController { if (response['status'] != 20) { //TODO: UPDATE GAGAL await this.transactionService.updateBill( - response['refid'], - null, - null, - false, - response['message'], + response['refid'], + null, + null, + false, + response['message'], ); return { @@ -57,21 +58,21 @@ export class PpobCallbackController { const splitMessage = response['message'].split('"'); //TODO: UPDATE BERHASIL await this.transactionService.updateBill( - response['refid'], - Number(splitMessage[21].replace(/^\D+/g, '')), - Number(splitMessage[17].replace(/^\D+/g, '')), - true, - response['message'], + response['refid'], + Number(splitMessage[21].replace(/^\D+/g, '')), + Number(splitMessage[17].replace(/^\D+/g, '')), + true, + response['message'], ); // } else { if (response['status'].toString() != '20') { //TODO: UPDATE GAGAL const updateTransaction = - await this.transactionService.callbackOrderFailed( - response['refid'], - response, - ); + await this.transactionService.callbackOrderFailed( + response['refid'], + response, + ); return { updateTransaction, @@ -82,10 +83,10 @@ export class PpobCallbackController { //TODO: UPDATE BERHASIL const updateTransaction = - await this.transactionService.callbackOrderSuccess( - response['refid'], - response, - ); + await this.transactionService.callbackOrderSuccess( + response['refid'], + response, + ); } this.logger.log({ diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index e33c081..4059304 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -1,35 +1,30 @@ -import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; -import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; -import { OrderTransactionDto } from './dto/order-transaction.dto'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Transactions } from './entities/transactions.entity'; -import { Between, Connection, EntityNotFoundError, In, Repository } from 'typeorm'; -import { COA } from './entities/coa.entity'; -import { TransactionJournal } from './entities/transaction-journal.entity'; -import { CoaService } from './coa.service'; +import {HttpException, HttpStatus, Injectable, Logger} from '@nestjs/common'; +import {DistributeTransactionDto} from './dto/distribute-transaction.dto'; +import {OrderTransactionDto} from './dto/order-transaction.dto'; +import {InjectRepository} from '@nestjs/typeorm'; +import {Transactions} from './entities/transactions.entity'; +import {Between, Connection, EntityNotFoundError, In, Repository} from 'typeorm'; +import {COA} from './entities/coa.entity'; +import {TransactionJournal} from './entities/transaction-journal.entity'; +import {CoaService} from './coa.service'; import * as uuid from 'uuid'; -import { uniq } from 'lodash'; -import { Decimal } from 'decimal.js'; -import { - balanceType, - coaType, - statusTransaction, - typeTransaction, -} from '../helper/enum-list'; -import { ProductService } from '../product/product.service'; -import { CreateJournalDto } from './dto/create-journal.dto'; -import { UsersService } from 'src/users/users.service'; -import { AddSaldoSupplier } from './dto/add-saldo-supplier.dto'; -import { SupplierService } from '../users/supplier/supplier.service'; -import { ProductHistoryPriceService } from '../product/history-price/history-price.service'; -import { CommissionService } from '../configurable/commission.service'; -import { DepositReturnDto } from './dto/deposit_return.dto'; -import { UserDetail } from '../users/entities/user_detail.entity'; -import { doTransaction } from '../helper/irs-api'; -import { ProductHistoryPrice } from '../product/entities/product-history-price.entity'; +import {uniq} from 'lodash'; +import {Decimal} from 'decimal.js'; +import {balanceType, coaType, statusTransaction, typeTransaction,} from '../helper/enum-list'; +import {ProductService} from '../product/product.service'; +import {CreateJournalDto} from './dto/create-journal.dto'; +import {UsersService} from 'src/users/users.service'; +import {AddSaldoSupplier} from './dto/add-saldo-supplier.dto'; +import {SupplierService} from '../users/supplier/supplier.service'; +import {ProductHistoryPriceService} from '../product/history-price/history-price.service'; +import {CommissionService} from '../configurable/commission.service'; +import {DepositReturnDto} from './dto/deposit_return.dto'; +import {UserDetail} from '../users/entities/user_detail.entity'; +import {doTransaction} from '../helper/irs-api'; +import {ProductHistoryPrice} from '../product/entities/product-history-price.entity'; import axios from 'axios'; -import { CheckBillHistory } from './entities/check-bill-history.entity'; -import { CallbackPartner } from './entities/callback-partner.entity'; +import {CheckBillHistory} from './entities/check-bill-history.entity'; +import {CallbackPartner} from './entities/callback-partner.entity'; @Injectable() export class TransactionService { @@ -1059,6 +1054,109 @@ export class TransactionService { return transactionData; } + async checkCallbackOrderFailed(supplier_trx_id: string, callback: any) { + + const transactionData = await this.findDataTransactionBySupplierTrxId( + supplier_trx_id + ); + + if (transactionData.status == statusTransaction.FAILED) { + throw new HttpException( + { + statusCode: HttpStatus.BAD_REQUEST, + error: 'failed to update, the transaction already failed', + }, + HttpStatus.BAD_REQUEST, + ); + } else if (transactionData.status == statusTransaction.SUCCESS) { + throw new HttpException( + { + statusCode: HttpStatus.BAD_REQUEST, + error: 'failed to update, the transaction already success', + }, + HttpStatus.BAD_REQUEST, + ); + } else { + + const updateTransaction = + await this.callbackOrderFailed( + supplier_trx_id, + callback, + ); + + return { + updateTransaction, + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to proccess', + }; + + } + + } + + async checkCallbackOrderSuccess(supplier_trx_id: string, callback: any) { + + const transactionData = await this.findDataTransactionBySupplierTrxId( + supplier_trx_id + ); + + if (transactionData.status == statusTransaction.FAILED) { + throw new HttpException( + { + statusCode: HttpStatus.BAD_REQUEST, + error: 'failed to update, the transaction already failed', + }, + HttpStatus.BAD_REQUEST, + ); + } else if (transactionData.status == statusTransaction.SUCCESS) { + throw new HttpException( + { + statusCode: HttpStatus.BAD_REQUEST, + error: 'failed to update, the transaction already success', + }, + HttpStatus.BAD_REQUEST, + ); + } else { + + const updateTransaction = + await this.callbackOrderSuccess( + supplier_trx_id, + callback, + ); + + return { + updateTransaction, + statusCode: HttpStatus.OK, + message: 'success', + }; + + } + + } + + async findDataTransactionBySupplierTrxId(supplier_trx_id: string) { + try { + return await this.transactionRepository.findOneOrFail({ + where: { + supplier_trx_id: supplier_trx_id, + }, + relations: ['product_price'], + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'data not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } + async callbackOrderFailed(supplier_trx_id: string, callback: any) { const dataTransaction = await this.transactionRepository.findOne({ where: {