added functions for validate outside bank, check beneficiary name and get beneficiary

This commit is contained in:
2025-08-08 20:45:13 +05:30
parent 29d0949039
commit 90cbddd248

View File

@@ -25,58 +25,83 @@ async function validateWithinBank(req, res) {
}
}
async function validateOutsideBank(req, res) {
const { accountNo, ifscCode, remitterName } = req.query;
if (!accountNo || !ifscCode || !remitterName) {
res.status(401).json({ error: 'BAD_REQUEST' });
return;
}
try {
const refNo = await beneficiaryService.validateOutsideBank(
accountNo,
ifscCode,
remitterName
);
if (!refNo)
return res.status(401).json({ error: 'invalid account number' });
return res.json({ refNo });
} catch (err) {
logger.error(err, 'beneficiary validation within bank failed');
res.status(500).json({ error: 'invalid account number' });
}
}
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' });
const query =
'INSERT INTO beneficiaries (customer_no, account_no, account_type, ifsc_code, name) VALUES ($1, $2, $3, $4, $5)';
await db.query(query, [customerNo, accountNo, accountType, ifscCode, name]);
} catch (error) {
logger.error(error, 'Error adding beneficiary');
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
}
async function checkBeneficiary(req, res) {
await delay(5000);
async function getBeneficiary(req, res) {
const { accountNo } = req.query;
const customerNo = req.user;
if (!accountNo) {
res.status(403).json({ error: 'BAS_REQUEST' });
let beneficiaryDetails;
try {
if (accountNo) {
beneficiaryDetails = await beneficiaryService.getSingleBeneficiary(
req.user,
accountNo
);
} else {
beneficiaryDetails = await beneficiaryService.getAllBeneficiaries(
req.user
);
}
if (!beneficiaryDetails) {
res.status(404).json({ error: 'NO_BENEFICIARY_FOUND' });
return;
}
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.json(beneficiaryDetails);
} catch (error) {
logger.error(error, 'error fetching beneficiaries');
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
}
async function checkBeneficiaryName(req, res) {
await delay(2000);
const { refNo } = req.query;
if (!refNo) {
res.status(403).json({ error: 'BAD_REQUEST' });
return;
}
//uncomment in production for actual name from beneficiary
// const beneficiaryName = await getJson(refNo);
const beneficiaryName = 'John Doe';
if (!beneficiaryName) {
res.status(404).json({ error: 'NOT_FOUND' });
return;
}
res.json({ message: 'FOUND' });
res.json({ message: beneficiaryName });
}
async function getIfscDetails(req, res) {
@@ -107,7 +132,9 @@ function delay(ms) {
module.exports = {
validateWithinBank,
validateOutsideBank,
addBeneficiary,
checkBeneficiary,
checkBeneficiaryName,
getIfscDetails,
getBeneficiary,
};