1+ import User from '../models/User.js' ;
2+ import bcrypt from 'bcrypt' ;
3+
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 ( ) ;
16+ res . status ( 201 ) . json ( receptionist ) ;
17+ } catch ( err ) {
18+ res . status ( 400 ) . json ( { message : err . message } ) ;
19+ }
20+ }
21+
22+ const changePassword = async ( req , res ) => {
23+ 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" } ) ;
34+ } catch ( err ) {
35+ res . status ( 500 ) . json ( { message : err . message } ) ;
36+ }
37+ } ;
38+
39+ const getReceptionist = async ( req , res ) => {
40+ try {
41+ const receptionist = await User . find ( { role : 'receptionist' } ) ;
42+ res . json ( receptionist ) ;
43+ } catch ( err ) {
44+ res . status ( 500 ) . json ( { message : err . message } ) ;
45+ }
46+ } ;
47+
48+ const updateReceptionist = async ( req , res ) => {
49+ try {
50+ 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+
66+ res . json ( receptionist ) ;
67+ } catch ( err ) {
68+ res . status ( 400 ) . json ( { message : err . message } ) ;
69+ }
70+ } ;
71+
72+ const deleteReceptionist = async ( req , res ) => {
73+ try {
74+ 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" } ) ;
83+ } catch ( err ) {
84+ res . status ( 500 ) . json ( { message : err . message } ) ;
85+ }
86+ } ;
87+
88+ export {
89+ creatReceptionist ,
90+ changePassword ,
91+ getReceptionist ,
92+ updateReceptionist ,
93+ deleteReceptionist
94+ } ;
0 commit comments