This commit is contained in:
2025-08-11 12:32:22 +05:30
20 changed files with 471 additions and 104 deletions

View File

@@ -1,8 +1,7 @@
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');
const randomName = require('../util/name.generator');
async function validateWithinBank(req, res) {
const { accountNumber } = req.query;
@@ -25,58 +24,75 @@ 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' });
//**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();
return res.json({ name });
} 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]);
res.json({ message: 'SUCCESS' });
} catch (error) {
logger.error(error, 'Error adding beneficiary');
if (
error.message ==
'duplicate key value violates unique constraint "beneficiaries_pkey"'
) {
res.status(409).json({ error: 'BENEFICIARY_ALREADY_EXITS' });
}
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' });
return;
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;
}
res.json(beneficiaryDetails);
} catch (error) {
logger.error(error, 'error fetching beneficiaries');
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
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 getIfscDetails(req, res) {
@@ -87,7 +103,7 @@ async function getIfscDetails(req, res) {
}
console.log(ifscCode);
try {
const query = 'SELECT * FROM ifsc_code_bank WHERE ifsc_code = $1';
const query = 'SELECT * FROM ifsc_details WHERE ifsc_code = $1';
const result = await db.query(query, [ifscCode]);
console.log(result.rows);
if (!result.rows) {
@@ -107,7 +123,8 @@ function delay(ms) {
module.exports = {
validateWithinBank,
validateOutsideBank,
addBeneficiary,
checkBeneficiary,
getIfscDetails,
getBeneficiary,
};

View File

@@ -0,0 +1,35 @@
const axios = require('axios');
async function send(
fromAccount,
toAccount,
amount,
ifscCode,
beneficiaryName,
remitterName
) {
try {
const response = await axios.post(
'http://localhost:8690/kccb/Neftfundtransfer',
{
stFromAcc: fromAccount,
stToAcc: toAccount,
stTranAmt: amount,
stCommission: 0,
stIfscCode: ifscCode,
stFullName: remitterName,
stBeneName: beneficiaryName,
stAddress1: '',
stAddress2: '',
stAddress3: '',
}
);
return response.data;
} catch (error) {
throw new Error(
'API call failed: ' + (error.response?.data?.message || error.message)
);
}
}
module.exports = { send };

View File

@@ -1,5 +1,5 @@
const db = require('../config/db');
const { getJson } = require('../config/redis');
const { getJson, setJson } = require('../config/redis');
const { logger } = require('../util/logger');
async function npciResponse(req, res) {
@@ -15,26 +15,9 @@ async function npciResponse(req, res) {
async function handleNPCISuccess(response) {
const { txnid, benename } = response;
try {
const beneficiaryDetails = await getJson(txnid);
if (!beneficiaryDetails) {
logger.warn('no txnid in redis');
return false;
}
const { customerNo, accountNo, ifscCode, accountType } = beneficiaryDetails;
const query =
'INSERT INTO beneficiaries (customer_no, account_no, name, account_type, ifsc_code) VALUES ($1, $2, $3, $4, $5)';
const result = await db.query(query, [
customerNo,
accountNo,
benename,
accountType,
ifscCode,
]);
logger.info(result);
return true;
await setJson(txnid, benename);
} catch (error) {
logger.error(error, 'error processing npci response');
return false;
}
}

View File

@@ -0,0 +1,36 @@
const axios = require('axios');
async function send(
fromAccount,
toAccount,
amount,
ifscCode,
beneficiaryName,
remitterName
) {
try {
const response = await axios.post(
'http://localhost:8690/kccb/Rtgsfundtransfer',
{
stFromAcc: fromAccount,
stToAcc: toAccount,
stTranAmt: amount,
stCommission: 0,
stIfscCode: ifscCode,
stFullName: remitterName,
stBeneName: beneficiaryName,
stAddress1: '',
stAddress2: '',
stAddress3: '',
}
);
return response.data;
} catch (error) {
throw new Error(
'API call to CBS failed' +
(error.response?.data?.message || error.message)
);
}
}
module.exports = { send };

View File

@@ -21,5 +21,27 @@ async function getLastTen(accountNumber) {
);
}
}
module.exports = { getLastTen };
async function getFiltered(accountNumber, fromDate, toDate) {
try {
const response = await axios.get(
`http://localhost:8688/kccb/cbs/montlyacctstmt/details`,
{
params: { stacctno: accountNumber, fromdate: fromDate, todate: toDate },
}
);
const transactions = response.data;
const processedTransactions = transactions.map((tx) => ({
id: tx.stTransactionNumber,
name: tx.stTransactionDesc,
date: tx.stTransactionDate,
amount: tx.stTransactionAmount.slice(0, -3),
type: tx.stTransactionAmount.slice(-2),
}));
return processedTransactions;
} catch (error) {
throw new Error(
'API call failde: ' + (error.response?.data?.message || error.message)
);
}
}
module.exports = { getLastTen, getFiltered };