From ecb9b146294f1a48ba6cca60ad4120e7d0df4263 Mon Sep 17 00:00:00 2001 From: asif Date: Thu, 7 Aug 2025 02:01:03 +0530 Subject: [PATCH] implemented a controller for processing all the responses we will get from NPCI for beneficiary validation --- src/controllers/npci.controller.js | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/controllers/npci.controller.js diff --git a/src/controllers/npci.controller.js b/src/controllers/npci.controller.js new file mode 100644 index 0000000..c677c03 --- /dev/null +++ b/src/controllers/npci.controller.js @@ -0,0 +1,45 @@ +const db = require('../config/db'); +const { getJson } = require('../config/redis'); +const { logger } = require('../util/logger'); + +async function npciResponse(req, res) { + const { resp } = req.body; + if (resp.status === 'Success') { + await handleNPCISuccess(resp); + } else { + await handleNPCIFailure(resp); + } + res.send('ok'); +} + +async function handleNPCISuccess(response) { + const { txnid, benename } = response; + try { + const beneficiaryDetails = await getJson(txnid); + if (!beneficiaryDetails) { + logger.warn('no txnid in redis'); + return false; + } + const { customerNo, accountNo, ifscCode, accountType } = beneficiaryDetails; + const query = + 'INSERT INTO beneficiaries (customer_no, account_no, name, account_type, ifsc_code) VALUES ($1, $2, $3, $4, $5)'; + const result = await db.query(query, [ + customerNo, + accountNo, + benename, + accountType, + ifscCode, + ]); + logger.info(result); + return true; + } catch (error) { + logger.error(error, 'error processing npci response'); + return false; + } +} + +async function handleNPCIFailure(response) { + console.log(response); +} + +module.exports = { npciResponse };