mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 10:56:27 +00:00
1869 lines
52 KiB
JavaScript
1869 lines
52 KiB
JavaScript
const { param, body, validationResult } = require("express-validator");
|
|
const helper = require("../../helpers/helper");
|
|
const winston = require("../../helpers/winston.logger");
|
|
const model = require("./loadbalance.model");
|
|
const moment = require("moment");
|
|
moment.locale("id");
|
|
|
|
var result = {};
|
|
var uniqueCode;
|
|
|
|
// VALIDATION
|
|
const validate = (method) => {
|
|
switch (method) {
|
|
case "loadingBalanceRequest":
|
|
return [
|
|
body("mid").not().isEmpty().withMessage("MID is required"),
|
|
body("tid").not().isEmpty().withMessage("TID is required"),
|
|
body("uid").not().isEmpty().withMessage("UID is required"),
|
|
body("flag").not().isEmpty().withMessage("Flag is required"),
|
|
body("card_number")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Card Number is required"),
|
|
];
|
|
case "loadingBalance":
|
|
return [
|
|
body("mid").not().isEmpty().withMessage("MID is required"),
|
|
body("tid").not().isEmpty().withMessage("TID is required"),
|
|
body("uid").not().isEmpty().withMessage("UID is required"),
|
|
body("flag").not().isEmpty().withMessage("Flag is required"),
|
|
body("card_number")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Card Number is required"),
|
|
body("amount")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Amount is required")
|
|
.isNumeric()
|
|
.withMessage("Amount is invalid format"),
|
|
body("reservation_number")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Reservation Number is required")
|
|
.isArray()
|
|
.withMessage("Reservation Number is invalid request format"),
|
|
];
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
|
|
// LOADING BALANCE REQUEST
|
|
const loadingBalanceRequest = async (req, res) => {
|
|
try {
|
|
// param
|
|
let { mid, tid, card_number, uid, balance, flag, unique_code } = req.body;
|
|
|
|
// uniqueCode = await helper.getUniqueCode();
|
|
uniqueCode = unique_code;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} REQUEST load balance validate: ${JSON.stringify(req.body)}`
|
|
);
|
|
|
|
// check validator
|
|
const err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
result = {
|
|
code: "400",
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance validate: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// 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...`);
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
if (!getPartner) {
|
|
result = {
|
|
code: "412",
|
|
message: "terminal id is not valid",
|
|
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 card number...`);
|
|
|
|
// check card number
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
if (!checkCardNumber) {
|
|
result = {
|
|
code: "413",
|
|
message: "card number is not registered",
|
|
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 flag on the card and the server...`
|
|
);
|
|
|
|
// check last flag
|
|
let checkFlag = await model.checkFlag(card_number, uid.toUpperCase());
|
|
let lastFlag = null;
|
|
if (checkFlag) {
|
|
lastFlag = checkFlag.c_flag;
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking reservation pending...`);
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now,
|
|
flag
|
|
);
|
|
|
|
// get total reservation pending with compare flag counter on card and the reservation transaction
|
|
let getReservationPendingWithFlagCounter =
|
|
await model.getReservationPendingWithFlagCounter(card_number, now, flag);
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} checking reservation pending with flag...`
|
|
);
|
|
// get total reservation pending
|
|
// let getReservationPendingWithFlag =
|
|
// await model.getReservationPendingWithFlag(card_number, now);
|
|
|
|
if (getReservationPendingWithFlagCounter.length > 0) {
|
|
if (lastFlag && parseInt(lastFlag) <= parseInt(flag)) {
|
|
let reservationNumberPendingSuccessLoaded = [];
|
|
let loadBalanceNumberPendingSuccessLoaded = [];
|
|
|
|
getReservationPendingWithFlagCounter.forEach((element) => {
|
|
reservationNumberPendingSuccessLoaded.push(element.c_reservation_no);
|
|
loadBalanceNumberPendingSuccessLoaded.push(element.c_load_balance_no);
|
|
});
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} updating reservation and load balance transaction...`
|
|
);
|
|
|
|
// update
|
|
let insertUpdate = await updateReservationAndLoadingBalance(
|
|
reservationNumberPendingSuccessLoaded,
|
|
loadBalanceNumberPendingSuccessLoaded,
|
|
mid,
|
|
tid,
|
|
card_number,
|
|
balance,
|
|
flag,
|
|
uniqueCode
|
|
);
|
|
|
|
if (insertUpdate) {
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} updating flag to server...`);
|
|
|
|
// update flag from Server
|
|
await model.updateFlag(
|
|
now,
|
|
card_number,
|
|
uid.toUpperCase(),
|
|
balance,
|
|
flag
|
|
);
|
|
|
|
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(
|
|
`${uniqueCode} RESPONSE load balance validate: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(`${uniqueCode} updating flag to server...`);
|
|
|
|
// // update flag from Server
|
|
// await model.updateFlag(now, card_number, uid.toUpperCase(), balance, flag);
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking reservation pending...`);
|
|
|
|
// reget total reservation pending
|
|
getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now,
|
|
flag
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance validate: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
let totalReservation = 0;
|
|
let amountTopup = 0;
|
|
let reservationNumber = [];
|
|
let reservationNumberTemp = [];
|
|
let i = 0;
|
|
getReservationPending.forEach((rsv) => {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
reservationNumber.push(rsv.c_reservation_no);
|
|
reservationNumberTemp.push(rsv.c_reservation_no);
|
|
i++;
|
|
});
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} updating status reservation to in progress...`
|
|
);
|
|
|
|
// update status to in progress
|
|
await model.updateStatusReservation(reservationNumberTemp, now);
|
|
|
|
totalReservation = i;
|
|
if (totalReservation == 0) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
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 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(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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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)}`
|
|
);
|
|
|
|
return res.json(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error - backend server | ${error.message}`
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error - backend server.",
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
// LOAD BALANCE
|
|
const loadingBalance = async (req, res) => {
|
|
try {
|
|
// param
|
|
let {
|
|
mid,
|
|
tid,
|
|
card_number,
|
|
amount,
|
|
reservation_number,
|
|
ref_no,
|
|
write_status,
|
|
uid,
|
|
balance,
|
|
flag,
|
|
unique_code,
|
|
} = req.body;
|
|
|
|
uniqueCode = unique_code ? unique_code : 9999999999999;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} REQUEST load balance insert: ${JSON.stringify(req.body)}`
|
|
);
|
|
|
|
// check validator
|
|
const err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
result = {
|
|
code: "400",
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
let today = moment().format("YYYY-MM-DD");
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking partner...`);
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
if (!getPartner) {
|
|
result = {
|
|
code: "412",
|
|
message: "terminal id is not valid",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking reservation status...`);
|
|
|
|
// check reservation status
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
let reservationStatus = await model.findReservation(
|
|
reservation_number[index]
|
|
);
|
|
|
|
if (!reservationStatus) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (reservationStatus.c_status == "A") {
|
|
var reservationLoaded = await model.findReservationLoaded(
|
|
reservationStatus.i_reservation_balance
|
|
);
|
|
let loadBalanceNumberExisting = reservationLoaded.c_load_balance_no;
|
|
let loadDate = reservationLoaded.d_load_balance;
|
|
result = {
|
|
code: "00",
|
|
message:
|
|
"reservation has been successfully loaded on " +
|
|
moment(loadDate).format("DD MMM YYYY HH:mm:ss"),
|
|
data: {
|
|
load_balance_number: loadBalanceNumberExisting,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking reservation pending...`);
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now,
|
|
flag
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking total topup amount...`);
|
|
|
|
// check total topup and request topup
|
|
let amountTopup = 0;
|
|
getReservationPending.forEach((rsv) => {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
});
|
|
|
|
if (amount != amountTopup) {
|
|
result = {
|
|
code: "418",
|
|
message: "topup amount is different",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// generate load balance number
|
|
let loadBalanceNumber;
|
|
let getLoadBalanceSequence;
|
|
let loadBalanceNoSequence;
|
|
let maxLoop = 0;
|
|
do {
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} getting loading balance sequence...`);
|
|
|
|
// get loading balance
|
|
getLoadBalanceSequence = await model.getLoadBalanceSequence(
|
|
getPartner,
|
|
today
|
|
);
|
|
loadBalanceNoSequence = getLoadBalanceSequence
|
|
? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1
|
|
: 1;
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} generating load balance number...`);
|
|
|
|
loadBalanceNumber = await generateLoadBalanceNumber(
|
|
getPartner,
|
|
moment(today).format("YYYYMMDD"),
|
|
loadBalanceNoSequence
|
|
);
|
|
|
|
if (maxLoop > 20) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error (max loop) - backend server | ${error.message}`
|
|
);
|
|
|
|
return {
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error (max loop) - backend server.",
|
|
data: {},
|
|
};
|
|
}
|
|
|
|
maxLoop++;
|
|
} while (loadBalanceNumber == "XX");
|
|
|
|
let data = {
|
|
c_load_balance_no: loadBalanceNumber,
|
|
d_load_balance: now,
|
|
c_ref_no: 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: card_number,
|
|
i_topup_amount: amount,
|
|
m_balance: balance,
|
|
c_return_sap: null,
|
|
i_load_balance_no_seq: loadBalanceNoSequence,
|
|
c_idm: uid,
|
|
c_status: write_status ? "F" : "A",
|
|
c_status_name: write_status ? "FAILED" : "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} getting reservation detail...`);
|
|
|
|
let detail = [];
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
let reservation = await model.findReservation(reservation_number[index]);
|
|
|
|
if (!reservation) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
detail.push({
|
|
i_reservation_balance: reservation.i_reservation_balance
|
|
? reservation.i_reservation_balance
|
|
: null,
|
|
c_reservation_no: reservation.i_reservation_balance
|
|
? reservation.c_reservation_no
|
|
: null,
|
|
i_amount: reservation.i_reservation_balance
|
|
? reservation.i_reservation_amount
|
|
: null,
|
|
i_issuer: reservation.i_reservation_balance
|
|
? reservation.i_issuer
|
|
: null,
|
|
i_issuer_code: reservation.i_reservation_balance
|
|
? reservation.c_issuer_code
|
|
: null,
|
|
i_issuer_name: reservation.i_reservation_balance
|
|
? reservation.c_issuer_name
|
|
: null,
|
|
i_issuer_fee: reservation.i_reservation_balance
|
|
? reservation.i_issuer_fee
|
|
: null,
|
|
i_channel: reservation.i_reservation_balance
|
|
? reservation.i_channel
|
|
: null,
|
|
i_channel_code: reservation.i_reservation_balance
|
|
? reservation.c_channel_code
|
|
: null,
|
|
i_channel_name: reservation.i_reservation_balance
|
|
? reservation.c_channel_name
|
|
: null,
|
|
i_channel_fee: reservation.i_reservation_balance
|
|
? reservation.i_channel_fee
|
|
: null,
|
|
});
|
|
}
|
|
|
|
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...`);
|
|
|
|
// log card history
|
|
await model.createLoadBalanceLog({
|
|
i_log_name: 89,
|
|
d_log: now,
|
|
c_card_number: card_number,
|
|
c_source: "CMS - LOAD BALANCE",
|
|
c_location: getPartner ? getPartner.terminal_name : null,
|
|
m_balance_before: parseInt(balance) > 0 ? parseInt(balance) : 0,
|
|
m_amount: parseInt(amount),
|
|
m_balance_after: parseInt(balance) + parseInt(amount),
|
|
j_new_data: { data: data, detail: detail },
|
|
j_old_data: null,
|
|
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: "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 = {
|
|
code: "00",
|
|
message: write_status
|
|
? "transaction saved successfully. Err: failed write to the card"
|
|
: "balance loaded successfully",
|
|
data: {
|
|
load_balance_number: loadBalanceNumber,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error - backend server | ${error.message}`
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error - backend server.",
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
// LOAD BALANCE INSERT
|
|
const loadingBalanceInsert = async (param) => {
|
|
try {
|
|
// param
|
|
let mid = param.mid;
|
|
let tid = param.tid;
|
|
let card_number = param.card_number;
|
|
let amount = param.amount;
|
|
let reservation_number = param.reservation_number;
|
|
let ref_no = param.ref_no;
|
|
let write_status = param.write_status;
|
|
let uid = param.uid;
|
|
let balance = param.balance;
|
|
let flag = param.flag;
|
|
let unique_code = param.unique_code;
|
|
|
|
uniqueCode = unique_code ? unique_code : 9999999999999;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} REQUEST load balance insert: ${JSON.stringify(param)}`
|
|
);
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
let today = moment().format("YYYY-MM-DD");
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking partner...`);
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
if (!getPartner) {
|
|
result = {
|
|
code: "412",
|
|
message: "terminal id is not valid",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking reservation status...`);
|
|
|
|
// check reservation status
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
let reservationStatus = await model.findReservation(
|
|
reservation_number[index]
|
|
);
|
|
|
|
if (!reservationStatus) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
if (reservationStatus.c_status == "A") {
|
|
var reservationLoaded = await model.findReservationLoaded(
|
|
reservationStatus.i_reservation_balance
|
|
);
|
|
let loadBalanceNumberExisting = reservationLoaded.c_load_balance_no;
|
|
let loadDate = reservationLoaded.d_load_balance;
|
|
result = {
|
|
code: "00",
|
|
message:
|
|
"reservation has been successfully loaded on " +
|
|
moment(loadDate).format("DD MMM YYYY HH:mm:ss"),
|
|
data: {
|
|
load_balance_number: loadBalanceNumberExisting,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking reservation pending...`);
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now,
|
|
flag
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking total topup amount...`);
|
|
|
|
// check total topup and request topup
|
|
let amountTopup = 0;
|
|
getReservationPending.forEach((rsv) => {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
});
|
|
|
|
if (amount != amountTopup) {
|
|
result = {
|
|
code: "418",
|
|
message: "topup amount is different",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
// generate load balance number
|
|
let loadBalanceNumber;
|
|
let getLoadBalanceSequence;
|
|
let loadBalanceNoSequence;
|
|
let maxLoop = 0;
|
|
do {
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} getting loading balance sequence...`);
|
|
|
|
// get loading balance
|
|
getLoadBalanceSequence = await model.getLoadBalanceSequence(
|
|
getPartner,
|
|
today
|
|
);
|
|
loadBalanceNoSequence = getLoadBalanceSequence
|
|
? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1
|
|
: 1;
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} generating load balance number...`);
|
|
|
|
loadBalanceNumber = await generateLoadBalanceNumber(
|
|
getPartner,
|
|
moment(today).format("YYYYMMDD"),
|
|
loadBalanceNoSequence
|
|
);
|
|
|
|
if (maxLoop > 20) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error (max loop) - backend server | ${error.message}`
|
|
);
|
|
|
|
return {
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error (max loop) - backend server.",
|
|
data: {},
|
|
};
|
|
}
|
|
|
|
maxLoop++;
|
|
} while (loadBalanceNumber == "XX");
|
|
|
|
let data = {
|
|
c_load_balance_no: loadBalanceNumber,
|
|
d_load_balance: now,
|
|
c_ref_no: 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: card_number,
|
|
i_topup_amount: amount,
|
|
m_balance: balance,
|
|
c_return_sap: null,
|
|
i_load_balance_no_seq: loadBalanceNoSequence,
|
|
c_idm: uid,
|
|
c_status: write_status ? "F" : "P",
|
|
c_status_name: write_status ? "FAILED" : "PENDING",
|
|
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} getting reservation detail...`);
|
|
|
|
let detail = [];
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
let reservation = await model.findReservation(reservation_number[index]);
|
|
|
|
if (!reservation) {
|
|
result = {
|
|
code: "414",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
detail.push({
|
|
i_reservation_balance: reservation.i_reservation_balance
|
|
? reservation.i_reservation_balance
|
|
: null,
|
|
c_reservation_no: reservation.i_reservation_balance
|
|
? reservation.c_reservation_no
|
|
: null,
|
|
i_amount: reservation.i_reservation_balance
|
|
? reservation.i_reservation_amount
|
|
: null,
|
|
i_issuer: reservation.i_reservation_balance
|
|
? reservation.i_issuer
|
|
: null,
|
|
i_issuer_code: reservation.i_reservation_balance
|
|
? reservation.c_issuer_code
|
|
: null,
|
|
i_issuer_name: reservation.i_reservation_balance
|
|
? reservation.c_issuer_name
|
|
: null,
|
|
i_issuer_fee: reservation.i_reservation_balance
|
|
? reservation.i_issuer_fee
|
|
: null,
|
|
i_channel: reservation.i_reservation_balance
|
|
? reservation.i_channel
|
|
: null,
|
|
i_channel_code: reservation.i_reservation_balance
|
|
? reservation.c_channel_code
|
|
: null,
|
|
i_channel_name: reservation.i_reservation_balance
|
|
? reservation.c_channel_name
|
|
: null,
|
|
i_channel_fee: reservation.i_reservation_balance
|
|
? reservation.i_channel_fee
|
|
: null,
|
|
});
|
|
}
|
|
|
|
// check data load balance by reservation_number exist or not
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} checking load balance transaction has been exists or not...`
|
|
);
|
|
|
|
// 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
|
|
);
|
|
|
|
result = {
|
|
code: "00",
|
|
message: write_status
|
|
? "transaction saved successfully. Err: failed write to the card"
|
|
: "balance loaded successfully",
|
|
data: {
|
|
load_balance_number: loadBalanceNumber,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error - backend server | ${error.message}`
|
|
);
|
|
|
|
return {
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error - backend server.",
|
|
data: {},
|
|
};
|
|
}
|
|
};
|
|
|
|
// LOAD BALANCE UPDATE
|
|
const loadingBalanceUpdate = async (param) => {
|
|
try {
|
|
// param
|
|
let mid = param.mid;
|
|
let tid = param.tid;
|
|
let card_number = param.card_number;
|
|
let amount = param.amount;
|
|
let reservation_number = param.reservation_number;
|
|
let ref_no = param.ref_no;
|
|
let write_status = param.write_status;
|
|
let uid = param.uid;
|
|
let balance = param.balance;
|
|
let flag = param.flag;
|
|
let unique_code = param.unique_code;
|
|
let loadBalanceNumber = param.load_balance_number;
|
|
|
|
uniqueCode = unique_code ? unique_code : 9999999999999;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} REQUEST load balance update: ${JSON.stringify(param)}`
|
|
);
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
let today = moment().format("YYYY-MM-DD");
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} checking partner...`);
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
if (!getPartner) {
|
|
result = {
|
|
code: "412",
|
|
message: "terminal id is not valid",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} RESPONSE load balance update: ${JSON.stringify(
|
|
result
|
|
)}`
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
let data = {
|
|
d_load_balance: now,
|
|
c_ref_no: ref_no,
|
|
m_balance: balance,
|
|
c_return_sap: null,
|
|
c_status: write_status ? "F" : "A",
|
|
c_status_name: write_status ? "FAILED" : "SUCCESS",
|
|
d_modify: now,
|
|
i_modify: null,
|
|
c_modify_username: null,
|
|
c_modify_fullname: null,
|
|
};
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} updating load balance transaction...`);
|
|
|
|
// update transaction
|
|
await model.updateLoadingBalance(
|
|
data,
|
|
now,
|
|
write_status,
|
|
card_number,
|
|
uid.toUpperCase(),
|
|
balance,
|
|
flag,
|
|
loadBalanceNumber
|
|
);
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} inserting load balance transaction history ${loadBalanceNumber}...`
|
|
);
|
|
|
|
let findDataLoadBalance = await model.findLoadBalance(loadBalanceNumber);
|
|
let findDataLoadBalanceDetail = await model.findLoadBalanceDetail(
|
|
findDataLoadBalance.i_load_balance
|
|
);
|
|
|
|
let dataLoadBalance = {};
|
|
let detailLoadBalance = [];
|
|
|
|
if (findDataLoadBalance) {
|
|
dataLoadBalance = {
|
|
c_load_balance_no: findDataLoadBalance.c_load_balance_no,
|
|
d_load_balance: now,
|
|
c_ref_no: findDataLoadBalance.c_ref_no,
|
|
i_partner: findDataLoadBalance.i_partner,
|
|
c_partner_code: findDataLoadBalance.c_partner_code,
|
|
c_partner_name: findDataLoadBalance.c_partner_name,
|
|
i_merchant: findDataLoadBalance.i_merchant,
|
|
c_merchant_code: findDataLoadBalance.c_merchant_code,
|
|
c_merchant_name: findDataLoadBalance.c_merchant_name,
|
|
c_mid: findDataLoadBalance.c_mid,
|
|
i_terminal: findDataLoadBalance.i_terminal,
|
|
c_terminal_code: findDataLoadBalance.c_terminal_code,
|
|
c_terminal_name: findDataLoadBalance.c_terminal_name,
|
|
c_tid: findDataLoadBalance.c_tid,
|
|
c_card_number: findDataLoadBalance.c_card_number,
|
|
i_topup_amount: findDataLoadBalance.i_topup_amount,
|
|
m_balance: findDataLoadBalance.m_balance,
|
|
c_return_sap: findDataLoadBalance.c_return_sap,
|
|
i_load_balance_no_seq: findDataLoadBalance.i_load_balance_no_seq,
|
|
c_idm: findDataLoadBalance.c_idm,
|
|
c_status: write_status ? "F" : "A",
|
|
c_status_name: write_status ? "FAILED" : "SUCCESS",
|
|
d_create: findDataLoadBalance.d_create,
|
|
i_create: findDataLoadBalance.i_create,
|
|
c_create_username: findDataLoadBalance.c_create_username,
|
|
c_create_fullname: findDataLoadBalance.c_create_fullname,
|
|
d_modify: now,
|
|
i_modify: findDataLoadBalance.i_modify,
|
|
c_modify_username: findDataLoadBalance.c_modify_username,
|
|
c_modify_fullname: findDataLoadBalance.c_modify_fullname,
|
|
d_delete: findDataLoadBalance.d_delete,
|
|
i_delete: findDataLoadBalance.i_delete,
|
|
c_delete_username: findDataLoadBalance.c_delete_username,
|
|
c_delete_fullname: findDataLoadBalance.c_delete_fullname,
|
|
};
|
|
|
|
let i = 0;
|
|
findDataLoadBalanceDetail.forEach((element) => {
|
|
detailLoadBalance[i] = {
|
|
i_reservation_balance: element.i_reservation_balance,
|
|
c_reservation_no: element.c_reservation_no,
|
|
i_amount: element.i_reservation_amount,
|
|
i_issuer: element.i_issuer,
|
|
i_issuer_code: element.c_issuer_code,
|
|
i_issuer_name: element.c_issuer_name,
|
|
i_issuer_fee: element.i_issuer_fee,
|
|
i_channel: element.i_channel,
|
|
i_channel_code: element.c_channel_code,
|
|
i_channel_name: element.c_channel_name,
|
|
i_channel_fee: element.i_channel_fee,
|
|
};
|
|
i++;
|
|
});
|
|
}
|
|
|
|
// insert transaction
|
|
await model.insertLoadingBalanceHistoryOnly(
|
|
dataLoadBalance,
|
|
detailLoadBalance,
|
|
reservation_number,
|
|
now,
|
|
write_status,
|
|
card_number,
|
|
uid.toUpperCase(),
|
|
balance,
|
|
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 - LOAD BALANCE",
|
|
c_location: getPartner ? getPartner.terminal_name : null,
|
|
m_balance_before: parseInt(balance) > 0 ? parseInt(balance) : 0,
|
|
m_amount: parseInt(amount),
|
|
m_balance_after: parseInt(balance) + parseInt(amount),
|
|
j_new_data: { data: dataLoadBalance, detail: detailLoadBalance },
|
|
j_old_data: null,
|
|
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: "T",
|
|
});
|
|
}
|
|
|
|
result = {
|
|
code: "00",
|
|
message: write_status
|
|
? "transaction saved successfully. Err: failed write to the card"
|
|
: "balance loaded successfully",
|
|
data: {
|
|
load_balance_number: loadBalanceNumber,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} RESPONSE load balance insert: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error - backend server | ${error.message}`
|
|
);
|
|
|
|
return {
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error - backend server.",
|
|
data: {},
|
|
};
|
|
}
|
|
};
|
|
|
|
// INQUIRY LOADING BALANCE
|
|
const inquiryLoadingBalance = async (req, res) => {
|
|
try {
|
|
// param
|
|
let { load_balance_number, card_number, start_date, end_date } = req.body;
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
// validation parameter inquiry
|
|
if (!load_balance_number && !card_number) {
|
|
return res.status(200).json({
|
|
code: "400",
|
|
message:
|
|
"bad request. please add the load balance number or card number!",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// validation inquiry date
|
|
if ((!start_date && end_date) || (start_date && !end_date)) {
|
|
return res.status(200).json({
|
|
code: "400",
|
|
message: "bad request. please check the date!",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// check load balance
|
|
if (load_balance_number) {
|
|
let byLoadBalanceNumber = await model.inquiryByLoadBalanceNumber(
|
|
load_balance_number
|
|
);
|
|
|
|
if (!byLoadBalanceNumber) {
|
|
return res.status(200).json({
|
|
code: "419",
|
|
message: "load balance doesn't exist",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "load balance has been found.",
|
|
data: byLoadBalanceNumber,
|
|
});
|
|
} else if (card_number) {
|
|
let byCardNumber = await model.inquiryByCardNumber(
|
|
card_number,
|
|
start_date,
|
|
end_date
|
|
);
|
|
|
|
if (!byCardNumber) {
|
|
return res.status(200).json({
|
|
code: "419",
|
|
message: "load balance doesn't exist",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "load balance has been found.",
|
|
data: byCardNumber,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.status(200).json({
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error - backend server.",
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
const updateReservationAndLoadingBalanceOld = 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);
|
|
|
|
// generate load balance number
|
|
let loadBalanceNumber;
|
|
let getLoadBalanceSequence;
|
|
let loadBalanceNoSequence;
|
|
let maxLoop = 0;
|
|
do {
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} getting loading balance sequence...`
|
|
);
|
|
|
|
// get loading balance
|
|
getLoadBalanceSequence = await model.getLoadBalanceSequence(
|
|
getPartner,
|
|
today
|
|
);
|
|
loadBalanceNoSequence = getLoadBalanceSequence
|
|
? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1
|
|
: 1;
|
|
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} generating load balance number...`);
|
|
|
|
loadBalanceNumber = await generateLoadBalanceNumber(
|
|
getPartner,
|
|
moment(today).format("YYYYMMDD"),
|
|
loadBalanceNoSequence
|
|
);
|
|
|
|
if (maxLoop > 20) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error (max loop) - backend server | ${error.message}`
|
|
);
|
|
|
|
return {
|
|
code: "500",
|
|
message:
|
|
process.env.NODE_ENV != "production"
|
|
? error.message
|
|
: "500 internal server error (max loop) - backend server.",
|
|
data: {},
|
|
};
|
|
}
|
|
|
|
maxLoop++;
|
|
} while (loadBalanceNumber == "XX");
|
|
|
|
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,
|
|
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,
|
|
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.updateReservationAndLoadingBalance(
|
|
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 - LOAD BALANCE",
|
|
c_location: getPartner ? getPartner.terminal_name : 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 ${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;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error - backend server | ${error.message}`
|
|
);
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const updateReservationAndLoadingBalance = async (
|
|
reservationNumber,
|
|
loadBalanceNumber,
|
|
mid,
|
|
tid,
|
|
cardNumber,
|
|
balance,
|
|
flag,
|
|
uniqueCode
|
|
) => {
|
|
try {
|
|
// log debug
|
|
winston.logger.debug(`${uniqueCode} getting last reservation with flag...`);
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} getting last load balance transaction...`
|
|
);
|
|
|
|
// get load balance
|
|
let dataLoadingBalance = null;
|
|
let getLastLoadingBalanceFlag = await model.getLastLoadingBalanceFlag(
|
|
loadBalanceNumber
|
|
);
|
|
if (getLastLoadingBalanceFlag) {
|
|
dataLoadingBalance = getLastLoadingBalanceFlag;
|
|
} else {
|
|
// insert new load balance transaction by data on card and update data reservation
|
|
}
|
|
|
|
// 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 (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);
|
|
|
|
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 load balance transaction...`
|
|
);
|
|
|
|
// update reservation and load balance transaction
|
|
await model.updateReservationAndLoadingBalance(
|
|
reservationNumber,
|
|
data,
|
|
dataLoadingBalanceDetail,
|
|
now,
|
|
flag
|
|
);
|
|
|
|
// 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: cardNumber,
|
|
c_source: "CMS - LOAD BALANCE",
|
|
c_location: getPartner ? getPartner.terminal_name : 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;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`500 internal server error - backend server | ${error.message}`
|
|
);
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// GENERATE LOAD BALANCE NUMBER
|
|
const generateLoadBalanceNumber = async (data, date, counter) => {
|
|
let code = data.merchant_code ? data.merchant_code : "XXXXXXX";
|
|
|
|
let prefix = "LDB";
|
|
let loadBalanceNumber =
|
|
prefix + "" + code + "" + "" + date + counter.toString().padStart(3, "0");
|
|
|
|
// insert to temp load balance number
|
|
let insert = await model.insertLoadBalanceNumber(loadBalanceNumber);
|
|
if (!insert) {
|
|
loadBalanceNumber = "XX";
|
|
}
|
|
return loadBalanceNumber;
|
|
};
|
|
|
|
module.exports = {
|
|
validate,
|
|
loadingBalanceRequest,
|
|
loadingBalance,
|
|
loadingBalanceInsert,
|
|
loadingBalanceUpdate,
|
|
inquiryLoadingBalance,
|
|
};
|