mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
318 lines
8.9 KiB
JavaScript
318 lines
8.9 KiB
JavaScript
const { param, body, validationResult } = require("express-validator");
|
|
const model = require("./reservebalance.model");
|
|
const moment = require("moment");
|
|
moment.locale("id");
|
|
|
|
// VALIDATION
|
|
const validate = (method) => {
|
|
switch (method) {
|
|
case "reservation":
|
|
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"),
|
|
];
|
|
case "updatePayment":
|
|
return [
|
|
body("reservation_number")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Reservation Number is required"),
|
|
body("payment_ref_no")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Payment Reference Number is required"),
|
|
];
|
|
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
|
|
// FIND RESERVATION
|
|
const findReservation = async (req, res) => {};
|
|
|
|
// RESERVATION
|
|
const reservation = async (req, res) => {
|
|
try {
|
|
// check validator
|
|
const err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message: err.errors[0].msg,
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// param
|
|
let { mid, tid, card_number, amount } = 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: "01",
|
|
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: "01",
|
|
message: "card number is not registered",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// get issuer
|
|
let getIssuer = await model.getIssuer("test");
|
|
// if (!getIssuer) {
|
|
// return res.status(200).json({
|
|
// code: "01",
|
|
// message: "issuer doesn't exist",
|
|
// data: [],
|
|
// });
|
|
// }
|
|
|
|
// get max reservation
|
|
let getMaxReservation = await model.getMaxReservation();
|
|
let maxReservation = getMaxReservation.c_value
|
|
? getMaxReservation.c_value
|
|
: 1;
|
|
|
|
// get total reservation pending
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now
|
|
);
|
|
let totalReservationPending =
|
|
getReservationPending.total_reservation > 0
|
|
? getReservationPending.total_reservation
|
|
: 0;
|
|
|
|
if (totalReservationPending >= maxReservation) {
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message:
|
|
"your card number has some pending transaction. Please finished it!",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// get BI regulation status
|
|
const getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
if (getBIRegulationStatus.c_value == "A") {
|
|
// get accumulated balance in this month
|
|
let accumulatedBalance = 19950000;
|
|
|
|
// validate max topup per month
|
|
const getMaxTopupPerMonth = await model.getMaxTopupPerMonth();
|
|
if (getMaxTopupPerMonth.c_value) {
|
|
if (
|
|
parseInt(accumulatedBalance) + parseInt(amount) >
|
|
parseInt(getMaxTopupPerMonth.c_value)
|
|
) {
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message: "your card number has been limit top up for this month",
|
|
data: [],
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// get reservation
|
|
const getReservationNumberSequence =
|
|
await model.getReservationNumberSequence(getPartner, today);
|
|
const reservationNoSequence = getReservationNumberSequence
|
|
? parseInt(getReservationNumberSequence.i_reservation_no_seq) + 1
|
|
: 1;
|
|
|
|
// generate reservation number
|
|
let reservationNumber = await model.generateReservationNumber(
|
|
getPartner,
|
|
moment(today).format("YYYYMMDD"),
|
|
reservationNoSequence
|
|
);
|
|
|
|
// get reservation expiration date by parameter
|
|
let getReservationExpirationDateValue =
|
|
await model.getReservationExpirationDateValue();
|
|
let time = getReservationExpirationDateValue.c_value
|
|
? parseInt(getReservationExpirationDateValue.c_value)
|
|
: 1;
|
|
|
|
// get reservation expiration date measure by parameter
|
|
let getReservationExpirationDateMeasure =
|
|
await model.getReservationExpirationDateMeasure();
|
|
let measure = getReservationExpirationDateMeasure.c_value
|
|
? getReservationExpirationDateMeasure.c_value
|
|
: "hours";
|
|
|
|
let expired = moment(now).add(time, measure).format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
// binding data
|
|
let data = {
|
|
c_reservation_no: reservationNumber,
|
|
d_reservation: 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_reservation_amount: amount,
|
|
i_issuer: getIssuer ? getIssuer.i_issuer : null,
|
|
c_issuer_code: getIssuer ? getIssuer.issuer_code : null,
|
|
c_issuer_name: getIssuer ? getIssuer.issuer_name : null,
|
|
i_issuer_fee: getIssuer ? getIssuer.issuer_fee : null,
|
|
i_channel: null,
|
|
c_channel_code: null,
|
|
c_channel_name: null,
|
|
i_channel_fee: null,
|
|
c_payment_ref_no: null,
|
|
i_reservation_no_seq: reservationNoSequence,
|
|
d_expired: expired,
|
|
c_status: "P",
|
|
c_status_name: "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,
|
|
};
|
|
|
|
await model.insertReservation(data);
|
|
|
|
return res.status(201).json({
|
|
code: "00",
|
|
message: "reservation saved successfully.",
|
|
data: reservationNumber,
|
|
});
|
|
} catch (error) {
|
|
return res.status(500).json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
// UPDATE PAYMENT
|
|
const updatePayment = async (req, res) => {
|
|
try {
|
|
// check validator
|
|
const err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message: err.errors[0].msg,
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// param
|
|
let { reservation_number, channel, channel_fee, payment_ref_no } = req.body;
|
|
|
|
// time
|
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
// check reservation
|
|
const findReservation = await model.findReservation(reservation_number);
|
|
if (!findReservation) {
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message: "reservation doesn't exist",
|
|
data: [],
|
|
});
|
|
} else {
|
|
if (findReservation.c_payment_ref_no) {
|
|
return res.json({
|
|
code: "00",
|
|
message: "payment already updated",
|
|
data: [],
|
|
});
|
|
}
|
|
}
|
|
|
|
// get channel
|
|
let getChannel = await model.getChannel(channel);
|
|
if (!getChannel) {
|
|
return res.status(200).json({
|
|
code: "01",
|
|
message: "channel doesn't exist",
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
// binding data
|
|
let data = {
|
|
i_channel: getChannel ? getChannel.i_channel : null,
|
|
c_channel_code: getChannel ? getChannel.channel_code : null,
|
|
c_channel_name: getChannel ? getChannel.channel_name : null,
|
|
i_channel_fee: channel_fee
|
|
? channel_fee
|
|
: getChannel
|
|
? getChannel.channel_fee
|
|
: null,
|
|
c_payment_ref_no: payment_ref_no,
|
|
c_status: "P",
|
|
c_status_name: "PENDING",
|
|
d_modify: now,
|
|
i_modify: null,
|
|
c_modify_username: null,
|
|
c_modify_fullname: null,
|
|
};
|
|
let result = await model.updatePayment(data, reservation_number);
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "payment updated successfully.",
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
return res.status(500).json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
validate,
|
|
findReservation,
|
|
reservation,
|
|
updatePayment,
|
|
};
|