diff --git a/src/controllers/beneficiary.controller.js b/src/controllers/beneficiary.controller.js index 6c67294..07fb52d 100644 --- a/src/controllers/beneficiary.controller.js +++ b/src/controllers/beneficiary.controller.js @@ -1,5 +1,8 @@ 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'); async function validateWithinBank(req, res) { const { accountNumber } = req.query; @@ -22,29 +25,89 @@ async function validateWithinBank(req, res) { } } -async function validateOutsideBank(req, res) { - res.status(400).send('WIP. Try after sometime'); -} +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 + ); -async function npciResponse(req, res) { - const { resp } = req.body; - console.log(req.body); - if (resp === 'Success') { - await handleNPCISuccess(resp); - } else { - await handleNPCIFailure(resp); + //------------------------------------------------------------------------- + //*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) { + logger.error(error, 'Error adding beneficiary'); + res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' }); } - res.send('ok'); } -async function handleNPCISuccess(response) { - const { txnid, benename } = response; - console.log(txnid); - console.log(benename); +async function checkBeneficiary(req, res) { + await delay(5000); + const { accountNo } = req.query; + 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) { - console.log(response); +async function getIfscDetails(req, res) { + 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, +};