Skip to content

Commit cf75456

Browse files
authored
Merge pull request #52 from PentabyteDevAlign/raffi
Update Branch
2 parents 84950b5 + 2c5114b commit cf75456

4 files changed

Lines changed: 342 additions & 127 deletions

File tree

Backend/controllers/dashboard.controller.js

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,40 @@ const getDashboardData = async (req, res) => {
4242

4343
// 2. Get project statistics
4444
const projectStats = await Project.aggregate([
45+
{
46+
$addFields: {
47+
// Normalize status to lowercase for consistent matching
48+
normalizedStatus: { $toLower: "$status" }
49+
}
50+
},
4551
{
4652
$group: {
47-
_id: '$status',
53+
_id: '$normalizedStatus',
4854
count: { $sum: 1 }
4955
}
5056
}
5157
]);
5258

59+
// Debug log to see actual statuses
60+
console.log('Raw Project Stats:', projectStats);
61+
5362
const projectStatistics = {
5463
completed: 0,
55-
inProgress: 0,
56-
onHold: 0,
57-
rejected: 0
64+
inProgress: 0
5865
};
5966

6067
projectStats.forEach(stat => {
61-
switch(stat._id.toLowerCase()) {
62-
case 'completed':
63-
projectStatistics.completed = stat.count;
64-
break;
65-
case 'in progress':
66-
projectStatistics.inProgress = stat.count;
67-
break;
68-
case 'on hold':
69-
projectStatistics.onHold = stat.count;
70-
break;
71-
case 'rejected':
72-
projectStatistics.rejected = stat.count;
73-
break;
68+
const status = stat._id?.toLowerCase() || '';
69+
if (status === 'completed') {
70+
projectStatistics.completed = stat.count;
71+
} else if (status === 'active') {
72+
projectStatistics.inProgress = stat.count;
7473
}
7574
});
7675

76+
// Debug log final statistics
77+
console.log('Final Project Statistics:', projectStatistics);
78+
7779
// 3. Get top contributors (existing logic)
7880

7981
const range = getDateRange(period);
@@ -205,8 +207,11 @@ const getManagerDashboard = async (req, res) => {
205207

206208
const managerId = new mongoose.Types.ObjectId(String(requesterId));
207209

208-
// find team members (direct reports)
209-
const teamMembers = await User.find({ managerId: managerId }).select('_id name email position').lean();
210+
// find active team members only (direct reports)
211+
const teamMembers = await User.find({
212+
managerId: managerId,
213+
active: true // only include active team members
214+
}).select('_id name email position').lean();
210215
const teamIds = teamMembers.map((m) => new mongoose.Types.ObjectId(String(m._id)));
211216

212217
// Get position details for team members
@@ -222,37 +227,11 @@ const getManagerDashboard = async (req, res) => {
222227
positions.forEach((p) => positionMap.set(String(p._id), p.name));
223228
}
224229

225-
// if no team members, return empty dashboard
226-
if (teamIds.length === 0) {
227-
return res.json({
228-
success: true,
229-
data: {
230-
statistics: {
231-
totalProjects: 0,
232-
projectsComplete: 0,
233-
projectsOnGoing: 0,
234-
},
235-
team: [],
236-
},
237-
});
238-
}
239-
240-
// aggregate distinct projects assigned to team and count by project.status
241-
const projectStatusAgg = await ProjectAssignment.aggregate([
242-
{ $match: { userId: { $in: teamIds } } },
243-
{
244-
$lookup: {
245-
from: 'projects',
246-
localField: 'projectId',
247-
foreignField: '_id',
248-
as: 'project',
249-
},
250-
},
251-
{ $unwind: '$project' },
252-
// group by project to dedupe assignments
253-
{ $group: { _id: '$project._id', status: { $first: '$project.status' } } },
254-
// now group by status
255-
{ $group: { _id: '$status', count: { $sum: 1 } } },
230+
// Count projects created by the manager grouped by status
231+
const Project = require('../models').Project;
232+
const projectStatusAgg = await Project.aggregate([
233+
{ $match: { createdBy: managerId } },
234+
{ $group: { _id: '$status', count: { $sum: 1 } } }
256235
]).exec();
257236

258237
let totalProjects = 0;

0 commit comments

Comments
 (0)