Compare commits
6 Commits
fetch-from
...
admin_feat
| Author | SHA1 | Date | |
|---|---|---|---|
| c481e4f139 | |||
| 12750e833c | |||
| 3262ff53bf | |||
| 1ebe666bb3 | |||
| 689b00aec7 | |||
| 56c69e54de |
@@ -62,6 +62,7 @@ async function getUserDetails(req, res) {
|
||||
res.status(500).json({ error: 'invalid CIF number' });
|
||||
}
|
||||
}
|
||||
|
||||
async function getUserRights(req, res) {
|
||||
const { CIF } = req.query;
|
||||
if (!CIF) {
|
||||
@@ -71,13 +72,13 @@ async function getUserRights(req, res) {
|
||||
}
|
||||
const userDetails = await adminAuthService.getCustomerDetailsFromDB(CIF);
|
||||
if (!userDetails)
|
||||
return res.status(401).json({ error: 'invalid CIF number or No rights is present for the user.' });
|
||||
return res.status(404).json({ error: 'invalid CIF number or No rights is present for the user.' });
|
||||
return res.json(userDetails);
|
||||
}
|
||||
|
||||
async function UserRights(req, res) {
|
||||
try {
|
||||
const { CIF, ib_access_level, mb_access_level } = req.body;
|
||||
const { CIF, ib_access_level, mb_access_level, ib_limit, mb_limit } = req.body;
|
||||
|
||||
if (!CIF) {
|
||||
return res.status(400).json({ error: 'CIF number is required' });
|
||||
@@ -93,23 +94,26 @@ async function UserRights(req, res) {
|
||||
if (FirstTimeLogin && dayjs(currentTime).diff(dayjs(user.created_at), 'day') > 8) {
|
||||
// Password expired, resend
|
||||
await db.query(
|
||||
'UPDATE users SET password_hash=$2, updated_at=$5, ib_access_level=$3, mb_access_level=$4 WHERE customer_no=$1',
|
||||
[CIF, password, ib_access_level, mb_access_level, currentTime]
|
||||
'UPDATE users SET password_hash=$2, updated_at=$5, ib_access_level=$3, mb_access_level=$4 ,inb_limit_amount=$6,mobile_limit_amount=$7 WHERE customer_no=$1',
|
||||
[CIF, password, ib_access_level, mb_access_level, currentTime, ib_limit, mb_limit]
|
||||
);
|
||||
logger.info("Admin sended the OTP");
|
||||
return res.json({ otp: first_time_pass });
|
||||
}
|
||||
// Just update access levels and timestamp
|
||||
await db.query(
|
||||
'UPDATE users SET updated_at=$4, ib_access_level=$2, mb_access_level=$3 WHERE customer_no=$1',
|
||||
[CIF, ib_access_level, mb_access_level, currentTime]
|
||||
'UPDATE users SET updated_at=$4, ib_access_level=$2, mb_access_level=$3 ,inb_limit_amount=$5,mobile_limit_amount=$6 WHERE customer_no=$1',
|
||||
[CIF, ib_access_level, mb_access_level, currentTime, ib_limit, mb_limit]
|
||||
);
|
||||
logger.info("Admin Updated the user.");
|
||||
return res.json({ message: "User updated successfully." });
|
||||
} else {
|
||||
// User does not exist, insert
|
||||
await db.query(
|
||||
'INSERT INTO users (customer_no, password_hash, ib_access_level, mb_access_level) VALUES ($1, $2, $3, $4)',
|
||||
[CIF, password, ib_access_level, mb_access_level]
|
||||
'INSERT INTO users (customer_no, password_hash, ib_access_level, mb_access_level ,inb_limit_amount,mobile_limit_amount) VALUES ($1, $2, $3, $4 ,$5 ,$6)',
|
||||
[CIF, password, ib_access_level, mb_access_level, ib_limit, mb_limit]
|
||||
);
|
||||
logger.info("New user enroll by admin.");
|
||||
return res.json({ otp: first_time_pass });
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -119,4 +123,30 @@ async function UserRights(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { login, fetchAdminDetails, getUserDetails, UserRights, getUserRights };
|
||||
async function handleUnlockUser(req, res) {
|
||||
try {
|
||||
const { user, action } = req.body;
|
||||
const adminUserName = req.admin;
|
||||
if (!user) {
|
||||
return res.status(400).json({ error: "CIF or username is required" });
|
||||
}
|
||||
const userDetails = await adminAuthService.getCustomerDetailsFromDB(user);
|
||||
if (!userDetails) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "Invalid CIF number or username" });
|
||||
}
|
||||
await adminAuthService.updateUserLockStatus(user, action,adminUserName);
|
||||
const statusText = action ? "locked" : "unlocked";
|
||||
logger.info(`User ${user} has been successfully ${statusText}.`);
|
||||
|
||||
return res.json({
|
||||
message: `User ${user} has been successfully ${statusText}.`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Unlock user error:", error);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { login, fetchAdminDetails, getUserDetails, UserRights, getUserRights, handleUnlockUser };
|
||||
|
||||
@@ -117,7 +117,9 @@ async function SendOtp(req, res) {
|
||||
default:
|
||||
return res.status(400).json({ error: 'Invalid OTP type' });
|
||||
}
|
||||
|
||||
if (message.includes('OTP')) {
|
||||
await setJson(`otp:${mobileNumber}`, otp, 300);
|
||||
}
|
||||
// Call SMS API
|
||||
const response = await axios.post(
|
||||
'http://localhost:9999/api/SendtoMessage',
|
||||
@@ -126,12 +128,8 @@ async function SendOtp(req, res) {
|
||||
stMessage: message,
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data) {
|
||||
// Save OTP only if it's OTP based (skip notifications without OTP)
|
||||
if (message.includes('OTP')) {
|
||||
await setJson(`otp:${mobileNumber}`, otp, 300);
|
||||
}
|
||||
logger.info(`Sent OTP [${otp}] for type [${type}] to ${mobileNumber}`);
|
||||
}
|
||||
return res.status(200).json({ message: 'Message sent successfully' });
|
||||
@@ -152,15 +150,18 @@ async function VerifyOtp(req, res) {
|
||||
|
||||
try {
|
||||
const storedOtp = await getJson(`otp:${mobileNumber}`);
|
||||
logger.info("OTP is stored");
|
||||
|
||||
if (!storedOtp) {
|
||||
logger.error("OTP expired or not found");
|
||||
return res.status(400).json({ error: 'OTP expired or not found' });
|
||||
}
|
||||
|
||||
if (parseInt(otp, 10) !== parseInt(storedOtp, 10)) {
|
||||
logger.error("Invalid OTP");
|
||||
return res.status(400).json({ error: 'Invalid OTP' });
|
||||
}
|
||||
|
||||
logger.info(`OTP verified with mobile number -${mobileNumber}`);
|
||||
return res.status(200).json({ message: 'OTP verified successfully' });
|
||||
} catch (err) {
|
||||
logger.error(err, 'Error verifying OTP');
|
||||
|
||||
178
src/controllers/report.controller.js
Normal file
178
src/controllers/report.controller.js
Normal file
@@ -0,0 +1,178 @@
|
||||
|
||||
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 };
|
||||
@@ -13,7 +13,6 @@ function checkAdmin (req,res,next){
|
||||
const token = authHeader.split(' ')[1];
|
||||
try {
|
||||
const payload = verifyToken(token);
|
||||
// console.log("hi",payload);
|
||||
if(payload.customerNo && payload.role === 'admin'){
|
||||
req.admin = payload.customerNo;
|
||||
next();
|
||||
|
||||
@@ -9,4 +9,5 @@ router.get('/admin_details', adminAuthenticate, adminAuthController.fetchAdminDe
|
||||
router.get('/fetch/customer_details',adminAuthenticate,adminAuthController.getUserDetails);
|
||||
router.post('/user/rights',adminAuthenticate,adminAuthController.UserRights);
|
||||
router.get('/user/rights',adminAuthenticate,adminAuthController.getUserRights);
|
||||
router.post('/user/unlock',adminAuthenticate,adminAuthController.handleUnlockUser);
|
||||
module.exports = router;
|
||||
|
||||
@@ -4,6 +4,7 @@ const adminAuthRoute = require('./admin_auth.route');
|
||||
const detailsRoute = require('./customer_details.route');
|
||||
const transactionRoute = require('./transactions.route');
|
||||
const authenticate = require('../middlewares/auth.middleware');
|
||||
const adminAuthenticate = require('../middlewares/admin.middleware');
|
||||
const transferRoute = require('./transfer.route');
|
||||
const beneficiaryRoute = require('./beneficiary.route');
|
||||
const neftRoute = require('./neft.route');
|
||||
@@ -13,7 +14,12 @@ const branchRoute = require('./branch.route');
|
||||
const atmRoute = require('./atm.route');
|
||||
const { npciResponse } = require('../controllers/npci.controller');
|
||||
const otp = require('./otp.route');
|
||||
<<<<<<< HEAD
|
||||
const reports =require('./report.route');
|
||||
|
||||
=======
|
||||
const eMandate = require('./emandate.route');
|
||||
>>>>>>> 7e162e741d4d126fd029b1875bd5e4e0d3c460cf
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/auth', authRoute);
|
||||
@@ -26,6 +32,7 @@ router.use('/payment/rtgs', authenticate, rtgsRoute);
|
||||
router.use('/payment/imps', authenticate, impsRoute);
|
||||
router.use('/beneficiary', authenticate, beneficiaryRoute);
|
||||
router.use('/npci/beneficiary-response', npciResponse);
|
||||
router.use('/report',adminAuthenticate,reports);
|
||||
router.use('/otp', otp);
|
||||
router.use('/e-mandate', authenticate, eMandate);
|
||||
router.use('/branch', authenticate, branchRoute);
|
||||
|
||||
12
src/routes/report.route.js
Normal file
12
src/routes/report.route.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const reportController = require('../controllers/report.controller');
|
||||
const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
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;
|
||||
@@ -38,10 +38,19 @@ async function getCustomerDetails(customerNo) {
|
||||
}
|
||||
|
||||
async function getCustomerDetailsFromDB(customerNo) {
|
||||
const result = await db.query('SELECT customer_no,created_at,last_login,is_first_login,ib_access_level,mb_access_level FROM users WHERE customer_no = $1', [
|
||||
const result = await db.query(
|
||||
'SELECT customer_no,created_at,last_login,is_first_login,ib_access_level,mb_access_level,inb_limit_amount,mobile_limit_amount,locked FROM users WHERE customer_no = $1 or preferred_name= $1', [
|
||||
customerNo,
|
||||
]);
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
module.exports = { validateAdmin, findAdminByUserName, getCustomerDetails,getCustomerDetailsFromDB };
|
||||
async function updateUserLockStatus(customerNo ,locked ,adminUserName) {
|
||||
const result = await db.query(
|
||||
'Update users set locked =$2 ,unlocked_by=$3 WHERE customer_no = $1 or preferred_name= $1', [
|
||||
customerNo, locked ,adminUserName
|
||||
]);
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
module.exports = { validateAdmin, findAdminByUserName, getCustomerDetails, getCustomerDetailsFromDB ,updateUserLockStatus };
|
||||
|
||||
106
src/services/report.service.js
Normal file
106
src/services/report.service.js
Normal file
@@ -0,0 +1,106 @@
|
||||
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' });
|
||||
}
|
||||
}
|
||||
|
||||
async function getTransactions(filters) {
|
||||
try {
|
||||
const { trx_type, from_date, to_date, client, amount_min, amount_max, customer_no } = filters;
|
||||
|
||||
let query = `SELECT * FROM transactions WHERE trx_type = $1 AND created_at >= $2 AND created_at < $3`;
|
||||
const params = [trx_type, from_date, to_date];
|
||||
let paramIndex = 4;
|
||||
|
||||
if (client) {
|
||||
query += ` AND client = $${paramIndex++}`;
|
||||
params.push(client);
|
||||
}
|
||||
|
||||
if (amount_min && amount_max) {
|
||||
query += ` AND amount BETWEEN $${paramIndex++} AND $${paramIndex++}`;
|
||||
params.push(amount_min, amount_max);
|
||||
} else if (amount_min) {
|
||||
query += ` AND amount >= $${paramIndex++}`;
|
||||
params.push(amount_min);
|
||||
} else if (amount_max) {
|
||||
query += ` AND amount <= $${paramIndex++}`;
|
||||
params.push(amount_max);
|
||||
}
|
||||
|
||||
if (customer_no) {
|
||||
query += ` AND customer_no = $${paramIndex++}`;
|
||||
params.push(customer_no);
|
||||
}
|
||||
|
||||
query += ` ORDER BY created_at DESC`;
|
||||
|
||||
const result = await db.query(query, params);
|
||||
logger.info(`Fetched ${result.rows.length} transactions`);
|
||||
return result.rows;
|
||||
|
||||
} catch (err) {
|
||||
logger.error(err, 'failed to fetch transactions');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async function getFailedTransactions(filters) {
|
||||
try {
|
||||
const { from_date, to_date, customer_no } = filters;
|
||||
|
||||
let query = `
|
||||
SELECT *
|
||||
FROM transactions
|
||||
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
|
||||
|
||||
if (customer_no) {
|
||||
query += ` AND customer_no = $${paramIndex}`;
|
||||
params.push(customer_no);
|
||||
}
|
||||
|
||||
query += ` ORDER BY created_at DESC`;
|
||||
console.log(query);
|
||||
|
||||
const result = await db.query(query, params);
|
||||
logger.info(`Fetched ${result.rows.length} failed transactions`);
|
||||
return result.rows;
|
||||
|
||||
} catch (err) {
|
||||
logger.error(err, 'Failed to fetch failed transactions');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
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, getNotLogin };
|
||||
Reference in New Issue
Block a user