diff --git a/application/modules/loadbalance/loadbalance.controller.js b/application/modules/loadbalance/loadbalance.controller.js index ddf3784..6fa75ad 100644 --- a/application/modules/loadbalance/loadbalance.controller.js +++ b/application/modules/loadbalance/loadbalance.controller.js @@ -202,7 +202,7 @@ const loadingBalanceRequest = async (req, res) => { const cardHistory = await model.checkCardHistory(card_number); if (cardHistory.length > 0) { - if (cardHistory.i_log_name == '21') { + if (cardHistory.i_log_name == 21 && cardHistory.c_source == 'CMS') { if (getReservationPending.length > 0) { // insert semua reserve balance ke fraud const dataFraud = []; @@ -408,122 +408,199 @@ const loadingBalanceRequest = async (req, res) => { } // log debug - winston.logger.debug(`${uniqueCode} checking total topup amount...`); + winston.logger.debug( + `${uniqueCode} checking the card number in list fraud transaction or not...` + ); - // check total amount topup - // if (parseInt(amountTopup) <= 0) { - // result = { - // code: '415', - // message: 'amount must be greater than 0', - // data: {}, - // }; + // check fraud transaction by card number and how much fraud amount + let fraudTransaction = await model.getFraudTopupAndDeductTransaction( + card_number + ); - // // log warn - // winston.logger.warn( - // `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( - // result - // )}` - // ); + let fraudReservationNumber = []; + let amountFraudTopup = 0; + let amountFraudDeduct = 0; + let restAmountFraud = 0; + let totalFraudReservation = 0; - // return res.status(200).json(result); - // } + // if card number has fraud transaction + if (fraudTransaction.length > 0) { + let i = 0; - // log debug - winston.logger.debug(`${uniqueCode} checking BI regulation status...`); - - // get BI regulation status - const getBIRegulationStatus = await model.getBIRegulationStatus(); - if (getBIRegulationStatus.c_value == 'A') { - // log debug - winston.logger.debug(`${uniqueCode} checking max balance on the card...`); - - // get max saldo on the card - const getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard(); - if (getMaxAmountOnTheCard.c_value) { - if ( - parseInt(balance) + parseInt(amountTopup) > - parseInt(getMaxAmountOnTheCard.c_value) - ) { - result = { - code: '416', - message: 'your card number has reached the maximum balance', - data: {}, - }; - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( - result - )}` - ); - - return res.status(200).json(result); - } - } - - // log debug - winston.logger.debug(`${uniqueCode} checking max topup per month...`); - - // get accumulated balance in this month - 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(); - if (getMaxTopupPerMonth.c_value) { - if ( - parseInt(accumulatedBalance) + parseInt(amountTopup) > - parseInt(getMaxTopupPerMonth.c_value) - ) { - result = { - code: '417', - message: 'your card number has been limit top up for this month', - data: {}, - }; - - // log warn - winston.logger.warn( - `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( - result - )}` - ); - - return res.status(200).json(result); - } + if (fraudTransaction.length > 0) { + fraudTransaction.forEach((rsv) => { + fraudReservationNumber.push(rsv.c_reservation_no); + amountFraudTopup += parseInt(rsv.i_reservation_amount); + i++; + }); } + totalFraudReservation = i; } - // log debug - winston.logger.debug( - `${uniqueCode} updating status reservation to in progress...` - ); + // calculate rest amount fraud + restAmountFraud = parseInt(amountFraudTopup) - parseInt(amountFraudDeduct); - // update status to in progress - await model.updateStatusReservation(reservationNumberTemp, now, flag); + // if card number has fraud transaction and amount greater than 0 + if (parseInt(restAmountFraud) > 0) { + // fraud reservation found, and tell the lib to deduct transaction - result = { - code: '00', - message: 'reservation exists', - data: { - amount: amountTopup, - total_reservation: totalReservation, - reservation_number: reservationNumber, - }, - }; + result = { + code: '00', + message: 'reservation exists', + data: { + amount: parseInt(amountFraudTopup) * -1, + total_reservation: totalFraudReservation, + reservation_number: fraudReservationNumber, + }, + }; + } else { + // card number does't have fraud transaction - // log info - winston.logger.info( - `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify(result)}` - ); + // log debug + winston.logger.debug(`${uniqueCode} checking total topup amount...`); + + // check total amount topup + if (parseInt(amountTopup) <= 0) { + result = { + code: '415', + message: 'amount must be greater than 0', + data: {}, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); + + return res.status(200).json(result); + } + + // log debug + winston.logger.debug(`${uniqueCode} checking BI regulation status...`); + + // get BI regulation status + const getBIRegulationStatus = await model.getBIRegulationStatus(); + if (getBIRegulationStatus.c_value == 'A') { + // log debug + winston.logger.debug( + `${uniqueCode} checking max balance on the card...` + ); + + // get max saldo on the card + const getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard(); + if (getMaxAmountOnTheCard.c_value) { + if ( + parseInt(balance) + parseInt(amountTopup) > + parseInt(getMaxAmountOnTheCard.c_value) + ) { + result = { + code: '416', + message: 'your card number has reached the maximum balance', + data: {}, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); + + return res.status(200).json(result); + } + } + + // log debug + winston.logger.debug(`${uniqueCode} checking max topup per month...`); + + // get accumulated balance in this month + 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(); + if (getMaxTopupPerMonth.c_value) { + if ( + parseInt(accumulatedBalance) + parseInt(amountTopup) > + parseInt(getMaxTopupPerMonth.c_value) + ) { + result = { + code: '417', + message: 'your card number has been limit top up for this month', + data: {}, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); + + return res.status(200).json(result); + } + } + } + + // log debug + winston.logger.debug( + `${uniqueCode} updating status reservation to in progress...` + ); + + // update status to in progress + let updateStatusReservation = await model.updateStatusReservation( + reservationNumberTemp, + now, + flag + ); + + if (updateStatusReservation) { + result = { + code: '00', + message: 'reservation exists', + data: { + amount: amountTopup, + total_reservation: totalReservation, + reservation_number: reservationNumber, + }, + }; + + // log info + winston.logger.info( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); + } else { + result = { + code: '434', + message: '[434] Failed, please try again later', + data: { + amount: amountTopup, + total_reservation: totalReservation, + reservation_number: reservationNumber, + }, + }; + + // log info + winston.logger.info( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); + } + } return res.json(result); } catch (error) { @@ -1105,8 +1182,8 @@ const loadingBalanceInsert = async (param) => { let lastHistory = await model.getLastHistory(card_number); if (lastHistory) { - // if last history card is topup (21 = log name id for topup) - if (lastHistory.i_log_name == 21) { + // if last history card is topup 21 = log name id for topup and source is CMS, that mean topup with online source) + if (lastHistory.i_log_name == 21 && lastHistory.c_source == 'CMS') { // log warn winston.logger.warn(`${uniqueCode} inserting fraud transaction...`); @@ -2215,7 +2292,7 @@ const updateReservationAndLoadingBalance = async ( // // log card history // await model.createLoadBalanceLog({ - // i_log_name: 21, + // i_log_name:21, // d_log: dataLoadingBalance.d_load_balance, // c_card_number: cardNumber, // c_source: 'CMS', diff --git a/application/modules/loadbalance/loadbalance.model.js b/application/modules/loadbalance/loadbalance.model.js index c09b6d9..8280c2b 100644 --- a/application/modules/loadbalance/loadbalance.model.js +++ b/application/modules/loadbalance/loadbalance.model.js @@ -715,7 +715,7 @@ const updateStatusReservation = (reservationNumber, now, flag) => { c_modify_fullname: null, }) .whereIn('c_reservation_no', reservationNumber); - return result; + return result.rowCount; }; // update flag @@ -853,6 +853,7 @@ const getLastHistory = (cardNumber) => { let result = knex('log.t_log_card') .where('c_card_number', cardNumber) .whereIn('i_log_name', [21, 90]) + .where('c_source', 'CMS') .where('c_status', 'S') .orderBy('d_log', 'desc') .first(); @@ -919,8 +920,9 @@ const suspendReservation = (reservationNumber, datetime) => { // CHECK HISTORY CARD const checkCardHistory = (c_card_number) => { let result = knex('log.t_log_card') - .select(['i_log_name']) + // .select(['i_log_name', 'c_source']) .where('c_card_number', c_card_number) + .where('c_source', 'CMS') .whereIn('i_log_name', ['21', '90']) .orderBy('d_log', 'DESC') .first();