diff --git a/src/controllers/report.controller.js b/src/controllers/report.controller.js index 39f583d..3d63d03 100644 --- a/src/controllers/report.controller.js +++ b/src/controllers/report.controller.js @@ -60,6 +60,90 @@ async function inactive_users(req, res) { } } +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' }); + } -module.exports = { active_users ,inactive_users }; \ No newline at end of file + 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' }); + } +} + +module.exports = { active_users ,inactive_users, getTransactions ,getFailedTransactions }; \ No newline at end of file diff --git a/src/routes/report.route.js b/src/routes/report.route.js index 507ffa9..9188c2b 100644 --- a/src/routes/report.route.js +++ b/src/routes/report.route.js @@ -5,5 +5,7 @@ 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); module.exports = router; diff --git a/src/services/report.service.js b/src/services/report.service.js index 809aaae..89161e4 100644 --- a/src/services/report.service.js +++ b/src/services/report.service.js @@ -15,4 +15,79 @@ async function total_users(from_date, to_date) { } } -module.exports = { total_users }; \ No newline at end of file +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; + } +} + + +module.exports = { total_users ,getTransactions ,getFailedTransactions }; \ No newline at end of file