Files
yume_js/src/controllers/report.controller.js

178 lines
4.9 KiB
JavaScript

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' });
}
}
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' });
}
}
async function getTransactions(req, res) {
const {
trx_type,
from_date,
to_date,
client,
amount_min,
amount_max,
customer_no
} = req.body;
if (!trx_type || !from_date || !to_date) {
return res.status(400).json({ error: 'trx_type, from_date and to_date are required' });
}
try {
const filters = {
trx_type,
from_date,
to_date,
client,
amount_min,
amount_max,
customer_no
};
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,
}));
res.json({ count: transactions.length, transactions_list });
} catch (err) {
logger.error(err, 'failed to fetch transactions');
res.status(500).json({ error: 'something went wrong' });
}
}
async function getFailedTransactions(req, res) {
const {
from_date,
to_date,
customer_no
} = req.body;
if (!from_date || !to_date) {
return res.status(400).json({ error: 'from_date and to_date are required' });
}
try {
const filters = {
from_date,
to_date,
customer_no
};
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,
}));
res.json({ count: transactions.length, transactions_list });
} catch (err) {
logger.error(err, 'failed to fetch transactions');
res.status(500).json({ error: 'something went wrong' });
}
}
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 };