added middleware to check if the transaction is within cooldown period

This commit is contained in:
2025-11-08 02:06:17 +05:30
parent 95fc26ef6b
commit f7bc0f6785

View File

@@ -0,0 +1,28 @@
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({
error: `beneficiary cooldown period active. Retry after ${remaining} minutes`,
});
}
}
next();
}
module.exports = { checkBeneficiaryCooldown };