mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 10:56:27 +00:00
325 lines
8.9 KiB
JavaScript
325 lines
8.9 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("card_number")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Card Number is required"),
|
|
body("issuer").not().isEmpty().withMessage("Issuer is required"),
|
|
];
|
|
case "loadingBalance":
|
|
return [
|
|
body("mid").not().isEmpty().withMessage("MID is required"),
|
|
body("tid").not().isEmpty().withMessage("TID 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("issuer").not().isEmpty().withMessage("Issuer is required"),
|
|
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(400).json({
|
|
code: "01",
|
|
message: err.errors[0].msg,
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// param
|
|
let { mid, tid, card_number, issuer } = 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(400).json({
|
|
code: "01",
|
|
message: "terminal id is not valid",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// check card number
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
if (!checkCardNumber) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: "card number is not registered",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// get issuer
|
|
let getIssuer = await model.getIssuer(issuer);
|
|
if (!getIssuer) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: "issuer doesn't exist",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
issuer,
|
|
now
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: "reservation doesn't exist",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
let totalReservation = getReservationPending.length;
|
|
let amountTopup = 0;
|
|
let reservationNumber = [];
|
|
getReservationPending.forEach((rsv) => {
|
|
amountTopup += parseInt(rsv.i_reservation_amount);
|
|
reservationNumber.push(rsv.c_reservation_no);
|
|
});
|
|
|
|
// get BI regulation status
|
|
const getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
if (getBIRegulationStatus.c_value == "A") {
|
|
// 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(400).json({
|
|
code: "01",
|
|
message: "your card number has been limit top up for this month",
|
|
data: [],
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
let result = {
|
|
amount_topup: amountTopup,
|
|
total_reservation: totalReservation,
|
|
reservation_number: reservationNumber,
|
|
};
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "reservation exists",
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
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(400).json({
|
|
code: "01",
|
|
message: err.errors[0].msg,
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// param
|
|
let { mid, tid, card_number, issuer, amount, reservation_number } =
|
|
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(400).json({
|
|
code: "01",
|
|
message: "terminal id is not valid",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// get issuer
|
|
let getIssuer = await model.getIssuer(issuer);
|
|
if (!getIssuer) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: "issuer doesn't exist",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
issuer,
|
|
now
|
|
);
|
|
|
|
if (getReservationPending.length < 1) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: "reservation doesn't exist",
|
|
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,
|
|
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,
|
|
i_issuer: getIssuer ? getIssuer.i_issuer : null,
|
|
i_issuer_code: getIssuer ? getIssuer.issuer_code : null,
|
|
i_issuer_name: getIssuer ? getIssuer.issuer_name : null,
|
|
i_issuer_fee: getIssuer ? getIssuer.issuer_fee : null,
|
|
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,
|
|
};
|
|
|
|
let detail = [];
|
|
for (let index = 0; index < reservation_number.length; index++) {
|
|
var reservation = await model.findReservation(reservation_number[index]);
|
|
|
|
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_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,
|
|
});
|
|
}
|
|
|
|
let result = await model.insertLoadingBalance(
|
|
data,
|
|
detail,
|
|
reservation_number,
|
|
now
|
|
);
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "balance loaded successfully",
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
validate,
|
|
loadingBalanceRequest,
|
|
loadingBalance,
|
|
};
|