112 lines
3.1 KiB
JavaScript
112 lines
3.1 KiB
JavaScript
// import hashPassword from '../utils/hashPassword';
|
|
|
|
import crypto from 'crypto';
|
|
import Patient from './PatientModel.js'
|
|
export const register = async (req, res) => {
|
|
const { name, mobileNumber } = req.body;
|
|
try {
|
|
let patient = await Patient.findOne({ mobileNumber });
|
|
|
|
if (patient && patient.isVerified) {
|
|
return res.status(400).json({ message: 'Patient already registered and verified for This Mobile No.' });
|
|
}
|
|
|
|
const otp = crypto.randomInt(100000, 1000000).toString();
|
|
|
|
// const otp ="123456";
|
|
|
|
const otpExpires = Date.now() + 10 * 60 * 1000; // 10 minutes
|
|
|
|
if (patient) {
|
|
patient.otp = otp;
|
|
patient.otpExpires = otpExpires;
|
|
} else {
|
|
patient = new Patient({ name, mobileNumber, otp, otpExpires });
|
|
}
|
|
|
|
await patient.save();
|
|
// await sendOtp(mobileNumber, otp);
|
|
|
|
res.status(200).json({patient, message: `OTP ${otp} sent to your mobile number successfully` });
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
message: error.message ? error.message : "Server error!",
|
|
});
|
|
}
|
|
};
|
|
|
|
export const verifyOtp = async (req, res) => {
|
|
const { mobileNumber, otp } = req.body;
|
|
try {
|
|
const patient = await Patient.findOne({ mobileNumber });
|
|
|
|
if (!patient) {
|
|
return res.status(400).json({ message: 'Invalid mobile number or OTP' });
|
|
}
|
|
|
|
if (patient.otp !== otp || patient.otpExpires < Date.now()) {
|
|
return res.status(400).json({ message: 'Invalid or expired OTP' });
|
|
}
|
|
|
|
patient.isVerified = true;
|
|
patient.otp = undefined;
|
|
patient.otpExpires = undefined;
|
|
|
|
await patient.save();
|
|
|
|
res.status(200).json({patient, message: 'Mobile number verified successfully' });
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
message: error.message ? error.message : "Server error!",
|
|
}); }
|
|
};
|
|
|
|
export const completeRegistration = async (req, res) => {
|
|
const {
|
|
mobileNumber,
|
|
email,
|
|
password,
|
|
confirmPassword,
|
|
gender,
|
|
weight,
|
|
height,
|
|
age,
|
|
commonHealthComplaints,
|
|
familyHistory,
|
|
personalHistory,
|
|
dailyRoutine,
|
|
} = req.body;
|
|
if (password !== confirmPassword) {
|
|
return res.status(400).json({ message: 'Password and confirm password do not match' });
|
|
}
|
|
try {
|
|
const patient = await Patient.findOne({ mobileNumber });
|
|
|
|
if (!patient || !patient.isVerified) {
|
|
return res.status(400).json({ message: 'Patient not found or not verified' });
|
|
}
|
|
|
|
// const hashedPassword = await hashPassword(password);
|
|
|
|
patient.email = email;
|
|
patient.password = hashedPassword;
|
|
patient.gender = gender;
|
|
patient.weight = weight;
|
|
patient.height = height;
|
|
patient.age = age;
|
|
patient.commonHealthComplaints = commonHealthComplaints;
|
|
patient.familyHistory = familyHistory;
|
|
patient.personalHistory = personalHistory;
|
|
patient.dailyRoutine = dailyRoutine;
|
|
|
|
await patient.save();
|
|
|
|
res.status(200).json({ message: 'Registration details updated successfully' });
|
|
} catch (error) {
|
|
|
|
res.status(500).json({
|
|
message: error.message ? error.message : "Server error!",
|
|
});
|
|
|
|
}
|
|
}; |