added beneficiary routes
This commit is contained in:
25
src/controllers/beneficiary.controller.js
Normal file
25
src/controllers/beneficiary.controller.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const { logger } = require('../util/logger');
|
||||
const beneficiaryService = require('../services/beneficiary.service');
|
||||
|
||||
async function validateWithinBank(req, res) {
|
||||
const { accountNumber } = req.query;
|
||||
|
||||
if (!accountNumber) {
|
||||
res.status(400).json({
|
||||
error: 'account number is required',
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const beneficiaryName =
|
||||
await beneficiaryService.validateWithinBank(accountNumber);
|
||||
if (!beneficiaryName)
|
||||
return res.status(401).json({ error: 'invalid account number' });
|
||||
return res.json({ name: beneficiaryName });
|
||||
} catch (err) {
|
||||
logger.error(err, 'beneficiary validation within bank failed');
|
||||
res.status(500).json({ error: 'invalid account number' });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { validateWithinBank };
|
8
src/routes/beneficiary.route.js
Normal file
8
src/routes/beneficiary.route.js
Normal file
@@ -0,0 +1,8 @@
|
||||
const express = require('express');
|
||||
const beneficiaryController = require('../controllers/beneficiary.controller');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/validate/within-bank', beneficiaryController.validateWithinBank);
|
||||
|
||||
module.exports = router;
|
@@ -4,6 +4,7 @@ const detailsRoute = require('./customer_details.route');
|
||||
const transactionRoute = require('./transactions.route');
|
||||
const authenticate = require('../middlewares/auth.middleware');
|
||||
const transferRoute = require('./transfer.route');
|
||||
const beneficiaryRoute = require('./beneficiary.route');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -11,5 +12,6 @@ router.use('/auth', authRoute);
|
||||
router.use('/customer', authenticate, detailsRoute);
|
||||
router.use('/transactions/account/:accountNo', authenticate, transactionRoute);
|
||||
router.use('/payment/transfer', authenticate, transferRoute);
|
||||
router.use('/beneficiary', beneficiaryRoute);
|
||||
|
||||
module.exports = router;
|
||||
|
16
src/services/beneficiary.service.js
Normal file
16
src/services/beneficiary.service.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const { logger } = require('../util/logger');
|
||||
|
||||
async function validateWithinBank(accountNo) {
|
||||
const url = `http://localhost:8687/kccb/cbs/acctInfo/details?stacctno=${accountNo}`;
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
const customerName = data.customername;
|
||||
return customerName;
|
||||
} catch (error) {
|
||||
logger.error(error, 'error while fetching customer details');
|
||||
throw new Error('unable to fetch customer name');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { validateWithinBank };
|
Reference in New Issue
Block a user