added functions for validate outside bank, check beneficiary name and get beneficiary
This commit is contained in:
@@ -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) {
|
async function addBeneficiary(req, res) {
|
||||||
try {
|
try {
|
||||||
const { accountNo, ifscCode, accountType, name } = req.body;
|
const { accountNo, ifscCode, accountType, name } = req.body;
|
||||||
const customerNo = req.user;
|
const customerNo = req.user;
|
||||||
console.log(`Customer Number: ${customerNo}`);
|
const query =
|
||||||
const uuid = await beneficiaryService.validateOutsideBank(
|
'INSERT INTO beneficiaries (customer_no, account_no, account_type, ifsc_code, name) VALUES ($1, $2, $3, $4, $5)';
|
||||||
accountNo,
|
await db.query(query, [customerNo, accountNo, accountType, ifscCode, name]);
|
||||||
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) {
|
} catch (error) {
|
||||||
logger.error(error, 'Error adding beneficiary');
|
logger.error(error, 'Error adding beneficiary');
|
||||||
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
|
res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkBeneficiary(req, res) {
|
async function getBeneficiary(req, res) {
|
||||||
await delay(5000);
|
|
||||||
const { accountNo } = req.query;
|
const { accountNo } = req.query;
|
||||||
const customerNo = req.user;
|
let beneficiaryDetails;
|
||||||
if (!accountNo) {
|
try {
|
||||||
res.status(403).json({ error: 'BAS_REQUEST' });
|
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' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkBeneficiaryName(req, res) {
|
||||||
|
await delay(2000);
|
||||||
|
const { refNo } = req.query;
|
||||||
|
if (!refNo) {
|
||||||
|
res.status(403).json({ error: 'BAD_REQUEST' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(customerNo, accountNo);
|
|
||||||
const query =
|
//uncomment in production for actual name from beneficiary
|
||||||
'SELECT EXISTS(SELECT 1 FROM beneficiaries WHERE customer_no = $1 AND account_no = $2)';
|
// const beneficiaryName = await getJson(refNo);
|
||||||
const result = await db.query(query, [customerNo, accountNo]);
|
const beneficiaryName = 'John Doe';
|
||||||
const exists = result.rows[0].exists;
|
if (!beneficiaryName) {
|
||||||
if (!exists) {
|
|
||||||
res.status(404).json({ error: 'NOT_FOUND' });
|
res.status(404).json({ error: 'NOT_FOUND' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.json({ message: 'FOUND' });
|
res.json({ message: beneficiaryName });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getIfscDetails(req, res) {
|
async function getIfscDetails(req, res) {
|
||||||
@@ -107,7 +132,9 @@ function delay(ms) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validateWithinBank,
|
validateWithinBank,
|
||||||
|
validateOutsideBank,
|
||||||
addBeneficiary,
|
addBeneficiary,
|
||||||
checkBeneficiary,
|
checkBeneficiaryName,
|
||||||
getIfscDetails,
|
getIfscDetails,
|
||||||
|
getBeneficiary,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user