Skip to content

Commit 5d5772d

Browse files
committed
feat: add nurse feature (some APIs pending)
1 parent 3e21793 commit 5d5772d

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

src/controllers/nurse.controller.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,56 @@ const createNurse= async (req, res) => {
1818
}
1919
};
2020

21-
export { createNurse };
21+
const changePassword = async (req, res) => {
22+
try {
23+
const nurse= await User.findById(req.user.id);
24+
const { oldPassword, newPassword }= req.body;
25+
26+
const isMatch= await bcrypt.compare(oldPassword, nurse.password);
27+
if (!isMatch) return res.status(400).json({ message: 'Old password is incorrect' });
28+
29+
res.status(200).json({ message: 'Password updated successfully' });
30+
} catch (err) {
31+
res.status(500).json({ message: err.message });
32+
}
33+
}
34+
35+
const getNurses = async (req, res) => {
36+
try {
37+
const nurses=await User.find({ role: 'nurse' });
38+
res.json(nurses);
39+
} catch (err) {
40+
res.status(500).json({ message: err.message });
41+
}
42+
};
43+
44+
const updateNurse = async (req,res) =>{
45+
try{
46+
const { id }= req.params;
47+
48+
if (req.body.password){
49+
req.body.password=await bcrypt.hash(req.body.password, 10);
50+
}
51+
52+
const nurse = await User.findOneAndUpdate(
53+
{ _id: id, role: 'nurse'},
54+
req.body,
55+
{ new: true, runValidators: true}
56+
);
57+
58+
if(!nurse){
59+
return res.status(404).json({ message: 'Nurse not found' });
60+
}
61+
62+
res.json(nurse);
63+
} catch(error) {
64+
res.status(400).json({ message: err.message });
65+
}
66+
};
67+
68+
export {
69+
createNurse,
70+
changePassword,
71+
getNurses,
72+
updateNurse
73+
};

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']
15+
enum: ['admin', 'doctor', 'nurse']
1616
},
1717

1818

src/routes/doctor.routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ router.get('/', protect, authorize('admin', 'doctor'), doctorController.getDocto
1010

1111
router.put('/:id', protect, authorize('admin'), doctorController.updateDoctor);
1212

13-
//router.put('/change-password', protect, authorize('doctor'), doctorController.changePassword);
13+
router.put('/change-password', protect, authorize('doctor'), doctorController.changePassword);
1414

15-
//router.delete('/:id', protect, authorize('admin'), doctorController.deleteDoctor);
15+
router.delete('/:id', protect, authorize('admin'), doctorController.deleteDoctor);
1616

1717
export default router;

src/routes/nurse.routes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
import { protect, authorize } from '../middleware/authmiddleware.js';
3+
import * as nurseController from '../controllers/nurse.controller.js';
4+
5+
const router = express.Router();
6+
7+
router.get('/', protect, authorize('admin'), nurseController.createNurse);
8+
9+
router.get('/', protect, authorize('admin', 'nurse'), nurseController.getNurses);
10+
11+
router.put('/:id', protect, authorize('admin'), nurseController.updateNurse);
12+
13+
router.put('/change-password', protect, authorize('nurse'), nurseController.changePassword);
14+
15+
router.delete('/:id', protect, authorize('nurse'), nurseController.delete)

0 commit comments

Comments
 (0)