From 645f6e01bd6aa374e54183b17df87051d8d15b98 Mon Sep 17 00:00:00 2001 From: Mohamad Ihsan Date: Sun, 12 Jun 2022 21:35:19 +0700 Subject: [PATCH] update --- ...e727b13210eef32232709400762e5fb-audit.json | 6 +- .../loadbalance/loadbalance.controller.js | 314 ++++++++++++------ .../modules/loadbalance/loadbalance.model.js | 36 +- .../reservebalance.controller.js | 33 +- docs/error_code | 1 + 5 files changed, 246 insertions(+), 144 deletions(-) diff --git a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json index 37ab6f0..ad2aa69 100644 --- a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json +++ b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json @@ -6,9 +6,9 @@ "auditLog": ".71c131f2ce727b13210eef32232709400762e5fb-audit.json", "files": [ { - "date": 1654709500492, - "name": "2022-06-09-onlinetopup.log", - "hash": "993e2e8dc6db8ef8e4dab349e223ddd177dbaa784b0545cd1171cae495d250ae" + "date": 1655004752426, + "name": "2022-06-12-onlinetopup.log", + "hash": "984c1c0ba67f63bc2f51c096864e54f9d8695ced7838e6846f18b5ccb2ff7966" } ], "hashType": "sha256" diff --git a/application/modules/loadbalance/loadbalance.controller.js b/application/modules/loadbalance/loadbalance.controller.js index 3a9ec2a..290e34e 100644 --- a/application/modules/loadbalance/loadbalance.controller.js +++ b/application/modules/loadbalance/loadbalance.controller.js @@ -57,7 +57,8 @@ const loadingBalanceRequest = async (req, res) => { uniqueCode = await helper.getUniqueCode(); // param - let { mid, tid, card_number, uid, balance, flag } = req.body; + let { mid, tid, card_number, uid, balance, balance_before, flag } = + req.body; // log info winston.logger.info( @@ -151,23 +152,40 @@ const loadingBalanceRequest = async (req, res) => { await model.getReservationPendingWithFlag(card_number, now); if (getReservationPendingWithFlag.length > 0) { if (lastFlag && lastFlag != flag) { + let reservationNumberPendingWithflag = []; + + getReservationPendingWithFlag.forEach((element) => { + reservationNumberPendingWithflag.push(element.c_reservation_no); + }); + // log debug winston.logger.debug( - `updating reservation and load balance transaction...` + `${uniqueCode} updating reservation and load balance transaction...` ); // update - await updateReservationAndLoadingBalance( - getReservationPendingWithFlag, + let insertUpdate = await updateReservationAndLoadingBalance( + reservationNumberPendingWithflag, mid, - tid + tid, + balance, + balance_before, + uniqueCode ); - result = { - code: "01", - message: "your card number has been cleared successfully", - data: {}, - }; + if (insertUpdate) { + result = { + code: "01", + message: "your card number has been cleared successfully", + data: {}, + }; + } else { + result = { + code: "423", + message: "updated failed", + data: {}, + }; + } // log warn winston.logger.warn( @@ -391,6 +409,7 @@ const loadingBalance = async (req, res) => { write_status, uid, balance, + balance_before, flag, unique_code, } = req.body; @@ -551,7 +570,7 @@ const loadingBalance = async (req, res) => { } // log debug - winston.logger.debug(`${uniqueCode} checking loading balance sequence...`); + winston.logger.debug(`${uniqueCode} getting loading balance sequence...`); // get loading balance const getLoadBalanceSequence = await model.getLoadBalanceSequence( @@ -671,6 +690,7 @@ const loadingBalance = async (req, res) => { // log debug winston.logger.debug(`${uniqueCode} inserting load balance transaction...`); + // insert transaction await model.insertLoadingBalance( data, detail, @@ -683,6 +703,32 @@ const loadingBalance = async (req, res) => { flag ); + if (!write_status) { + // log debug + winston.logger.debug(`${uniqueCode} inserting log for card history...`); + + // log card history + await model.createLoadBalanceLog({ + i_log_name: 89, + d_log: now, + c_card_number: card_number, + c_source: "CMS - HSM", + c_location: null, + m_balance_before: balance_before, + m_amount: amount, + m_balance_after: balance, + j_new_data: { data: data, detail: detail }, + j_old_data: null, + c_note: "SUCCESSFULLY LOADED", + c_status: "S", + d_created: now, + i_create: null, + c_create_username: "HSM", + c_create_fullname: "HARDWARE SECURE MODULE", + c_category: null, + }); + } + result = { code: "00", message: write_status @@ -799,102 +845,170 @@ const inquiryLoadingBalance = async (req, res) => { const updateReservationAndLoadingBalance = async ( reservationNumber, mid, - tid + tid, + balance, + balance_before, + uniqueCode ) => { - // get reservation - let dataReservation = []; - let getLastReservationFlag = await model.getLastReservationFlag( - reservationNumber - ); - if (getLastReservationFlag) { - dataReservation = getLastReservationFlag; - } + try { + // log debug + winston.logger.debug(`${uniqueCode} getting last reservation with flag...`); - // get load balance - let dataLoadingBalance = []; - let getLastLoadingBalanceFlag = await model.getLastLoadingBalanceFlag( - reservationNumber - ); - if (getLastLoadingBalanceFlag) { - dataLoadingBalance = getLastLoadingBalanceFlag; - } - - // get detail load balance - let dataLoadingBalanceDetail = []; - if (dataLoadingBalance) { - let getLastLoadingBalanceDetailFlag = - await model.getLastLoadingBalanceDetailFlag( - dataLoadingBalance.i_load_balance - ); - if (getLastLoadingBalanceDetailFlag) { - dataLoadingBalanceDetail = getLastLoadingBalanceDetailFlag; + // get reservation + let dataReservation = []; + let getLastReservationFlag = await model.getLastReservationFlag( + reservationNumber + ); + if (getLastReservationFlag) { + dataReservation = getLastReservationFlag; } - } - if (dataReservation && dataLoadingBalance && dataLoadingBalanceDetail) { - // time - let now = moment().format("YYYY-MM-DD HH:mm:ss"); - let today = moment().format("YYYY-MM-DD"); - let getPartner = await model.getPartner(mid, tid); - - // get loading balance - const getLoadBalanceSequence = await model.getLoadBalanceSequence( - getPartner, - today - ); - const loadBalanceNoSequence = getLoadBalanceSequence - ? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1 - : 1; - - // generate load balance number - let loadBalanceNumber = await model.generateLoadBalanceNumber( - getPartner, - moment(today).format("YYYYMMDD"), - loadBalanceNoSequence + // log debug + winston.logger.debug( + `${uniqueCode} getting last load balance transaction...` ); - let data = { - c_load_balance_no: loadBalanceNumber, - d_load_balance: now, - c_ref_no: dataReservation.c_ref_no, - i_partner: getPartner ? getPartner.i_partner : null, - c_partner_code: getPartner ? getPartner.partner_code : null, - c_partner_name: getPartner ? getPartner.partner_name : null, - i_merchant: getPartner ? getPartner.i_merchant : null, - c_merchant_code: getPartner ? getPartner.merchant_code : null, - c_merchant_name: getPartner ? getPartner.merchant_name : null, - c_mid: getPartner ? getPartner.mid : null, - i_terminal: getPartner ? getPartner.i_terminal : null, - c_terminal_code: getPartner ? getPartner.terminal_code : null, - c_terminal_name: getPartner ? getPartner.terminal_name : null, - c_tid: getPartner ? getPartner.tid : null, - c_card_number: dataReservation.c_card_number, - i_topup_amount: dataReservation.i_reservation_amount, - c_return_sap: null, - i_load_balance_no_seq: loadBalanceNoSequence, - c_status: "A", - c_status_name: "SUCCESS", - d_create: now, - i_create: null, - c_create_username: null, - c_create_fullname: null, - d_modify: null, - i_modify: null, - c_modify_username: null, - c_modify_fullname: null, - d_delete: null, - i_delete: null, - c_delete_username: null, - c_delete_fullname: null, - }; - - // insert reservation and load balance transaction - await model.insertReservationAndLoadingBalance( - reservationNumber, - data, - dataLoadingBalanceDetail, - now + // get load balance + let dataLoadingBalance = []; + let getLastLoadingBalanceFlag = await model.getLastLoadingBalanceFlag( + reservationNumber ); + if (getLastLoadingBalanceFlag) { + dataLoadingBalance = getLastLoadingBalanceFlag; + } + + // get detail load balance + let dataLoadingBalanceDetail = []; + if (dataLoadingBalance) { + // log debug + winston.logger.debug( + `${uniqueCode} getting last load balance detail transaction...` + ); + + let getLastLoadingBalanceDetailFlag = + await model.getLastLoadingBalanceDetailFlag( + dataLoadingBalance.i_load_balance + ); + if (getLastLoadingBalanceDetailFlag) { + dataLoadingBalanceDetail = getLastLoadingBalanceDetailFlag; + } + } + + if ( + dataReservation && + dataLoadingBalance && + dataLoadingBalanceDetail.length > 0 + ) { + // time + let now = moment().format("YYYY-MM-DD HH:mm:ss"); + let today = moment().format("YYYY-MM-DD"); + let getPartner = await model.getPartner(mid, tid); + + // log debug + winston.logger.debug(`${uniqueCode} getting loading balance sequence...`); + + // get loading balance + const getLoadBalanceSequence = await model.getLoadBalanceSequence( + getPartner, + today + ); + const loadBalanceNoSequence = getLoadBalanceSequence + ? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1 + : 1; + + // log debug + winston.logger.debug(`${uniqueCode} generating load balance number...`); + + // generate load balance number + let loadBalanceNumber = await model.generateLoadBalanceNumber( + getPartner, + moment(today).format("YYYYMMDD"), + loadBalanceNoSequence + ); + + let data = { + c_load_balance_no: loadBalanceNumber, + d_load_balance: now, + c_ref_no: dataReservation.c_ref_no, + i_partner: getPartner ? getPartner.i_partner : null, + c_partner_code: getPartner ? getPartner.partner_code : null, + c_partner_name: getPartner ? getPartner.partner_name : null, + i_merchant: getPartner ? getPartner.i_merchant : null, + c_merchant_code: getPartner ? getPartner.merchant_code : null, + c_merchant_name: getPartner ? getPartner.merchant_name : null, + c_mid: getPartner ? getPartner.mid : null, + i_terminal: getPartner ? getPartner.i_terminal : null, + c_terminal_code: getPartner ? getPartner.terminal_code : null, + c_terminal_name: getPartner ? getPartner.terminal_name : null, + c_tid: getPartner ? getPartner.tid : null, + c_card_number: dataReservation.c_card_number, + i_topup_amount: dataLoadingBalance.i_topup_amount, + c_return_sap: null, + i_load_balance_no_seq: loadBalanceNoSequence, + c_status: "A", + c_status_name: "SUCCESS", + d_create: now, + i_create: null, + c_create_username: null, + c_create_fullname: null, + d_modify: null, + i_modify: null, + c_modify_username: null, + c_modify_fullname: null, + d_delete: null, + i_delete: null, + c_delete_username: null, + c_delete_fullname: null, + }; + + // log debug + winston.logger.debug( + `${uniqueCode} updating reservation and inserting load balance transaction...` + ); + + // insert reservation and load balance transaction + await model.insertReservationAndLoadingBalance( + reservationNumber, + data, + dataLoadingBalanceDetail, + now + ); + + // log debug + winston.logger.debug(`${uniqueCode} inserting log for card history...`); + + // log card history + await model.createLoadBalanceLog({ + i_log_name: 89, + d_log: now, + c_card_number: dataReservation.c_card_number, + c_source: "CMS - HSM", + c_location: null, + m_balance_before: balance_before, + m_amount: dataLoadingBalance.i_topup_amount, + m_balance_after: balance, + j_new_data: { data: data, detail: dataLoadingBalanceDetail }, + j_old_data: null, + c_note: "SUCCESSFULLY LOADED", + c_status: "S", + d_created: now, + i_create: null, + c_create_username: "HSM", + c_create_fullname: "HARDWARE SECURE MODULE", + c_category: null, + }); + + return true; + } else { + return false; + } + } catch (error) { + // create log + winston.logger.error( + `500 internal server error - backend server | ${error.message}` + ); + + return false; } }; diff --git a/application/modules/loadbalance/loadbalance.model.js b/application/modules/loadbalance/loadbalance.model.js index 4e0c986..4af8a42 100644 --- a/application/modules/loadbalance/loadbalance.model.js +++ b/application/modules/loadbalance/loadbalance.model.js @@ -397,25 +397,20 @@ const getLastLoadingBalanceDetailFlag = (loadBalanceId) => { // update const updateStatusReservation = (reservationNumber, now) => { let result = knex("trx.t_d_reservation_balance") - .update({ - c_status: "I", - c_status_name: "IN PROGRESS", - d_modify: now, - i_modify: null, - c_modify_username: null, - c_modify_fullname: null, - }) - .whereIn("c_reservation_no", reservationNumber); + .update({ + c_status: "I", + c_status_name: "IN PROGRESS", + d_modify: now, + i_modify: null, + c_modify_username: null, + c_modify_fullname: null, + }) + .whereIn("c_reservation_no", reservationNumber); return result; -} +}; // update flag -const updateFlag = async ( - now, - cardNumber, - uid, - balance, - flag) => { +const updateFlag = async (now, cardNumber, uid, balance, flag) => { await knex .transaction((trx) => { knex("trx.t_d_flag_counting") @@ -424,7 +419,6 @@ const updateFlag = async ( .whereRaw("UPPER(c_uid) = ?", [uid]) .del() .then(() => { - // insert flag counting return knex("trx.t_d_flag_counting").transacting(trx).insert({ d_request: now, @@ -445,7 +439,12 @@ const updateFlag = async ( console.log(error.message); return error.message; }); -} +}; + +const createLoadBalanceLog = (data) => { + let result = knex("log.t_log_card").insert(data); + return result; +}; module.exports = { checkCardNumber, @@ -470,4 +469,5 @@ module.exports = { getLastLoadingBalanceDetailFlag, updateStatusReservation, updateFlag, + createLoadBalanceLog, }; diff --git a/application/modules/reservebalance/reservebalance.controller.js b/application/modules/reservebalance/reservebalance.controller.js index aeba9d2..ed1f6d0 100644 --- a/application/modules/reservebalance/reservebalance.controller.js +++ b/application/modules/reservebalance/reservebalance.controller.js @@ -81,9 +81,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -100,9 +98,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -120,9 +116,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -155,9 +149,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -185,9 +177,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -226,9 +216,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -257,9 +245,7 @@ const reservation = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify( - result - )}` + `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -273,8 +259,8 @@ const reservation = async (req, res) => { let channelFee = 0; let getIssuer = await model.getIssuer(getPartner.i_partner); if (getIssuer) { - issuerFee = (getIssuer.i_issuer_fee_perc/100) * 1500; - channelFee = (getIssuer.i_channel_fee_perc/100) * 1500; + issuerFee = (getIssuer.i_issuer_fee_perc / 100) * 1500; + channelFee = (getIssuer.i_channel_fee_perc / 100) * 1500; } // if (!getIssuer) { // return res.status(200).json({ @@ -378,6 +364,7 @@ const reservation = async (req, res) => { // log debug winston.logger.debug(`${uniqueCode} inserting reservation transaction...`); + // insert transaction await model.insertReservation(data); result = { diff --git a/docs/error_code b/docs/error_code index 110d54e..4b477b4 100644 --- a/docs/error_code +++ b/docs/error_code @@ -23,6 +23,7 @@ 420 your card number has incomplete transaction. Please unload your balance! 421 your card number has some pending transaction. Please finished it! 422 history doesn't exist + 423 update/insert failed # ERROR MESSAGE CODE CORE HSM 5001 error catch HSM