-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathdashboardController.js
More file actions
184 lines (158 loc) · 7.3 KB
/
dashboardController.js
File metadata and controls
184 lines (158 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// controllers/dashboardController.js
const Feedback = require("../models/feedbackSchema");
const Achievement = require("../models/achievementSchema");
const Position = require("../models/positionSchema");
const PositionHolder = require("../models/positionHolderSchema");
const OrganizationalUnit = require("../models/organizationSchema");
const Event = require("../models/eventSchema");
const { UserSkill, Skill } = require("../models/schema");
const ROLES = {
PRESIDENT: "PRESIDENT",
GENSEC_SCITECH: "GENSEC_SCITECH",
GENSEC_ACADEMIC: "GENSEC_ACADEMIC",
GENSEC_CULTURAL: "GENSEC_CULTURAL",
GENSEC_SPORTS: "GENSEC_SPORTS",
CLUB_COORDINATOR: "CLUB_COORDINATOR",
STUDENT: "STUDENT",
};
const roleToCategoryMap = {
[ROLES.GENSEC_SCITECH]: "scitech",
[ROLES.GENSEC_ACADEMIC]: "academic",
[ROLES.GENSEC_CULTURAL]: "cultural",
[ROLES.GENSEC_SPORTS]: "sports",
};
exports.getDashboardStats = async (req, res) => {
const { _id: userId, username, role } = req.user;
const email =username;
let stats = {};
try {
switch (role.toUpperCase()) {
// STUDENT
case ROLES.STUDENT: {
const [totalSkills, totalFeedbacksGiven, totalAchievements, totalPORs] =
await Promise.all([
UserSkill.countDocuments({ user_id: userId }),
Feedback.countDocuments({ feedback_by: userId }),
Achievement.countDocuments({ user_id: userId }),
PositionHolder.countDocuments({ user_id: userId, status: "active" }),
]);
stats = {
totalSkills,
totalFeedbacksGiven,
totalAchievements,
totalPORs,
};
break;
}
// ALL GENSECS (SciTech, Cultural, etc.)
case ROLES.GENSEC_SCITECH:
case ROLES.GENSEC_ACADEMIC:
case ROLES.GENSEC_CULTURAL:
case ROLES.GENSEC_SPORTS: {
const roleCategory = roleToCategoryMap[role];
if (!roleCategory) {
return res.status(400).json({ msg: "Invalid GenSec role category." });
}
// Find the GenSec's organizational unit (Council) by their email
const orgUnit = await OrganizationalUnit.findOne({ "contact_info.email": email });
if (!orgUnit) {
console.error("Organizational Unit for GenSec not found.");
return res.status(404).json({ msg: "Organizational Unit for GenSec not found." });
}
// Query for pending user skills of the correct category using aggregation
const pendingUserSkillsAggregation = await UserSkill.aggregate([
{ $match: { is_endorsed: false } }, // Find un-endorsed user skills
{
$lookup: {
from: "skills",
localField: "skill_id",
foreignField: "_id",
as: "skillDoc",
},
},
{ $unwind: "$skillDoc" },
{ $match: { "skillDoc.type": roleCategory } },
{ $count: "count" },
]);
const [childClubsCount, pendingSkills, pendingAchievements] =
await Promise.all([
OrganizationalUnit.countDocuments({ parent_unit_id: orgUnit._id }),
Skill.countDocuments({ is_endorsed: false, type: roleCategory }),
Achievement.countDocuments({ verified: false }),
]);
stats = {
budget: {
used: orgUnit.budget_info.spent_amount,
total: orgUnit.budget_info.allocated_budget,
},
parentOfClubs: childClubsCount,
pendingSkillsEndorsement: pendingSkills,
pendingUserSkillsEndorsement: (pendingUserSkillsAggregation.length > 0 ? pendingUserSkillsAggregation[0].count : 0),
pendingAchievementEndorsement: pendingAchievements,
};
break;
}
// CLUB COORDINATOR
case ROLES.CLUB_COORDINATOR: {
const clubUnit = await OrganizationalUnit.findOne({ "contact_info.email": email });
if (!clubUnit) {
console.error("Club unit for Coordinator not found.");
console.log(email);
return res.status(404).json({ msg: "Club unit for Coordinator not found." });
}
// Find all positions associated with this club
const positionsInClub = await Position.find({ unit_id: clubUnit._id }).select('_id');
const positionIds = positionsInClub.map(p => p._id);
// Find all active members holding those positions
const activeMembers = await PositionHolder.find({
position_id: { $in: positionIds },
status: 'active'
});
const memberUserIds = activeMembers.map(m => m.user_id);
const [totalEvents, pendingReviews] = await Promise.all([
Event.countDocuments({ organizing_unit_id: clubUnit._id }),
Achievement.countDocuments({ user_id: { $in: memberUserIds }, verified: false }),
]);
stats = {
budget: {
used: clubUnit.budget_info.spent_amount,
total: clubUnit.budget_info.allocated_budget,
},
totalEvents,
totalActiveMembers: activeMembers.length,
pendingReviews,
};
break;
}
// PRESIDENT
case ROLES.PRESIDENT: {
const budgetAggregation = await OrganizationalUnit.aggregate([
{ $group: {
_id: null,
totalBudget: { $sum: "$budget_info.allocated_budget" },
usedBudget: { $sum: "$budget_info.spent_amount" }
}}
]);
const [pendingRoomRequests, totalOrgUnits] = await Promise.all([
Event.countDocuments({ "room_requests.status": "Pending" }),
OrganizationalUnit.countDocuments()
]);
stats = {
pendingRoomRequests,
totalOrgUnits,
budget: {
used: (budgetAggregation[0] && budgetAggregation[0].usedBudget) || 0,
total: (budgetAggregation[0] && budgetAggregation[0].totalBudget) || 0,
}
};
break;
}
default:
return res.status(400).json({ msg: "Invalid user role for stats." });
}
res.status(200).json(stats);
} catch (error) {
console.error("Error fetching dashboard stats:", error);
res.status(500).send("Server Error");
}
};