84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
const axios = require('axios');
|
|
const { logger } = require('../util/logger');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
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 data = response.data;
|
|
const customerName = data.customername;
|
|
return customerName;
|
|
} catch (error) {
|
|
logger.error(error, 'error while fetching customer details');
|
|
throw new Error('unable to fetch customer name');
|
|
}
|
|
}
|
|
|
|
async function validateOutsideBank(accountNo, ifscCode, name) {
|
|
const uuid = `KCC${uuidv4().replace(/-/g, '')}`;
|
|
const url = `http://192.168.1.39:9091/kccb/benenamelookup/ReqBeneDetails/${uuid}`;
|
|
try {
|
|
const response = await axios.post(url, {
|
|
acctNo: accountNo,
|
|
ifsccode: ifscCode,
|
|
remittername: name,
|
|
});
|
|
if (response.data) {
|
|
console.log(response.data);
|
|
return uuid;
|
|
}
|
|
} catch (error) {
|
|
logger.error(error, 'error while validating customer from NPCI');
|
|
throw new Error('error in beneficiary validation');
|
|
}
|
|
}
|
|
|
|
async function getSingleBeneficiary(customerNo, accountNo) {
|
|
const queryStr =
|
|
'SELECT b.account_no, b.name, b.account_type, b.ifsc_code, i.bank_name, i.branch_name FROM beneficiaries b JOIN ifsc_details i ON b.ifsc_code = i.ifsc_code WHERE customer_no = $1 AND account_no = $2';
|
|
const result = await db.query(queryStr, [customerNo, accountNo]);
|
|
return result.rows[0];
|
|
}
|
|
|
|
async function deleteBeneficiary(customerNo, beneficiaryAccountNo) {
|
|
const queryStr =
|
|
'DELETE FROM beneficiaries WHERE customer_no = $1 AND account_no = $2';
|
|
const result = await db.query(queryStr, [customerNo, beneficiaryAccountNo]);
|
|
if (result.rowCount == 0) {
|
|
throw new Error('ACCOUNT_NOT_FOUND');
|
|
}
|
|
return;
|
|
}
|
|
|
|
async function getAllBeneficiaries(customerNo) {
|
|
const queryStr =
|
|
'SELECT b.account_no, b.name, b.account_type, b.ifsc_code, i.bank_name, i.branch_name FROM beneficiaries b JOIN LATERAL( SELECT * FROM ifsc_details i WHERE i.ifsc_code = b.ifsc_code LIMIT 1 ) i ON true WHERE customer_no = $1';
|
|
const result = await db.query(queryStr, [customerNo]);
|
|
const list = result.rows.map((row) => {
|
|
const details = {
|
|
accountNo: row['account_no'],
|
|
name: row['name'],
|
|
accountType: row['account_type'],
|
|
};
|
|
if (row['ifsc_code'] === '_') {
|
|
details['bankName'] = 'THE KANGRA CENTRAL COOPERATIVE BANK LIMITED';
|
|
} else {
|
|
details['ifscCode'] = row['ifsc_code'];
|
|
details['bankName'] = row['bank_name'];
|
|
details['branchName'] = row['branch_name'];
|
|
}
|
|
return details;
|
|
});
|
|
|
|
return list;
|
|
}
|
|
module.exports = {
|
|
validateWithinBank,
|
|
validateOutsideBank,
|
|
getAllBeneficiaries,
|
|
getSingleBeneficiary,
|
|
deleteBeneficiary,
|
|
};
|