Skip to content

Commit f0825d7

Browse files
committed
refactor: extract reception logic into service layer and standardize create method responses
1 parent 253dfbb commit f0825d7

7 files changed

Lines changed: 104 additions & 64 deletions

File tree

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,31 @@
1-
import User from '../models/User.js';
2-
import bcrypt from 'bcrypt';
1+
import receptionService from "../services/reception.service.js";
32

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();
3+
const creatReceptionist = async (req, res) => {
4+
try {
5+
const receptionist = await receptionService.creatReceptionist(req.body);
166
res.status(201).json(receptionist);
17-
} catch(err) {
7+
} catch (err) {
188
res.status(400).json({ message: err.message });
199
}
20-
}
10+
};
2111

2212
const changePassword = async (req, res) => {
2313
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" });
14+
const { oldPassword, newPassword } = req.body;
15+
const result = await receptionService.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 getReceptionist = async (req, res) => {
26+
const getReceptionists = async (req, res) => {
4027
try {
41-
const receptionist = await User.find({ role: 'receptionist'});
28+
const receptionist = await receptionService.getReceptionists();
4229
res.json(receptionist);
4330
} catch (err) {
4431
res.status(500).json({ message: err.message });
@@ -48,47 +35,28 @@ const getReceptionist = async (req, res) => {
4835
const updateReceptionist = async (req, res) => {
4936
try {
5037
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-
38+
const receptionist = await receptionService.updateReceptionist(id, req.body);
6639
res.json(receptionist);
6740
} catch (err) {
6841
res.status(400).json({ message: err.message });
6942
}
7043
};
7144

7245
const deleteReceptionist = async (req, res) => {
73-
try{
46+
try {
7447
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" });
48+
const result = await receptionService.deleteReceptionist(id);
49+
res.json(result);
8350
} catch (err) {
8451
res.status(500).json({ message: err.message });
8552
}
8653
};
8754

55+
8856
export {
8957
creatReceptionist,
9058
changePassword,
91-
getReceptionist,
59+
getReceptionists,
9260
updateReceptionist,
9361
deleteReceptionist
9462
};

src/routes/reception.routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const router = express.Router();
66

77
router.post('/', protect, authorize('admin'), receptionController.creatReceptionist);
88

9-
router.get('/', protect, authorize('admin', 'receptionist'), receptionController.getReceptionist);
9+
router.get('/', protect, authorize('admin', 'receptionist'), receptionController.getReceptionists);
1010

1111
router.put('/change-password', protect, authorize('receptionist'), receptionController.changePassword);
1212

src/services/doctor.service.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import bcrypt from 'bcrypt';
44
const createDoctor = async (data) => {
55
const password = data.password || 'doctor@123';
66
const hashedPassword = await bcrypt.hash(password, 10);
7-
7+
88
const doctor = new User({
99
...data,
1010
password: hashedPassword,
1111
role: 'doctor',
1212
});
1313

14-
return await doctor.save();
14+
await doctor.save();
15+
16+
return {
17+
success: true,
18+
message: 'Doctor created successfully',
19+
};
1520
};
1621

22+
1723
const changePassword = async (userId, oldPassword, newPassword) => {
1824
const doctor = await User.findById(userId);
1925
if(!doctor) throw new Error('Doctor not found');

src/services/lab.service.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import User from '../models/User.js';
22
import bcrypt from 'bcrypt';
3-
import doctorService from './doctor.service.js';
43

54
const createLabtechnician = async (data) => {
65
const password = data.password || 'labtech@123';
@@ -12,7 +11,12 @@ const createLabtechnician = async (data) => {
1211
role: 'lab-technician'
1312
});
1413

15-
return await technician.save();
14+
await technician.save();
15+
16+
return {
17+
success: true,
18+
message: 'Lab technician created successfully'
19+
};
1620
};
1721

1822
const changePassword = async (userId,oldPassword, newPassword) => {

src/services/nurse.service.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const createNurse = async (data) => {
1111
role: 'nurse',
1212
});
1313

14-
return await nurse.save();
14+
await nurse.save();
15+
16+
return {
17+
success: true,
18+
message: 'Nurse created successfully'
19+
};
1520
};
1621

1722
const changePassword = async (userId, oldPassword, newPassword) => {

src/services/pharmacy.service.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const createPharmacist = async (data) => {
1111
role: 'pharmacist',
1212
});
1313

14-
return await pharmacist.save();
14+
await pharmacist.save();
15+
16+
return {
17+
success: true,
18+
message: 'Pharmacist created successfully'
19+
};
1520
};
1621

1722
const changePassword = async (userId, oldPassword, newPassword) => {

src/services/reception.service.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,61 @@ const creatReceptionist = async (data) => {
1111
role: 'receptionist'
1212
});
1313

14-
return await receptionist.save();
14+
await receptionist.save();
15+
16+
return {
17+
success: true,
18+
message: 'Receptionist created successfully'
19+
};
1520
};
1621

1722
const changePassword = async (userId, oldPassword, newPassword) => {
18-
const receptionist = await User.findById(userId)
19-
}
23+
const receptionist = await User.findById(userId);
24+
if(!receptionist) throw new Error('Receptionist not found');
25+
26+
const isMatch = await bcrypt.compare(oldPassword, receptionist.password);
27+
if(!isMatch) throw new Error('Old password is incorrect');
28+
29+
receptionist.password = await bcrypt.hash(newPassword, 10);
30+
await receptionist.save();
31+
32+
return { message: 'Password changes successfully' };
33+
};
34+
35+
const getReceptionists = async () => {
36+
return await User.find({ role: 'receptionist' });
37+
};
38+
39+
const updateReceptionist = async (id, data) => {
40+
if(data.password) {
41+
data.password = await bcrypt.hash(data.password, 10);
42+
}
43+
44+
const receptionist = await User.findOneAndUpdate(
45+
{ _id: id, role: 'receptionist' },
46+
data,
47+
{ new: true, runValidators:true }
48+
);
49+
50+
if(!receptionist) throw new Error('Receptionist not found');
51+
52+
return receptionist;
53+
};
54+
55+
const deleteReceptionist = async (id) => {
56+
const receptionist = await User.findOneAndDelete(
57+
{ _id: id, role: 'receptionist' }
58+
);
59+
60+
if(!receptionist) throw new Error('Receptionist not found');
61+
62+
return { message: 'Receptionist deleted successfully' };
63+
};
64+
65+
export default {
66+
creatReceptionist,
67+
changePassword,
68+
getReceptionists,
69+
updateReceptionist,
70+
deleteReceptionist
71+
};

0 commit comments

Comments
 (0)