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

@@ -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 };