Files
yume_js/src/middlewares/cooldown.middleware.js

30 lines
956 B
JavaScript

const { logger } = require('../util/logger');
const { getSingleBeneficiary } = require('../services/beneficiary.service');
async function checkBeneficiaryCooldown(req, res, next) {
const cooldownTime = parseInt(
process.env.BENEFICIARY_COOLDOWN_TIME || '60',
10
);
const customerNo = req.user;
const { toAccount } = req.body;
const beneficiary = await getSingleBeneficiary(customerNo, toAccount);
if (beneficiary) {
const now = new Date();
const cooldownPeriod = new Date(now.getTime() - cooldownTime * 60 * 1000);
const createdAt = new Date(beneficiary['created_at']);
if (createdAt > cooldownPeriod) {
const remaining = (now - createdAt) / (60 * 1000);
logger.warn('TRANSACTION_FAILED BENEFICIARY_COOLDOWN_ACTIVE');
return res.status(403).json({
remaining,
error: 'beneficiary cooldown period active',
});
}
}
next();
}
module.exports = { checkBeneficiaryCooldown };