1- import User from "../models/User.js" ;
1+ // doctor.service.js
22import bcrypt from "bcrypt" ;
33import { 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+
526const 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-
5561const 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
110134const 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
127145export default {
@@ -131,4 +149,4 @@ export default {
131149 updateDoctor,
132150 deleteDoctor,
133151 updateWorkingHours,
134- } ;
152+ } ;
0 commit comments