diff --git a/middlewares/PatientAuth.js b/middlewares/PatientAuth.js index aec0fdb..4be402c 100644 --- a/middlewares/PatientAuth.js +++ b/middlewares/PatientAuth.js @@ -48,4 +48,3 @@ export const isAuthenticatedPatient = async (req, res, next) => { // }); } }; - diff --git a/resources/Patients/PatientController.js b/resources/Patients/PatientController.js index a88e2f4..6419c4a 100644 --- a/resources/Patients/PatientController.js +++ b/resources/Patients/PatientController.js @@ -174,7 +174,7 @@ export const create1RegistrationDetails = async (req, res) => { // Check if another patient with the same email exists const emailExists = await Patient.findOne({ email }); - if (emailExists && emailExists._id.toString() !== req.patient._id) { + if (emailExists && emailExists._id.toString() !== req.patient._id.toString()) { return res.status(400).json({ message: 'This Email ID is already in use By Another patient' }); } patient.email = email; @@ -575,7 +575,7 @@ export const UpdateProile = async (req, res) => { dailyRoutine, } = req.body; // Validate email - if (email && !validator.isEmail(email)) { + if (!validator.isEmail(email)) { return res.status(400).json({ message: 'Invalid email address' }); } @@ -613,7 +613,7 @@ export const UpdateProile = async (req, res) => { } if (email) { const emailExists = await Patient.findOne({ email }); - if (emailExists && emailExists._id.toString() !== req.patient._id) { + if (emailExists && emailExists._id.toString() !== req.patient._id.toString()) { return res.status(400).json({ message: 'This Email ID is already in use By Another patient' }); } } @@ -635,19 +635,7 @@ export const UpdateProile = async (req, res) => { , { new: true } // Return the updated document ); - // patient.gender = gender; - // patient.weight = { - // value: weightValue, - // unit: weightUnit, - // }; - // patient.height = { - // value: heightValue, - // unit: heightUnit, - // }; - // patient.age = age; - // await patient.save(); - // const patientResponse = patient.toObject(); - // delete patientResponse.password; + return res.status(200).json({ patient: NewPatientDetail, message: 'Profile updated successfully' }); } catch (error) { res.status(500).json({ @@ -656,6 +644,50 @@ export const UpdateProile = async (req, res) => { } } +//change Patient password +export const ChangePassword = async (req, res) => { + const { oldPassword, newPassword, confirmPassword } = req.body + + if (!oldPassword) { + return res.status(400).json({ message: 'Please Enter Old password' }); + + } + if (!newPassword) { + return res.status(400).json({ message: 'Please Enter New Password ' }); + + } + if (!confirmPassword) { + return res.status(400).json({ message: 'Please Enter Confirm Password' }); + + } + try { + const patient = await Patient.findById(req.patient._id).select("+password"); + + const isPasswordMatched = await patient.comparePassword(req.body.oldPassword); + + if (!isPasswordMatched) { + return res.status(400).json({ message: 'Old password is incorrect' }); + + } + + if (req.body.newPassword !== req.body.confirmPassword) { + return res.status(400).json({ message: 'old password and confirm Password does not match' }); + + } + patient.password = req.body.newPassword; + await patient.save(); + // const token = patient.getJWTToken(); + + return res.status(200).json({ success: true, message: 'Password updated successfully' }); + + // sendToken(patient, 200, res); + } catch (error) { + res.status(500).json({ + message: error.message ? error.message : "Server error!", + }); + } + +}; diff --git a/resources/Patients/PatientRoute.js b/resources/Patients/PatientRoute.js index eb0e223..4f17f89 100644 --- a/resources/Patients/PatientRoute.js +++ b/resources/Patients/PatientRoute.js @@ -1,7 +1,7 @@ import express from "express"; const router = express.Router(); -import { EnterPatientDetails, EnterPersonalDetails, Otp, UploadProfileImage, create1RegistrationDetails, deletePatient, forgotPassword, getAllPatient, loginPatient, register, updateMobileNumber, verifyUpdatedMobileOtp, verifyOtp, UpdateProile, getmyProfile } from "./PatientController.js"; +import { EnterPatientDetails, EnterPersonalDetails, Otp, UploadProfileImage, create1RegistrationDetails, deletePatient, forgotPassword, getAllPatient, loginPatient, register, updateMobileNumber, verifyUpdatedMobileOtp, verifyOtp, UpdateProile, getmyProfile, ChangePassword } from "./PatientController.js"; import { isAuthenticatedPatient } from "../../middlewares/PatientAuth.js"; import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; @@ -25,6 +25,9 @@ router.post('/forgot-password', forgotPassword); router.post('/profile-image/upload', isAuthenticatedPatient, UploadProfileImage); router.patch('/profile/update', isAuthenticatedPatient, UpdateProile); +//change password +router.put('/password/update', isAuthenticatedPatient, ChangePassword); + //delete Patient