feat: sim verification routes

This commit is contained in:
2025-12-23 11:39:13 +05:30
parent 9446abd88b
commit c3875afbd2

View File

@@ -0,0 +1,69 @@
const db = require('../config/db');
const { getJson, setJson } = require('../config/redis');
const { logger } = require('../util/logger');
const customerController = require('../controllers/customer_details.controller');
async function simDetailsResponse(req, res) {
const { phoneNo, uuid } = req.body;
try {
console.log(`phone no from sms: ${phoneNo}`);
console.log(`message body from sms: ${uuid}`);
await setJson(uuid, phoneNo);
} catch (error) {
logger.error(error, 'error processing sim details response');
}
res.json({ message: 'OK' });
}
async function simDetailsRequest(req, res) {
const { cifNo, uuid } = req.body;
try {
const customerDetails = await customerController.getDetails(cifNo);
const phoneNo = customerDetails[0].mobileno?.slice(-10);
const phoneNoFromVendor = await pollRedisKey(uuid);
if (!phoneNoFromVendor) {
return res.json({ error: 'Could not verify phone number' });
}
const strippedPhoneNo = phoneNoFromVendor.slice(-10);
console.log('phone no from CBS: ', phoneNo);
console.log('phone no from SIM: ', strippedPhoneNo);
if (phoneNo === strippedPhoneNo) {
return res.json({ status: 'VERIFIED' });
}
return res.json({ status: 'NOT_VERIFIED' });
} catch (error) {
logger.error(error, 'sim verification failed');
res.status(500).json({ error: 'SIM verification failed' });
}
}
async function pollRedisKey(key) {
const timeout = 2 * 60 * 1000;
const interval = 2000;
const startTime = Date.now();
return new Promise((resolve, reject) => {
const poll = async () => {
try {
const phoneNo = await getJson(key);
if (phoneNo !== null) {
console.log(phoneNo, 'payload from redis');
return resolve(phoneNo);
}
if (Date.now() - startTime >= timeout) {
return resolve(null);
}
console.log('not found retrying for uuid');
setTimeout(poll, interval);
} catch (error) {
reject(error);
}
};
poll();
});
}
module.exports = { simDetailsResponse, simDetailsRequest };