From 3603da8977772d394e9e9a4677a047abacdc20c0 Mon Sep 17 00:00:00 2001 From: mfadiln2018 Date: Tue, 9 Aug 2022 20:31:47 +0700 Subject: [PATCH] Feat: add try catch at callback metro --- src/transaction/ppob_callback.controller.ts | 125 ++++++++++++-------- 1 file changed, 75 insertions(+), 50 deletions(-) diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index 983d833..e75846d 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -1,7 +1,15 @@ -import { Controller, Get, HttpStatus, Logger, Req } from '@nestjs/common'; +import { + Controller, + Get, + HttpException, + HttpStatus, + Logger, + Req, +} from '@nestjs/common'; import { TransactionService } from './transaction.service'; import { FastifyRequest } from 'fastify'; import { Public } from '../auth/public.decorator'; +import { EntityNotFoundError } from 'typeorm'; @Controller({ path: 'ppob_callback', @@ -20,15 +28,14 @@ export class PpobCallbackController { if (response['statuscode'] == 2) { //TODO: UPDATE GAGAL await this.transactionService.checkCallbackOrderFailed( - response['clientid'], - response, - ); + response['clientid'], + response, + ); } else { - //TODO: UPDATE BERHASIL await this.transactionService.checkCallbackOrderSuccess( - response['clientid'], - response, + response['clientid'], + response, ); } } @@ -36,60 +43,64 @@ export class PpobCallbackController { @Public() @Get('/metro') async getMetro(@Req() request: FastifyRequest) { - const response = request.query; + try { + const response = request.query; - if (response['message'].toLowerCase().includes('cek tagihan')) { - if (response['status'] != 20) { - //TODO: UPDATE GAGAL - await this.transactionService.updateBill( + if (response['message'].toLowerCase().includes('cek tagihan')) { + if (response['status'] != 20) { + //TODO: UPDATE GAGAL + await this.transactionService.updateBill( response['refid'], null, null, false, response['message'], - ); + ); - return { - statusCode: HttpStatus.OK, - message: 'success', - }; - } + return { + statusCode: HttpStatus.OK, + message: 'success', + }; + } - const splitMessage = response['message'].split('"'); - //TODO: UPDATE BERHASIL - await this.transactionService.updateBill( + 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'], - ); - // - } else { - console.log('statusapani', response['status']); - console.log('statusapani2', response.toString()); - if (response['status'].toString() != '20') { - console.log("masukkesiniga", "msk") - //TODO: UPDATE GAGAL - const updateTransaction = + ); + // + } else { + console.log('statusapani', response['status']); + console.log('statusapani2', response.toString()); + + if (response['status'].toString() != '20') { + console.log('masukkesiniga', 'msk'); + + //TODO: UPDATE GAGAL + const updateTransaction = await this.transactionService.callbackOrderFailed( - response['refid'], - response, + response['refid'], + response, ); - return { - updateTransaction, - statusCode: HttpStatus.BAD_REQUEST, - message: 'failed to proccess', - }; - } else { + return { + updateTransaction, + statusCode: HttpStatus.BAD_REQUEST, + message: 'failed to proccess', + }; + } //TODO: UPDATE BERHASIL const updateTransaction = - await this.transactionService.callbackOrderSuccess( - response['refid'], - response, - ); + await this.transactionService.callbackOrderSuccess( + response['refid'], + response, + ); return { updateTransaction, @@ -97,14 +108,28 @@ export class PpobCallbackController { message: 'success', }; } - } - this.logger.log({ - requestQuery: request.query, - }); - return { - statusCode: HttpStatus.OK, - message: 'success', - }; + this.logger.log({ + requestQuery: request.query, + }); + + return { + statusCode: HttpStatus.OK, + message: 'success', + }; + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Error callback', + message: e + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } } }