From 4263b959b2ac4527e06917ecd8d5c1e8fc8f9fed Mon Sep 17 00:00:00 2001 From: rohitcpp Date: Sat, 18 Apr 2026 08:10:30 +0530 Subject: [PATCH 1/2] fix: doctor form validation --- src/models/User.js | 2 + src/services/doctor.service.js | 106 +++++++++++++++++++-------------- src/utils/uniqueness.js | 7 ++- 3 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/models/User.js b/src/models/User.js index e7db380..6d0245f 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -112,6 +112,8 @@ const userSchema = new mongoose.Schema( match: /^[0-9]{10}$/, unique: true, }, + dob: { type: Date }, + joining_date: { type: Date }, spec: { type: String }, diff --git a/src/services/doctor.service.js b/src/services/doctor.service.js index b2aff4b..065282b 100644 --- a/src/services/doctor.service.js +++ b/src/services/doctor.service.js @@ -1,18 +1,41 @@ -import User from "../models/User.js"; +// doctor.service.js import bcrypt from "bcrypt"; import { checkUniqueUser } from "../utils/uniqueness.js"; +// Import User model here (inside the file, not at top if there's any issue) +let User; +const getUserModel = async () => { + if (!User) { + const module = await import("../models/User.js"); + User = module.default; + } + return User; +}; + +// Helper functions +const normalizeEmail = (email) => { + if (!email) return email; + return email.trim().toLowerCase(); +}; + +const sanitizePhone = (phno) => { + if (!phno) return phno; + return phno.toString().replace(/\D/g, ''); +}; + const createDoctor = async (data) => { + const UserModel = await getUserModel(); + const email = normalizeEmail(data.email); const phno = sanitizePhone(data.phno); - await checkUniqueUser({ email, phno }); + await checkUniqueUser(UserModel, { email, phno }); const password = data.password || "doctor@123"; const hashedPassword = await bcrypt.hash(password, 10); try { - const doctor = new User({ + const doctor = new UserModel({ ...data, email, phno, @@ -35,40 +58,20 @@ const createDoctor = async (data) => { } }; -const changePassword = async (userId, oldPassword, newPassword) => { - const doctor = await User.findById(userId); - if (!doctor) throw new Error("Doctor not found"); - - const isMatch = await bcrypt.compare(oldPassword, doctor.password); - if (!isMatch) throw new Error("Old password is incorrect"); - - doctor.password = await bcrypt.hash(newPassword, 10); - await doctor.save(); - - return { message: "Password changed successfully" }; -}; - -const getDoctors = async () => { - return await User.find({ role: "doctor" }); -}; - const updateDoctor = async (id, data) => { + const UserModel = await getUserModel(); + if (data.workingHours) { throw new Error("Use dedicated endpoint to update working hours"); } let email, phno; - if (data.email) { - email = normalizeEmail(data.email); - } - - if (data.phno) { - phno = sanitizePhone(data.phno); - } + if (data.email) email = normalizeEmail(data.email); + if (data.phno) phno = sanitizePhone(data.phno); if (email || phno) { - await checkUniqueUser({ + await checkUniqueUser(UserModel, { email: email || undefined, phno: phno || undefined, excludeId: id, @@ -83,10 +86,11 @@ const updateDoctor = async (id, data) => { if (phno) data.phno = phno; try { - const doctor = await User.findOneAndUpdate({ _id: id, role: "doctor" }, data, { - new: true, - runValidators: true, - }); + const doctor = await UserModel.findOneAndUpdate( + { _id: id, role: "doctor" }, + data, + { new: true, runValidators: true } + ); if (!doctor) throw new Error("Doctor not found"); @@ -100,28 +104,42 @@ const updateDoctor = async (id, data) => { } }; -const deleteDoctor = async (id) => { - const doctor = await User.findOneAndDelete({ _id: id, role: "doctor" }); +// Other functions (keep them simple) +const changePassword = async (userId, oldPassword, newPassword) => { + const UserModel = await getUserModel(); + const doctor = await UserModel.findById(userId); if (!doctor) throw new Error("Doctor not found"); + const isMatch = await bcrypt.compare(oldPassword, doctor.password); + if (!isMatch) throw new Error("Old password is incorrect"); + + doctor.password = await bcrypt.hash(newPassword, 10); + await doctor.save(); + + return { message: "Password changed successfully" }; +}; + +const getDoctors = async () => { + const UserModel = await getUserModel(); + return await UserModel.find({ role: "doctor" }).populate('dept'); +}; + +const deleteDoctor = async (id) => { + const UserModel = await getUserModel(); + const doctor = await UserModel.findOneAndDelete({ _id: id, role: "doctor" }); + if (!doctor) throw new Error("Doctor not found"); return { message: "Doctor deleted successfully" }; }; const updateWorkingHours = async (doctorId, workingHours) => { - const doctor = await User.findOne({ - _id: doctorId, - role: "doctor", - }); - + const UserModel = await getUserModel(); + const doctor = await UserModel.findOne({ _id: doctorId, role: "doctor" }); if (!doctor) throw new Error("Doctor not found"); doctor.workingHours = workingHours; - await doctor.save(); - return { - message: "Working hours updated successfully", - }; + return { message: "Working hours updated successfully" }; }; export default { @@ -131,4 +149,4 @@ export default { updateDoctor, deleteDoctor, updateWorkingHours, -}; +}; \ No newline at end of file diff --git a/src/utils/uniqueness.js b/src/utils/uniqueness.js index 865b704..212af69 100644 --- a/src/utils/uniqueness.js +++ b/src/utils/uniqueness.js @@ -1,4 +1,5 @@ -const checkUniqueUser = async ({ email, phno, excludeId = null }) => { +// uniqueness.js +const checkUniqueUser = async (UserModel, { email, phno, excludeId = null }) => { const query = { $or: [{ email }, { phno }], }; @@ -7,7 +8,7 @@ const checkUniqueUser = async ({ email, phno, excludeId = null }) => { query._id = { $ne: excludeId }; } - const existing = await User.findOne(query); + const existing = await UserModel.findOne(query); if (existing) { if (existing.email === email) { @@ -19,4 +20,4 @@ const checkUniqueUser = async ({ email, phno, excludeId = null }) => { } }; -export { checkUniqueUser }; +export { checkUniqueUser }; \ No newline at end of file From 8d0d814f46ca620b96483ffa870c68c2eb59c7a8 Mon Sep 17 00:00:00 2001 From: rohitcpp Date: Sat, 18 Apr 2026 08:36:42 +0530 Subject: [PATCH 2/2] fix: patient form, duplicate data pending --- src/models/patient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/patient.js b/src/models/patient.js index f270027..675207a 100644 --- a/src/models/patient.js +++ b/src/models/patient.js @@ -25,7 +25,7 @@ const Patientschema = new mongoose.Schema( }, gender: { type: String, - enum: ["male", "female", "other"], + enum: ["Male", "Female", "Other"], required: true, }, status: {