added controllers for beneficiary addition, checking and fetching ifsc details
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
const { logger } = require('../util/logger');
|
const { logger } = require('../util/logger');
|
||||||
|
const { setJson, getJson } = require('../config/redis');
|
||||||
const beneficiaryService = require('../services/beneficiary.service');
|
const beneficiaryService = require('../services/beneficiary.service');
|
||||||
|
const db = require('../config/db');
|
||||||
|
const axios = require('axios');
|
||||||
|
|
||||||
async function validateWithinBank(req, res) {
|
async function validateWithinBank(req, res) {
|
||||||
const { accountNumber } = req.query;
|
const { accountNumber } = req.query;
|
||||||
@@ -22,29 +25,89 @@ async function validateWithinBank(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function validateOutsideBank(req, res) {
|
async function addBeneficiary(req, res) {
|
||||||
res.status(400).send('WIP. Try after sometime');
|
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
|
||||||
|
);
|
||||||
|
|
||||||
async function npciResponse(req, res) {
|
//-------------------------------------------------------------------------
|
||||||
const { resp } = req.body;
|
//*REMOVE IN PRODUCTION* firing this from here since no NPCI in test region
|
||||||
console.log(req.body);
|
const r = await axios.post(
|
||||||
if (resp === 'Success') {
|
'http://localhost:8081/api/npci/beneficiary-response',
|
||||||
await handleNPCISuccess(resp);
|
{
|
||||||
} else {
|
resp: { status: 'Success', txnid: uuid, benename: name },
|
||||||
await handleNPCIFailure(resp);
|
}
|
||||||
|
);
|
||||||
|
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' });
|
||||||
}
|
}
|
||||||
res.send('ok');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleNPCISuccess(response) {
|
async function checkBeneficiary(req, res) {
|
||||||
const { txnid, benename } = response;
|
await delay(5000);
|
||||||
console.log(txnid);
|
const { accountNo } = req.query;
|
||||||
console.log(benename);
|
const customerNo = req.user;
|
||||||
|
if (!accountNo) {
|
||||||
|
res.status(403).json({ error: 'BAS_REQUEST' });
|
||||||
|
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.status(404).json({ error: 'NOT_FOUND' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.json({ message: 'FOUND' });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleNPCIFailure(response) {
|
async function getIfscDetails(req, res) {
|
||||||
console.log(response);
|
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' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { validateWithinBank, npciResponse, validateOutsideBank };
|
function delay(ms) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
validateWithinBank,
|
||||||
|
addBeneficiary,
|
||||||
|
checkBeneficiary,
|
||||||
|
getIfscDetails,
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user