Files
Online-Topup-V1/application/modules/paymentrecon/paymentrecon.controller.js
T
2022-07-06 19:50:52 +07:00

379 lines
9.0 KiB
JavaScript

const { param, body, validationResult } = require("express-validator");
const helper = require("../../helpers/helper");
const winston = require("../../helpers/winston.logger");
const model = require("./paymentrecon.model");
const moment = require("moment");
moment.locale("id");
var result = {};
var uniqueCode;
// VALIDATION
const validate = (method) => {
switch (method) {
case "paymentRecon":
return [
body("date")
.not()
.isEmpty()
.withMessage("Date is required")
.isDate({ format: "YYYY-MM-DD" })
.withMessage("Invalid format date. Format is YYYY-MM-DD"),
body("ref_no").not().isEmpty().withMessage("Ref No is required"),
body("amount")
.not()
.isEmpty()
.withMessage("Amount is required")
.isNumeric()
.withMessage("Amount is invalid format"),
];
case "paymentReconBulk":
return [
body("date")
.not()
.isEmpty()
.withMessage("Date is required")
.isDate({ format: "YYYY-MM-DD" })
.withMessage("Invalid format date. Format is YYYY-MM-DD"),
body("log")
.not()
.isEmpty()
.withMessage("Log is required")
.isArray()
.withMessage("Log is invalid request"),
];
default:
break;
}
};
// PAYMENT RECON
const paymentRecon = async (req, res) => {
try {
// generate unique code
uniqueCode = await helper.getUniqueCode();
// param
let { date, bank_ref, ref_no, amount } = req.body;
let partner_id = req.partner_id_token;
// log info
winston.logger.info(
`${uniqueCode} REQUEST payment recon: ${JSON.stringify(req.body)}`
);
// check validator
const err = validationResult(req, res);
if (!err.isEmpty()) {
result = {
code: "400",
message: err.errors[0].msg,
data: {},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
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...`);
// get partner, merchant, terminal from tid
let getPartner = await model.getPartnerByID(partner_id);
if (!getPartner) {
result = {
code: "426",
message: "Partner doesn't exists.",
data: {},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
// check ref no and amount
let checkRefNoAndAmountAlreadyRecon =
await model.checkRefNoAndAmountAlreadyRecon(ref_no);
if (checkRefNoAndAmountAlreadyRecon) {
result = {
code: "04",
message: `success. Transaction has been recon at ${checkRefNoAndAmountAlreadyRecon.d_payment_recon}`,
data: {
payment_recon_no: checkRefNoAndAmountAlreadyRecon.c_payment_recon_no,
detail: {
recon_date: checkRefNoAndAmountAlreadyRecon.d_payment_recon,
duplicate_count: 0,
duplicate_data: [],
},
},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
// check ref no and amount
let check = await model.checkRefNoAndAmount(ref_no);
if (!check) {
result = {
code: "427",
message: "transaction doesn't exists",
data: {
payment_recon_no: "",
detail: {
recon_date: now,
duplicate_count: 0,
duplicate_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: "",
detail: {
recon_date: now,
duplicate_count: totalTransactions,
duplicate_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: "",
detail: {
recon_date: now,
duplicate_count: 0,
duplicate_data: [],
},
},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
let PRNumber = "XXX";
let data = {
ref_no: ref_no,
payment_recon_no: PRNumber,
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: "",
detail: {
recon_date: now,
duplicate_count: 0,
duplicate_data: [],
},
},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
result = {
code: "00",
message: "success",
data: {
payment_recon_no: PRNumber,
detail: {
recon_date: now,
duplicate_count: 0,
duplicate_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(
`500 internal server error - backend server | ${error.message}`
);
return res.status(200).json({
code: "500",
message:
process.env.NODE_ENV != "production"
? error.message
: "500 internal server error - backend server.",
data: {},
});
}
};
// PAYMENT RECON BULK
const paymentReconBulk = async (req, res) => {
try {
// generate unique code
uniqueCode = await helper.getUniqueCode();
// param
let { log } = req.body;
let partner_id = req.partner_id_token;
// log info
winston.logger.info(
`${uniqueCode} REQUEST payment recon: ${JSON.stringify(req.body)}`
);
// check validator
const err = validationResult(req, res);
if (!err.isEmpty()) {
result = {
code: "400",
message: err.errors[0].msg,
data: {},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
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...`);
// get partner, merchant, terminal from tid
let getPartner = await model.getPartnerByID(partner_id);
if (!getPartner) {
result = {
code: "403",
message: "Partner doesn't exists.",
data: {},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
let data = [];
log.forEach(async (element) => {
// check ref no and amount log
let check = await model.checkRefNoAndAmount(
element.ref_no,
element.amount
);
if (!check) {
result = {
code: "412",
message: "terminal id is not valid",
data: {},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE payment recon: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
});
// 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(
`500 internal server error - backend server | ${error.message}`
);
return res.status(200).json({
code: "500",
message:
process.env.NODE_ENV != "production"
? error.message
: "500 internal server error - backend server.",
data: {},
});
}
};
module.exports = {
validate,
paymentRecon,
paymentReconBulk,
};