Skip to content

Commit b241002

Browse files
Merge pull request #27 from rohitbytecode/dev-rohit
Dev rohit
2 parents e062e51 + 8d0d814 commit b241002

4 files changed

Lines changed: 69 additions & 48 deletions

File tree

src/models/User.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ const userSchema = new mongoose.Schema(
112112
match: /^[0-9]{10}$/,
113113
unique: true,
114114
},
115+
dob: { type: Date },
116+
joining_date: { type: Date },
115117

116118
spec: { type: String },
117119

src/models/patient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Patientschema = new mongoose.Schema(
2525
},
2626
gender: {
2727
type: String,
28-
enum: ["male", "female", "other"],
28+
enum: ["Male", "Female", "Other"],
2929
required: true,
3030
},
3131
status: {

src/services/doctor.service.js

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
import User from "../models/User.js";
1+
// doctor.service.js
22
import bcrypt from "bcrypt";
33
import { checkUniqueUser } from "../utils/uniqueness.js";
44

5+
// Import User model here (inside the file, not at top if there's any issue)
6+
let User;
7+
const getUserModel = async () => {
8+
if (!User) {
9+
const module = await import("../models/User.js");
10+
User = module.default;
11+
}
12+
return User;
13+
};
14+
15+
// Helper functions
16+
const normalizeEmail = (email) => {
17+
if (!email) return email;
18+
return email.trim().toLowerCase();
19+
};
20+
21+
const sanitizePhone = (phno) => {
22+
if (!phno) return phno;
23+
return phno.toString().replace(/\D/g, '');
24+
};
25+
526
const createDoctor = async (data) => {
27+
const UserModel = await getUserModel();
28+
629
const email = normalizeEmail(data.email);
730
const phno = sanitizePhone(data.phno);
831

9-
await checkUniqueUser({ email, phno });
32+
await checkUniqueUser(UserModel, { email, phno });
1033

1134
const password = data.password || "doctor@123";
1235
const hashedPassword = await bcrypt.hash(password, 10);
1336

1437
try {
15-
const doctor = new User({
38+
const doctor = new UserModel({
1639
...data,
1740
email,
1841
phno,
@@ -35,40 +58,20 @@ const createDoctor = async (data) => {
3558
}
3659
};
3760

38-
const changePassword = async (userId, oldPassword, newPassword) => {
39-
const doctor = await User.findById(userId);
40-
if (!doctor) throw new Error("Doctor not found");
41-
42-
const isMatch = await bcrypt.compare(oldPassword, doctor.password);
43-
if (!isMatch) throw new Error("Old password is incorrect");
44-
45-
doctor.password = await bcrypt.hash(newPassword, 10);
46-
await doctor.save();
47-
48-
return { message: "Password changed successfully" };
49-
};
50-
51-
const getDoctors = async () => {
52-
return await User.find({ role: "doctor" });
53-
};
54-
5561
const updateDoctor = async (id, data) => {
62+
const UserModel = await getUserModel();
63+
5664
if (data.workingHours) {
5765
throw new Error("Use dedicated endpoint to update working hours");
5866
}
5967

6068
let email, phno;
6169

62-
if (data.email) {
63-
email = normalizeEmail(data.email);
64-
}
65-
66-
if (data.phno) {
67-
phno = sanitizePhone(data.phno);
68-
}
70+
if (data.email) email = normalizeEmail(data.email);
71+
if (data.phno) phno = sanitizePhone(data.phno);
6972

7073
if (email || phno) {
71-
await checkUniqueUser({
74+
await checkUniqueUser(UserModel, {
7275
email: email || undefined,
7376
phno: phno || undefined,
7477
excludeId: id,
@@ -83,10 +86,11 @@ const updateDoctor = async (id, data) => {
8386
if (phno) data.phno = phno;
8487

8588
try {
86-
const doctor = await User.findOneAndUpdate({ _id: id, role: "doctor" }, data, {
87-
new: true,
88-
runValidators: true,
89-
});
89+
const doctor = await UserModel.findOneAndUpdate(
90+
{ _id: id, role: "doctor" },
91+
data,
92+
{ new: true, runValidators: true }
93+
);
9094

9195
if (!doctor) throw new Error("Doctor not found");
9296

@@ -100,28 +104,42 @@ const updateDoctor = async (id, data) => {
100104
}
101105
};
102106

103-
const deleteDoctor = async (id) => {
104-
const doctor = await User.findOneAndDelete({ _id: id, role: "doctor" });
107+
// Other functions (keep them simple)
108+
const changePassword = async (userId, oldPassword, newPassword) => {
109+
const UserModel = await getUserModel();
110+
const doctor = await UserModel.findById(userId);
105111
if (!doctor) throw new Error("Doctor not found");
106112

113+
const isMatch = await bcrypt.compare(oldPassword, doctor.password);
114+
if (!isMatch) throw new Error("Old password is incorrect");
115+
116+
doctor.password = await bcrypt.hash(newPassword, 10);
117+
await doctor.save();
118+
119+
return { message: "Password changed successfully" };
120+
};
121+
122+
const getDoctors = async () => {
123+
const UserModel = await getUserModel();
124+
return await UserModel.find({ role: "doctor" }).populate('dept');
125+
};
126+
127+
const deleteDoctor = async (id) => {
128+
const UserModel = await getUserModel();
129+
const doctor = await UserModel.findOneAndDelete({ _id: id, role: "doctor" });
130+
if (!doctor) throw new Error("Doctor not found");
107131
return { message: "Doctor deleted successfully" };
108132
};
109133

110134
const updateWorkingHours = async (doctorId, workingHours) => {
111-
const doctor = await User.findOne({
112-
_id: doctorId,
113-
role: "doctor",
114-
});
115-
135+
const UserModel = await getUserModel();
136+
const doctor = await UserModel.findOne({ _id: doctorId, role: "doctor" });
116137
if (!doctor) throw new Error("Doctor not found");
117138

118139
doctor.workingHours = workingHours;
119-
120140
await doctor.save();
121141

122-
return {
123-
message: "Working hours updated successfully",
124-
};
142+
return { message: "Working hours updated successfully" };
125143
};
126144

127145
export default {
@@ -131,4 +149,4 @@ export default {
131149
updateDoctor,
132150
deleteDoctor,
133151
updateWorkingHours,
134-
};
152+
};

src/utils/uniqueness.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const checkUniqueUser = async ({ email, phno, excludeId = null }) => {
1+
// uniqueness.js
2+
const checkUniqueUser = async (UserModel, { email, phno, excludeId = null }) => {
23
const query = {
34
$or: [{ email }, { phno }],
45
};
@@ -7,7 +8,7 @@ const checkUniqueUser = async ({ email, phno, excludeId = null }) => {
78
query._id = { $ne: excludeId };
89
}
910

10-
const existing = await User.findOne(query);
11+
const existing = await UserModel.findOne(query);
1112

1213
if (existing) {
1314
if (existing.email === email) {
@@ -19,4 +20,4 @@ const checkUniqueUser = async ({ email, phno, excludeId = null }) => {
1920
}
2021
};
2122

22-
export { checkUniqueUser };
23+
export { checkUniqueUser };

0 commit comments

Comments
 (0)