Feat: add try catch at callback metro

This commit is contained in:
mfadiln2018 2022-08-09 20:31:47 +07:00
parent 5ec93b43aa
commit 3603da8977

View File

@ -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 { TransactionService } from './transaction.service';
import { FastifyRequest } from 'fastify'; import { FastifyRequest } from 'fastify';
import { Public } from '../auth/public.decorator'; import { Public } from '../auth/public.decorator';
import { EntityNotFoundError } from 'typeorm';
@Controller({ @Controller({
path: 'ppob_callback', path: 'ppob_callback',
@ -20,15 +28,14 @@ export class PpobCallbackController {
if (response['statuscode'] == 2) { if (response['statuscode'] == 2) {
//TODO: UPDATE GAGAL //TODO: UPDATE GAGAL
await this.transactionService.checkCallbackOrderFailed( await this.transactionService.checkCallbackOrderFailed(
response['clientid'], response['clientid'],
response, response,
); );
} else { } else {
//TODO: UPDATE BERHASIL //TODO: UPDATE BERHASIL
await this.transactionService.checkCallbackOrderSuccess( await this.transactionService.checkCallbackOrderSuccess(
response['clientid'], response['clientid'],
response, response,
); );
} }
} }
@ -36,60 +43,64 @@ export class PpobCallbackController {
@Public() @Public()
@Get('/metro') @Get('/metro')
async getMetro(@Req() request: FastifyRequest) { async getMetro(@Req() request: FastifyRequest) {
const response = request.query; try {
const response = request.query;
if (response['message'].toLowerCase().includes('cek tagihan')) { if (response['message'].toLowerCase().includes('cek tagihan')) {
if (response['status'] != 20) { if (response['status'] != 20) {
//TODO: UPDATE GAGAL //TODO: UPDATE GAGAL
await this.transactionService.updateBill( await this.transactionService.updateBill(
response['refid'], response['refid'],
null, null,
null, null,
false, false,
response['message'], response['message'],
); );
return { return {
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
message: 'success', message: 'success',
}; };
} }
const splitMessage = response['message'].split('"'); const splitMessage = response['message'].split('"');
//TODO: UPDATE BERHASIL
await this.transactionService.updateBill( //TODO: UPDATE BERHASIL
await this.transactionService.updateBill(
response['refid'], response['refid'],
Number(splitMessage[21].replace(/^\D+/g, '')), Number(splitMessage[21].replace(/^\D+/g, '')),
Number(splitMessage[17].replace(/^\D+/g, '')), Number(splitMessage[17].replace(/^\D+/g, '')),
true, true,
response['message'], response['message'],
); );
// //
} else { } else {
console.log('statusapani', response['status']); console.log('statusapani', response['status']);
console.log('statusapani2', response.toString()); console.log('statusapani2', response.toString());
if (response['status'].toString() != '20') {
console.log("masukkesiniga", "msk") if (response['status'].toString() != '20') {
//TODO: UPDATE GAGAL console.log('masukkesiniga', 'msk');
const updateTransaction =
//TODO: UPDATE GAGAL
const updateTransaction =
await this.transactionService.callbackOrderFailed( await this.transactionService.callbackOrderFailed(
response['refid'], response['refid'],
response, response,
); );
return { return {
updateTransaction, updateTransaction,
statusCode: HttpStatus.BAD_REQUEST, statusCode: HttpStatus.BAD_REQUEST,
message: 'failed to proccess', message: 'failed to proccess',
}; };
} else { }
//TODO: UPDATE BERHASIL //TODO: UPDATE BERHASIL
const updateTransaction = const updateTransaction =
await this.transactionService.callbackOrderSuccess( await this.transactionService.callbackOrderSuccess(
response['refid'], response['refid'],
response, response,
); );
return { return {
updateTransaction, updateTransaction,
@ -97,14 +108,28 @@ export class PpobCallbackController {
message: 'success', message: 'success',
}; };
} }
}
this.logger.log({
requestQuery: request.query,
});
return { this.logger.log({
statusCode: HttpStatus.OK, requestQuery: request.query,
message: 'success', });
};
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;
}
}
} }
} }