Skip to content

Commit eb1b177

Browse files
committed
feat: add Lab Technician CRUD (API returning 404, pending fix)
1 parent 45cf02f commit eb1b177

4 files changed

Lines changed: 114 additions & 1 deletion

File tree

src/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import adminRoutes from './routes/adminroutes.js';
99
import doctorRoutes from './routes/doctor.routes.js';
1010
import nurseRoutes from './routes/nurse.routes.js';
1111
import receptionRoutes from './routes/reception.routes.js';
12+
import labTechnicianRoutes from './routes/lab.routes.js';
1213

1314
import patientRoutes from './routes/patient.js';
1415
import deptRoutes from './routes/dept.js';
@@ -32,6 +33,7 @@ app.use('/api/admin', adminRoutes);
3233
app.use('/api/users/doctors', doctorRoutes);
3334
app.use('/api/users/nurses', nurseRoutes);
3435
app.use('/api/users/receptionist', receptionRoutes);
36+
app.use('/api/users/lab-technician', labTechnicianRoutes);
3537

3638
app.use('/api/patients', patientRoutes);
3739
app.use('/api/departments', deptRoutes);

src/controllers/lab.controller.js

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 createLabtechnician = async (req,res) => {
5+
try {
6+
const password = req.body.password || 'labtech@123';
7+
const hashedPassword = await bcrypt.hash(password, 10);
8+
9+
const technician = new User({
10+
...req.body,
11+
password: hashedPassword,
12+
role: 'lab-technician'
13+
});
14+
15+
await technician.save();
16+
res.status(201).json(technician);
17+
} catch (err) {
18+
res.status(400).json({ message: err.message});
19+
}
20+
};
21+
22+
const changePassword = async (req,res) => {
23+
try {
24+
const technician = await User.findById(req.user.id);
25+
const { oldPassword, newPassword } = req.body;
26+
27+
const isMatch = await bcrypt.compare(oldPassword, technician.password);
28+
if (!isMatch) return res.status(400).json({ message: 'Old password is incorrect' });
29+
30+
technician.password = await bcrypt.hash(newPassword, 10);
31+
await technician.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 getLabtechnicians = async (req,res) => {
40+
try {
41+
const technician = await User.find({ role: 'lab-technician' });
42+
res.json(technician);
43+
} catch (err) {
44+
res.status(500).json({ message: err.message });
45+
}
46+
};
47+
48+
const updateLabtechnician = async (req,res) => {
49+
try {
50+
const { id } = req.params;
51+
52+
if (req.body.password) {
53+
req.body.password = await bcrypt.hash(req.body.password, 10);
54+
}
55+
56+
const technician = await User.findOneAndUpdate(
57+
{ _id: id, role: 'lab-technician' },
58+
req.body,
59+
{ new: true, runValidators: true }
60+
);
61+
62+
if (!technician) {
63+
return res.status(404).json({ message: 'Lab technician not found'});
64+
}
65+
66+
res.json(technician);
67+
} catch (err) {
68+
res.status(400).json({ message: err.message });
69+
}
70+
};
71+
72+
const deleteLabtechnician = async (req,res) => {
73+
try {
74+
const { id } = req.params;
75+
76+
const technician = await User.findOneAndDelete({ _id: id, role: 'lab-technician' });
77+
78+
if(!technician) {
79+
return res.status(404).json({ message: 'Lab technician not found' });
80+
}
81+
82+
res.json({ message: 'Lab technician deleted successfully' });
83+
} catch (err) {
84+
res.status(500).json({ message: err.message });
85+
}
86+
};
87+
88+
export {
89+
createLabtechnician,
90+
changePassword,
91+
getLabtechnicians,
92+
updateLabtechnician,
93+
deleteLabtechnician
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','receptionist']
15+
enum: ['admin', 'doctor', 'nurse','receptionist', 'lab-technician']
1616
},
1717

1818

src/routes/lab.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 labTechnicianController from '../controllers/lab.controller.js';
4+
5+
const router = express.Router();
6+
7+
router.post('/', protect, authorize('admin'), labTechnicianController.createLabtechnician);
8+
9+
router.get('/', protect, authorize('admin', 'lab-technician'), labTechnicianController.getLabtechnicians);
10+
11+
router.put('/change-password', protect, authorize('lab-technician'), labTechnicianController.changePassword);
12+
13+
router.put('/:id', protect, authorize('admin'), labTechnicianController.updateLabtechnician);
14+
15+
router.delete('/:id', protect, authorize('admin'), labTechnicianController.deleteLabtechnician);
16+
17+
export default router;

0 commit comments

Comments
 (0)