added two routes for getting and setting the daily limit of users

This commit is contained in:
2025-10-28 17:59:22 +05:30
parent 7757b464b3
commit 654b4ddaf7
4 changed files with 75 additions and 16 deletions

View File

@@ -10,7 +10,7 @@ function verifyClient(req, res, next) {
return res
.status(401)
.json({ error: 'missing or invalid client type header' });
.json({ error: 'MISSING OR INVALID CLIENT TYPE HEADER' });
}
req.client = clientHeader;

View File

@@ -8,14 +8,14 @@ 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;
logger.info(
`dailyLimit = ${dailyLimit} | usedLimit = ${usedLimit} | remainingLimit = ${remainingLimit}`
);
if (amount > remainingLimit) {
const midnight = new Date();
midnight.setHours(24, 0, 0, 0);

View File

@@ -1,5 +1,13 @@
const customerController = require('../controllers/customer_details.controller');
const {
getDailyLimit,
getUsedLimit,
setDailyLimit,
} = require('../services/paymentLimit.service');
const { logger } = require('../util/logger');
const express = require('express');
const router = express.Router();
const customerRoute = async (req, res) => {
const customerNo = req.user;
@@ -12,4 +20,45 @@ const customerRoute = async (req, res) => {
}
};
module.exports = customerRoute;
const limitRoute = async (req, res) => {
const customerNo = req.user;
const client = req.client;
try {
const dailyLimit = await getDailyLimit(customerNo, client);
if (!dailyLimit) {
return res.status(400).json({ error: 'NO DAILY LIMIT SET FOR USER' });
}
const usedLimit = await getUsedLimit(customerNo, client);
res.json({ dailyLimit: dailyLimit, usedLimit: usedLimit });
} catch (err) {
logger.error(err, 'Unknown error encountered while checking daily limit');
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
};
const limitChangeRoute = async (req, res) => {
const customerNo = req.user;
const client = req.client;
const { amount } = req.body;
const numericLimit = Number(amount);
if (!Number.isFinite(numericLimit)) {
logger.error(`Invalid new Limit, found: ${newLimit}`);
return res
.status(400)
.json({ error: 'NEW LIMIT AMOUNT IS REQUIRED WHEN SETTING LIMIT' });
}
try {
await setDailyLimit(customerNo, client, numericLimit);
return res.status(200).json({ message: 'LIMIT SET' });
} catch (err) {
logger.error(err, 'Unexpected error while setting limit amount');
res.status(500).json({ error: 'INTERNAL SERVER ERROR' });
}
};
router.get('/', customerRoute);
router.get('/daily-limit', limitRoute);
router.post('/daily-limit', limitChangeRoute);
module.exports = router;

View File

@@ -1,10 +1,7 @@
const db = require('../config/db');
const { logger } = require('../util/logger');
async function getDailyLimit(customerNo, clientType) {
if (clientType !== 'IB' && clientType !== 'MB') {
throw new Error('Invalid client type. IB and MB accepted');
}
let query = '';
if (clientType === 'IB') {
query = `SELECT inb_limit_amount AS daily_limit FROM users WHERE customer_no = $1`;
@@ -17,12 +14,25 @@ async function getDailyLimit(customerNo, clientType) {
}
async function getUsedLimit(customerNo, clientType) {
if (clientType !== 'IB' && clientType !== 'MB') {
throw new Error('Invalid client type. IB and MB accepted');
}
let query = `SELECT SUM(amount) AS used_limit FROM transactions WHERE created_at BETWEEN CURRENT_DATE AND (CURRENT_DATE + INTERVAL '1 day') AND customer_no = $1 AND client = $2`;
let query = `SELECT SUM(amount::numeric) AS used_limit FROM transactions WHERE created_at BETWEEN CURRENT_DATE AND (CURRENT_DATE + INTERVAL '1 day') AND customer_no = $1 AND client = $2`;
const result = await db.query(query, [customerNo, clientType]);
return result.rows[0].used_limit;
const usedLimit = result.rows[0].used_limit;
return Number(usedLimit);
}
module.exports = { getDailyLimit, getUsedLimit };
async function setDailyLimit(customerNo, clientType, amount) {
let query = '';
if (clientType === 'IB') {
query = `UPDATE users SET inb_limit_amount = $1 WHERE customer_no = $2`;
} else {
query = `UPDATE users SET mobile_limit_amount = $1 WHERE customer_no = $2`;
}
const result = await db.query(query, [amount, customerNo]);
if (result.rowCount === 0) {
throw new Error('No rows affected');
}
logger.info(`set new limit: ${result.rowCount} rows affected`);
}
module.exports = { getDailyLimit, getUsedLimit, setDailyLimit };