Skip to content

Commit 1ff8b9c

Browse files
committed
feat: reception crud add, user receptionist add
1 parent 7948d0b commit 1ff8b9c

6 files changed

Lines changed: 118 additions & 5 deletions

File tree

src/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import adminRoutes from './routes/adminroutes.js';
88

99
import doctorRoutes from './routes/doctor.routes.js';
1010
import nurseRoutes from './routes/nurse.routes.js';
11+
import receptionRoutes from './routes/reception.routes.js';
1112

1213
import patientRoutes from './routes/patient.js';
1314
import deptRoutes from './routes/dept.js';
@@ -30,6 +31,7 @@ app.use('/api/admin', adminRoutes);
3031

3132
app.use('/api/users/doctors', doctorRoutes);
3233
app.use('/api/users/nurses', nurseRoutes);
34+
app.use('/api/users/receptionist', receptionRoutes);
3335

3436
app.use('/api/patients', patientRoutes);
3537
app.use('/api/departments', deptRoutes);

src/controllers/patient.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const updatePatient = async (req, res) => {
3838
}
3939
};
4040

41-
/*const deletePatient = async (req, res) => {
41+
const deletePatient = async (req, res) => {
4242
try {
4343
const { id } = req.params;
4444

@@ -52,11 +52,11 @@ const updatePatient = async (req, res) => {
5252
} catch (err) {
5353
res.status(500).json({ message: err.message });
5454
}
55-
};*/
55+
};
5656

5757
export {
5858
createPatient,
5959
getPatients,
6060
updatePatient,
61-
//deletePatient
61+
deletePatient
6262
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
};

src/models/User.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const userSchema = new mongoose.Schema({
1212
role: {
1313
type: String,
1414
required: true,
15-
enum: ['admin', 'doctor', 'nurse']
15+
enum: ['admin', 'doctor', 'nurse','receptionist']
1616
},
1717

1818

src/routes/patient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ router.post('/patient', protect, authorize('admin', 'doctor'), patientController
1111

1212
router.put('/:id', protect, authorize('admin', 'doctor'), patientController.updatePatient);
1313

14-
//router.delete('/:id', protect, authorize('admin'), patientController.deletePatient);
14+
router.delete('/:id', protect, authorize('admin'), patientController.deletePatient);
1515

1616
export default router;

src/routes/reception.routes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import express from 'express';
2+
import { protect, authorize } from '../middleware/authmiddleware.js';
3+
import * as receptionController from '../controllers/reception.controller.js';
4+
5+
const router = express.Router();
6+
7+
router.post('/', protect, authorize('admin'), receptionController.creatReceptionist);
8+
9+
router.get('/', protect, authorize('admin', 'receptionist'), receptionController.getReceptionist);
10+
11+
router.put('/change-password', protect, authorize('receptionist'), receptionController.changePassword);
12+
13+
router.put('/:id', protect, authorize('admin'), receptionController.updateReceptionist);
14+
15+
router.delete('/:id', protect, authorize('admin'), receptionController.deleteReceptionist);
16+
17+
export default router;

0 commit comments

Comments
 (0)