Skip to content

Commit fa36cfd

Browse files
committed
refactor: nurse service add
1 parent e2c135a commit fa36cfd

4 files changed

Lines changed: 69 additions & 92 deletions

File tree

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,9 @@
11
import User from "../models/User.js";
22
import bcrypt from "bcrypt";
3+
import nurseService from "../services/nurse.service.js";
34

4-
const createNurse= async (req, res) => {
5+
const createNurse = async => {
56
try {
6-
const password= req.body.password || 'nurse@123';
7-
const hashedPassword= await bcrypt.hash(password, 10);
8-
9-
const nurse= new User({
10-
...req.body,
11-
password: hashedPassword,
12-
role: 'nurse'
13-
});
14-
await nurse.save();
15-
res.status(201).json(nurse);
16-
} catch (err) {
17-
res.status(400).json({ message: err.message });
18-
}
19-
};
20-
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-
39-
if(!nurses){
40-
res.status(404).json({ message: 'Nurse not found' });
41-
}
42-
43-
res.json(nurses);
44-
} catch (err) {
45-
res.status(500).json({ message: err.message });
46-
}
47-
};
48-
49-
const updateNurse = async (req,res) =>{
50-
try{
51-
const { id }= req.params;
52-
53-
if (req.body.password){
54-
req.body.password=await bcrypt.hash(req.body.password, 10);
55-
}
56-
57-
const nurse = await User.findOneAndUpdate(
58-
{ _id: id, role: 'nurse'},
59-
req.body,
60-
{ new: true, runValidators: true}
61-
);
62-
63-
if(!nurse){
64-
return res.status(404).json({ message: 'Nurse not found' });
65-
}
66-
67-
res.json(nurse);
68-
} catch(error) {
69-
res.status(400).json({ message: err.message });
7+
const nurse = await nurseService.createNurse(req.body);
708
}
71-
};
72-
73-
const deleteNurse = async (req,res)=> {
74-
try{
75-
const { id }= req.params;
76-
77-
const nurse= await User.findOneAndDelete({ _id: id, role: 'nurse' });
78-
79-
if(!nurse){
80-
return res.status(404).json({ message: 'Nurse not found'});
81-
}
82-
83-
res.json({ message: 'Nurse deleted successfully'});
84-
} catch(error){
85-
res.status(500).json({ message: err.message});
86-
}
87-
};
88-
89-
export {
90-
createNurse,
91-
changePassword,
92-
getNurses,
93-
updateNurse,
94-
deleteNurse
95-
};
9+
}

src/services/doctor.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const createDoctor = async (data) => {
1414
return await doctor.save();
1515
};
1616

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

src/services/lab.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const createLabtechnician = async (data) => {
1515
return await technician.save();
1616
};
1717

18-
const changePassword = async (oldPassword, newPassword) => {
18+
const changePassword = async (userId,oldPassword, newPassword) => {
1919
const technician = await User.findById(userId);
2020
if(!technician) throw new Error('Lab technician not found');
2121

src/services/nurse.service.js

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

0 commit comments

Comments
 (0)