Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },

Expand Down
2 changes: 1 addition & 1 deletion src/models/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Patientschema = new mongoose.Schema(
},
gender: {
type: String,
enum: ["male", "female", "other"],
enum: ["Male", "Female", "Other"],
required: true,
},
status: {
Expand Down
106 changes: 62 additions & 44 deletions src/services/doctor.service.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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");

Expand All @@ -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 {
Expand All @@ -131,4 +149,4 @@ export default {
updateDoctor,
deleteDoctor,
updateWorkingHours,
};
};
7 changes: 4 additions & 3 deletions src/utils/uniqueness.js
Original file line number Diff line number Diff line change
@@ -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 }],
};
Expand All @@ -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) {
Expand All @@ -19,4 +20,4 @@ const checkUniqueUser = async ({ email, phno, excludeId = null }) => {
}
};

export { checkUniqueUser };
export { checkUniqueUser };
Loading