From 656bc6aab4eca0c1900ac427fe485c1803fa499c Mon Sep 17 00:00:00 2001 From: Denish kalariya Date: Fri, 24 Jan 2025 23:52:23 -0600 Subject: [PATCH] New Filed Added --- src/controllers/reportsController.js | 8 ++++ src/helpers/overviewReportHelper.js | 62 ++++++++++++++++++++++++++++ src/models/userProfile.js | 2 + 3 files changed, 72 insertions(+) diff --git a/src/controllers/reportsController.js b/src/controllers/reportsController.js index 192b4ba27..4bb80a63a 100644 --- a/src/controllers/reportsController.js +++ b/src/controllers/reportsController.js @@ -91,6 +91,7 @@ const reportsController = function () { taskAndProjectStats, volunteersOverAssignedTime, completedAssignedHours, + totalSummariesSubmitted, ] = await Promise.all([ overviewReportHelper.getVolunteerNumberStats( isoStartDate, @@ -158,6 +159,12 @@ const reportsController = function () { isoComparisonStartDate, isoComparisonEndDate, ), + overviewReportHelper.getTotalSummariesSubmitted( + isoStartDate, + isoEndDate, + isoComparisonStartDate, + isoComparisonEndDate, + ), ]); res.status(200).send({ volunteerNumberStats, @@ -176,6 +183,7 @@ const reportsController = function () { taskAndProjectStats, volunteersOverAssignedTime, completedAssignedHours, + totalSummariesSubmitted, }); } catch (err) { console.log(err); diff --git a/src/helpers/overviewReportHelper.js b/src/helpers/overviewReportHelper.js index 5ff9051e2..da7c9494a 100644 --- a/src/helpers/overviewReportHelper.js +++ b/src/helpers/overviewReportHelper.js @@ -1822,6 +1822,67 @@ const overviewReportHelper = function () { return { count: currentCount }; } + const getTotalSummariesSubmitted = async (startDate, endDate, comparisonStartDate, comparisonEndDate) => { + // Helper function to count summaries submitted within a date range + const getSummariesCount = async (start, end) => { + return await UserProfile.aggregate([ + { + $match: { + summarySubmissionDates: { + $elemMatch: { + $gte: new Date(start), + $lte: new Date(end), + }, + }, + }, + }, + { + $project: { + totalSummaries: { + $size: { + $filter: { + input: '$summarySubmissionDates', + as: 'date', + cond: { + $and: [ + { $gte: ['$$date', new Date(start)] }, + { $lte: ['$$date', new Date(end)] }, + ], + }, + }, + }, + }, + }, + }, + { + $group: { + _id: null, + totalSummaries: { $sum: '$totalSummaries' }, + }, + }, + ]); + }; + + // Get summaries count for the current date range + const currentSummaries = await getSummariesCount(startDate, endDate); + const totalCurrentSummaries = currentSummaries[0]?.totalSummaries || 0; + + // If comparison dates are provided, calculate the comparison percentage + if (comparisonStartDate && comparisonEndDate) { + const comparisonSummaries = await getSummariesCount(comparisonStartDate, comparisonEndDate); + const totalComparisonSummaries = comparisonSummaries[0]?.totalSummaries || 0; + const comparisonPercentage = calculateGrowthPercentage(totalCurrentSummaries, totalComparisonSummaries); + + return { count: totalCurrentSummaries, comparisonPercentage }; + } + + // If no comparison dates, return only the count + return { count: totalCurrentSummaries }; + }; + + + + return { getVolunteerTrends, getMapLocations, @@ -1846,6 +1907,7 @@ const overviewReportHelper = function () { getTeamsWithActiveMembers, getVolunteersOverAssignedTime, getVolunteersCompletedAssignedHours, + getTotalSummariesSubmitted, }; }; diff --git a/src/models/userProfile.js b/src/models/userProfile.js index 61e8e8f91..c6f71df8a 100644 --- a/src/models/userProfile.js +++ b/src/models/userProfile.js @@ -12,6 +12,8 @@ const SALT_ROUNDS = 10; const today = new Date(); const userProfileSchema = new Schema({ + // Updated filed + summarySubmissionDates: [{ type: Date }], password: { type: String, required: true,