Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/controllers/reportsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ const reportsController = function () {
volunteerNumberStats,
mentorNumberStats,
volunteerHoursStats,
volunteerCommittedHoursStats,
totalHoursWorked,
tasksStats,
workDistributionStats,
Expand Down Expand Up @@ -191,6 +192,7 @@ const reportsController = function () {
comparisonStartDate,
comparisonEndDate,
),
overviewReportHelper.getCommittedHoursStats(),
overviewReportHelper.getTotalHoursWorked(startDate, endDate),
overviewReportHelper.getTasksStats(
isoStartDate,
Expand Down Expand Up @@ -291,6 +293,7 @@ const reportsController = function () {
volunteerNumberStats,
mentorNumberStats,
volunteerHoursStats,
volunteerCommittedHoursStats,
totalHoursWorked,
tasksStats,
workDistributionStats,
Expand Down
25 changes: 25 additions & 0 deletions src/helpers/overviewReportHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,30 @@
return hoursStats;
}

/**
* Distribution of active volunteers by their weekly committed hours.
* Bucket ids are UPPER limits, matching the frontend labels:
* '10' = 1-10 hrs, '20' = 11-20, '30' = 21-30, '40' = 31-40, '40+' = over 40.
* Same population as the other volunteer stats: active, committed >= 1, not a Mentor.
*/
async function getCommittedHoursStats() {

Check warning on line 1483 in src/helpers/overviewReportHelper.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move async function 'getCommittedHoursStats' to the outer scope.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HGNRest&issues=AaEaZyq-1kKqhVJlmo_7&open=AaEaZyq-1kKqhVJlmo_7&pullRequest=2388
const users = await UserProfile.find(
{ isActive: true, weeklycommittedHours: { $gte: 1 }, role: { $ne: 'Mentor' } },
'weeklycommittedHours',
).lean();

const buckets = { 10: 0, 20: 0, 30: 0, 40: 0, '40+': 0 };
users.forEach(({ weeklycommittedHours: hours }) => {
if (hours <= 10) buckets[10] += 1;
else if (hours <= 20) buckets[20] += 1;
else if (hours <= 30) buckets[30] += 1;
else if (hours <= 40) buckets[40] += 1;
else buckets['40+'] += 1;
});

return ['10', '20', '30', '40', '40+'].map((id) => ({ _id: id, count: buckets[id] }));
}

/**
* Aggregates total hours worked in the selected date range across all active volunteers,
* matching the dashboard's getOrgData logic exactly:
Expand Down Expand Up @@ -2733,6 +2757,7 @@
getWorkDistributionStats,
getTotalHoursWorked,
getHoursStats,
getCommittedHoursStats,
getFourPlusMembersTeamCount,
getTotalBadgesAwardedCount,
getAnniversaryCount,
Expand Down
45 changes: 45 additions & 0 deletions src/helpers/overviewReportHelper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,48 @@ describe('overviewReportHelper tests', () => {
// });
// });
// });

describe('getCommittedHoursStats', () => {
afterEach(() => {
jest.restoreAllMocks();
});

const mockUsers = (users) =>
jest.spyOn(UserProfile, 'find').mockReturnValue({ lean: jest.fn().mockResolvedValue(users) });

it('groups active volunteers into upper-limit committed-hours buckets', async () => {
const findSpy = mockUsers(
[1, 5, 10, 11, 20, 21, 30, 31, 35, 40, 41, 60].map((h) => ({ weeklycommittedHours: h })),
);
const { getCommittedHoursStats } = overviewReportHelper();

const result = await getCommittedHoursStats();

expect(result).toEqual([
{ _id: '10', count: 3 },
{ _id: '20', count: 2 },
{ _id: '30', count: 2 },
{ _id: '40', count: 3 },
{ _id: '40+', count: 2 },
]);
expect(findSpy).toHaveBeenCalledWith(
{ isActive: true, weeklycommittedHours: { $gte: 1 }, role: { $ne: 'Mentor' } },
'weeklycommittedHours',
);
});

it('returns every bucket with a zero count when there are no volunteers', async () => {
mockUsers([]);
const { getCommittedHoursStats } = overviewReportHelper();

const result = await getCommittedHoursStats();

expect(result).toEqual([
{ _id: '10', count: 0 },
{ _id: '20', count: 0 },
{ _id: '30', count: 0 },
{ _id: '40', count: 0 },
{ _id: '40+', count: 0 },
]);
});
});
Loading