diff --git a/Utils/sendEmail.js b/Utils/sendEmail.js index 626a44d..e35135b 100644 --- a/Utils/sendEmail.js +++ b/Utils/sendEmail.js @@ -38,20 +38,29 @@ export default sendEmail; // const messagebirdClient = messagebird(apiKey); import { initClient } from "messagebird"; const messagebird = initClient("e7HGr3kMl6su4c79DKjNAwlLQ"); - export const sendOtp = (recipient, message) => { +// e7HGr3kMl6su4c79DKjNAwlLQ +//7oOgyzfNuwBnqMc2oK6aGfczs +//11yKY8EbdFJpugJzaKyAH3YaK + + +export const sendOtp = async (recipient, message) => { + if (!recipient || !message) { + return; + } const params = { - originator: "TestMessage", - recipients: `+918874747774`, - body: "hi how are you", + originator: "tavisa", + recipients: [recipient], + body: message, }; + messagebird.messages.create(params, (err, response) => { if (err) { - console.error("Error sending message:", err); + console.error("Error sending message-------:", err); return; } - console.log("Message sent successfully:", response); - console.log("Message rrrrrrrrrrrrrrrr:", response?.recipients?.items); + // console.log("Message sent successfully:", response); + // console.log("Message details:", response, response?.recipients?.items); }); }; diff --git a/resources/Patients/PatientController.js b/resources/Patients/PatientController.js index 5cf22ad..f72b58d 100644 --- a/resources/Patients/PatientController.js +++ b/resources/Patients/PatientController.js @@ -2,32 +2,36 @@ import crypto from 'crypto'; import Patient from './PatientModel.js' +import { sendOtp } from '../../Utils/sendEmail.js'; export const register = async (req, res) => { - const { name, mobileNumber } = req.body; + let { name, countryCode, mobileNumber } = req.body; + // Trim the country code and mobile number + countryCode = countryCode.trim(); + mobileNumber = mobileNumber.trim(); + const fullMobileNumber = `${countryCode}${mobileNumber}`; try { - let patient = await Patient.findOne({ mobileNumber }); - + let patient = await Patient.findOne({ mobileNumber: fullMobileNumber }); 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 + const otp = crypto.randomInt(100000, 1000000).toString(); + const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes if (patient) { patient.otp = otp; patient.otpExpires = otpExpires; } else { - patient = new Patient({ name, mobileNumber, otp, otpExpires }); + patient = new Patient({ name, mobileNumber: fullMobileNumber, otp, otpExpires }); } - await patient.save(); - // await sendOtp(mobileNumber, otp); + await sendOtp(fullMobileNumber, `Your Verification OTP is: ${otp}`); + res.cookie('patientId', patient._id.toString(), + { httpOnly: true } + ); // 1 day in milliseconds + // { httpOnly: true, maxAge: 24 * 60 * 60 * 1000 } - res.status(200).json({patient, message: `OTP ${otp} sent to your mobile number successfully` }); + + return res.status(200).json({ patient, message: `OTP ${otp} sent to your mobile number ${fullMobileNumber} successfully` }); } catch (error) { res.status(500).json({ message: error.message ? error.message : "Server error!", @@ -38,6 +42,7 @@ export const register = async (req, res) => { export const verifyOtp = async (req, res) => { const { mobileNumber, otp } = req.body; try { + mobileNumber = mobileNumber.trim(); const patient = await Patient.findOne({ mobileNumber }); if (!patient) { @@ -54,13 +59,214 @@ export const verifyOtp = async (req, res) => { await patient.save(); - res.status(200).json({patient, message: 'Mobile number verified successfully' }); + res.status(200).json({ patient, message: 'Mobile number verified successfully' }); } catch (error) { -res.status(500).json({ + res.status(500).json({ message: error.message ? error.message : "Server error!", - }); } + }); + } }; +//get All Product +export const getAllPatient = async (req, res) => { + try { + const PAGE_SIZE = parseInt(req.query?.show || "10"); + const page = parseInt(req.query?.page - 1 || "0"); + let obj = {}; + if (req.query?.name) + obj.name = { + $regex: new RegExp(req.query.name), + $options: "i", + }; + if (req.query?.category) obj.category = req.query.category; + if (req.query?.isVerified) + obj.isVerified = req.query.isVerified; + const total = await Patient.countDocuments(obj); + const patient = await Patient.find(obj) + // .populate({ + // path: "category addedBy master_GST variants.gst_Id", + // select: "name categoryName tax", + // }) + .limit(PAGE_SIZE) + .skip(PAGE_SIZE * page) + // .sort("name") + .sort({ + createdAt: -1, + }) + .exec(); + + if (patient) { + return res.status(200).json({ + success: true, + total_data: total, + total_pages: Math.ceil(total / PAGE_SIZE), + patient, + }); + } + } catch (error) { + res.status(500).json({ + success: false, + msg: error.message ? error.message : "Something went wrong!", + }); + } +}; + + +export const create1RegistrationDetails = async (req, res) => { + const { + email, + password, + confirmPassword, + } = req.body; + const patientId = req.cookies.patientId; + if (!patientId) { + return res.status(400).json({ message: 'Patient did not registered Or verified' }); + } + if (password !== confirmPassword) { + return res.status(400).json({ message: 'Password and confirm password do not match' }); + } + try { + const patient = await Patient.findById(patientId); + if (!patient || !patient.isVerified) { + return res.status(400).json({ message: 'Patient not found or not verified' }); + } + + // Check if another patient with the same email exists + const emailExists = await Patient.findOne({ email }); + if (emailExists && emailExists._id.toString() !== patientId) { + return res.status(400).json({ message: 'Email is already in use By Another patient' }); + } + patient.email = email; + patient.password = password; + + await patient.save(); + + res.status(200).json({ patient, message: 'Registration details updated successfully' }); + } catch (error) { + + res.status(500).json({ + message: error.message ? error.message : "Server error!", + }); + + } +}; + + +export const EnterPatientDetails = async (req, res) => { + const { + gender, + weightValue, + weightUnit, + heightValue, + heightUnit, + age, + } = req.body; + + switch (true) { + case !gender: + return res.status(400).json({ message: 'Gender is required' }); + case !weightValue: + return res.status(400).json({ message: 'weight Value is required' }); + case !weightUnit: + return res.status(400).json({ message: 'weight Unit is required' }); + case !heightValue: + return res.status(400).json({ message: 'height Value is required' }); + case !heightUnit: + return res.status(400).json({ message: 'height Unit is required' }); + case !age: + return res.status(400).json({ message: 'Age is required and Must Be a Number' }); + default: + + //gender Validate + if (!['Male', 'Female'].includes(gender)) { + return res.status(400).json({ message: 'Invalid gender:gender Must be "Male" or "Female"' }); + } + // Validate weightUnit + if (!['Kgs', 'Lbs'].includes(weightUnit)) { + return res.status(400).json({ message: 'Invalid weight unit. Must be "Kgs" or "Lbs"' }); + } + // Ensure weightValue is a number + if (isNaN(weightValue)) { + return res.status(400).json({ message: 'Weight value must be a number' }); + } + // Validate heightUnit + if (!['Ft', 'Cm'].includes(heightUnit)) { + return res.status(400).json({ message: 'Invalid height unit. Must be "Ft" or "Cm"' }); + } + // Ensure heightValue is a number + if (isNaN(heightValue)) { + return res.status(400).json({ message: 'Height value must be a number' }); + } + if (isNaN(age)) { + return res.status(400).json({ message: 'age must be a number' }); + } + const patientId = req.cookies.patientId; + if (!patientId) { + return res.status(400).json({ message: 'Patient did not registered Or verified' }); + } + try { + const patient = await Patient.findById(patientId); + if (!patient || !patient.isVerified) { + return res.status(400).json({ message: 'Patient not found or not verified' }); + } + patient.gender = gender; + patient.weight = { + value: weightValue, + unit: weightUnit, + }; + patient.height = { + value: heightValue, + unit: heightUnit, + }; + patient.age = age; + await patient.save(); + res.status(200).json({ patient, message: 'Patient details updated successfully' }); + } catch (error) { + + res.status(500).json({ + message: error.message ? error.message : "Server error!", + }); + + } + } +}; + +export const EnterPersonalDetails = async (req, res) => { + const { + commonHealthComplaints, + familyHistory, + personalHistory, + dailyRoutine, + } = req.body; + const patientId = req.cookies.patientId; + if (!patientId) { + return res.status(400).json({ message: 'Patient did not registered Or verified' }); + } + try { + const patient = await Patient.findById(patientId); + if (!patient || !patient.isVerified) { + return res.status(400).json({ message: 'Patient not found or not verified' }); + } + + // Check if another patient with the same email exists + + patient.commonHealthComplaints = commonHealthComplaints; + patient.familyHistory = familyHistory; + patient.personalHistory = personalHistory; + patient.dailyRoutine = dailyRoutine; + await patient.save(); + res.status(200).json({ patient, message: 'Patient Pesonal details updated successfully' }); + } catch (error) { + + res.status(500).json({ + message: error.message ? error.message : "Server error!", + }); + + } + +}; + + export const completeRegistration = async (req, res) => { const { mobileNumber, @@ -104,9 +310,43 @@ export const completeRegistration = async (req, res) => { res.status(200).json({ message: 'Registration details updated successfully' }); } catch (error) { -res.status(500).json({ + res.status(500).json({ message: error.message ? error.message : "Server error!", - }); + }); } -}; \ No newline at end of file +}; + + +export const Otp = 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(); + + // 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!", + }); + } +}; diff --git a/resources/Patients/PatientModel.js b/resources/Patients/PatientModel.js index d570f42..b233c8b 100644 --- a/resources/Patients/PatientModel.js +++ b/resources/Patients/PatientModel.js @@ -7,31 +7,37 @@ import jwt from "jsonwebtoken"; import crypto from "crypto"; const patientSchema = new mongoose.Schema( { - name: { - type: String, - required: true, - }, - mobileNumber: { - type: String, - required: true, - unique: true, - }, - otp: { - type: String, - required: false, - }, - otpExpires: { - type: Date, - }, - isVerified: { - type: Boolean, - default: false, - }, - deviceAdded: { - type: Boolean, - default: false, - }, - email: String, + name: { + type: String, + required: true, + }, + mobileNumber: { + type: String, + required: true, + unique: true, + }, + otp: { + type: String, + required: false, + }, + otpExpires: { + type: Date, + }, + isVerified: { + type: Boolean, + default: false, + }, + deviceAdded: { + type: Boolean, + default: false, + }, + email: { + type: String, + required: false, + unique: true, + validate: [validator.isEmail, "Please Enter a valid Email"], + }, + password: { type: String, minLength: [6, "Password should be greater than 6 characters"], @@ -47,23 +53,35 @@ const patientSchema = new mongoose.Schema( // required: true, }, }, - gender: { - type: String, - enum: ['Male', 'Female'], - }, - weight: { - type: String, - enum: ['Kilos', 'Lbs'], - }, - height: { - type: String, - enum: ['Feet', 'Cms'], - }, - age: Number, - commonHealthComplaints: String, - familyHistory: String, - personalHistory: String, - dailyRoutine: String, + gender: { + type: String, + enum: ['Male', 'Female'], + }, + + weight: { + value: { + type: Number, + }, + unit: { + type: String, + enum: ['Kgs', 'Lbs'], + }, + }, + height: { + value: { + type: Number, + }, + unit: { + type: String, + enum: ['Ft', 'Cm'], + }, + }, + + age: Number, + commonHealthComplaints: String, + familyHistory: String, + personalHistory: String, + dailyRoutine: String, resetPasswordToken: String, resetPasswordExpire: Date, @@ -108,7 +126,7 @@ patientSchema.methods.getResetPasswordToken = function () { const Patient = mongoose.model("Patient", patientSchema); - export default Patient; +export default Patient; diff --git a/resources/Patients/PatientRoute.js b/resources/Patients/PatientRoute.js index ec9f384..dbdefc7 100644 --- a/resources/Patients/PatientRoute.js +++ b/resources/Patients/PatientRoute.js @@ -2,11 +2,19 @@ import express from "express"; const router = express.Router(); // import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; -import { completeRegistration, register, verifyOtp } from "./PatientController.js"; +import { EnterPatientDetails, EnterPersonalDetails, Otp, completeRegistration, create1RegistrationDetails, getAllPatient, register, verifyOtp } from "./PatientController.js"; router.post('/register', register); router.post('/verify-otp', verifyOtp); +router.post('/rgstr_details-p1', create1RegistrationDetails); +router.post('/rgstr_details-p2', EnterPatientDetails); +router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails); + +router.get('/getAll', getAllPatient); + router.post('/complete-registration', completeRegistration); +router.get('/otp', Otp); + export default router; diff --git a/resources/user/userController.js b/resources/user/userController.js index 224fa06..ba73bcb 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -26,7 +26,7 @@ export const registerUser = async (req, res) => { const myCloud = await cloudinary.uploader.upload( files.tempFilePath, { - folder: "ATP/user-image", + folder: "tavisa/user-image", }, function (error, result) { result, error; @@ -66,11 +66,10 @@ export const registerUser = async (req, res) => { Welcome to AyurPulse - Let the Shopping Begin!
You can login into :${ - role === "Employee" || role === "admin" - ? `https://admin.smellika.com/` - : `https://smellika.com` - }
+You can login into :${role === "Employee" || role === "admin" + ? `https://admin.smellika.com/` + : `https://smellika.com` + }
Below are your login credentials:
Email: ${email}
@@ -338,7 +337,7 @@ export const updateProfile = catchAsyncErrors(async (req, res, next) => { // const myCloud = await cloudinary.v2.uploader.upload(userImage.tempFilePath, // { - // folder: "ATP/user-image", + // folder: "tavisa/user-image", // });