diff --git a/src/controllers/report.controller.js b/src/controllers/report.controller.js index 3d63d03..7163f84 100644 --- a/src/controllers/report.controller.js +++ b/src/controllers/report.controller.js @@ -3,61 +3,61 @@ const reportService = require('../services/report.service'); const { logger } = require('../util/logger'); async function active_users(req, res) { - const { from_date, to_date } = req.body; - if (!from_date || !to_date) { - return res.status(400).json({ error: 'from_date and to_date are required' }); - } - try { - const users = await reportService.total_users(from_date, to_date); - const activeUsers = users.filter(u => u.is_first_login === false); - const inactiveUsers = users.filter(u => u.is_first_login === true); - const active_user_list = activeUsers.map(u => ({ - customer_no: u.customer_no, - user_name: u.preferred_name, - created_at: u.created_at, - last_login: u.last_login, - status: "active" - })); - logger.info(`fetch total number of users and active users from date ${from_date} to ${to_date}`); - res.json({ - total_users: users.length, - active_users: activeUsers.length, - inactive_users: inactiveUsers.length, - active_user_list: active_user_list - }); - } catch (err) { - logger.error(err, 'failed to fetch data'); - res.status(500).json({ error: 'something went wrong' }); - } + const { from_date, to_date } = req.body; + if (!from_date || !to_date) { + return res.status(400).json({ error: 'from_date and to_date are required' }); + } + try { + const users = await reportService.total_users(from_date, to_date); + const activeUsers = users.filter(u => u.is_first_login === false); + const inactiveUsers = users.filter(u => u.is_first_login === true); + const active_user_list = activeUsers.map(u => ({ + customer_no: u.customer_no, + user_name: u.preferred_name, + created_at: u.created_at, + last_login: u.last_login, + status: "active" + })); + logger.info(`fetch total number of users and active users from date ${from_date} to ${to_date}`); + res.json({ + total_users: users.length, + active_users: activeUsers.length, + inactive_users: inactiveUsers.length, + active_user_list: active_user_list + }); + } catch (err) { + logger.error(err, 'failed to fetch data'); + res.status(500).json({ error: 'something went wrong' }); + } } async function inactive_users(req, res) { - const { from_date, to_date } = req.body; - if (!from_date || !to_date) { - return res.status(400).json({ error: 'from_date and to_date are required' }); - } - try { - const users = await reportService.total_users(from_date, to_date); - const activeUsers = users.filter(u => u.is_first_login === false); - const inactiveUsers = users.filter(u => u.is_first_login === true); - const inactive_user_list = inactiveUsers.map(u => ({ - customer_no: u.customer_no, - user_name: u.preferred_name, - created_at: u.created_at, - last_login: u.last_login, - status: "in-active" - })); - logger.info(`fetch total number of users and inactive users from date ${from_date} to ${to_date}`); - res.json({ - total_users: users.length, - active_users: activeUsers.length, - inactive_users: inactiveUsers.length, - inactive_user_list: inactive_user_list - }); - } catch (err) { - logger.error(err, 'failed to fetch data'); - res.status(500).json({ error: 'something went wrong' }); - } + const { from_date, to_date } = req.body; + if (!from_date || !to_date) { + return res.status(400).json({ error: 'from_date and to_date are required' }); + } + try { + const users = await reportService.total_users(from_date, to_date); + const activeUsers = users.filter(u => u.is_first_login === false); + const inactiveUsers = users.filter(u => u.is_first_login === true); + const inactive_user_list = inactiveUsers.map(u => ({ + customer_no: u.customer_no, + user_name: u.preferred_name, + created_at: u.created_at, + last_login: u.last_login, + status: "in-active" + })); + logger.info(`fetch total number of users and inactive users from date ${from_date} to ${to_date}`); + res.json({ + total_users: users.length, + active_users: activeUsers.length, + inactive_users: inactiveUsers.length, + inactive_user_list: inactive_user_list + }); + } catch (err) { + logger.error(err, 'failed to fetch data'); + res.status(500).json({ error: 'something went wrong' }); + } } async function getTransactions(req, res) { @@ -89,15 +89,15 @@ async function getTransactions(req, res) { const transactions = await reportService.getTransactions(filters); const transactions_list = transactions.map(u => ({ - customer_no: u.customer_no, - from_account: u.from_account, - to_account: u.to_account, - ifsc_code: u.ifsc_code, - amount :u.amount, - created_at :u.created_at, - trx_type:u.trx_type, - status: u.status, - })); + customer_no: u.customer_no, + from_account: u.from_account, + to_account: u.to_account, + ifsc_code: u.ifsc_code, + amount: u.amount, + created_at: u.created_at, + trx_type: u.trx_type, + status: u.status, + })); res.json({ count: transactions.length, transactions_list }); @@ -128,15 +128,15 @@ async function getFailedTransactions(req, res) { const transactions = await reportService.getFailedTransactions(filters); const transactions_list = transactions.map(u => ({ - customer_no: u.customer_no, - from_account: u.from_account, - to_account: u.to_account, - ifsc_code: u.ifsc_code, - amount :u.amount, - created_at :u.created_at, - trx_type:u.trx_type, - status: u.status, - })); + customer_no: u.customer_no, + from_account: u.from_account, + to_account: u.to_account, + ifsc_code: u.ifsc_code, + amount: u.amount, + created_at: u.created_at, + trx_type: u.trx_type, + status: u.status, + })); res.json({ count: transactions.length, transactions_list }); @@ -146,4 +146,33 @@ async function getFailedTransactions(req, res) { } } -module.exports = { active_users ,inactive_users, getTransactions ,getFailedTransactions }; \ No newline at end of file +async function getDetailsOfNotLoginWithinDuration(req, res) { + const { + duration + } = req.body; + + if (!duration) { + return res.status(400).json({ error: 'Duration are required' }); + } + + try { + const users = await reportService.getNotLogin(duration); + const user_list = users.map(u => ({ + + customer_no: u.customer_no, + user_name: u.preferred_name, + last_login: u.last_login, + created_at: u.created_at, + locked: u.locked, + status: u.status, + })); + + res.json({ count: users.length, user_list }); + + } catch (err) { + logger.error(err, 'failed to fetch not logged in user details'); + res.status(500).json({ error: 'something went wrong' }); + } +} + +module.exports = { active_users, inactive_users, getTransactions, getFailedTransactions, getDetailsOfNotLoginWithinDuration }; \ No newline at end of file diff --git a/src/routes/report.route.js b/src/routes/report.route.js index 9188c2b..30d223b 100644 --- a/src/routes/report.route.js +++ b/src/routes/report.route.js @@ -7,5 +7,6 @@ router.post('/active_users', reportController.active_users); router.post('/in-active_users', reportController.inactive_users); router.post('/transaction_report', reportController.getTransactions); router.post('/failed_transaction_report', reportController.getFailedTransactions); +router.post('/not_logged_in', reportController.getDetailsOfNotLoginWithinDuration); module.exports = router; diff --git a/src/services/report.service.js b/src/services/report.service.js index 89161e4..2acf853 100644 --- a/src/services/report.service.js +++ b/src/services/report.service.js @@ -2,17 +2,17 @@ const db = require('../config/db'); const { logger } = require('../util/logger'); async function total_users(from_date, to_date) { - try { - const result = await db.query( - `SELECT * FROM users WHERE created_at BETWEEN $1 AND $2`, - [from_date, to_date] - ); - logger.info("data fetch for users"); - return result.rows; - } catch (err) { - logger.error(err, 'failed to fetch data'); - res.status(500).json({ error: 'something went wrong' }); - } + try { + const result = await db.query( + `SELECT * FROM users WHERE created_at BETWEEN $1 AND $2`, + [from_date, to_date] + ); + logger.info("data fetch for users"); + return result.rows; + } catch (err) { + logger.error(err, 'failed to fetch data'); + res.status(500).json({ error: 'something went wrong' }); + } } async function getTransactions(filters) { @@ -66,7 +66,7 @@ async function getFailedTransactions(filters) { WHERE created_at >= $1 AND created_at < $2 AND status LIKE 'FAILURE%'`; - + // params should match $1, $2, etc. const params = [from_date, to_date]; let paramIndex = params.length + 1; // start from 3 @@ -89,5 +89,18 @@ async function getFailedTransactions(filters) { } } +async function getNotLogin(duration) { + try { + const result = await db.query( + `SELECT * FROM users WHERE last_login <= NOW() - ($1 || ' month')::interval`, + [duration] + ); + logger.info("data fetch for users who have not logged-in in mentioned duration"); + return result.rows; + } catch (err) { + logger.error(err, 'failed to fetch not logged-in users data'); + res.status(500).json({ error: 'something went wrong' }); + } +} -module.exports = { total_users ,getTransactions ,getFailedTransactions }; \ No newline at end of file +module.exports = { total_users, getTransactions, getFailedTransactions, getNotLogin }; \ No newline at end of file