added middleware to check if the transaction is within cooldown period
This commit is contained in:
28
src/middlewares/cooldown.middleware.js
Normal file
28
src/middlewares/cooldown.middleware.js
Normal 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 };
|
||||||
Reference in New Issue
Block a user