From bbabb3b035d8a8d8a2685e7e257f61879905ba77 Mon Sep 17 00:00:00 2001 From: Potenz Date: Fri, 20 Jun 2025 14:27:30 +0530 Subject: [PATCH] updated added new routes there --- apps/Backend/src/routes/appointements.ts | 74 ++++++++++++++++++++++-- apps/Backend/src/routes/patients.ts | 14 +++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/apps/Backend/src/routes/appointements.ts b/apps/Backend/src/routes/appointements.ts index 7c22a40..07e374d 100644 --- a/apps/Backend/src/routes/appointements.ts +++ b/apps/Backend/src/routes/appointements.ts @@ -57,9 +57,6 @@ const updatePatientSchema = ( type UpdatePatient = z.infer; - - - // Get all appointments router.get("/all", async (req: Request, res: Response): Promise => { try { @@ -104,6 +101,74 @@ router.get( } ); +// Get all appointments for a specific patient +router.get( + "/:patientId/appointments", + async (req: Request, res: Response): Promise => { + try { + const rawPatientId = req.params.patientId; + if (!rawPatientId) { + return res.status(400).json({ message: "Patient ID is required" }); + } + + const patientId = parseInt(rawPatientId); + if (isNaN(patientId)) { + return res.status(400).json({ message: "Invalid patient ID" }); + } + + const patient = await storage.getPatient(patientId); + if (!patient) + return res.status(404).json({ message: "Patient not found" }); + if (patient.userId !== req.user!.id) + return res.status(403).json({ message: "Forbidden" }); + + const appointments = await storage.getAppointmentsByPatientId(patientId); + res.json(appointments); + } catch (err) { + res.status(500).json({ message: "Failed to get patient appointments" }); + } + } +); + +// Get appointments on a specific date +router.get( + "/appointments/on/:date", + async (req: Request, res: Response): Promise => { + try { + const rawDate = req.params.date; + if (!rawDate) { + return res.status(400).json({ message: "Date parameter is required" }); + } + + const date = new Date(rawDate); + if (isNaN(date.getTime())) { + return res.status(400).json({ message: "Invalid date format" }); + } + + const all = await storage.getAppointmentsOn(date); + const appointments = all.filter((a) => a.userId === req.user!.id); + + res.json(appointments); + } catch (err) { + res.status(500).json({ message: "Failed to get appointments on date" }); + } + } +); + +// Get recent appointments (paginated) +router.get("/appointments/recent", async (req: Request, res: Response) => { + try { + const limit = Math.max(1, parseInt(req.query.limit as string) || 10); + const offset = Math.max(0, parseInt(req.query.offset as string) || 0); + + const all = await storage.getRecentAppointments(limit, offset); + const filtered = all.filter((a) => a.userId === req.user!.id); + + res.json({ data: filtered, limit, offset }); + } catch (err) { + res.status(500).json({ message: "Failed to get recent appointments" }); + } +}); // Create a new appointment router.post( @@ -196,7 +261,6 @@ router.put( } const appointmentId = parseInt(appointmentIdParam); - // Check if appointment exists and belongs to user const existingAppointment = await storage.getAppointment(appointmentId); if (!existingAppointment) { @@ -335,4 +399,4 @@ router.delete( } ); -export default router; \ No newline at end of file +export default router; diff --git a/apps/Backend/src/routes/patients.ts b/apps/Backend/src/routes/patients.ts index d003b67..a131f0d 100644 --- a/apps/Backend/src/routes/patients.ts +++ b/apps/Backend/src/routes/patients.ts @@ -101,6 +101,20 @@ router.get( } ); +// Get recent patients (paginated) +router.get("/recent", async (req: Request, res: Response) => { + try { + const limit = parseInt(req.query.limit as string) || 10; + const offset = parseInt(req.query.offset as string) || 0; + + const recentPatients = await storage.getRecentPatients(limit, offset); + res.json(recentPatients); + } catch (error) { + console.error("Failed to retrieve recent patients:", error); + res.status(500).json({ message: "Failed to retrieve recent patients" }); + } +}); + // Create a new patient router.post("/", async (req: Request, res: Response): Promise => { try {