fix: added feature to validate beneficiaries from NPCI for real

This commit is contained in:
2025-09-11 22:50:36 +05:30
parent a122882764
commit 64e3cf3182
2 changed files with 32 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
const { logger } = require('../util/logger');
const beneficiaryService = require('../services/beneficiary.service');
const { getJson, setJson } = require('../config/redis');
const db = require('../config/db');
const randomName = require('../util/name.generator');
@@ -39,9 +40,7 @@ async function validateOutsideBank(req, res) {
);
if (!refNo)
return res.status(401).json({ error: 'invalid account number' });
//**IN PRODUCTION** poll the redis server continuously giving the refNo since the response from NPCI will be stored there
await delay(3000);
const name = randomName();
const name = await pollRedisKey(refNo);
return res.json({ name });
} catch (err) {
logger.error(err, 'beneficiary validation within bank failed');
@@ -119,11 +118,9 @@ async function getIfscDetails(req, res) {
res.status(403).json({ error: 'BAD_REQUEST' });
return;
}
console.log(ifscCode);
try {
const query = 'SELECT * FROM ifsc_details 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;
@@ -139,6 +136,34 @@ function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function pollRedisKey(key) {
const timeout = 2 * 60 * 1000;
const interval = 2000;
const startTime = Date.now();
return new Promise((resolve, reject) => {
const poll = async () => {
try {
const beneficiaryName = await getJson(key);
if (beneficiaryName !== null) {
logger.info(beneficiaryName, 'payload from redis');
return resolve(beneficiaryName);
}
if (Date.now() - startTime >= timeout) {
return resolve(null);
}
setTimeout(poll, interval);
} catch (error) {
reject(error);
}
};
poll();
});
}
module.exports = {
validateWithinBank,
validateOutsideBank,

View File

@@ -6,9 +6,7 @@ const db = require('../config/db');
async function validateWithinBank(accountNo) {
const url = 'http://localhost:8687/kccb/cbs/acctInfo/details';
try {
const response = await axios.get(url, {
params: { stacctno: accountNo },
});
const response = await axios.get(url, { params: { stacctno: accountNo } });
const data = response.data;
const customerName = data.customername;
return customerName;
@@ -20,7 +18,7 @@ async function validateWithinBank(accountNo) {
async function validateOutsideBank(accountNo, ifscCode, name) {
const uuid = `KCC${uuidv4().replace(/-/g, '')}`;
const url = `http://localhost:9091/kccb/benenamelookup/ReqBeneDetails/${uuid}`;
const url = `http://192.168.1.39:9091/kccb/benenamelookup/ReqBeneDetails/${uuid}`;
try {
const response = await axios.post(url, {
acctNo: accountNo,