From 67fc15e88541e6022e7791fa077dc9f1216263a2 Mon Sep 17 00:00:00 2001 From: rohitcpp Date: Mon, 20 Apr 2026 00:07:18 +0530 Subject: [PATCH] fix: doctor metrics --- src/controllers/report.controller.js | 40 +++++++++++++++++----------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/controllers/report.controller.js b/src/controllers/report.controller.js index d1066f7..8196675 100644 --- a/src/controllers/report.controller.js +++ b/src/controllers/report.controller.js @@ -67,26 +67,36 @@ export const getAppointmentReport = async (req, res, next) => { export const getDoctorSummary = async (req, res, next) => { try { - const doctorId = req.user.id; - - const totalAppointments = await Appointment.countDocuments({ - doctor: doctorId - }); - - const completedAppointments = await Appointment.countDocuments({ - doctor: doctorId, - status: "completed" - }); + const doctorId = req.user.id + + const now = new Date() + const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()) + const endOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999) + + const [ + completedAppointments, + todaysAppointments, + upcomingAppointments, + allAppointments + ] = await Promise.all([ + Appointment.countDocuments({ doctor: doctorId, status: 'completed' }), + Appointment.countDocuments({ doctor: doctorId, date: { $gte: startOfToday, $lte: endOfToday } }), + Appointment.countDocuments({ doctor: doctorId, status: 'scheduled', date: { $gt: endOfToday } }), + Appointment.find({ doctor: doctorId }, 'patient').lean() + ]) + + const uniquePatients = new Set(allAppointments.map((a) => String(a.patient))).size res.status(200).json({ success: true, data: { - totalAppointments, + uniquePatients, + todaysAppointments, + upcomingAppointments, completedAppointments } - }); - + }) } catch (error) { - next(error); + next(error) } -}; \ No newline at end of file +} \ No newline at end of file