added controllers for beneficiary addition, checking and fetching ifsc details

This commit is contained in:
2025-08-07 01:57:50 +05:30
parent df68fc4a29
commit 6d675ad81c

View File

@@ -1,5 +1,8 @@
const { logger } = require('../util/logger');
const { setJson, getJson } = require('../config/redis');
const beneficiaryService = require('../services/beneficiary.service');
const db = require('../config/db');
const axios = require('axios');
async function validateWithinBank(req, res) {
const { accountNumber } = req.query;
@@ -22,29 +25,89 @@ async function validateWithinBank(req, res) {
}
}
async function validateOutsideBank(req, res) {
res.status(400).send('WIP. Try after sometime');
async function addBeneficiary(req, res) {
try {
const { accountNo, ifscCode, accountType, name } = req.body;
const customerNo = req.user;
console.log(`Customer Number: ${customerNo}`);
const uuid = await beneficiaryService.validateOutsideBank(
accountNo,
ifscCode,
name
);
await setJson(
uuid,
{ customerNo, accountNo, ifscCode, accountType, name },
300
);
//-------------------------------------------------------------------------
//*REMOVE IN PRODUCTION* firing this from here since no NPCI in test region
const r = await axios.post(
'http://localhost:8081/api/npci/beneficiary-response',
{
resp: { status: 'Success', txnid: uuid, benename: name },
}
);
console.log(r.data);
//-------------------------------------------------------------------------
res.json({ message: 'SENT_FOR_VALIDATION' });
} catch (error) {
logger.error(error, 'Error adding beneficiary');
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
}
async function npciResponse(req, res) {
const { resp } = req.body;
console.log(req.body);
if (resp === 'Success') {
await handleNPCISuccess(resp);
} else {
await handleNPCIFailure(resp);
async function checkBeneficiary(req, res) {
await delay(5000);
const { accountNo } = req.query;
const customerNo = req.user;
if (!accountNo) {
res.status(403).json({ error: 'BAS_REQUEST' });
return;
}
res.send('ok');
console.log(customerNo, accountNo);
const query =
'SELECT EXISTS(SELECT 1 FROM beneficiaries WHERE customer_no = $1 AND account_no = $2)';
const result = await db.query(query, [customerNo, accountNo]);
const exists = result.rows[0].exists;
if (!exists) {
res.status(404).json({ error: 'NOT_FOUND' });
return;
}
res.json({ message: 'FOUND' });
}
async function handleNPCISuccess(response) {
const { txnid, benename } = response;
console.log(txnid);
console.log(benename);
async function getIfscDetails(req, res) {
const { ifscCode } = req.query;
if (!ifscCode) {
res.status(403).json({ error: 'BAD_REQUEST' });
return;
}
console.log(ifscCode);
try {
const query = 'SELECT * FROM ifsc_code_bank WHERE ifsc_code = $1';
const result = await db.query(query, [ifscCode]);
console.log(result.rows);
if (!result.rows) {
res.status(404).json({ error: 'NOT_FOUND' });
return;
}
res.json(result.rows[0]);
} catch (error) {
logger.error(error, 'error fetching ifsc code');
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
}
async function handleNPCIFailure(response) {
console.log(response);
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
module.exports = { validateWithinBank, npciResponse, validateOutsideBank };
module.exports = {
validateWithinBank,
addBeneficiary,
checkBeneficiary,
getIfscDetails,
};