1- import User from '../models/User.js' ;
2- import bcrypt from 'bcrypt' ;
1+ import doctorService from '../services/doctor.service.js' ;
32
43const createDoctor = async ( req , res ) => {
54 try {
6- const password = req . body . password || "doctor@123" ;
7- const hashedPassword = await bcrypt . hash ( password , 10 ) ;
8-
9- const doctor = new User ( {
10- ...req . body ,
11- password : hashedPassword ,
12- role : 'doctor'
13- } ) ;
14-
15- await doctor . save ( ) ;
5+ const doctor = await doctorService . createDoctor ( req . body ) ;
166 res . status ( 201 ) . json ( doctor ) ;
177 } catch ( err ) {
188 res . status ( 400 ) . json ( { message : err . message } ) ;
@@ -21,25 +11,20 @@ const createDoctor = async (req, res) => {
2111
2212const changePassword = async ( req , res ) => {
2313 try {
24- const doctor = await User . findById ( req . user . id ) ;
2514 const { oldPassword, newPassword } = req . body ;
26-
27- const isMatch = await bcrypt . compare ( oldPassword , doctor . password ) ;
28- if ( ! isMatch ) return res . status ( 400 ) . json ( { message : 'Old password is incorrect' } ) ;
29-
30- doctor . password = await bcrypt . hash ( newPassword , 10 ) ;
31- await doctor . save ( ) ;
32-
33- res . status ( 200 ) . json ( { message : 'Password updated successfully' } ) ;
15+ const result = await doctorService . changePassword (
16+ req . user . id ,
17+ oldPassword ,
18+ newPassword ) ;
19+ res . status ( 200 ) . json ( result ) ;
3420 } catch ( err ) {
35- res . status ( 500 ) . json ( { message : err . message } ) ;
21+ res . status ( 400 ) . json ( { message : err . message } ) ;
3622 }
3723} ;
3824
39-
4025const getDoctors = async ( req , res ) => {
4126 try {
42- const doctors = await User . find ( { role : 'doctor' } ) ;
27+ const doctors = await doctorService . getDoctors ( ) ;
4328 res . json ( doctors ) ;
4429 } catch ( err ) {
4530 res . status ( 500 ) . json ( { message : err . message } ) ;
@@ -49,21 +34,7 @@ const getDoctors = async (req, res) => {
4934const updateDoctor = async ( req , res ) => {
5035 try {
5136 const { id } = req . params ;
52-
53- if ( req . body . password ) {
54- req . body . password = await bcrypt . hash ( req . body . password , 10 ) ;
55- }
56-
57- const doctor = await User . findOneAndUpdate (
58- { _id : id , role : 'doctor' } ,
59- req . body ,
60- { new : true , runValidators : true }
61- ) ;
62-
63- if ( ! doctor ) {
64- return res . status ( 404 ) . json ( { message : 'Doctor not found' } ) ;
65- }
66-
37+ const doctor = await doctorService . updateDoctor ( id , req . body ) ;
6738 res . json ( doctor ) ;
6839 } catch ( err ) {
6940 res . status ( 400 ) . json ( { message : err . message } ) ;
@@ -73,19 +44,13 @@ const updateDoctor = async (req, res) => {
7344const deleteDoctor = async ( req , res ) => {
7445 try {
7546 const { id } = req . params ;
76-
77- const doctor = await User . findOneAndDelete ( { _id : id , role : 'doctor' } ) ;
78-
79- if ( ! doctor ) {
80- return res . status ( 404 ) . json ( { message : 'Doctor not found' } ) ;
81- }
82-
83- res . json ( { message : 'Doctor deleted successfully' } ) ;
47+ const result = await doctorService . deleteDoctor ( id ) ;
48+ res . json ( result ) ;
8449 } catch ( err ) {
8550 res . status ( 500 ) . json ( { message : err . message } ) ;
8651 }
8752} ;
88-
53+
8954export {
9055 createDoctor ,
9156 changePassword ,
0 commit comments