diff --git a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json index f6595c7..dff8e9c 100644 --- a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json +++ b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json @@ -6,14 +6,9 @@ "auditLog": ".71c131f2ce727b13210eef32232709400762e5fb-audit.json", "files": [ { - "date": 1655004752426, - "name": "2022-06-12-onlinetopup.log", - "hash": "984c1c0ba67f63bc2f51c096864e54f9d8695ced7838e6846f18b5ccb2ff7966" - }, - { - "date": 1655054074401, - "name": "2022-06-13-onlinetopup.log", - "hash": "4a6c5caeb3f4ccc38429ce95f762a04f650055a2d0831cbe85ce7fa4b5c50021" + "date": 1655273674261, + "name": "2022-06-15-onlinetopup.log", + "hash": "d42a4aa0e8a85a0dc8015c4aa2ea03b91b58f2448c304c530b855af3bd72a09c" } ], "hashType": "sha256" diff --git a/application/modules/auth/auth.controller.js b/application/modules/auth/auth.controller.js index 08efc31..c259d9d 100644 --- a/application/modules/auth/auth.controller.js +++ b/application/modules/auth/auth.controller.js @@ -101,7 +101,7 @@ const login = async (req, res, next) => { if (!checkDataLogin) { result = { code: "403", - message: "Terminal Login already exists.", + message: "Terminal Login doesn't exists.", data: {}, }; diff --git a/application/modules/loadbalance/loadbalance.controller.js b/application/modules/loadbalance/loadbalance.controller.js index f88d6f1..9eab521 100644 --- a/application/modules/loadbalance/loadbalance.controller.js +++ b/application/modules/loadbalance/loadbalance.controller.js @@ -152,7 +152,7 @@ const loadingBalanceRequest = async (req, res) => { let getReservationPendingWithFlag = await model.getReservationPendingWithFlag(card_number, now); if (getReservationPendingWithFlag.length > 0) { - if (lastFlag && lastFlag != flag) { + if (lastFlag && lastFlag == flag) { let reservationNumberPendingWithflag = []; getReservationPendingWithFlag.forEach((element) => { @@ -211,10 +211,10 @@ const loadingBalanceRequest = async (req, res) => { } // log debug - winston.logger.debug(`${uniqueCode} updating flag to server...`); + // winston.logger.debug(`${uniqueCode} updating flag to server...`); - // update flag from Server - await model.updateFlag(now, card_number, uid.toUpperCase(), balance, flag); + // // update flag from Server + // await model.updateFlag(now, card_number, uid.toUpperCase(), balance, flag); // log debug winston.logger.debug(`${uniqueCode} checking reservation pending...`); @@ -249,7 +249,7 @@ const loadingBalanceRequest = async (req, res) => { let i = 0; getReservationPending.forEach((rsv) => { if (rsv.c_flag) { - if (lastFlag == flag) { + if (lastFlag != flag) { amountTopup += parseInt(rsv.i_reservation_amount); reservationNumber.push(rsv.c_reservation_no); i++; @@ -709,23 +709,25 @@ const loadingBalance = async (req, res) => { }); } - // log debug - winston.logger.debug(`${uniqueCode} inserting load balance transaction...`); - - // insert transaction - await model.insertLoadingBalance( - data, - detail, - reservation_number, - now, - write_status, - card_number, - uid.toUpperCase(), - balance, - flag - ); - if (!write_status) { + // log debug + winston.logger.debug( + `${uniqueCode} inserting load balance transaction...` + ); + + // insert transaction + await model.insertLoadingBalance( + data, + detail, + reservation_number, + now, + write_status, + card_number, + uid.toUpperCase(), + balance, + flag + ); + // log debug winston.logger.debug(`${uniqueCode} inserting log for card history...`); @@ -734,21 +736,44 @@ const loadingBalance = async (req, res) => { i_log_name: 89, d_log: now, c_card_number: card_number, - c_source: "CMS - HSM", - c_location: null, - m_balance_before: balance - amount > 0 ? balance - amount : 0, - m_amount: amount, - m_balance_after: balance, + c_source: "CMS - LOAD BALANCE", + c_location: getPartner ? getPartner.terminal_name : null, + m_balance_before: + parseInt(balance) - parseInt(amount) > 0 + ? parseInt(balance) - parseInt(amount) + : 0, + m_amount: parseInt(amount), + m_balance_after: parseInt(balance), j_new_data: { data: data, detail: detail }, j_old_data: null, - c_note: "SUCCESSFULLY LOADED", + c_note: `LOAD BALANCE ${amount} FROM ${ + getPartner ? getPartner.terminal_name : null + } SUCCESSFULLY`, c_status: "S", d_created: now, i_create: null, c_create_username: "HSM", c_create_fullname: "HARDWARE SECURE MODULE", - c_category: null, + c_category: "T", }); + } else { + // log debug + winston.logger.debug( + `${uniqueCode} inserting load balance transaction...` + ); + + // insert transaction + await model.insertLoadingBalanceHistoryOnly( + data, + detail, + reservation_number, + now, + write_status, + card_number, + uid.toUpperCase(), + balance, + flag + ); } result = { @@ -864,7 +889,7 @@ const inquiryLoadingBalance = async (req, res) => { } }; -const updateReservationAndLoadingBalance = async ( +const updateReservationAndLoadingBalanceOld = async ( reservationNumber, mid, tid, @@ -967,6 +992,7 @@ const updateReservationAndLoadingBalance = async ( m_balance: dataLoadingBalance.m_balance, c_return_sap: null, i_load_balance_no_seq: loadBalanceNoSequence, + c_idm: dataLoadingBalance.c_idm, c_status: "A", c_status_name: "SUCCESS", d_create: now, @@ -1006,18 +1032,193 @@ const updateReservationAndLoadingBalance = async ( c_card_number: dataReservation.c_card_number, c_source: "CMS - HSM", c_location: null, - m_balance_before: dataLoadingBalance.m_balance, - m_amount: dataLoadingBalance.i_topup_amount, - m_balance_after: balance, + m_balance_before: parseInt(dataLoadingBalance.m_balance), + m_amount: parseInt(dataLoadingBalance.i_topup_amount), + m_balance_after: parseInt(balance), j_new_data: { data: data, detail: dataLoadingBalanceDetail }, j_old_data: null, - c_note: "SUCCESSFULLY LOADED", + c_note: `LOAD BALANCE ${amount} FROM ${ + getPartner ? getPartner.terminal_name : null + } SUCCESSFULLY`, c_status: "S", d_created: now, i_create: null, c_create_username: "HSM", c_create_fullname: "HARDWARE SECURE MODULE", - c_category: null, + c_category: "T", + }); + + return true; + } else { + return false; + } + } catch (error) { + // create log + winston.logger.error( + `500 internal server error - backend server | ${error.message}` + ); + + return false; + } +}; + +const updateReservationAndLoadingBalance = async ( + reservationNumber, + mid, + tid, + balance, + uniqueCode +) => { + try { + // log debug + winston.logger.debug(`${uniqueCode} getting last reservation with flag...`); + + // get reservation + let dataReservation = []; + let getLastReservationFlag = await model.getLastReservationFlag( + reservationNumber + ); + if (getLastReservationFlag) { + dataReservation = getLastReservationFlag; + } + + // log debug + winston.logger.debug( + `${uniqueCode} getting last load balance transaction...` + ); + + // 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: dataLoadingBalance.c_load_balance_no, + d_load_balance: dataLoadingBalance.d_load_balance, + c_ref_no: dataLoadingBalance.c_ref_no, + i_partner: dataLoadingBalance.i_partner, + c_partner_code: dataLoadingBalance.c_partner_code, + c_partner_name: dataLoadingBalance.c_partner_name, + i_merchant: dataLoadingBalance.i_merchant, + c_merchant_code: dataLoadingBalance.c_merchant_code, + c_merchant_name: dataLoadingBalance.c_merchant_name, + c_mid: dataLoadingBalance.c_mid, + i_terminal: dataLoadingBalance.i_terminal, + c_terminal_code: dataLoadingBalance.c_terminal_code, + c_terminal_name: dataLoadingBalance.c_terminal_name, + c_tid: dataLoadingBalance.c_tid, + c_card_number: dataLoadingBalance.c_card_number, + i_topup_amount: dataLoadingBalance.i_topup_amount, + m_balance: dataLoadingBalance.m_balance, + c_return_sap: null, + i_load_balance_no_seq: dataLoadingBalance.i_load_balance_no_seq, + c_idm: dataLoadingBalance.c_idm, + 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: dataLoadingBalance.d_load_balance, + c_card_number: dataReservation.c_card_number, + c_source: "CMS - HSM", + c_location: null, + m_balance_before: parseInt(dataLoadingBalance.m_balance), + m_amount: parseInt(dataLoadingBalance.i_topup_amount), + m_balance_after: parseInt(balance), + j_new_data: { data: data, detail: dataLoadingBalanceDetail }, + j_old_data: null, + c_note: `LOAD BALANCE ${dataLoadingBalance.i_topup_amount} FROM ${ + getPartner ? getPartner.terminal_name : null + } SUCCESSFULLY`, + c_status: "S", + d_created: now, + i_create: null, + c_create_username: "HSM", + c_create_fullname: "HARDWARE SECURE MODULE", + c_category: "T", }); return true; diff --git a/application/modules/loadbalance/loadbalance.model.js b/application/modules/loadbalance/loadbalance.model.js index 4eb0bb5..e2fdb16 100644 --- a/application/modules/loadbalance/loadbalance.model.js +++ b/application/modules/loadbalance/loadbalance.model.js @@ -215,6 +215,38 @@ const insertLoadingBalance = async ( .transacting(trx) .insert(loadDetail); }) + .then(() => { + // insert load balance history + return knex("trx.t_d_load_balance_history") + .transacting(trx) + .returning("i_load_balance") + .insert(data); + }) + .then((history) => { + let id = history[0]; + let loadDetail = []; + detail.forEach((element) => { + loadDetail.push({ + i_load_balance: id, + i_reservation_balance: element.i_reservation_balance, + c_reservation_no: element.c_reservation_no, + i_amount: element.i_amount, + i_issuer: element.i_issuer, + i_issuer_code: element.i_issuer_code, + i_issuer_name: element.i_issuer_name, + i_issuer_fee: element.i_issuer_fee, + i_channel: element.i_channel, + i_channel_code: element.i_channel_code, + i_channel_name: element.i_channel_name, + i_channel_fee: element.i_channel_fee, + }); + }); + + // insert detail load balance + return knex("trx.t_d_load_balance_history_detail") + .transacting(trx) + .insert(loadDetail); + }) .then(() => { // update reservation return knex("trx.t_d_reservation_balance") @@ -223,6 +255,99 @@ const insertLoadingBalance = async ( c_flag: writeStatus ? "FW" : null, c_status: writeStatus ? "I" : "A", c_status_name: writeStatus ? "IN PROGRESS" : "LOADED", + d_load_balance: now, + c_load_balance_no: data.c_load_balance_no, + d_modify: now, + i_modify: null, + c_modify_username: null, + c_modify_fullname: null, + }) + .whereIn("c_reservation_no", reservationNumber); + }) + .then(() => { + // delete flag counting + return knex("trx.t_d_flag_counting") + .transacting(trx) + .where("c_card_number", cardNumber) + .whereRaw("UPPER(c_uid) = ?", [uid]) + .del(); + }) + .then(() => { + // insert flag counting + return knex("trx.t_d_flag_counting").transacting(trx).insert({ + d_request: now, + c_card_number: cardNumber, + c_uid: uid, + i_balance: balance, + c_flag: flag, + }); + }) + .then(trx.commit) + .catch(trx.rollback); + }) + .then((result) => { + // console.log(result); + return result; + }) + .catch((error) => { + console.log(error.message); + return error.message; + }); +}; + +// INSERT LOADING BALANCE HISTORY +const insertLoadingBalanceHistoryOnly = async ( + data, + detail, + reservationNumber, + now, + writeStatus, + cardNumber, + uid, + balance, + flag +) => { + await knex + .transaction((trx) => { + knex("trx.t_d_load_balance_history") + .transacting(trx) + .insert(data) + .returning("i_load_balance") + .then((resp) => { + let id = resp[0]; + let loadDetail = []; + detail.forEach((element) => { + loadDetail.push({ + i_load_balance: id, + i_reservation_balance: element.i_reservation_balance, + c_reservation_no: element.c_reservation_no, + i_amount: element.i_amount, + i_issuer: element.i_issuer, + i_issuer_code: element.i_issuer_code, + i_issuer_name: element.i_issuer_name, + i_issuer_fee: element.i_issuer_fee, + i_channel: element.i_channel, + i_channel_code: element.i_channel_code, + i_channel_name: element.i_channel_name, + i_channel_fee: element.i_channel_fee, + }); + }); + + // insert detail load balance + return knex("trx.t_d_load_balance_history_detail") + .transacting(trx) + .insert(loadDetail); + }) + .then(() => { + // update reservation + return knex("trx.t_d_reservation_balance") + .transacting(trx) + .update({ + c_flag: writeStatus ? "FW" : null, + c_status: writeStatus ? "I" : "A", + c_status_name: writeStatus ? "IN PROGRESS" : "LOADED", + // d_load_balance: now, + // c_load_balance_no: data.c_load_balance_no, d_modify: now, i_modify: null, c_modify_username: null, @@ -333,11 +458,45 @@ const insertReservationAndLoadingBalance = async ( .transacting(trx) .insert(loadDetail); }) + .then(() => { + // insert load balance history + return knex("trx.t_d_load_balance_history") + .transacting(trx) + .returning("i_load_balance") + .insert(data); + }) + .then((history) => { + let id = history[0]; + let loadDetail = []; + detail.forEach((element) => { + loadDetail.push({ + i_load_balance: id, + i_reservation_balance: element.i_reservation_balance, + c_reservation_no: element.c_reservation_no, + i_amount: element.i_amount, + i_issuer: element.i_issuer, + i_issuer_code: element.i_issuer_code, + i_issuer_name: element.i_issuer_name, + i_issuer_fee: element.i_issuer_fee, + i_channel: element.i_channel, + i_channel_code: element.i_channel_code, + i_channel_name: element.i_channel_name, + i_channel_fee: element.i_channel_fee, + }); + }); + + // insert detail load balance + return knex("trx.t_d_load_balance_history_detail") + .transacting(trx) + .insert(loadDetail); + }) .then(() => { // update reservation return knex("trx.t_d_reservation_balance") .transacting(trx) .update({ + d_load_balance: data.d_load_balance, + c_load_balance_no: data.c_load_balance_no, c_flag: null, c_status: "A", c_status_name: "LOADED", @@ -372,22 +531,22 @@ const getLastReservationFlag = (reservationNumber) => { // const getLastLoadingBalanceFlag = (reservationNumber) => { - let result = knex("trx.t_d_load_balance") - .select("trx.t_d_load_balance.*") + let result = knex("trx.t_d_load_balance_history") + .select("trx.t_d_load_balance_history.*") .leftJoin( - "trx.t_d_load_balance_detail", - "trx.t_d_load_balance_detail.i_load_balance", - "trx.t_d_load_balance.i_load_balance" + "trx.t_d_load_balance_history_detail", + "trx.t_d_load_balance_history_detail.i_load_balance", + "trx.t_d_load_balance_history.i_load_balance" ) - .whereIn("trx.t_d_load_balance_detail.c_reservation_no", reservationNumber) - .where("trx.t_d_load_balance.c_status", "F") + .whereIn("trx.t_d_load_balance_history_detail.c_reservation_no", reservationNumber) + .where("trx.t_d_load_balance_history.c_status", "F") .first(); return result; }; // const getLastLoadingBalanceDetailFlag = (loadBalanceId) => { - let result = knex("trx.t_d_load_balance_detail").where( + let result = knex("trx.t_d_load_balance_history_detail").where( "i_load_balance", loadBalanceId ); @@ -477,6 +636,7 @@ module.exports = { findReservationLoaded, findReservation, insertLoadingBalance, + insertLoadingBalanceHistoryOnly, getLoadBalanceSequence, generateLoadBalanceNumber, inquiryByLoadBalanceNumber, diff --git a/application/modules/reservebalance/reservebalance.controller.js b/application/modules/reservebalance/reservebalance.controller.js index 4f31d23..6f22647 100644 --- a/application/modules/reservebalance/reservebalance.controller.js +++ b/application/modules/reservebalance/reservebalance.controller.js @@ -124,6 +124,19 @@ const reservation = async (req, res) => { return res.status(200).json(result); } + // log debug + winston.logger.debug( + `${uniqueCode} getting last balance on the card by card history...` + ); + + // get last balance on the card + let balance = 0; + let getLastBalanceFromCardHistory = + await model.getLastBalanceFromCardHistory(card_number); + if (getLastBalanceFromCardHistory) { + balance = getLastBalanceFromCardHistory.m_balance_after; + } + // log debug winston.logger.debug(`${uniqueCode} checking BI regulation status...`); @@ -133,14 +146,6 @@ const reservation = async (req, res) => { // log debug winston.logger.debug(`${uniqueCode} checking max balance on the card...`); - // get last balance on the card - let balance = 0; - let getLastBalanceFromCardHistory = - await model.getLastBalanceFromCardHistory(card_number); - if (getLastBalanceFromCardHistory) { - balance = getLastBalanceFromCardHistory.m_balance_after; - } - // get max saldo on the card const getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard(); if (getMaxAmountOnTheCard.c_value) { @@ -211,33 +216,33 @@ const reservation = async (req, res) => { ? getMaxReservation.c_value : 1; - // log debug - winston.logger.debug(`${uniqueCode} checking reservation with flag...`); + // // log debug + // winston.logger.debug(`${uniqueCode} checking reservation with flag...`); - // get reservation with flag - let reservationWithFlag = await model.getReservationWithFlag( - card_number, - now - ); - let totalReservationWithFlag = - reservationWithFlag.total_reservation > 0 - ? reservationWithFlag.total_reservation - : 0; - if (totalReservationWithFlag > 0) { - result = { - code: "420", - message: - "your card number has incomplete transaction. Please unload your balance!", - data: {}, - }; + // // get reservation with flag + // let reservationWithFlag = await model.getReservationWithFlag( + // card_number, + // now + // ); + // let totalReservationWithFlag = + // reservationWithFlag.total_reservation > 0 + // ? reservationWithFlag.total_reservation + // : 0; + // if (totalReservationWithFlag > 0) { + // result = { + // code: "420", + // message: + // "your card number has incomplete transaction. Please unload your balance!", + // data: {}, + // }; - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` - ); + // // log warn + // winston.logger.warn( + // `${uniqueCode} RESPONSE reservation: ${JSON.stringify(result)}` + // ); - return res.status(200).json(result); - } + // return res.status(200).json(result); + // } // log debug winston.logger.debug(`${uniqueCode} checking total reservation pending...`); @@ -362,6 +367,8 @@ const reservation = async (req, res) => { c_payment_ref_no: null, i_reservation_no_seq: reservationNoSequence, d_expired: expired, + d_load_balance: null, + c_load_balance_no: null, c_status: "P", c_status_name: "PENDING", d_create: now, @@ -384,6 +391,32 @@ const reservation = async (req, res) => { // insert transaction await model.insertReservation(data); + // log debug + winston.logger.debug(`${uniqueCode} inserting log for card history...`); + + // log card history + await model.createReserveBalanceLog({ + i_log_name: 90, + d_log: now, + c_card_number: card_number, + c_source: "CMS - RESERVE BALANCE", + c_location: getPartner ? getPartner.terminal_name : null, + m_balance_before: balance > 0 ? balance : 0, + m_amount: amount, + m_balance_after: parseInt(balance) + parseInt(amount), + j_new_data: data, + j_old_data: null, + c_note: `RESERVED BALANCE ${amount} FROM ${ + getPartner ? getPartner.terminal_name : null + } SUCCESSFULLY`, + c_status: "S", + d_created: now, + i_create: null, + c_create_username: "HSM", + c_create_fullname: "HARDWARE SECURE MODULE", + c_category: "T", + }); + result = { code: "00", message: "reservation saved successfully.", diff --git a/application/modules/reservebalance/reservebalance.model.js b/application/modules/reservebalance/reservebalance.model.js index 8454efa..8d1d2f1 100644 --- a/application/modules/reservebalance/reservebalance.model.js +++ b/application/modules/reservebalance/reservebalance.model.js @@ -243,6 +243,12 @@ const getAccumulationBalanceInThisMonthFromCardHistory = ( return result; }; +// CREATE LOG FOR CARD HISTORY +const createReserveBalanceLog = (data) => { + let result = knex("log.t_log_card").insert(data); + return result; +}; + module.exports = { checkCardNumber, getReservationNumberSequence, @@ -262,4 +268,5 @@ module.exports = { insertReservation, getLastBalanceFromCardHistory, getAccumulationBalanceInThisMonthFromCardHistory, + createReserveBalanceLog, }; diff --git a/knexfile.js b/knexfile.js index cd6d190..7d7c4c0 100644 --- a/knexfile.js +++ b/knexfile.js @@ -22,9 +22,10 @@ module.exports = { production: { client: "pg", connection: { - database: "DB_TOPUP_ONLINE_APP", - user: "developer", - password: "hisnutech", + host : '10.1.201.95', + database: "postgres", + user: "cms", + password: "Cm5kc1#2021", }, pool: { min: 2,