Skip to content

Commit 01a0d7c

Browse files
committed
appointment_backned
1 parent 5e9dadb commit 01a0d7c

File tree

9 files changed

+633
-158
lines changed

9 files changed

+633
-158
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
const sql = require('mssql');
2+
3+
// Create new appointment
4+
const createAppointment = async (req, res) => {
5+
const {
6+
patient_id,
7+
doctor_id,
8+
appointment_date,
9+
reason
10+
} = req.body;
11+
12+
try {
13+
const request = new sql.Request();
14+
request.input('patient_id', sql.Int, patient_id);
15+
request.input('doctor_id', sql.Int, doctor_id);
16+
request.input('appointment_date', sql.DateTime, appointment_date);
17+
request.input('status', sql.VarChar(20), 'Scheduled');
18+
19+
const result = await request.query(`
20+
INSERT INTO Appointments (
21+
patient_id, doctor_id, appointment_date, status
22+
)
23+
OUTPUT INSERTED.appointment_id
24+
VALUES (
25+
@patient_id, @doctor_id, @appointment_date, @status
26+
)
27+
`);
28+
29+
const appointmentId = result.recordset[0].appointment_id;
30+
31+
// Create feedback placeholder
32+
await request.query(`
33+
INSERT INTO Feedback (appointment_id, patient_id)
34+
VALUES (@appointmentId, @patient_id)
35+
`, { appointmentId });
36+
37+
res.status(201).json({
38+
message: 'Appointment created successfully',
39+
appointmentId: appointmentId
40+
});
41+
} catch (err) {
42+
console.error('Database error:', err);
43+
res.status(500).json({ message: 'Error creating appointment', error: err.message });
44+
}
45+
};
46+
47+
// Get appointments by doctor ID
48+
const getAppointmentsByDoctor = async (req, res) => {
49+
const { doctorId } = req.params;
50+
51+
try {
52+
const request = new sql.Request();
53+
request.input('doctorId', sql.Int, doctorId);
54+
55+
const result = await request.query(`
56+
SELECT
57+
a.appointment_id,
58+
p.patient_id,
59+
p.full_name as patient_name,
60+
a.appointment_date,
61+
a.status,
62+
f.feedback_id,
63+
f.comments,
64+
f.rating
65+
FROM Appointments a
66+
JOIN Patients p ON a.patient_id = p.patient_id
67+
LEFT JOIN Feedback f ON a.appointment_id = f.appointment_id
68+
WHERE a.doctor_id = @doctorId
69+
ORDER BY a.appointment_date DESC
70+
`);
71+
72+
res.json(result.recordset);
73+
} catch (err) {
74+
console.error('Database error:', err);
75+
res.status(500).json({ message: 'Error fetching appointments', error: err.message });
76+
}
77+
};
78+
79+
// Get appointments by patient ID
80+
const getAppointmentsByPatient = async (req, res) => {
81+
const { patientId } = req.params;
82+
83+
try {
84+
const request = new sql.Request();
85+
request.input('patientId', sql.Int, patientId);
86+
87+
const result = await request.query(`
88+
SELECT
89+
a.appointment_id,
90+
d.doctor_id,
91+
u.full_name as doctor_name,
92+
d.specialization,
93+
a.appointment_date,
94+
a.status,
95+
f.feedback_id,
96+
f.comments,
97+
f.rating
98+
FROM Appointments a
99+
JOIN Doctors d ON a.doctor_id = d.doctor_id
100+
JOIN Users u ON d.user_id = u.user_id
101+
LEFT JOIN Feedback f ON a.appointment_id = f.appointment_id
102+
WHERE a.patient_id = @patientId
103+
ORDER BY a.appointment_date DESC
104+
`);
105+
106+
res.json(result.recordset);
107+
} catch (err) {
108+
console.error('Database error:', err);
109+
res.status(500).json({ message: 'Error fetching appointments', error: err.message });
110+
}
111+
};
112+
113+
// Update appointment status
114+
const updateAppointmentStatus = async (req, res) => {
115+
const { id } = req.params;
116+
const { status } = req.body;
117+
118+
try {
119+
const request = new sql.Request();
120+
request.input('id', sql.Int, id);
121+
request.input('status', sql.VarChar(20), status);
122+
123+
await request.query(`
124+
UPDATE Appointments
125+
SET status = @status
126+
WHERE appointment_id = @id
127+
`);
128+
129+
res.json({ message: 'Appointment status updated successfully' });
130+
} catch (err) {
131+
console.error('Database error:', err);
132+
res.status(500).json({ message: 'Error updating appointment', error: err.message });
133+
}
134+
};
135+
136+
// Submit feedback for appointment
137+
const submitFeedback = async (req, res) => {
138+
const { id } = req.params;
139+
const { comments, rating } = req.body;
140+
141+
try {
142+
const request = new sql.Request();
143+
request.input('id', sql.Int, id);
144+
request.input('comments', sql.Text, comments);
145+
request.input('rating', sql.Float, rating);
146+
147+
await request.query(`
148+
UPDATE Feedback
149+
SET comments = @comments,
150+
rating = @rating,
151+
submitted_date = GETDATE()
152+
WHERE appointment_id = @id
153+
`);
154+
155+
// Update doctor's feedback score
156+
await request.query(`
157+
UPDATE d
158+
SET d.feedback_score = (
159+
SELECT AVG(CAST(f.rating AS FLOAT))
160+
FROM Feedback f
161+
JOIN Appointments a ON f.appointment_id = a.appointment_id
162+
WHERE a.doctor_id = d.doctor_id
163+
AND f.rating IS NOT NULL
164+
)
165+
FROM Doctors d
166+
JOIN Appointments a ON d.doctor_id = a.doctor_id
167+
WHERE a.appointment_id = @id
168+
`);
169+
170+
res.json({ message: 'Feedback submitted successfully' });
171+
} catch (err) {
172+
console.error('Database error:', err);
173+
res.status(500).json({ message: 'Error submitting feedback', error: err.message });
174+
}
175+
};
176+
177+
module.exports = {
178+
createAppointment,
179+
getAppointmentsByDoctor,
180+
getAppointmentsByPatient,
181+
updateAppointmentStatus,
182+
submitFeedback
183+
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const sql = require('mssql');
2+
3+
// Get all doctors
4+
const getAllDoctors = async (req, res) => {
5+
try {
6+
const request = new sql.Request();
7+
const result = await request.query(`
8+
SELECT
9+
d.doctor_id,
10+
u.full_name,
11+
u.email,
12+
u.phone,
13+
d.specialization,
14+
d.availability_schedule,
15+
d.feedback_score
16+
FROM Doctors d
17+
JOIN Users u ON d.user_id = u.user_id
18+
`);
19+
20+
res.json(result.recordset);
21+
} catch (err) {
22+
console.error('Database error:', err);
23+
res.status(500).json({ message: 'Error fetching doctors', error: err.message });
24+
}
25+
};
26+
27+
// Get doctor by ID
28+
const getDoctorById = async (req, res) => {
29+
const { id } = req.params;
30+
31+
try {
32+
const request = new sql.Request();
33+
request.input('id', sql.Int, id);
34+
35+
const doctorResult = await request.query(`
36+
SELECT
37+
d.doctor_id,
38+
u.full_name,
39+
u.email,
40+
u.phone,
41+
d.specialization,
42+
d.availability_schedule,
43+
d.feedback_score
44+
FROM Doctors d
45+
JOIN Users u ON d.user_id = u.user_id
46+
WHERE d.doctor_id = @id
47+
`);
48+
49+
if (doctorResult.recordset.length === 0) {
50+
return res.status(404).json({ message: 'Doctor not found' });
51+
}
52+
53+
const reviewsResult = await request.query(`
54+
SELECT
55+
f.feedback_id,
56+
p.full_name as patient_name,
57+
f.comments,
58+
f.submitted_date,
59+
f.rating
60+
FROM Feedback f
61+
JOIN Appointments a ON f.appointment_id = a.appointment_id
62+
JOIN Patients p ON a.patient_id = p.patient_id
63+
WHERE a.doctor_id = @id
64+
ORDER BY f.submitted_date DESC
65+
`);
66+
67+
const doctor = doctorResult.recordset[0];
68+
doctor.reviews = reviewsResult.recordset;
69+
70+
res.json(doctor);
71+
} catch (err) {
72+
console.error('Database error:', err);
73+
res.status(500).json({ message: 'Error fetching doctor details', error: err.message });
74+
}
75+
};
76+
77+
// Get doctor availability
78+
const getDoctorAvailability = async (req, res) => {
79+
const { id } = req.params;
80+
81+
try {
82+
const request = new sql.Request();
83+
request.input('id', sql.Int, id);
84+
85+
const result = await request.query(`
86+
SELECT availability_schedule
87+
FROM Doctors
88+
WHERE doctor_id = @id
89+
`);
90+
91+
if (result.recordset.length === 0) {
92+
return res.status(404).json({ message: 'Doctor not found' });
93+
}
94+
95+
res.json(result.recordset[0].availability_schedule);
96+
} catch (err) {
97+
console.error('Database error:', err);
98+
res.status(500).json({ message: 'Error fetching doctor availability', error: err.message });
99+
}
100+
};
101+
102+
module.exports = {
103+
getAllDoctors,
104+
getDoctorById,
105+
getDoctorAvailability
106+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const sql = require('mssql');
2+
3+
const createPatient = async (req, res) => {
4+
const { full_name, date_of_birth, gender, contact_info } = req.body;
5+
6+
try {
7+
const request = new sql.Request();
8+
request.input('full_name', sql.VarChar(100), full_name);
9+
request.input('date_of_birth', sql.Date, date_of_birth);
10+
request.input('gender', sql.VarChar(10), gender);
11+
request.input('contact_info', sql.VarChar(255), contact_info);
12+
13+
const result = await request.query(`
14+
INSERT INTO Patients (full_name, date_of_birth, gender, contact_info)
15+
OUTPUT INSERTED.patient_id
16+
VALUES (@full_name, @date_of_birth, @gender, @contact_info)
17+
`);
18+
19+
res.status(201).json({
20+
patient_id: result.recordset[0].patient_id
21+
});
22+
} catch (err) {
23+
console.error('Database error:', err);
24+
res.status(500).json({ message: 'Error creating patient', error: err.message });
25+
}
26+
};
27+
28+
module.exports = { createPatient };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const {
4+
createAppointment,
5+
getAppointmentsByDoctor,
6+
getAppointmentsByPatient,
7+
updateAppointmentStatus,
8+
submitFeedback
9+
} = require('../controller/appointmentController');
10+
11+
// Add these routes
12+
router.post('/', createAppointment); // POST /api/appointments
13+
router.post('/feedback', submitFeedback); // POST /api/appointments/feedback
14+
router.get('/doctor/:doctorId', getAppointmentsByDoctor);
15+
router.get('/patient/:patientId', getAppointmentsByPatient);
16+
router.put('/:id/status', updateAppointmentStatus);
17+
18+
module.exports = router;

back_end/routes/doctorRoutes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const express = require('express');
2+
const {
3+
getAllDoctors,
4+
getDoctorById,
5+
getDoctorAvailability
6+
} = require('../controller/doctorController');
7+
8+
const router = express.Router();
9+
10+
router.get('/', getAllDoctors); // GET all doctors
11+
router.get('/:id', getDoctorById); // GET doctor by ID
12+
router.get('/:id/availability', getDoctorAvailability); // GET doctor availability
13+
14+
module.exports = router;

back_end/routes/patientRoutes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const express = require('express');
2+
const { createPatient } = require('../controller/patientController');
3+
const router = express.Router();
4+
5+
router.post('/', createPatient);
6+
module.exports = router;

back_end/server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ const app = express();
88
app.use(cors());
99
app.use(express.json());
1010

11+
const patientRoutes = require('./routes/patientRoutes');
1112
const pharmacyRoutes = require('./routes/pharmacy_routes');
1213
const labRoutes = require('./routes/lab_routes');
14+
const doctorRoutes = require('./routes/doctorRoutes');
15+
const appointmentRoutes = require('./routes/appointmentRoutes');
1316

1417
app.use('/api/pharmacy', pharmacyRoutes);
1518
app.use('/api/lab', labRoutes);
19+
app.use('/api/doctors', doctorRoutes);
20+
app.use('/api/appointments', appointmentRoutes);
21+
app.use('/api/patients', patientRoutes);
1622

1723
app.get('/', (req, res) => {
1824
res.send("🚀 Healvista Backend Running");

0 commit comments

Comments
 (0)