mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 20:46:28 +00:00
update
This commit is contained in:
@@ -10,6 +10,8 @@ const validate = (method) => {
|
||||
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()
|
||||
@@ -19,6 +21,8 @@ const validate = (method) => {
|
||||
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()
|
||||
@@ -48,14 +52,14 @@ const loadingBalanceRequest = async (req, res) => {
|
||||
const err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "400",
|
||||
message: err.errors[0].msg,
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
|
||||
// param
|
||||
let { mid, tid, card_number } = req.body;
|
||||
let { mid, tid, card_number, uid, balance, flag } = req.body;
|
||||
|
||||
// time
|
||||
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||
@@ -64,7 +68,7 @@ const loadingBalanceRequest = async (req, res) => {
|
||||
let getPartner = await model.getPartner(mid, tid);
|
||||
if (!getPartner) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "terminal id is not valid",
|
||||
data: {},
|
||||
});
|
||||
@@ -74,12 +78,36 @@ const loadingBalanceRequest = async (req, res) => {
|
||||
let checkCardNumber = await model.checkCardNumber(card_number);
|
||||
if (!checkCardNumber) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
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,
|
||||
@@ -88,23 +116,57 @@ const loadingBalanceRequest = async (req, res) => {
|
||||
|
||||
if (getReservationPending.length < 1) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "reservation doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
|
||||
let totalReservation = getReservationPending.length;
|
||||
let totalReservation = 0;
|
||||
let amountTopup = 0;
|
||||
let reservationNumber = [];
|
||||
let i = 0;
|
||||
getReservationPending.forEach((rsv) => {
|
||||
amountTopup += parseInt(rsv.i_reservation_amount);
|
||||
reservationNumber.push(rsv.c_reservation_no);
|
||||
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: {},
|
||||
});
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
@@ -116,7 +178,7 @@ const loadingBalanceRequest = async (req, res) => {
|
||||
parseInt(getMaxTopupPerMonth.c_value)
|
||||
) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "your card number has been limit top up for this month",
|
||||
data: {},
|
||||
});
|
||||
@@ -136,14 +198,109 @@ const loadingBalanceRequest = async (req, res) => {
|
||||
data: result,
|
||||
});
|
||||
} catch (error) {
|
||||
return res.status(500).json({
|
||||
code: "01",
|
||||
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
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// LOAD BALANCE
|
||||
const loadingBalance = async (req, res) => {
|
||||
try {
|
||||
@@ -151,15 +308,25 @@ const loadingBalance = async (req, res) => {
|
||||
const err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "400",
|
||||
message: err.errors[0].msg,
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
|
||||
// param
|
||||
let { mid, tid, card_number, amount, reservation_number, ref_no } =
|
||||
req.body;
|
||||
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");
|
||||
@@ -169,7 +336,7 @@ const loadingBalance = async (req, res) => {
|
||||
let getPartner = await model.getPartner(mid, tid);
|
||||
if (!getPartner) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "terminal id is not valid",
|
||||
data: {},
|
||||
});
|
||||
@@ -183,7 +350,7 @@ const loadingBalance = async (req, res) => {
|
||||
|
||||
if (!reservationStatus) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "reservation number doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
@@ -197,7 +364,9 @@ const loadingBalance = async (req, res) => {
|
||||
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"),
|
||||
message:
|
||||
"reservation has been successfully loaded on " +
|
||||
moment(loadDate).format("DD MMM YYYY HH:mm:ss"),
|
||||
data: {
|
||||
load_balance_number: loadBalanceNumberExisting,
|
||||
},
|
||||
@@ -213,7 +382,7 @@ const loadingBalance = async (req, res) => {
|
||||
|
||||
if (getReservationPending.length < 1) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "reservation doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
@@ -227,7 +396,7 @@ const loadingBalance = async (req, res) => {
|
||||
|
||||
if (amount != amountTopup) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "topup amount is different",
|
||||
data: {},
|
||||
});
|
||||
@@ -268,8 +437,8 @@ const loadingBalance = async (req, res) => {
|
||||
i_topup_amount: amount,
|
||||
c_return_sap: null,
|
||||
i_load_balance_no_seq: loadBalanceNoSequence,
|
||||
c_status: "A",
|
||||
c_status_name: "SUCCESS",
|
||||
c_status: write_status ? "F" : "A",
|
||||
c_status_name: write_status ? "FAILED" : "SUCCESS",
|
||||
d_create: now,
|
||||
i_create: null,
|
||||
c_create_username: null,
|
||||
@@ -286,6 +455,15 @@ const loadingBalance = async (req, res) => {
|
||||
|
||||
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
|
||||
@@ -323,18 +501,30 @@ const loadingBalance = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
await model.insertLoadingBalance(data, detail, reservation_number, now);
|
||||
await model.insertLoadingBalance(
|
||||
data,
|
||||
detail,
|
||||
reservation_number,
|
||||
now,
|
||||
write_status,
|
||||
card_number,
|
||||
uid,
|
||||
balance,
|
||||
flag
|
||||
);
|
||||
|
||||
return res.status(201).json({
|
||||
return res.status(200).json({
|
||||
code: "00",
|
||||
message: "balance loaded successfully",
|
||||
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(500).json({
|
||||
code: "01",
|
||||
return res.status(200).json({
|
||||
code: "404",
|
||||
message: error.message,
|
||||
data: {},
|
||||
});
|
||||
@@ -353,7 +543,7 @@ const inquiryLoadingBalance = async (req, res) => {
|
||||
// validation parameter inquiry
|
||||
if (!load_balance_number && !card_number) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "400",
|
||||
message:
|
||||
"bad request. please add the load balance number or card number!",
|
||||
data: {},
|
||||
@@ -363,7 +553,7 @@ const inquiryLoadingBalance = async (req, res) => {
|
||||
// validation inquiry date
|
||||
if ((!start_date && end_date) || (start_date && !end_date)) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "400",
|
||||
message: "bad request. please check the date!",
|
||||
data: {},
|
||||
});
|
||||
@@ -377,7 +567,7 @@ const inquiryLoadingBalance = async (req, res) => {
|
||||
|
||||
if (!byLoadBalanceNumber) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "load balance doesn't exist",
|
||||
data: [],
|
||||
});
|
||||
@@ -397,7 +587,7 @@ const inquiryLoadingBalance = async (req, res) => {
|
||||
|
||||
if (!byCardNumber) {
|
||||
return res.status(200).json({
|
||||
code: "01",
|
||||
code: "403",
|
||||
message: "load balance doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
@@ -410,8 +600,8 @@ const inquiryLoadingBalance = async (req, res) => {
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
return res.status(500).json({
|
||||
code: "01",
|
||||
return res.status(200).json({
|
||||
code: "404",
|
||||
message: error.message,
|
||||
data: {},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user