added setting password feature fo migrated users

This commit is contained in:
2025-09-11 12:07:44 +05:30
parent cb3b34899b
commit a122882764
4 changed files with 165 additions and 36 deletions

View File

@@ -1,16 +1,31 @@
const { setJson, getJson } = require('../config/redis');
const { generateOTP } = require('../otpgenerator');
const { generateToken } = require('../util/jwt');
const authService = require('../services/auth.service.js');
const customerController = require('../controllers/customer_details.controller.js');
const { logger } = require('../util/logger');
const axios = require('axios');
const templates = require('../util/sms_template');
// Send OTP
async function SendOtp(req, res) {
const { mobileNumber, type, amount, beneficiary, ifsc, acctFrom, acctTo, ref, date, userOtp } = req.body;
const {
mobileNumber,
type,
amount,
beneficiary,
ifsc,
acctFrom,
acctTo,
ref,
date,
userOtp,
} = req.body;
if (!mobileNumber || !type) {
return res.status(400).json({ error: 'Mobile number and type are required' });
return res
.status(400)
.json({ error: 'Mobile number and type are required' });
}
try {
@@ -71,10 +86,13 @@ async function SendOtp(req, res) {
}
// Call SMS API
const response = await axios.post('http://localhost:9999/api/SendtoMessage', {
mobileNumber,
stMessage: message,
});
const response = await axios.post(
'http://localhost:9999/api/SendtoMessage',
{
mobileNumber,
stMessage: message,
}
);
if (response.data) {
// Save OTP only if it's OTP based (skip notifications without OTP)
@@ -86,18 +104,16 @@ async function SendOtp(req, res) {
}
return res.status(200).json({ message: 'Message sent successfully' });
} catch (err) {
logger.error(err, 'Error sending OTP');
return res.status(500).json({ error: 'Internal server error' });
}
}
// Verify OTP
async function VerifyOtp(req, res) {
const { mobileNumber } = req.query;
const { otp } = req.body
const { otp } = req.body;
if (!mobileNumber || !otp) {
return res.status(400).json({ error: 'Phone number and OTP are required' });
@@ -121,4 +137,79 @@ async function VerifyOtp(req, res) {
}
}
module.exports = { SendOtp, VerifyOtp };
async function sendForSetPassword(req, res) {
try {
const { customerNo } = req.query;
if (!customerNo)
return res.status(400).json({ error: 'CUSTOMER_NO_REQUIRED' });
// check if user is registered for in mobile banking data
const user = await authService.findUserByCustomerNo(customerNo);
if (!user) return res.status(404).json({ error: 'USER_NOT_FOUND' });
// if present then get his phone number from CBS
const userDetails = await customerController.getDetails(customerNo);
const singleUserDetail = userDetails[0];
singleUserDetail.mobileno = '7004774978';
if (!singleUserDetail?.mobileno)
return res.status(400).json({ error: 'USER_PHONE_NOT_FOUND' });
const mobileNumber = singleUserDetail.mobileno;
const otp = generateOTP(6);
const message = templates.CHANGE_LPWORD(otp);
const response = await axios.post(
'http://localhost:9999/api/SendtoMessage',
{
mobileNumber,
stMessage: message,
}
);
await setJson(`otp:${mobileNumber}`, otp, 300);
return res.status(200).json({ message: 'OTP_SENT' });
} catch (err) {
logger.error(err, 'Error sending OTP');
return res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
}
async function verifyForSetPassword(req, res) {
try {
const { customerNo, otp } = req.query;
if (!customerNo || !otp)
return res.status(400).json({ error: 'CUSTOMER_NO_REQUIRED' });
// check if user is registered for mobile banking database
const user = await authService.findUserByCustomerNo(customerNo);
if (!user) return res.status(404).json({ error: 'USER_NOT_FOUND' });
// if present then get his phone number from CBS
const userDetails = await customerController.getDetails(customerNo);
// temp check
const singleUserDetail = userDetails[0];
singleUserDetail.mobileno = '7004774978';
if (!singleUserDetail?.mobileno)
return res.status(400).json({ error: 'USER_PHONE_NOT_FOUND' });
const mobileNumber = singleUserDetail.mobileno;
const storedOtp = await getJson(`otp:${mobileNumber}`);
if (!storedOtp) {
return res.status(400).json({ error: 'OTP expired or not found' });
}
if (parseInt(otp, 10) !== parseInt(storedOtp, 10)) {
return res.status(400).json({ error: 'Invalid OTP' });
}
const token = generateToken(customerNo, 'user', '5m');
return res
.status(200)
.json({ message: 'OTP verified successfully', token: token });
} catch (err) {
logger.error(err, 'Error sending OTP');
return res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' });
}
}
module.exports = {
SendOtp,
VerifyOtp,
sendForSetPassword,
verifyForSetPassword,
};