29 lines
784 B
JavaScript
29 lines
784 B
JavaScript
const { logger } = require('../util/logger');
|
|
const {
|
|
getDailyLimit,
|
|
getUsedLimit,
|
|
} = require('../services/paymentLimit.service');
|
|
|
|
async function checkLimit(req, res, next) {
|
|
const { amount } = req.body;
|
|
const { user, client } = req;
|
|
const dailyLimit = await getDailyLimit(user, client);
|
|
if (!dailyLimit) {
|
|
logger.info('NO LIMIT SET FOR CUSTOMER. ALLOWING TRANSACTIONS');
|
|
next();
|
|
}
|
|
const usedLimit = await getUsedLimit(user, client);
|
|
|
|
const remainingLimit = dailyLimit - usedLimit;
|
|
|
|
if (amount > remainingLimit) {
|
|
const midnight = new Date();
|
|
midnight.setHours(24, 0, 0, 0);
|
|
res.set('Retry-After', midnight.toUTCString());
|
|
return res.status(403).json({ error: 'Daily limit exhausted' });
|
|
}
|
|
next();
|
|
}
|
|
|
|
module.exports = { checkLimit };
|