diff --git a/application/modules/loadbalance/loadbalance.controller.js b/application/modules/loadbalance/loadbalance.controller.js index 3c21339..503edbe 100644 --- a/application/modules/loadbalance/loadbalance.controller.js +++ b/application/modules/loadbalance/loadbalance.controller.js @@ -412,194 +412,147 @@ const loadingBalanceRequest = async (req, res) => { `${uniqueCode} checking the card number in list fraud transaction or not...` ); - // check fraud transaction by card number and how much fraud amount - let fraudTransaction = await model.getFraudTopupAndDeductTransaction( - card_number - ); + // log debug + winston.logger.debug(`${uniqueCode} checking total topup amount...`); - let fraudReservationNumber = []; - let amountFraudTopup = 0; - let amountFraudDeduct = 0; - let restAmountFraud = 0; - let totalFraudReservation = 0; + // check total amount topup + if (parseInt(amountTopup) <= 0) { + result = { + code: '415', + message: 'amount must be greater than 0', + data: {}, + }; - // if card number has fraud transaction - if (fraudTransaction.length > 0) { - let i = 0; + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); - if (fraudTransaction.length > 0) { - fraudTransaction.forEach((rsv) => { - fraudReservationNumber.push(rsv.c_reservation_no); - amountFraudTopup += parseInt(rsv.i_reservation_amount); - i++; - }); - } - totalFraudReservation = i; + return res.status(200).json(result); } - // calculate rest amount fraud - restAmountFraud = parseInt(amountFraudTopup) - parseInt(amountFraudDeduct); + // log debug + winston.logger.debug(`${uniqueCode} checking BI regulation status...`); - // 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 + // 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: parseInt(amountFraudTopup) * -1, - total_reservation: totalFraudReservation, - reservation_number: fraudReservationNumber, + amount: amountTopup, + total_reservation: totalReservation, + reservation_number: reservationNumber, }, }; + + // log info + winston.logger.info( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` + ); } else { - // card number does't have fraud transaction + result = { + code: '434', + message: '[434] Failed, please try again later', + data: { + amount: amountTopup, + total_reservation: totalReservation, + reservation_number: reservationNumber, + }, + }; - // 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...` + // log info + winston.logger.info( + `${uniqueCode} RESPONSE load balance validate: ${JSON.stringify( + result + )}` ); - - // 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);