This commit is contained in:
2022-06-07 16:49:59 +07:00
parent 6e587b2c08
commit 3a23ae0bc1
2 changed files with 93 additions and 24 deletions
@@ -85,21 +85,24 @@ const loadingBalanceRequest = async (req, res) => {
}
// check last flag
let checkFlag = await model.checkFlag(card_number, uid.toLowerCase());
let checkFlag = await model.checkFlag(card_number, uid.toUpperCase());
let lastFlag = null;
if (checkFlag) {
lastFlag = checkFlag.c_flag;
}
// get total reservation pending
let getReservationPendingWithFlag = await model.getReservationPendingWithFlag(
card_number,
now
);
let getReservationPendingWithFlag =
await model.getReservationPendingWithFlag(card_number, now);
if (getReservationPendingWithFlag) {
if (lastFlag != flag) {
console.log(getReservationPendingWithFlag);
if (lastFlag && lastFlag != flag) {
// update
await updateReservationAndLoadingBalance(getReservationPendingWithFlag.c_reservation_no, mid, tid);
await updateReservationAndLoadingBalance(
getReservationPendingWithFlag,
mid,
tid
);
return res.status(200).json({
code: "01",
message: "your card number has been cleared successfully",
@@ -108,6 +111,9 @@ const loadingBalanceRequest = async (req, res) => {
}
}
// update flag from Server
await model.updateFlag(now, card_number, uid.toUpperCase(), balance, flag);
// get total reservation pending
let getReservationPending = await model.getReservationPending(
card_number,
@@ -125,6 +131,7 @@ const loadingBalanceRequest = async (req, res) => {
let totalReservation = 0;
let amountTopup = 0;
let reservationNumber = [];
let reservationNumberTemp = [];
let i = 0;
getReservationPending.forEach((rsv) => {
if (rsv.c_flag) {
@@ -138,8 +145,12 @@ const loadingBalanceRequest = async (req, res) => {
reservationNumber.push(rsv.c_reservation_no);
i++;
}
reservationNumberTemp.push(rsv.c_reservation_no);
});
// update status to in progress
await model.updateStatusReservation(reservationNumberTemp, now);
totalReservation = i;
if (totalReservation == 0) {
return res.status(200).json({
@@ -423,7 +434,7 @@ const loadingBalance = async (req, res) => {
now,
write_status,
card_number,
uid.toLowerCase(),
uid.toUpperCase(),
balance,
flag
);
@@ -523,8 +534,11 @@ const inquiryLoadingBalance = async (req, res) => {
}
};
const updateReservationAndLoadingBalance = async (reservationNumber, mid, tid) => {
const updateReservationAndLoadingBalance = async (
reservationNumber,
mid,
tid
) => {
// get reservation
let dataReservation = [];
let getLastReservationFlag = await model.getLastReservationFlag(
@@ -546,18 +560,19 @@ const updateReservationAndLoadingBalance = async (reservationNumber, mid, tid) =
// get detail load balance
let dataLoadingBalanceDetail = [];
let getLastLoadingBalanceDetailFlag =
await model.getLastLoadingBalanceDetailFlag(dataLoadingBalance.i_load_balance);
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,
@@ -58,7 +58,7 @@ const getReservationPending = (cardNumber, time) => {
// .count("* as total_reservation")
.select("i_reservation_amount", "c_reservation_no", "c_flag")
.where("c_card_number", cardNumber)
.where("c_status", "P")
.whereIn("c_status", ["P", "I"])
.where("d_expired", ">=", time);
return result;
};
@@ -70,9 +70,8 @@ const getReservationPendingWithFlag = (cardNumber, time) => {
// .count("* as total_reservation")
.select("i_reservation_amount", "c_reservation_no", "c_flag")
.where("c_card_number", cardNumber)
.where("c_status", "P")
.whereNotNull("c_flag")
.first();
.where("c_status", "I")
.whereNotNull("c_flag");
return result;
};
@@ -222,8 +221,8 @@ const insertLoadingBalance = async (
.transacting(trx)
.update({
c_flag: writeStatus ? "FW" : null,
c_status: writeStatus ? "P" : "A",
c_status_name: writeStatus ? "PENDING" : "LOADED",
c_status: writeStatus ? "I" : "A",
c_status_name: writeStatus ? "IN PROGRESS" : "LOADED",
d_modify: now,
i_modify: null,
c_modify_username: null,
@@ -236,7 +235,7 @@ const insertLoadingBalance = async (
return knex("trx.t_d_flag_counting")
.transacting(trx)
.where("c_card_number", cardNumber)
.whereRaw("LOWER(c_uid) = ?", [uid])
.whereRaw("UPPER(c_uid) = ?", [uid])
.del();
})
.then(() => {
@@ -290,7 +289,7 @@ const generateLoadBalanceNumber = (data, date, counter) => {
const checkFlag = (cardNumber, uid) => {
let result = knex("trx.t_d_flag_counting")
.where("c_card_number", cardNumber)
.whereRaw("LOWER(c_uid) = ?", [uid])
.whereRaw("UPPER(c_uid) = ?", [uid])
.orderBy("d_request", "desc")
.first();
return result;
@@ -347,7 +346,7 @@ const insertReservationAndLoadingBalance = async (
c_modify_username: null,
c_modify_fullname: null,
})
.where("c_reservation_no", reservationNumber);
.whereIn("c_reservation_no", reservationNumber);
})
.then(trx.commit)
.catch(trx.rollback);
@@ -365,7 +364,7 @@ const insertReservationAndLoadingBalance = async (
//
const getLastReservationFlag = (reservationNumber) => {
let result = knex("trx.t_d_reservation_balance")
.where("c_reservation_no", reservationNumber)
.whereIn("c_reservation_no", reservationNumber)
.whereNotNull("c_flag")
.first();
return result;
@@ -380,7 +379,7 @@ const getLastLoadingBalanceFlag = (reservationNumber) => {
"trx.t_d_load_balance_detail.i_load_balance",
"trx.t_d_load_balance.i_load_balance"
)
.where("trx.t_d_load_balance_detail.c_reservation_no", reservationNumber)
.whereIn("trx.t_d_load_balance_detail.c_reservation_no", reservationNumber)
.where("trx.t_d_load_balance.c_status", "F")
.first();
return result;
@@ -395,6 +394,59 @@ const getLastLoadingBalanceDetailFlag = (loadBalanceId) => {
return result;
};
// update
const updateStatusReservation = (reservationNumber, now) => {
let result = knex("trx.t_d_reservation_balance")
.update({
c_status: "I",
c_status_name: "IN PROGRESS",
d_modify: now,
i_modify: null,
c_modify_username: null,
c_modify_fullname: null,
})
.whereIn("c_reservation_no", reservationNumber);
return result;
}
// update flag
const updateFlag = async (
now,
cardNumber,
uid,
balance,
flag) => {
await knex
.transaction((trx) => {
knex("trx.t_d_flag_counting")
.transacting(trx)
.where("c_card_number", cardNumber)
.whereRaw("UPPER(c_uid) = ?", [uid])
.del()
.then(() => {
// insert flag counting
return knex("trx.t_d_flag_counting").transacting(trx).insert({
d_request: now,
c_card_number: cardNumber,
c_uid: uid,
i_balance: balance,
c_flag: flag,
});
})
.then(trx.commit)
.catch(trx.rollback);
})
.then((result) => {
// console.log(result);
return result;
})
.catch((error) => {
console.log(error.message);
return error.message;
});
}
module.exports = {
checkCardNumber,
getPartner,
@@ -416,4 +468,6 @@ module.exports = {
getLastReservationFlag,
getLastLoadingBalanceFlag,
getLastLoadingBalanceDetailFlag,
updateStatusReservation,
updateFlag,
};