diff --git a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json index ad2aa69..f6595c7 100644 --- a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json +++ b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json @@ -9,6 +9,11 @@ "date": 1655004752426, "name": "2022-06-12-onlinetopup.log", "hash": "984c1c0ba67f63bc2f51c096864e54f9d8695ced7838e6846f18b5ccb2ff7966" + }, + { + "date": 1655054074401, + "name": "2022-06-13-onlinetopup.log", + "hash": "4a6c5caeb3f4ccc38429ce95f762a04f650055a2d0831cbe85ce7fa4b5c50021" } ], "hashType": "sha256" diff --git a/application/modules/loadbalance/loadbalance.controller.js b/application/modules/loadbalance/loadbalance.controller.js index 290e34e..f6ba1c3 100644 --- a/application/modules/loadbalance/loadbalance.controller.js +++ b/application/modules/loadbalance/loadbalance.controller.js @@ -86,6 +86,8 @@ const loadingBalanceRequest = async (req, res) => { // time let now = moment().format("YYYY-MM-DD HH:mm:ss"); + let year = moment().format("Y"); + let month = moment().format("M"); // log debug winston.logger.debug(`${uniqueCode} checking partner...`); @@ -334,7 +336,17 @@ const loadingBalanceRequest = async (req, res) => { winston.logger.debug(`${uniqueCode} checking max topup per month...`); // get accumulated balance in this month - let accumulatedBalance = 19900000; + let accumulatedBalance = 0; + let getAccumulationBalanceInThisMonthFromCardHistory = + await model.getAccumulationBalanceInThisMonthFromCardHistory( + card_number, + year, + month + ); + if (getAccumulationBalanceInThisMonthFromCardHistory) { + accumulatedBalance = + getAccumulationBalanceInThisMonthFromCardHistory.accumulation_balance; + } // validate max topup per month const getMaxTopupPerMonth = await model.getMaxTopupPerMonth(); diff --git a/application/modules/loadbalance/loadbalance.model.js b/application/modules/loadbalance/loadbalance.model.js index 4af8a42..4eb0bb5 100644 --- a/application/modules/loadbalance/loadbalance.model.js +++ b/application/modules/loadbalance/loadbalance.model.js @@ -441,6 +441,25 @@ const updateFlag = async (now, cardNumber, uid, balance, flag) => { }); }; +// GET ACCUMULATION BALANCE IN THIS MONTH FROM CARD HISTORY +const getAccumulationBalanceInThisMonthFromCardHistory = ( + cardNumber, + year, + month +) => { + let result = knex("log.t_log_card") + .sum("m_balance_after as accumulation_balance") + .where("c_card_number", cardNumber) + .where("c_status", "S") + .where("m_balance_after", ">", 0) + .whereRaw("DATE_PART('YEAR', ??) = ?", ["d_log", year]) + .whereRaw("DATE_PART('MONTH', ??) = ?", ["d_log", month]) + .groupBy("c_card_number") + .first(); + return result; +}; + +// CREATE LOG FOR CARD HISTORY const createLoadBalanceLog = (data) => { let result = knex("log.t_log_card").insert(data); return result; @@ -469,5 +488,6 @@ module.exports = { getLastLoadingBalanceDetailFlag, updateStatusReservation, updateFlag, + getAccumulationBalanceInThisMonthFromCardHistory, createLoadBalanceLog, }; diff --git a/application/modules/reservebalance/reservebalance.controller.js b/application/modules/reservebalance/reservebalance.controller.js index ed1f6d0..1458f82 100644 --- a/application/modules/reservebalance/reservebalance.controller.js +++ b/application/modules/reservebalance/reservebalance.controller.js @@ -66,6 +66,8 @@ const reservation = async (req, res) => { // time let now = moment().format("YYYY-MM-DD HH:mm:ss"); let today = moment().format("YYYY-MM-DD"); + let year = moment().format("Y"); + let month = moment().format("M"); // log debug winston.logger.debug(`${uniqueCode} checking partner...`); @@ -132,7 +134,12 @@ const reservation = async (req, res) => { winston.logger.debug(`${uniqueCode} checking max balance on the card...`); // get last balance on the card - let balance = 1950000; + 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(); @@ -160,7 +167,19 @@ const reservation = async (req, res) => { winston.logger.debug(`${uniqueCode} checking max topup per month...`); // get accumulated balance in this month - let accumulatedBalance = 19950000; + let accumulatedBalance = 0; + let getAccumulationBalanceInThisMonthFromCardHistory = + await model.getAccumulationBalanceInThisMonthFromCardHistory( + card_number, + year, + month + ); + if (getAccumulationBalanceInThisMonthFromCardHistory) { + accumulatedBalance = + getAccumulationBalanceInThisMonthFromCardHistory.accumulation_balance; + } + + console.log("accumulatedBalance: " + accumulatedBalance); // validate max topup per month const getMaxTopupPerMonth = await model.getMaxTopupPerMonth(); diff --git a/application/modules/reservebalance/reservebalance.model.js b/application/modules/reservebalance/reservebalance.model.js index 7d6ae51..574982f 100644 --- a/application/modules/reservebalance/reservebalance.model.js +++ b/application/modules/reservebalance/reservebalance.model.js @@ -212,6 +212,37 @@ const insertReservation = (data) => { return result; }; +// GET LAST BALANCE FROM CARD HISTORY +const getLastBalanceFromCardHistory = (cardNumber) => { + let result = knex("log.t_log_card") + .select("m_balance_after") + .where("c_card_number", cardNumber) + .where("c_status", "S") + .where("m_balance_after", ">", 0) + .orderBy("d_log", "desc") + .limit(1) + .first(); + return result; +}; + +// GET ACCUMULATION BALANCE IN THIS MONTH FROM CARD HISTORY +const getAccumulationBalanceInThisMonthFromCardHistory = ( + cardNumber, + year, + month +) => { + let result = knex("log.t_log_card") + .sum("m_balance_after as accumulation_balance") + .where("c_card_number", cardNumber) + .where("c_status", "S") + .where("m_balance_after", ">", 0) + .whereRaw("DATE_PART('YEAR', ??) = ?", ["d_log", year]) + .whereRaw("DATE_PART('MONTH', ??) = ?", ["d_log", month]) + .groupBy("c_card_number") + .first(); + return result; +}; + module.exports = { checkCardNumber, getReservationNumberSequence, @@ -229,4 +260,6 @@ module.exports = { getReservationPending, getReservationWithFlag, insertReservation, + getLastBalanceFromCardHistory, + getAccumulationBalanceInThisMonthFromCardHistory, };