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
26 changes: 22 additions & 4 deletions src/helpers/overviewReportHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,21 @@ const overviewReportHelper = function () {
return '50+';
}

/**
* Match users who are active now OR logged time in the period. Hours logged in the
* selected period still count after the volunteer is deactivated or paused, even
* though pausing does not record an end date.
*/
const workedDuringPeriodMatch = async (periodStart, periodEnd) => {
// Coerce request dates to plain YYYY-MM-DD strings so they can't inject query operators
const safeStart = moment(String(periodStart)).format('YYYY-MM-DD');
const safeEnd = moment(String(periodEnd)).format('YYYY-MM-DD');
const personIds = await TimeEntries.distinct('personId', {
dateOfWork: { $gte: safeStart, $lte: safeEnd },
});
return { $or: [{ isActive: true }, { _id: { $in: personIds } }] };
};

/**
* Get volunteer hours distribution stats based on weekly averages
* Requirements:
Expand All @@ -1361,11 +1376,11 @@ const overviewReportHelper = function () {
return { error: validation.error };
}

// Get all active users with their time entries
// Users active at any point in the period (incl. deactivated later), with time entries
const usersWithTimeEntries = await UserProfile.aggregate([
{
$match: {
isActive: true,
...(await workedDuringPeriodMatch(startDate, endDate)),
},
},
{
Expand Down Expand Up @@ -1489,7 +1504,7 @@ const overviewReportHelper = function () {
const data = await UserProfile.aggregate([
{
$match: {
isActive: true,
...(await workedDuringPeriodMatch(pdtstart, pdtend)),
weeklycommittedHours: { $gte: 1 },
role: { $ne: 'Mentor' },
},
Expand Down Expand Up @@ -2567,7 +2582,10 @@ const overviewReportHelper = function () {
const hoursStats = await UserProfile.aggregate([
{
$match: {
isActive: true,
...(await workedDuringPeriodMatch(
moment(start).format('YYYY-MM-DD'),
moment(end).format('YYYY-MM-DD'),
)),
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/overviewReportHelper.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const UserProfile = require('../models/userProfile');
const Task = require('../models/task');
const TimeEntries = require('../models/timeentry');

// getHoursStats / getTotalHoursWorked look up who logged time in the period
beforeEach(() => {
jest.spyOn(TimeEntries, 'distinct').mockResolvedValue([]);
});
const overviewReportHelper = require('./overviewReportHelper');

// const makeSut = () => {
Expand Down
Loading