Skip to content

Commit e62abd8

Browse files
Merge pull request #2066 from OneCommunityGlobal/Sohail-Total-Hours-Worked-Not-Matching-Leaderboard
Sohail: Total Hours Worked Not Matching Leaderboard
2 parents 2da4545 + 3f88e93 commit e62abd8

2 files changed

Lines changed: 54 additions & 88 deletions

File tree

src/controllers/reportsController.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,7 @@ const reportsController = function () {
191191
isoComparisonStartDate,
192192
isoComparisonEndDate,
193193
),
194-
overviewReportHelper.getTotalHoursWorked(
195-
isoStartDate,
196-
isoEndDate,
197-
isoComparisonStartDate,
198-
isoComparisonEndDate,
199-
),
194+
overviewReportHelper.getTotalHoursWorked(),
200195
overviewReportHelper.getTasksStats(
201196
isoStartDate,
202197
isoEndDate,

src/helpers/overviewReportHelper.js

Lines changed: 53 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,101 +1498,72 @@ const overviewReportHelper = function () {
14981498
}
14991499

15001500
/**
1501-
* Aggregates total number of hours worked across all volunteers within the specified date range
1501+
* Aggregates total hours worked this week across all active volunteers,
1502+
* matching the dashboard's getOrgData logic exactly:
1503+
* - Current week (America/Los_Angeles) date range, ignoring any passed-in date filters
1504+
* - Only active users with weeklycommittedHours >= 1 and role != Mentor
1505+
* - Excludes entryType of 'person', 'team', or 'project'
15021506
*/
1503-
async function getTotalHoursWorked(startDate, endDate, comparisonStartDate, comparisonEndDate) {
1504-
// Validate date parameters
1505-
const validation = validateDateParameters(
1506-
startDate,
1507-
endDate,
1508-
comparisonStartDate,
1509-
comparisonEndDate,
1510-
);
1511-
if (!validation.isValid) {
1512-
return { error: validation.error };
1513-
}
1507+
async function getTotalHoursWorked() {
1508+
const pdtstart = moment().tz('America/Los_Angeles').startOf('week').format('YYYY-MM-DD');
1509+
const pdtend = moment().tz('America/Los_Angeles').endOf('week').format('YYYY-MM-DD');
15141510

1515-
if (!comparisonStartDate && !comparisonEndDate) {
1516-
const data = await TimeEntries.aggregate([
1517-
{
1518-
$match: {
1519-
dateOfWork: {
1520-
$gte: moment(startDate).format('YYYY-MM-DD'),
1521-
$lte: moment(endDate).format('YYYY-MM-DD'),
1511+
const data = await UserProfile.aggregate([
1512+
{
1513+
$match: {
1514+
isActive: true,
1515+
weeklycommittedHours: { $gte: 1 },
1516+
role: { $ne: 'Mentor' },
1517+
},
1518+
},
1519+
{
1520+
$lookup: {
1521+
from: 'timeEntries',
1522+
localField: '_id',
1523+
foreignField: 'personId',
1524+
as: 'timeEntryData',
1525+
},
1526+
},
1527+
{
1528+
$project: {
1529+
timeEntryData: {
1530+
$filter: {
1531+
input: '$timeEntryData',
1532+
as: 'timeentry',
1533+
cond: {
1534+
$and: [
1535+
{ $gte: ['$$timeentry.dateOfWork', pdtstart] },
1536+
{ $lte: ['$$timeentry.dateOfWork', pdtend] },
1537+
{ $not: [{ $in: ['$$timeentry.entryType', ['person', 'team', 'project']] }] },
1538+
],
1539+
},
15221540
},
15231541
},
15241542
},
1525-
{
1526-
$group: {
1527-
_id: null,
1528-
totalSeconds: { $sum: '$totalSeconds' },
1543+
},
1544+
{ $unwind: { path: '$timeEntryData', preserveNullAndEmptyArrays: true } },
1545+
{
1546+
$project: {
1547+
totalSeconds: {
1548+
$cond: [{ $gte: ['$timeEntryData.totalSeconds', 0] }, '$timeEntryData.totalSeconds', 0],
15291549
},
15301550
},
1531-
{
1532-
$project: {
1533-
_id: 0,
1534-
totalHours: { $divide: ['$totalSeconds', 3600] },
1535-
},
1551+
},
1552+
{
1553+
$group: {
1554+
_id: '$_id',
1555+
time_hrs: { $sum: { $divide: ['$totalSeconds', 3600] } },
15361556
},
1537-
]);
1538-
1539-
return { current: data[0]?.totalHours || 0 };
1540-
}
1541-
const data = await TimeEntries.aggregate([
1557+
},
15421558
{
1543-
$facet: {
1544-
currentTotalHours: [
1545-
{
1546-
$match: {
1547-
dateOfWork: {
1548-
$gte: moment(startDate).format('YYYY-MM-DD'),
1549-
$lte: moment(endDate).format('YYYY-MM-DD'),
1550-
},
1551-
},
1552-
},
1553-
{
1554-
$group: {
1555-
_id: null,
1556-
totalSeconds: { $sum: '$totalSeconds' },
1557-
},
1558-
},
1559-
{
1560-
$project: {
1561-
_id: 0,
1562-
totalHours: { $divide: ['$totalSeconds', 3600] },
1563-
},
1564-
},
1565-
],
1566-
1567-
comparisonTotalHours: [
1568-
{
1569-
$match: {
1570-
dateOfWork: {
1571-
$gte: moment(comparisonStartDate).format('YYYY-MM-DD'),
1572-
$lte: moment(comparisonEndDate).format('YYYY-MM-DD'),
1573-
},
1574-
},
1575-
},
1576-
{
1577-
$group: {
1578-
_id: null,
1579-
totalSeconds: { $sum: '$totalSeconds' },
1580-
},
1581-
},
1582-
{
1583-
$project: {
1584-
_id: 0,
1585-
totalHours: { $divide: ['$totalSeconds', 3600] },
1586-
},
1587-
},
1588-
],
1559+
$group: {
1560+
_id: 0,
1561+
totaltime_hrs: { $sum: '$time_hrs' },
15891562
},
15901563
},
15911564
]);
15921565

1593-
const current = data[0].currentTotalHours[0]?.totalHours || 0;
1594-
const comparison = data[0].comparisonTotalHours[0]?.totalHours || 0;
1595-
return { current, comparison, percentage: calculateGrowthPercentage(current, comparison) };
1566+
return { current: data[0]?.totaltime_hrs || 0 };
15961567
}
15971568

15981569
/**

0 commit comments

Comments
 (0)