mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 19:06:40 +00:00
628 lines
17 KiB
JavaScript
628 lines
17 KiB
JavaScript
const { param, body, validationResult } = require("express-validator");
|
|
const model = require("./loadbalance.model");
|
|
const moment = require("moment");
|
|
moment.locale("id");
|
|
|
|
// 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 {
|
|
// check validator
|
|
const err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
return res.status(200).json({
|
|
code: "400",
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// param
|
|
let { mid, tid, card_number, uid, balance, flag } = req.body;
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
if (!getPartner) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "terminal id is not valid",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// check card number
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
if (!checkCardNumber) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "card number is not registered",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// check last flag
|
|
let checkFlag = await model.checkFlag(card_number, uid);
|
|
let lastFlag = null;
|
|
if (checkFlag) {
|
|
lastFlag = checkFlag.c_flag;
|
|
}
|
|
|
|
// get total reservation pending
|
|
let getReservationPendingWithFlag = await model.getReservationPendingWithFlag(
|
|
card_number,
|
|
now
|
|
);
|
|
if (getReservationPendingWithFlag) {
|
|
if (lastFlag != flag) {
|
|
// update
|
|
await updateReservationAndLoadingBalance(getReservationPendingWithFlag.c_reservation_no, mid, tid);
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message: "your card number has been cleared successfully",
|
|
data: {},
|
|
});
|
|
}
|
|
}
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
let totalReservation = 0;
|
|
let amountTopup = 0;
|
|
let reservationNumber = [];
|
|
let i = 0;
|
|
getReservationPending.forEach((rsv) => {
|
|
if (rsv.c_flag) {
|
|
if (lastFlag == flag) {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
reservationNumber.push(rsv.c_reservation_no);
|
|
i++;
|
|
}
|
|
} else {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
reservationNumber.push(rsv.c_reservation_no);
|
|
i++;
|
|
}
|
|
});
|
|
|
|
totalReservation = i;
|
|
if (totalReservation == 0) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// check total amount topup
|
|
if (parseInt(amountTopup) <= 0) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "can't topup with amount = 0",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// get BI regulation status
|
|
const getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
if (getBIRegulationStatus.c_value == "A") {
|
|
// get max saldo on the card
|
|
const getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard();
|
|
if (getMaxAmountOnTheCard.c_value) {
|
|
if (
|
|
parseInt(balance) + parseInt(amountTopup) >
|
|
parseInt(getMaxAmountOnTheCard.c_value)
|
|
) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "your card number has reached the maximum balance",
|
|
data: {},
|
|
});
|
|
}
|
|
}
|
|
|
|
// get accumulated balance in this month
|
|
let accumulatedBalance = 19900000;
|
|
|
|
// validate max topup per month
|
|
const getMaxTopupPerMonth = await model.getMaxTopupPerMonth();
|
|
if (getMaxTopupPerMonth.c_value) {
|
|
if (
|
|
parseInt(accumulatedBalance) + parseInt(amountTopup) >
|
|
parseInt(getMaxTopupPerMonth.c_value)
|
|
) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "your card number has been limit top up for this month",
|
|
data: {},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
let result = {
|
|
amount: amountTopup,
|
|
total_reservation: totalReservation,
|
|
reservation_number: reservationNumber,
|
|
};
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "reservation exists",
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
return res.status(200).json({
|
|
code: "404",
|
|
message: error.message,
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
// LOAD BALANCE
|
|
const loadingBalance = async (req, res) => {
|
|
try {
|
|
// check validator
|
|
const err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
return res.status(200).json({
|
|
code: "400",
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// param
|
|
let {
|
|
mid,
|
|
tid,
|
|
card_number,
|
|
amount,
|
|
reservation_number,
|
|
ref_no,
|
|
write_status,
|
|
uid,
|
|
balance,
|
|
flag,
|
|
} = req.body;
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
let today = moment().format("YYYY-MM-DD");
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
if (!getPartner) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "terminal id is not valid",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// check reservation status
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
let reservationStatus = await model.findReservation(
|
|
reservation_number[index]
|
|
);
|
|
|
|
if (!reservationStatus) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "reservation number doesn't exist",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
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;
|
|
return res.status(200).json({
|
|
code: "00",
|
|
message:
|
|
"reservation has been successfully loaded on " +
|
|
moment(loadDate).format("DD MMM YYYY HH:mm:ss"),
|
|
data: {
|
|
load_balance_number: loadBalanceNumberExisting,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "reservation doesn't exist",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// check total topup and request topup
|
|
let amountTopup = 0;
|
|
getReservationPending.forEach((rsv) => {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
});
|
|
|
|
if (amount != amountTopup) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "topup amount is different",
|
|
data: {},
|
|
});
|
|
}
|
|
|
|
// get loading balance
|
|
const getLoadBalanceSequence = await model.getLoadBalanceSequence(
|
|
getPartner,
|
|
today
|
|
);
|
|
const loadBalanceNoSequence = getLoadBalanceSequence
|
|
? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1
|
|
: 1;
|
|
|
|
// generate load balance number
|
|
let loadBalanceNumber = await model.generateLoadBalanceNumber(
|
|
getPartner,
|
|
moment(today).format("YYYYMMDD"),
|
|
loadBalanceNoSequence
|
|
);
|
|
|
|
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,
|
|
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,
|
|
};
|
|
|
|
let detail = [];
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
let reservation = await model.findReservation(reservation_number[index]);
|
|
|
|
if (!reservation) {
|
|
return res.status(200).json({
|
|
code: "403",
|
|
message: "reservation number doesn't exist",
|
|
data: {},
|
|
});
|
|
}
|
|
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,
|
|
});
|
|
}
|
|
|
|
await model.insertLoadingBalance(
|
|
data,
|
|
detail,
|
|
reservation_number,
|
|
now,
|
|
write_status,
|
|
card_number,
|
|
uid,
|
|
balance,
|
|
flag
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: "00",
|
|
message: write_status
|
|
? "transaction saved successfully. Err: failed write to the card"
|
|
: "balance loaded successfully",
|
|
data: {
|
|
load_balance_number: loadBalanceNumber,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
return res.status(200).json({
|
|
code: "404",
|
|
message: error.message,
|
|
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: "403",
|
|
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: "403",
|
|
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: "404",
|
|
message: error.message,
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
const updateReservationAndLoadingBalance = async (reservationNumber, mid, tid) => {
|
|
// get reservation
|
|
let dataReservation = [];
|
|
let getLastReservationFlag = await model.getLastReservationFlag(
|
|
reservationNumber
|
|
);
|
|
if (getLastReservationFlag) {
|
|
dataReservation = getLastReservationFlag;
|
|
}
|
|
|
|
// get load balance
|
|
let dataLoadingBalance = [];
|
|
let getLastLoadingBalanceFlag = await model.getLastLoadingBalanceFlag(
|
|
reservationNumber
|
|
);
|
|
if (getLastLoadingBalanceFlag) {
|
|
dataLoadingBalance = getLastLoadingBalanceFlag;
|
|
}
|
|
|
|
// get detail load balance
|
|
let dataLoadingBalanceDetail = [];
|
|
let getLastLoadingBalanceDetailFlag =
|
|
await model.getLastLoadingBalanceDetailFlag(dataLoadingBalance.i_load_balance);
|
|
if (getLastLoadingBalanceDetailFlag) {
|
|
dataLoadingBalanceDetail = getLastLoadingBalanceDetailFlag;
|
|
}
|
|
|
|
if (dataReservation && dataLoadingBalance && dataLoadingBalanceDetail) {
|
|
|
|
// 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);
|
|
|
|
// get loading balance
|
|
const getLoadBalanceSequence = await model.getLoadBalanceSequence(
|
|
getPartner,
|
|
today
|
|
);
|
|
const loadBalanceNoSequence = getLoadBalanceSequence
|
|
? parseInt(getLoadBalanceSequence.i_load_balance_no_seq) + 1
|
|
: 1;
|
|
|
|
// generate load balance number
|
|
let loadBalanceNumber = await model.generateLoadBalanceNumber(
|
|
getPartner,
|
|
moment(today).format("YYYYMMDD"),
|
|
loadBalanceNoSequence
|
|
);
|
|
|
|
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: dataReservation.i_reservation_amount,
|
|
c_return_sap: null,
|
|
i_load_balance_no_seq: loadBalanceNoSequence,
|
|
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,
|
|
};
|
|
|
|
// insert reservation and load balance transaction
|
|
await model.insertReservationAndLoadingBalance(
|
|
reservationNumber,
|
|
data,
|
|
dataLoadingBalanceDetail,
|
|
now
|
|
);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
validate,
|
|
loadingBalanceRequest,
|
|
loadingBalance,
|
|
inquiryLoadingBalance,
|
|
};
|