39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
const db = require('../config/db');
|
|
const { logger } = require('../util/logger');
|
|
|
|
async function getDailyLimit(customerNo, clientType) {
|
|
let query = '';
|
|
if (clientType === 'IB') {
|
|
query = `SELECT inb_limit_amount AS daily_limit FROM users WHERE customer_no = $1`;
|
|
} else {
|
|
query = `SELECT mobile_limit_amount AS daily_limit FROM users WHERE customer_no = $1`;
|
|
}
|
|
|
|
const result = await db.query(query, [customerNo]);
|
|
return result.rows[0].daily_limit;
|
|
}
|
|
|
|
async function getUsedLimit(customerNo, clientType) {
|
|
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]);
|
|
const usedLimit = result.rows[0].used_limit;
|
|
return Number(usedLimit);
|
|
}
|
|
|
|
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 };
|