From d8779294934e97a45e7954de0145cfa3b62fd74c Mon Sep 17 00:00:00 2001 From: pawan-dot <71133473+pawan-dot@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:59:03 +0530 Subject: [PATCH] login and upload image --- resources/Patients/PatientController.js | 110 ++++++++++++++++++++++-- resources/Patients/PatientRoute.js | 8 +- 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/resources/Patients/PatientController.js b/resources/Patients/PatientController.js index 2fb3758..45609d0 100644 --- a/resources/Patients/PatientController.js +++ b/resources/Patients/PatientController.js @@ -5,6 +5,7 @@ import Patient from './PatientModel.js' import sendEmail, { sendOtp } from '../../Utils/sendEmail.js'; import validator from "validator"; import password from "secure-random-password"; +import cloudinary from '../../Utils/cloudinary.js'; export const register = async (req, res) => { let { name, countryCode, mobileNumber } = req.body; @@ -28,10 +29,7 @@ export const register = async (req, res) => { } await patient.save(); await sendOtp(fullMobileNumber, `Your tavisa verification OTP is: ${otp}`); - res.cookie('patientId', patient._id.toString(), - { httpOnly: true } - ); // 1 day in milliseconds - // { httpOnly: true, maxAge: 24 * 60 * 60 * 1000 } + return res.status(200).json({ patient, message: `OTP sent to your mobile number ${fullMobileNumber} successfully` }); @@ -61,6 +59,10 @@ export const verifyOtp = async (req, res) => { patient.otpExpires = undefined; await patient.save(); + 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: 'Mobile number verified successfully' }); } catch (error) { @@ -69,6 +71,37 @@ export const verifyOtp = async (req, res) => { }); } }; +// Login Patient +export const loginPatient = async (req, res) => { + const { email, password } = req.body; + // checking if patient has given password and email both + + try { + if (!email || !password) { + return res.status(400).json({ message: "Please Enter Email & Password" }); + } + + const patient = await Patient.findOne({ email }).select("+password"); + + if (!patient) { + return res.status(400).json({ message: "Invalid Email or Password" }); + } + + const isPasswordMatched = await patient.comparePassword(password); + + if (!isPasswordMatched) { + return res.status(400).json({ message: "Invalid Email or Password" }); + } + await res.cookie('patientId', patient._id.toString(), + { httpOnly: true } + ); + return res.status(200).json({ success: true, message: "Login Successfully" }); + } catch (error) { + return res + .status(500) + .json({ message: "Something went wrong!", error: error?.message || "" }); + } +}; //get All Product export const getAllPatient = async (req, res) => { @@ -280,7 +313,9 @@ export const EnterPersonalDetails = async (req, res) => { export const forgotPassword = async (req, res, next) => { const patient = await Patient.findOne({ email: req.body.email }); - + if (!req.body.email) { + return res.status(400).json({ message: 'please Enter Email!' }); + } if (!patient) { return res.status(404).json({ message: "Patient not found" }); } @@ -328,6 +363,71 @@ export const forgotPassword = async (req, res, next) => { .json({ message: "Something went wrong!", error: error?.message || "" }); } }; + +//update Patient Profile Image +export const UploadProfileImage = async (req, res) => { + if (!req.files) { + return res.status(404).json({ message: "Please Select Image" }); + } + // const patientId = req.cookies.patientId; + // console.log(req.params?.patientId) + if (!req.params?.patientId) { + return res.status(400).json({ message: 'Please Provide patientId!' }); + } + let newPatientData = {}; + try { + if (req.files) { + const patientImage = req.files?.avatar; + const patient = await Patient.findById(req.params?.patientId); + + if (patient?.avatar === null) { + // console.log(patient?.avatar) + const imageId = patient?.avatar?.public_id; + + await cloudinary.uploader.destroy(imageId) + } + + const myCloud = await cloudinary.v2.uploader.upload(patientImage.tempFilePath, + { + folder: "tavisa/patient-image", + + }); + + newPatientData.avatar = { + public_id: myCloud.public_id, + url: myCloud.secure_url, + }; + let patientDetail = await Patient.findByIdAndUpdate( + req.params?.patientId, newPatientData, + { new: true } // Return the updated document + ); + // console.log(patientDetail) + + // const patientDetail = await Patient.findByIdAndUpdate(patientId, newPatientData, { + // new: true, + // runValidators: true, + // useFindAndModify: false, + // }); + + return res.status(200).json({ + success: true, + message: "Image Uploaded Successfully!", + patientDetail, + }); + } + } catch (error) { + + return res + .status(500) + .json({ message: "Something went wrong!", error: error?.message || "" }); + } + + + +}; + + + export const completeRegistration = async (req, res) => { const { mobileNumber, diff --git a/resources/Patients/PatientRoute.js b/resources/Patients/PatientRoute.js index 2d8b089..e928aba 100644 --- a/resources/Patients/PatientRoute.js +++ b/resources/Patients/PatientRoute.js @@ -2,11 +2,14 @@ import express from "express"; const router = express.Router(); // import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; -import { EnterPatientDetails, EnterPersonalDetails, Otp, completeRegistration, create1RegistrationDetails, forgotPassword, getAllPatient, register, verifyOtp } from "./PatientController.js"; +import { EnterPatientDetails, EnterPersonalDetails, Otp, UploadProfileImage, completeRegistration, create1RegistrationDetails, forgotPassword, getAllPatient, loginPatient, register, verifyOtp } from "./PatientController.js"; router.post('/register', register); router.post('/verify-otp', verifyOtp); +router.post('/login', loginPatient); + + router.post('/rgstr_details-p1', create1RegistrationDetails); router.post('/rgstr_details-p2', EnterPatientDetails); router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails); @@ -14,7 +17,10 @@ router.post('/rgstr_psrnl_details-p3', EnterPersonalDetails); router.get('/getAll', getAllPatient); router.post('/complete-registration', completeRegistration); + router.post('/forgot-password', forgotPassword); +router.post('/upload-image/:patientId', UploadProfileImage); + router.get('/otp', Otp);