Skip to content

Commit 86f0415

Browse files
committed
feat: Nurse APIs; fix app.js/server.js conflict, routing priorities
1 parent 5d5772d commit 86f0415

6 files changed

Lines changed: 65 additions & 37 deletions

File tree

server.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
import 'dotenv/config';
22
import app from './src/app.js';
3-
//import bodyParser from 'body-parser';
43
import connectDB from './src/config/db.js';
54

65
connectDB();
76

8-
// app.use(cors());
9-
// app.use(bodyParser.json());
10-
11-
import patientRoutes from './src/routes/patient.js';
12-
import deptRoutes from './src/routes/dept.js';
13-
import doctorRoutes from './src/routes/doctor.routes.js';
14-
import appointmentRoutes from './src/routes/appointment.js';
15-
import authRoutes from './src/routes/auth.routes.js';
16-
import signupRoutes from './src/routes/signup.js';
17-
import adminRoutes from './src/routes/adminroutes.js';
18-
19-
app.use('/api/patients', patientRoutes);
20-
app.use('/api/departments', deptRoutes);
21-
app.use('/api/users/doctors', doctorRoutes);
22-
app.use('/api/appointments', appointmentRoutes);
23-
app.use('/api/signup', signupRoutes);
24-
app.use('/api/auth', authRoutes);
25-
app.use('/api/admin', adminRoutes);
26-
277
const PORT = process.env.PORT || 5000;
28-
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
8+
9+
app.listen(PORT, () => {
10+
console.log(`Server running on port ${PORT}`);
11+
});

src/app.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,43 @@ import cors from 'cors';
33
import morgan from 'morgan';
44

55
import authRoutes from './routes/auth.routes.js';
6+
import signupRoutes from './routes/signup.js';
7+
import adminRoutes from './routes/adminroutes.js';
8+
69
import doctorRoutes from './routes/doctor.routes.js';
7-
10+
import nurseRoutes from './routes/nurse.routes.js';
11+
12+
import patientRoutes from './routes/patient.js';
13+
import deptRoutes from './routes/dept.js';
14+
import appointmentRoutes from './routes/appointment.js';
815

916
const app = express();
1017

18+
// Global middleware
1119
app.use(cors());
1220
app.use(express.json());
1321
app.use(morgan('dev'));
1422

15-
app.get('/', (req, res) => res.json({ ok: true }));
23+
app.get('/', (req, res) => {
24+
res.json({ ok: true });
25+
});
1626

1727
app.use('/api/auth', authRoutes);
18-
app.use('/api', doctorRoutes);
28+
app.use('/api/signup', signupRoutes);
29+
app.use('/api/admin', adminRoutes);
30+
31+
app.use('/api/users/doctors', doctorRoutes);
32+
app.use('/api/users/nurses', nurseRoutes);
33+
34+
app.use('/api/patients', patientRoutes);
35+
app.use('/api/departments', deptRoutes);
36+
app.use('/api/appointments', appointmentRoutes);
1937

2038
app.use((err, req, res, next) => {
2139
console.error(err);
22-
res.status(err.status || 500).json({ error: err.message || 'Server error' });
40+
res.status(err.status || 500).json({
41+
error: err.message || 'Server error'
42+
});
2343
});
2444

25-
export default app;
45+
export default app;

src/controllers/doctor.controller.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const updateDoctor = async (req, res) => {
7070
}
7171
};
7272

73-
/*const deleteDoctor = async (req, res) => {
73+
const deleteDoctor = async (req, res) => {
7474
try {
7575
const { id } = req.params;
7676

@@ -84,11 +84,12 @@ const updateDoctor = async (req, res) => {
8484
} catch (err) {
8585
res.status(500).json({ message: err.message });
8686
}
87-
};*/
87+
};
8888

8989
export {
9090
createDoctor,
91+
changePassword,
9192
getDoctors,
9293
updateDoctor,
93-
//deleteDoctor
94+
deleteDoctor
9495
};

src/controllers/nurse.controller.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import User from "../models/User";
1+
import User from "../models/User.js";
22
import bcrypt from "bcrypt";
33

44
const createNurse= async (req, res) => {
@@ -35,6 +35,11 @@ const changePassword = async (req, res) => {
3535
const getNurses = async (req, res) => {
3636
try {
3737
const nurses=await User.find({ role: 'nurse' });
38+
39+
if(!nurses){
40+
res.status(404).json({ message: 'Nurse not found' });
41+
}
42+
3843
res.json(nurses);
3944
} catch (err) {
4045
res.status(500).json({ message: err.message });
@@ -63,11 +68,28 @@ const updateNurse = async (req,res) =>{
6368
} catch(error) {
6469
res.status(400).json({ message: err.message });
6570
}
66-
};
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+
};
6788

6889
export {
6990
createNurse,
7091
changePassword,
7192
getNurses,
72-
updateNurse
93+
updateNurse,
94+
deleteNurse
7395
};

src/routes/doctor.routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ router.post('/', protect, authorize('admin'), doctorController.createDoctor);
88

99
router.get('/', protect, authorize('admin', 'doctor'), doctorController.getDoctors);
1010

11-
router.put('/:id', protect, authorize('admin'), doctorController.updateDoctor);
12-
1311
router.put('/change-password', protect, authorize('doctor'), doctorController.changePassword);
1412

13+
router.put('/:id', protect, authorize('admin'), doctorController.updateDoctor);
14+
1515
router.delete('/:id', protect, authorize('admin'), doctorController.deleteDoctor);
1616

1717
export default router;

src/routes/nurse.routes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import * as nurseController from '../controllers/nurse.controller.js';
44

55
const router = express.Router();
66

7-
router.get('/', protect, authorize('admin'), nurseController.createNurse);
7+
router.post('/', protect, authorize('admin'), nurseController.createNurse);
88

99
router.get('/', protect, authorize('admin', 'nurse'), nurseController.getNurses);
1010

11+
router.put('/change-password', protect, authorize('nurse'), nurseController.changePassword);
12+
1113
router.put('/:id', protect, authorize('admin'), nurseController.updateNurse);
1214

13-
router.put('/change-password', protect, authorize('nurse'), nurseController.changePassword);
15+
router.delete('/:id', protect, authorize('admin'), nurseController.deleteNurse);
1416

15-
router.delete('/:id', protect, authorize('nurse'), nurseController.delete)
17+
export default router;

0 commit comments

Comments
 (0)