|
1 | | -import User from '../models/User.js'; |
2 | | -import bcrypt from 'bcrypt'; |
| 1 | +import labService from "../services/lab.service.js"; |
3 | 2 |
|
4 | | -const createLabtechnician = async (req,res) => { |
| 3 | +const createLabtechnician = async (req, res) => { |
5 | 4 | 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); |
16 | 6 | res.status(201).json(technician); |
17 | 7 | } catch (err) { |
18 | | - res.status(400).json({ message: err.message}); |
| 8 | + res.status(400).json({ message: err.message }); |
19 | 9 | } |
20 | 10 | }; |
21 | 11 |
|
22 | | -const changePassword = async (req,res) => { |
| 12 | +const changePassword = async (req, res) => { |
23 | 13 | try { |
24 | | - const technician = await User.findById(req.user.id); |
25 | 14 | 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); |
34 | 21 | } catch (err) { |
35 | | - res.status(500).json({ message: err.message }); |
| 22 | + res.status(400).json({ message: err.message }); |
36 | 23 | } |
37 | 24 | }; |
38 | 25 |
|
39 | | -const getLabtechnicians = async (req,res) => { |
| 26 | +const getLabtechnicians = async (req, res) => { |
40 | 27 | try { |
41 | | - const technician = await User.find({ role: 'lab-technician' }); |
| 28 | + const technician = await labService.getLabtechnicians(); |
42 | 29 | res.json(technician); |
43 | 30 | } catch (err) { |
44 | 31 | res.status(500).json({ message: err.message }); |
45 | 32 | } |
46 | 33 | }; |
47 | 34 |
|
48 | | -const updateLabtechnician = async (req,res) => { |
| 35 | +const updateLabtechnician = async (req, res) => { |
49 | 36 | try { |
50 | 37 | 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); |
66 | 39 | res.json(technician); |
67 | 40 | } catch (err) { |
68 | 41 | res.status(400).json({ message: err.message }); |
69 | 42 | } |
70 | 43 | }; |
71 | 44 |
|
72 | | -const deleteLabtechnician = async (req,res) => { |
| 45 | +const deleteLabtechnician = async (req, res) => { |
73 | 46 | try { |
74 | 47 | 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); |
83 | 50 | } catch (err) { |
84 | 51 | res.status(500).json({ message: err.message }); |
85 | 52 | } |
|
0 commit comments