mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 22:46:26 +00:00
add queue payment recon
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
var queueRecon = [];
|
||||
|
||||
const reconReceive = (reservationNumber) => {
|
||||
queueRecon.push(reservationNumber);
|
||||
|
||||
// console.log(`QUEUE RECON : ${queueRecon}`);
|
||||
|
||||
return queueRecon;
|
||||
};
|
||||
|
||||
const reconFinish = (queueRecon) => {
|
||||
queueRecon.shift();
|
||||
|
||||
// console.log(`SHIFT QUEUE RECON : ${queueRecon}`);
|
||||
|
||||
return queueRecon;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
reconReceive,
|
||||
reconFinish,
|
||||
};
|
||||
@@ -2,12 +2,14 @@ const { param, body, validationResult } = require("express-validator");
|
||||
const helper = require("../../helpers/helper");
|
||||
const winston = require("../../helpers/winston.logger");
|
||||
const model = require("./paymentrecon.model");
|
||||
const queue = require("../../helpers/queue");
|
||||
const moment = require("moment");
|
||||
const { default: axios } = require("axios");
|
||||
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
moment.locale("id");
|
||||
|
||||
var maxLoopParameter = 100;
|
||||
var dataQueue = [];
|
||||
|
||||
// VALIDATION
|
||||
const validate = (method) => {
|
||||
@@ -104,9 +106,6 @@ const paymentRecon = async (req, res) => {
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// time
|
||||
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`${uniqueCode} checking partner...`);
|
||||
|
||||
@@ -127,296 +126,291 @@ const paymentRecon = async (req, res) => {
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
dataQueue = queue.reconReceive(reservation_number);
|
||||
|
||||
let partnerCode = getPartner.partner_code;
|
||||
let PaymentReconNumber;
|
||||
let maxLoop = 0;
|
||||
do {
|
||||
await _sleep(helper.randomNumberInt());
|
||||
// await _sleep(1000);
|
||||
PaymentReconNumber = await generatePaymentReconNumber(partnerCode, now);
|
||||
|
||||
if (maxLoop > maxLoopParameter) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
`server is busy. please try again.`
|
||||
var i = 0;
|
||||
while (dataQueue.length != 0) {
|
||||
PaymentReconNumber = await generatePaymentReconNumber(
|
||||
partnerCode,
|
||||
moment().format("YYYY-MM-DD HH:mm:ss")
|
||||
);
|
||||
if (PaymentReconNumber != "XX") {
|
||||
queue.reconFinish(dataQueue);
|
||||
i++;
|
||||
|
||||
// check ref no and amount
|
||||
let checkReservationNumberAndAmountAlreadyRecon =
|
||||
await model.checkReservationNumberAndAmountAlreadyRecon(
|
||||
reservation_number
|
||||
);
|
||||
if (checkReservationNumberAndAmountAlreadyRecon) {
|
||||
result = {
|
||||
code: "04",
|
||||
message: `success. Transaction has been recon at ${checkReservationNumberAndAmountAlreadyRecon.d_payment_recon}`,
|
||||
data: {
|
||||
payment_recon_no:
|
||||
checkReservationNumberAndAmountAlreadyRecon.c_payment_recon_no,
|
||||
recon_date:
|
||||
checkReservationNumberAndAmountAlreadyRecon.d_payment_recon,
|
||||
},
|
||||
};
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} checking reservation canceled status...`
|
||||
);
|
||||
|
||||
return {
|
||||
code: "500",
|
||||
message: "server is busy. please try again.",
|
||||
data: {},
|
||||
// check reservation status
|
||||
let checkReservationCanceledStatus =
|
||||
await model.checkReservationCanceledStatus(reservation_number);
|
||||
if (checkReservationCanceledStatus) {
|
||||
result = {
|
||||
code: "431",
|
||||
message: "transaction has been canceled",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} checking total transaction and amount by reservation number...`
|
||||
);
|
||||
|
||||
// check ref no and amount
|
||||
let check = await model.checkReservationNumberAndAmount(
|
||||
reservation_number
|
||||
);
|
||||
if (!check) {
|
||||
result = {
|
||||
code: "427",
|
||||
message: "transaction doesn't exists",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let totalTransactions = check.total_transaction;
|
||||
|
||||
if (totalTransactions > 1) {
|
||||
result = {
|
||||
code: "424",
|
||||
message: "Duplicate Ref No.",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let totalAmount = check.total_amount;
|
||||
|
||||
if (parseInt(totalAmount) != parseInt(amount)) {
|
||||
result = {
|
||||
code: "425",
|
||||
message: "Total amount is different.",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let data = {
|
||||
reservation_number: reservation_number,
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
date: date,
|
||||
time: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`${uniqueCode} updating payment recon number...`);
|
||||
|
||||
let update = await model.updatePaymentReconNo(data);
|
||||
if (!update) {
|
||||
result = {
|
||||
code: "428",
|
||||
message:
|
||||
"failed recon. The other transaction with same data is running.",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
},
|
||||
};
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
result = {
|
||||
code: "00",
|
||||
message: "success",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
},
|
||||
};
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
moment().format("YYYY-MM-DD HH:mm:ss"),
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"S",
|
||||
"SUCCESS",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log info
|
||||
winston.logger.info(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
maxLoop++;
|
||||
} while (PaymentReconNumber == "XX");
|
||||
|
||||
// check ref no and amount
|
||||
let checkReservationNumberAndAmountAlreadyRecon =
|
||||
await model.checkReservationNumberAndAmountAlreadyRecon(
|
||||
reservation_number
|
||||
);
|
||||
if (checkReservationNumberAndAmountAlreadyRecon) {
|
||||
result = {
|
||||
code: "04",
|
||||
message: `success. Transaction has been recon at ${checkReservationNumberAndAmountAlreadyRecon.d_payment_recon}`,
|
||||
data: {
|
||||
payment_recon_no:
|
||||
checkReservationNumberAndAmountAlreadyRecon.c_payment_recon_no,
|
||||
recon_date:
|
||||
checkReservationNumberAndAmountAlreadyRecon.d_payment_recon,
|
||||
},
|
||||
};
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} checking reservation canceled status...`
|
||||
);
|
||||
|
||||
// check reservation status
|
||||
let checkReservationCanceledStatus =
|
||||
await model.checkReservationCanceledStatus(reservation_number);
|
||||
if (checkReservationCanceledStatus) {
|
||||
result = {
|
||||
code: "431",
|
||||
message: "transaction has been canceled",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: now,
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
now,
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} checking total transaction and amount by reservation number...`
|
||||
);
|
||||
|
||||
// check ref no and amount
|
||||
let check = await model.checkReservationNumberAndAmount(reservation_number);
|
||||
if (!check) {
|
||||
result = {
|
||||
code: "427",
|
||||
message: "transaction doesn't exists",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: now,
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
now,
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let totalTransactions = check.total_transaction;
|
||||
|
||||
if (totalTransactions > 1) {
|
||||
result = {
|
||||
code: "424",
|
||||
message: "Duplicate Ref No.",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: now,
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
now,
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let totalAmount = check.total_amount;
|
||||
|
||||
if (parseInt(totalAmount) != parseInt(amount)) {
|
||||
result = {
|
||||
code: "425",
|
||||
message: "Total amount is different.",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: now,
|
||||
},
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
now,
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"F",
|
||||
"FAILED",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let data = {
|
||||
reservation_number: reservation_number,
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
date: date,
|
||||
time: now,
|
||||
};
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`${uniqueCode} updating payment recon number...`);
|
||||
|
||||
let update = await model.updatePaymentReconNo(data);
|
||||
if (!update) {
|
||||
result = {
|
||||
code: "428",
|
||||
message:
|
||||
"failed recon. The other transaction with same data is running.",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: now,
|
||||
},
|
||||
};
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
`${uniqueCode} inserting payment recon transaction...`
|
||||
);
|
||||
|
||||
result = {
|
||||
code: "00",
|
||||
message: "success",
|
||||
data: {
|
||||
payment_recon_no: PaymentReconNumber,
|
||||
recon_date: now,
|
||||
},
|
||||
};
|
||||
|
||||
// insert payment recon transaction
|
||||
await model.insertPaymentReconTransaction(
|
||||
date,
|
||||
now,
|
||||
getPartner.partner_code,
|
||||
PaymentReconNumber,
|
||||
bank_ref,
|
||||
reservation_number,
|
||||
amount,
|
||||
"S",
|
||||
"SUCCESS",
|
||||
result.code,
|
||||
result.message.toUpperCase(),
|
||||
result.data
|
||||
);
|
||||
|
||||
// log info
|
||||
winston.logger.info(
|
||||
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
@@ -499,9 +493,7 @@ const paymentReconSemiTopupOnline = async (req, res) => {
|
||||
|
||||
if (maxLoop > maxLoopParameter) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
`server is busy. please try again.`
|
||||
);
|
||||
winston.logger.error(`server is busy. please try again.`);
|
||||
|
||||
return {
|
||||
code: "500",
|
||||
@@ -908,6 +900,16 @@ const paymentReconToCMS = async (req, res) => {
|
||||
amount: "",
|
||||
};
|
||||
|
||||
if (recon.length < 1) {
|
||||
let result = {
|
||||
code: "01",
|
||||
message: "no data sent to payment recon.",
|
||||
data: {},
|
||||
};
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
recon.forEach((element) => {
|
||||
bodyParameters = {
|
||||
date: date,
|
||||
@@ -916,11 +918,11 @@ const paymentReconToCMS = async (req, res) => {
|
||||
amount: element.i_reservation_amount,
|
||||
};
|
||||
|
||||
axios.post(
|
||||
"http://localhost:4000/api/payment-recon",
|
||||
bodyParameters,
|
||||
config
|
||||
);
|
||||
axios
|
||||
.post("http://localhost:4000/api/payment-recon", bodyParameters, config)
|
||||
.then((response) => {
|
||||
console.log(`response: ${JSON.stringify(response.data)}`);
|
||||
});
|
||||
});
|
||||
|
||||
let result = {
|
||||
|
||||
@@ -147,8 +147,10 @@ const getLastCounterPaymentReconNo = (partnerCode, reconDate) => {
|
||||
const getReservationToRecon = (date) => {
|
||||
let result = knex("trx.t_d_reservation_balance")
|
||||
.select("c_reservation_no", "i_reservation_amount")
|
||||
.whereNull('c_payment_recon_no')
|
||||
.whereRaw("??::date = ?", ["d_reservation", date]);
|
||||
.whereNull("d_cancel")
|
||||
.whereNull("c_payment_recon_no")
|
||||
.whereRaw("??::date = ?", ["d_reservation", date])
|
||||
.orderBy("c_reservation_no", "asc");
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user