1- import User from '../models/User.js' ;
2- import bcrypt from 'bcrypt' ;
1+ import receptionService from "../services/reception.service.js" ;
32
4- const creatReceptionist = async ( req , res ) => {
5- try {
6- const password = req . body . password || 'reception@123' ;
7- const hashedPassword = await bcrypt . hash ( password , 10 ) ;
8-
9- const receptionist = new User ( {
10- ...req . body ,
11- password : hashedPassword ,
12- role : 'receptionist'
13- } ) ;
14-
15- await receptionist . save ( ) ;
3+ const creatReceptionist = async ( req , res ) => {
4+ try {
5+ const receptionist = await receptionService . creatReceptionist ( req . body ) ;
166 res . status ( 201 ) . json ( receptionist ) ;
17- } catch ( err ) {
7+ } catch ( err ) {
188 res . status ( 400 ) . json ( { message : err . message } ) ;
199 }
20- }
10+ } ;
2111
2212const changePassword = async ( req , res ) => {
2313 try {
24- const receptionist = await User . findById ( req . user . id ) ;
25- const { oldPassword, newPassword} = req . body ;
26-
27- const isMatch = await bcrypt . compare ( oldPassword , receptionist . password ) ;
28- if ( ! isMatch ) return res . status ( 400 ) . json ( { message : "Old password is incorrect" } ) ;
29-
30- receptionist . password = await bcrypt . hash ( newPassword , 10 ) ;
31- await receptionist . save ( ) ;
32-
33- res . status ( 200 ) . json ( { message : "Password updated successfully" } ) ;
14+ const { oldPassword, newPassword } = req . body ;
15+ const result = await receptionService . changePassword (
16+ req . user . id ,
17+ oldPassword ,
18+ newPassword
19+ ) ;
20+ res . status ( 200 ) . json ( result ) ;
3421 } catch ( err ) {
35- res . status ( 500 ) . json ( { message : err . message } ) ;
22+ res . status ( 400 ) . json ( { message : err . message } ) ;
3623 }
3724} ;
3825
39- const getReceptionist = async ( req , res ) => {
26+ const getReceptionists = async ( req , res ) => {
4027 try {
41- const receptionist = await User . find ( { role : 'receptionist' } ) ;
28+ const receptionist = await receptionService . getReceptionists ( ) ;
4229 res . json ( receptionist ) ;
4330 } catch ( err ) {
4431 res . status ( 500 ) . json ( { message : err . message } ) ;
@@ -48,47 +35,28 @@ const getReceptionist = async (req, res) => {
4835const updateReceptionist = async ( req , res ) => {
4936 try {
5037 const { id } = req . params ;
51-
52- if ( res . body . password ) {
53- req . body . password = await bcrypt . hash ( req . body . password , 10 ) ;
54- }
55-
56- const receptionist = await User . findOneAndUpdate (
57- { _id : id , role : 'receptionist' } ,
58- req . body ,
59- { new : true , runValidators : true }
60- ) ;
61-
62- if ( ! receptionist ) {
63- return res . status ( 404 ) . json ( { message : 'Receptionist not found' } ) ;
64- }
65-
38+ const receptionist = await receptionService . updateReceptionist ( id , req . body ) ;
6639 res . json ( receptionist ) ;
6740 } catch ( err ) {
6841 res . status ( 400 ) . json ( { message : err . message } ) ;
6942 }
7043} ;
7144
7245const deleteReceptionist = async ( req , res ) => {
73- try {
46+ try {
7447 const { id } = req . params ;
75-
76- const receptionist = await User . findOneAndDelete ( { _id : id , role : 'receptionist' } ) ;
77-
78- if ( ! receptionist ) {
79- return res . status ( 404 ) . json ( { message : "Receptionist not found" } ) ;
80- }
81-
82- res . json ( { message : "Receptionist deleted successfully" } ) ;
48+ const result = await receptionService . deleteReceptionist ( id ) ;
49+ res . json ( result ) ;
8350 } catch ( err ) {
8451 res . status ( 500 ) . json ( { message : err . message } ) ;
8552 }
8653} ;
8754
55+
8856export {
8957 creatReceptionist ,
9058 changePassword ,
91- getReceptionist ,
59+ getReceptionists ,
9260 updateReceptionist ,
9361 deleteReceptionist
9462} ;
0 commit comments