Skip to content

Commit e2c135a

Browse files
committed
refactor: migrate lab technician logic to service layer
1 parent 3664b33 commit e2c135a

3 files changed

Lines changed: 88 additions & 55 deletions

File tree

src/controllers/lab.controller.js

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,52 @@
1-
import User from '../models/User.js';
2-
import bcrypt from 'bcrypt';
1+
import labService from "../services/lab.service.js";
32

4-
const createLabtechnician = async (req,res) => {
3+
const createLabtechnician = async (req, res) => {
54
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();
5+
const technician = await labService.createLabtechnician(req.body);
166
res.status(201).json(technician);
177
} catch (err) {
18-
res.status(400).json({ message: err.message});
8+
res.status(400).json({ message: err.message });
199
}
2010
};
2111

22-
const changePassword = async (req,res) => {
12+
const changePassword = async (req, res) => {
2313
try {
24-
const technician = await User.findById(req.user.id);
2514
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'});
15+
const result = await labService.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 getLabtechnicians = async (req,res) => {
26+
const getLabtechnicians = async (req, res) => {
4027
try {
41-
const technician = await User.find({ role: 'lab-technician' });
28+
const technician = await labService.getLabtechnicians();
4229
res.json(technician);
4330
} catch (err) {
4431
res.status(500).json({ message: err.message });
4532
}
4633
};
4734

48-
const updateLabtechnician = async (req,res) => {
35+
const updateLabtechnician = async (req, res) => {
4936
try {
5037
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-
38+
const result = await labService.updateLabtechnician(id, req.body);
6639
res.json(technician);
6740
} catch (err) {
6841
res.status(400).json({ message: err.message });
6942
}
7043
};
7144

72-
const deleteLabtechnician = async (req,res) => {
45+
const deleteLabtechnician = async (req, res) => {
7346
try {
7447
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' });
48+
const result = await labService.deleteLabtechnician(id);
49+
res.json(result);
8350
} catch (err) {
8451
res.status(500).json({ message: err.message });
8552
}

src/services/doctor.service.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const createDoctor = async (data) => {
1212
});
1313

1414
return await doctor.save();
15-
}
15+
};
1616

17-
const changePassword = async (useImperativeHandle, oldPassword, newPassword) => {
17+
const changePassword = async (oldPassword, newPassword) => {
1818
const doctor = await User.findById(userId);
1919
if(!doctor) throw new Error('Doctor not found');
2020

@@ -54,7 +54,7 @@ const deleteDoctor = async (id) => {
5454
return { message: 'Doctor deleted successfully' };
5555
};
5656

57-
export {
57+
export default{
5858
createDoctor,
5959
changePassword,
6060
getDoctors,

src/services/lab.service.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import User from '../models/User.js';
2+
import bcrypt from 'bcrypt';
3+
import doctorService from './doctor.service.js';
4+
5+
const createLabtechnician = async (data) => {
6+
const password = data.password || 'labtech@123';
7+
const hashedPassword = await bcrypt.hash(password, 10);
8+
9+
const technician = new User({
10+
...data,
11+
password: hashedPassword,
12+
role: 'lab-technician'
13+
});
14+
15+
return await technician.save();
16+
};
17+
18+
const changePassword = async (oldPassword, newPassword) => {
19+
const technician = await User.findById(userId);
20+
if(!technician) throw new Error('Lab technician not found');
21+
22+
const isMatch = await bcrypt.compare(oldPassword, technician.password);
23+
if(!isMatch) throw new Error('Old password is incorrect');
24+
25+
technician.password = await bcrypt.hash(newPassword, 10);
26+
await technician.save();
27+
28+
return { message: 'Password changes successfully' };
29+
};
30+
31+
const getLabtechnicians = async () => {
32+
return await User.find({ role: 'lab-technician' });
33+
};
34+
35+
const updateLabtechnician = async(id, data) => {
36+
if(data.password) {
37+
data.password = await bcrypt.hash(data.password, 10);
38+
}
39+
40+
const technician = await User.findOneAndUpdate(
41+
{ _id: id, role: 'lab-technician' },
42+
data,
43+
{ new: true, runValidators: true }
44+
);
45+
46+
if(!technician) throw new Error('Lab technician not found');
47+
48+
return technician;
49+
};
50+
51+
const deleteLabtechnician = async (id) => {
52+
const technician = await User.findOneAndDelete(
53+
{ _id: id, role: 'lab-technician' }
54+
);
55+
if(!technician) throw new Error('Lab technician not found');
56+
57+
return { message: 'Lab technician deleted successfully' };
58+
}
59+
60+
export default{
61+
createLabtechnician,
62+
changePassword,
63+
getLabtechnicians,
64+
updateLabtechnician,
65+
deleteLabtechnician
66+
};

0 commit comments

Comments
 (0)