Skip to content

Commit 437e709

Browse files
committed
feat: add admin analytics
1 parent 348bc55 commit 437e709

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import paymentRoutes from './routes/payment.routes.js';
1010
import signupRoutes from './routes/signup.js';
1111
import adminRoutes from './routes/adminroutes.js';
1212

13+
import dashboardRoutes from './routes/dashboard.routes.js';
1314
import doctorRoutes from './routes/doctor.routes.js';
1415
import receptionRoutes from './routes/reception.routes.js';
1516
import billingRoutes from './routes/billing.routes.js';
@@ -55,6 +56,7 @@ app.use('/api/payment', paymentRoutes);
5556
app.use('/api/signup', signupRoutes);
5657
app.use('/api/admin', adminRoutes);
5758

59+
app.use('/api/admin/dashboard', dashboardRoutes);
5860
app.use('/api/users/doctors', doctorRoutes);
5961
app.use('/api/users/receptionist', receptionRoutes);
6062
app.use('/api/billing', billingRoutes);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import User from '../models/User.js';
2+
import Patient from '../models/patient.js';
3+
import Appointment from '../models/appointment.js';
4+
import Billing from '../models/billing.js';
5+
6+
export const getAdminDashboardSummary = async (req, res, next) => {
7+
try {
8+
const totalDoctors = await User.countDocuments({ role: "doctor" });
9+
const totalPatients = await Patient.countDocuments();
10+
const totalAppointments = await Appointment.countDocuments();
11+
12+
const revenueAgg = await Billing.aggregate([
13+
{ $match: { status: "paid" } },
14+
{
15+
$group: {
16+
_id: null,
17+
total: { $sum: "$amount" }
18+
}
19+
}
20+
]);
21+
22+
const totalRevenue = revenueAgg.length ? revenueAgg[0].total : 0;
23+
24+
const pendingAgg = await Billing.aggregate([
25+
{ $match: { status: "pending" } },
26+
{
27+
$group: {
28+
_id: null,
29+
total: { $sum: "$amount" }
30+
}
31+
}
32+
]);
33+
34+
const pendingRevenue = pendingAgg.length ? pendingAgg[0].total : 0;
35+
36+
const todayStart = new Date();
37+
todayStart.setHours(0, 0, 0, 0);
38+
39+
const todayEnd = new Date();
40+
todayEnd.setHours(23, 59, 59, 999);
41+
42+
const todayAppointments = await Appointment.countDocuments({
43+
createdAt: { $gte: todayStart, $lte: todayEnd }
44+
});
45+
46+
res.status(200).json({
47+
success: true,
48+
data: {
49+
totalDoctors,
50+
totalPatients,
51+
totalAppointments,
52+
totalRevenue,
53+
pendingRevenue,
54+
todayAppointments
55+
}
56+
});
57+
58+
} catch (error) {
59+
next(error);
60+
}
61+
};

src/routes/dashboard.routes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
import { protect, authorize } from "../middleware/authmiddleware.js";
3+
import { getAdminDashboardSummary } from '../controllers/dashboard.controller.js';
4+
5+
const router = express.Router();
6+
7+
router.get(
8+
"/summary",
9+
protect,
10+
authorize(["admin"]),
11+
getAdminDashboardSummary
12+
);
13+
14+
export default router;

0 commit comments

Comments
 (0)