From b24056f53a6f23766d6c41af2c5bc0cc9cf2291c Mon Sep 17 00:00:00 2001 From: Mohamad Ihsan Date: Tue, 27 Sep 2022 14:39:27 +0700 Subject: [PATCH] add queue payment recon --- application/helpers/queue.js | 22 + .../paymentrecon/paymentrecon.controller.js | 588 +++++++++--------- .../paymentrecon/paymentrecon.model.js | 6 +- 3 files changed, 321 insertions(+), 295 deletions(-) create mode 100644 application/helpers/queue.js diff --git a/application/helpers/queue.js b/application/helpers/queue.js new file mode 100644 index 0000000..66a01a1 --- /dev/null +++ b/application/helpers/queue.js @@ -0,0 +1,22 @@ +var queueRecon = []; + +const reconReceive = (reservationNumber) => { + queueRecon.push(reservationNumber); + + // console.log(`QUEUE RECON : ${queueRecon}`); + + return queueRecon; +}; + +const reconFinish = (queueRecon) => { + queueRecon.shift(); + + // console.log(`SHIFT QUEUE RECON : ${queueRecon}`); + + return queueRecon; +}; + +module.exports = { + reconReceive, + reconFinish, +}; diff --git a/application/modules/paymentrecon/paymentrecon.controller.js b/application/modules/paymentrecon/paymentrecon.controller.js index 70b43b5..ee8c00c 100644 --- a/application/modules/paymentrecon/paymentrecon.controller.js +++ b/application/modules/paymentrecon/paymentrecon.controller.js @@ -2,12 +2,14 @@ const { param, body, validationResult } = require("express-validator"); const helper = require("../../helpers/helper"); const winston = require("../../helpers/winston.logger"); const model = require("./paymentrecon.model"); +const queue = require("../../helpers/queue"); const moment = require("moment"); const { default: axios } = require("axios"); const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); moment.locale("id"); var maxLoopParameter = 100; +var dataQueue = []; // VALIDATION const validate = (method) => { @@ -104,9 +106,6 @@ const paymentRecon = async (req, res) => { return res.status(200).json(result); } - // time - let now = moment().format("YYYY-MM-DD HH:mm:ss"); - // log debug winston.logger.debug(`${uniqueCode} checking partner...`); @@ -127,296 +126,291 @@ const paymentRecon = async (req, res) => { return res.status(200).json(result); } + dataQueue = queue.reconReceive(reservation_number); + let partnerCode = getPartner.partner_code; let PaymentReconNumber; - let maxLoop = 0; - do { - await _sleep(helper.randomNumberInt()); - // await _sleep(1000); - PaymentReconNumber = await generatePaymentReconNumber(partnerCode, now); - if (maxLoop > maxLoopParameter) { - // create log - winston.logger.error( - `server is busy. please try again.` + var i = 0; + while (dataQueue.length != 0) { + PaymentReconNumber = await generatePaymentReconNumber( + partnerCode, + moment().format("YYYY-MM-DD HH:mm:ss") + ); + if (PaymentReconNumber != "XX") { + queue.reconFinish(dataQueue); + i++; + + // check ref no and amount + let checkReservationNumberAndAmountAlreadyRecon = + await model.checkReservationNumberAndAmountAlreadyRecon( + reservation_number + ); + if (checkReservationNumberAndAmountAlreadyRecon) { + result = { + code: "04", + message: `success. Transaction has been recon at ${checkReservationNumberAndAmountAlreadyRecon.d_payment_recon}`, + data: { + payment_recon_no: + checkReservationNumberAndAmountAlreadyRecon.c_payment_recon_no, + recon_date: + checkReservationNumberAndAmountAlreadyRecon.d_payment_recon, + }, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + // log debug + winston.logger.debug( + `${uniqueCode} checking reservation canceled status...` ); - return { - code: "500", - message: "server is busy. please try again.", - data: {}, + // check reservation status + let checkReservationCanceledStatus = + await model.checkReservationCanceledStatus(reservation_number); + if (checkReservationCanceledStatus) { + result = { + code: "431", + message: "transaction has been canceled", + data: { + payment_recon_no: PaymentReconNumber, + recon_date: moment().format("YYYY-MM-DD HH:mm:ss"), + }, + }; + + // log debug + winston.logger.debug( + `${uniqueCode} inserting payment recon transaction...` + ); + + // insert payment recon transaction + await model.insertPaymentReconTransaction( + date, + moment().format("YYYY-MM-DD HH:mm:ss"), + getPartner.partner_code, + PaymentReconNumber, + bank_ref, + reservation_number, + amount, + "F", + "FAILED", + result.code, + result.message.toUpperCase(), + result.data + ); + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + // log debug + winston.logger.debug( + `${uniqueCode} checking total transaction and amount by reservation number...` + ); + + // check ref no and amount + let check = await model.checkReservationNumberAndAmount( + reservation_number + ); + if (!check) { + result = { + code: "427", + message: "transaction doesn't exists", + data: { + payment_recon_no: PaymentReconNumber, + recon_date: moment().format("YYYY-MM-DD HH:mm:ss"), + }, + }; + + // log debug + winston.logger.debug( + `${uniqueCode} inserting payment recon transaction...` + ); + + // insert payment recon transaction + await model.insertPaymentReconTransaction( + date, + moment().format("YYYY-MM-DD HH:mm:ss"), + getPartner.partner_code, + PaymentReconNumber, + bank_ref, + reservation_number, + amount, + "F", + "FAILED", + result.code, + result.message.toUpperCase(), + result.data + ); + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + let totalTransactions = check.total_transaction; + + if (totalTransactions > 1) { + result = { + code: "424", + message: "Duplicate Ref No.", + data: { + payment_recon_no: PaymentReconNumber, + recon_date: moment().format("YYYY-MM-DD HH:mm:ss"), + }, + }; + + // log debug + winston.logger.debug( + `${uniqueCode} inserting payment recon transaction...` + ); + + // insert payment recon transaction + await model.insertPaymentReconTransaction( + date, + moment().format("YYYY-MM-DD HH:mm:ss"), + getPartner.partner_code, + PaymentReconNumber, + bank_ref, + reservation_number, + amount, + "F", + "FAILED", + result.code, + result.message.toUpperCase(), + result.data + ); + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + let totalAmount = check.total_amount; + + if (parseInt(totalAmount) != parseInt(amount)) { + result = { + code: "425", + message: "Total amount is different.", + data: { + payment_recon_no: PaymentReconNumber, + recon_date: moment().format("YYYY-MM-DD HH:mm:ss"), + }, + }; + + // log debug + winston.logger.debug( + `${uniqueCode} inserting payment recon transaction...` + ); + + // insert payment recon transaction + await model.insertPaymentReconTransaction( + date, + moment().format("YYYY-MM-DD HH:mm:ss"), + getPartner.partner_code, + PaymentReconNumber, + bank_ref, + reservation_number, + amount, + "F", + "FAILED", + result.code, + result.message.toUpperCase(), + result.data + ); + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + let data = { + reservation_number: reservation_number, + payment_recon_no: PaymentReconNumber, + date: date, + time: moment().format("YYYY-MM-DD HH:mm:ss"), }; + + // log debug + winston.logger.debug(`${uniqueCode} updating payment recon number...`); + + let update = await model.updatePaymentReconNo(data); + if (!update) { + result = { + code: "428", + message: + "failed recon. The other transaction with same data is running.", + data: { + payment_recon_no: PaymentReconNumber, + recon_date: moment().format("YYYY-MM-DD HH:mm:ss"), + }, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + // log debug + winston.logger.debug( + `${uniqueCode} inserting payment recon transaction...` + ); + + result = { + code: "00", + message: "success", + data: { + payment_recon_no: PaymentReconNumber, + recon_date: moment().format("YYYY-MM-DD HH:mm:ss"), + }, + }; + + // insert payment recon transaction + await model.insertPaymentReconTransaction( + date, + moment().format("YYYY-MM-DD HH:mm:ss"), + getPartner.partner_code, + PaymentReconNumber, + bank_ref, + reservation_number, + amount, + "S", + "SUCCESS", + result.code, + result.message.toUpperCase(), + result.data + ); + + // log info + winston.logger.info( + `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); } - - maxLoop++; - } while (PaymentReconNumber == "XX"); - - // check ref no and amount - let checkReservationNumberAndAmountAlreadyRecon = - await model.checkReservationNumberAndAmountAlreadyRecon( - reservation_number - ); - if (checkReservationNumberAndAmountAlreadyRecon) { - result = { - code: "04", - message: `success. Transaction has been recon at ${checkReservationNumberAndAmountAlreadyRecon.d_payment_recon}`, - data: { - payment_recon_no: - checkReservationNumberAndAmountAlreadyRecon.c_payment_recon_no, - recon_date: - checkReservationNumberAndAmountAlreadyRecon.d_payment_recon, - }, - }; - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); } - - // log debug - winston.logger.debug( - `${uniqueCode} checking reservation canceled status...` - ); - - // check reservation status - let checkReservationCanceledStatus = - await model.checkReservationCanceledStatus(reservation_number); - if (checkReservationCanceledStatus) { - result = { - code: "431", - message: "transaction has been canceled", - data: { - payment_recon_no: PaymentReconNumber, - recon_date: now, - }, - }; - - // log debug - winston.logger.debug( - `${uniqueCode} inserting payment recon transaction...` - ); - - // insert payment recon transaction - await model.insertPaymentReconTransaction( - date, - now, - getPartner.partner_code, - PaymentReconNumber, - bank_ref, - reservation_number, - amount, - "F", - "FAILED", - result.code, - result.message.toUpperCase(), - result.data - ); - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); - } - - // log debug - winston.logger.debug( - `${uniqueCode} checking total transaction and amount by reservation number...` - ); - - // check ref no and amount - let check = await model.checkReservationNumberAndAmount(reservation_number); - if (!check) { - result = { - code: "427", - message: "transaction doesn't exists", - data: { - payment_recon_no: PaymentReconNumber, - recon_date: now, - }, - }; - - // log debug - winston.logger.debug( - `${uniqueCode} inserting payment recon transaction...` - ); - - // insert payment recon transaction - await model.insertPaymentReconTransaction( - date, - now, - getPartner.partner_code, - PaymentReconNumber, - bank_ref, - reservation_number, - amount, - "F", - "FAILED", - result.code, - result.message.toUpperCase(), - result.data - ); - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); - } - - let totalTransactions = check.total_transaction; - - if (totalTransactions > 1) { - result = { - code: "424", - message: "Duplicate Ref No.", - data: { - payment_recon_no: PaymentReconNumber, - recon_date: now, - }, - }; - - // log debug - winston.logger.debug( - `${uniqueCode} inserting payment recon transaction...` - ); - - // insert payment recon transaction - await model.insertPaymentReconTransaction( - date, - now, - getPartner.partner_code, - PaymentReconNumber, - bank_ref, - reservation_number, - amount, - "F", - "FAILED", - result.code, - result.message.toUpperCase(), - result.data - ); - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); - } - - let totalAmount = check.total_amount; - - if (parseInt(totalAmount) != parseInt(amount)) { - result = { - code: "425", - message: "Total amount is different.", - data: { - payment_recon_no: PaymentReconNumber, - recon_date: now, - }, - }; - - // log debug - winston.logger.debug( - `${uniqueCode} inserting payment recon transaction...` - ); - - // insert payment recon transaction - await model.insertPaymentReconTransaction( - date, - now, - getPartner.partner_code, - PaymentReconNumber, - bank_ref, - reservation_number, - amount, - "F", - "FAILED", - result.code, - result.message.toUpperCase(), - result.data - ); - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); - } - - let data = { - reservation_number: reservation_number, - payment_recon_no: PaymentReconNumber, - date: date, - time: now, - }; - - // log debug - winston.logger.debug(`${uniqueCode} updating payment recon number...`); - - let update = await model.updatePaymentReconNo(data); - if (!update) { - result = { - code: "428", - message: - "failed recon. The other transaction with same data is running.", - data: { - payment_recon_no: PaymentReconNumber, - recon_date: now, - }, - }; - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); - } - - // log debug - winston.logger.debug( - `${uniqueCode} inserting payment recon transaction...` - ); - - result = { - code: "00", - message: "success", - data: { - payment_recon_no: PaymentReconNumber, - recon_date: now, - }, - }; - - // insert payment recon transaction - await model.insertPaymentReconTransaction( - date, - now, - getPartner.partner_code, - PaymentReconNumber, - bank_ref, - reservation_number, - amount, - "S", - "SUCCESS", - result.code, - result.message.toUpperCase(), - result.data - ); - - // log info - winston.logger.info( - `${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}` - ); - - return res.status(200).json(result); } catch (error) { // create log winston.logger.error( @@ -499,9 +493,7 @@ const paymentReconSemiTopupOnline = async (req, res) => { if (maxLoop > maxLoopParameter) { // create log - winston.logger.error( - `server is busy. please try again.` - ); + winston.logger.error(`server is busy. please try again.`); return { code: "500", @@ -908,6 +900,16 @@ const paymentReconToCMS = async (req, res) => { amount: "", }; + if (recon.length < 1) { + let result = { + code: "01", + message: "no data sent to payment recon.", + data: {}, + }; + + return res.status(200).json(result); + } + recon.forEach((element) => { bodyParameters = { date: date, @@ -916,11 +918,11 @@ const paymentReconToCMS = async (req, res) => { amount: element.i_reservation_amount, }; - axios.post( - "http://localhost:4000/api/payment-recon", - bodyParameters, - config - ); + axios + .post("http://localhost:4000/api/payment-recon", bodyParameters, config) + .then((response) => { + console.log(`response: ${JSON.stringify(response.data)}`); + }); }); let result = { diff --git a/application/modules/paymentrecon/paymentrecon.model.js b/application/modules/paymentrecon/paymentrecon.model.js index 868bff1..7fd8e9b 100644 --- a/application/modules/paymentrecon/paymentrecon.model.js +++ b/application/modules/paymentrecon/paymentrecon.model.js @@ -147,8 +147,10 @@ const getLastCounterPaymentReconNo = (partnerCode, reconDate) => { const getReservationToRecon = (date) => { let result = knex("trx.t_d_reservation_balance") .select("c_reservation_no", "i_reservation_amount") - .whereNull('c_payment_recon_no') - .whereRaw("??::date = ?", ["d_reservation", date]); + .whereNull("d_cancel") + .whereNull("c_payment_recon_no") + .whereRaw("??::date = ?", ["d_reservation", date]) + .orderBy("c_reservation_no", "asc"); return result; };