From 6b1f329699a553f456357ec71978236a1789e04f Mon Sep 17 00:00:00 2001 From: cpride Date: Fri, 10 Jan 2025 11:29:20 -0600 Subject: [PATCH] fix: made requested changes --- backend/src/database/activity.go | 13 +++++-------- backend/src/handlers/dashboard.go | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/backend/src/database/activity.go b/backend/src/database/activity.go index 5bee3b3e..4cac2dae 100644 --- a/backend/src/database/activity.go +++ b/backend/src/database/activity.go @@ -376,19 +376,16 @@ func (db *DB) GetTotalCoursesOffered(facilityID *uint) (int, error) { func (db *DB) GetTotalStudentsEnrolled(facilityID *uint) (int, error) { var totalStudents int - subQry := db.Table("courses c"). - Select("COUNT(DISTINCT m.user_id) AS students_enrolled"). - Joins("LEFT JOIN milestones m ON m.course_id = c.id"). - Joins("INNER JOIN users u on m.user_id = u.id"). + query := db.Table("user_enrollments ue"). + Select("COUNT(DISTINCT ue.user_id) AS students_enrolled"). + Joins("INNER JOIN users u on ue.user_id = u.id"). Where("u.role = ?", "student") if facilityID != nil { - subQry = subQry.Where("u.facility_id = ?", facilityID) + query = query.Where("u.facility_id = ?", facilityID) } - err := db.Table("(?) as sub", subQry). - Select("CASE WHEN SUM(students_enrolled) IS NULL THEN 0 ELSE SUM(students_enrolled) END AS students_enrolled"). - Scan(&totalStudents).Error + err := query.Scan(&totalStudents).Error if err != nil { return 0, NewDBError(err, "error getting total students enrolled") } diff --git a/backend/src/handlers/dashboard.go b/backend/src/handlers/dashboard.go index 05890a1e..0189c825 100644 --- a/backend/src/handlers/dashboard.go +++ b/backend/src/handlers/dashboard.go @@ -53,7 +53,7 @@ func (srv *Server) handleAdminLayer2(w http.ResponseWriter, r *http.Request, log facility := r.URL.Query().Get("facility") claims := r.Context().Value(ClaimsKey).(*Claims) var facilityId *uint - + switch facility { case "all": facilityId = nil