Skip to content

Commit 23f4964

Browse files
committed
refactor: pharmacy controller migrate to service layer
1 parent c64382e commit 23f4964

2 files changed

Lines changed: 22 additions & 57 deletions

File tree

src/controllers/nurse.controller.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import User from "../models/User.js";
2-
import bcrypt from "bcrypt";
31
import nurseService from "../services/nurse.service.js";
42

53
const createNurse = async (req, res) => {
Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,52 @@
1-
import User from '../models/User.js';
2-
import bcrypt from 'bcrypt';
1+
import pharmacyService from "../services/pharmacy.service.js";
32

4-
const createPharmacist = async (req,res) => {
3+
const createPharmacist = async (req, res) => {
54
try {
6-
const password = req.body.password || 'pharmacist@123';
7-
const hashedPassword = await bcrypt.hash(password, 10);
8-
9-
const pharmacist = new User({
10-
...req.body,
11-
password: hashedPassword,
12-
role: 'pharmacist'
13-
});
14-
15-
await pharmacist.save();
5+
const pharmacist = await pharmacyService.createPharmacist(req.body);
166
res.status(201).json(pharmacist);
177
} catch (err) {
188
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 pharmacist = await User.findById(req.user.id);
25-
const { oldPassword, newPassword } = req.body;
26-
27-
const isMatch = await bcrypt.compare(oldPassword, pharmacist.password);
28-
if (!isMatch) return res.status(400).json({ message: 'Old password is incorrect' });
29-
30-
pharmacist.password = await bcrypt.hash(newPassword, 10);
31-
await pharmacist.save();
32-
33-
res.status(200).json({ message: 'Password updated successfully' });
14+
const { oldPassword, newPassword }= req.body;
15+
const result = await pharmacyService.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 getPharmacists = async (req,res) => {
26+
const getPharmacists = async (req, res) => {
4027
try {
41-
const pharmacist = await User.find({ role: 'pharmacist' });
42-
res.json(pharmacist);
28+
const pharmacists = await pharmacyService.getPharmacists();
29+
res.json(pharmacists);
4330
} catch (err) {
4431
res.status(500).json({ message: err.message });
4532
}
4633
};
4734

48-
const updatePharmacist = async (req,res) => {
35+
const updatePharmacist = 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-
57-
const pharmacist = await User.findOneAndUpdate(
58-
{ _id: id, role: 'pharmacist' },
59-
req.body,
60-
{ new: true, runValidators: true }
61-
);
62-
63-
if (!pharmacist) {
64-
return res.status(404).json({ message: 'Pharmacist not found' });
65-
}
66-
38+
const pharmacist = await pharmacyService.updatePharmacist(id, req.body);
6739
res.json(pharmacist);
6840
} catch (err) {
6941
res.status(400).json({ message: err.message });
7042
}
7143
};
7244

73-
const deletePharmacist = async (req,res) => {
45+
const deletePharmacist = async (req, res) => {
7446
try {
7547
const { id } = req.params;
76-
77-
const pharmacist = await User.findOneAndDelete({ _id: id, role: 'pharmacist' });
78-
79-
if (!pharmacist) {
80-
return res.status(400).json({ message: 'Pharmacist not found' });
81-
}
82-
83-
res.json({ message: 'Pharmacist deleted successfully' });
48+
const result = await pharmacyService.deletePharmacist(id);
49+
res.json(result);
8450
} catch (err) {
8551
res.status(500).json({ message: err.message });
8652
}
@@ -92,4 +58,5 @@ export {
9258
getPharmacists,
9359
updatePharmacist,
9460
deletePharmacist
95-
};
61+
}
62+

0 commit comments

Comments
 (0)