From 3b2890ab12418c5b962fbe55ee57f3b76712020f Mon Sep 17 00:00:00 2001 From: ArtemisNyx3 Date: Sat, 16 Nov 2024 23:25:32 -0500 Subject: [PATCH 1/3] Initial solution --- src/cronjobs/userProfileJobs.js | 6 +- src/helpers/userHelper.js | 268 ++++++++++++++++++++++---------- 2 files changed, 191 insertions(+), 83 deletions(-) diff --git a/src/cronjobs/userProfileJobs.js b/src/cronjobs/userProfileJobs.js index e7a8662a6..b64610b66 100644 --- a/src/cronjobs/userProfileJobs.js +++ b/src/cronjobs/userProfileJobs.js @@ -4,9 +4,11 @@ const moment = require('moment-timezone'); const userhelper = require('../helpers/userHelper')(); const userProfileJobs = () => { + console.log('User Profile Jobs Started'); const allUserProfileJobs = new CronJob( - // '* * * * *', // Comment out for testing. Run Every minute. - '1 0 * * 0', // Every Sunday, 1 minute past midnight. + // '*/5 * * * *', // Comment out for testing. Run Every 5 minutes. + '* * * * *', // Comment out for testing. Run Every minute. + // '1 0 * * 0', // Every Sunday, 1 minute past midnight. async () => { const SUNDAY = 0; // will change back to 0 after fix diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 9ea062422..23cb863da 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -1920,16 +1920,142 @@ const userHelper = function () { }; // 'Total Hrs in Category' + // const checkTotalHrsInCat = async function (personId, user, badgeCollection) { + // console.log('Category Badge'); + // const hoursByCategory = user.hoursByCategory || {}; + // const categories = [ + // 'food', + // 'energy', + // 'housing', + // 'education', + // 'society', + // 'economics', + // 'stewardship', + // ]; + // // const categories = [ + // // 'Food', + // // 'Energy', + // // 'Housing', + // // 'Education', + // // 'Society', + // // 'Economics', + // // 'Stewardship', + // // ]; + + // const badgesOfType = badgeCollection + // .filter((object) => object.badge.type === 'Total Hrs in Category') + // .map((object) => object.badge); + + // categories.forEach(async (category) => { + // const categoryHrs = Object.keys(hoursByCategory).find((elem) => elem === category); + + // let badgeOfType; + // for (let i = 0; i < badgeCollection.length; i += 1) { + // if ( + // badgeCollection[i].badge?.type === 'Total Hrs in Category' && + // badgeCollection[i].badge?.category === category + // ) { + // if (badgeOfType && badgeOfType.totalHrs <= badgeCollection[i].badge.totalHrs) { + // removeDupBadge(personId, badgeOfType._id); + // badgeOfType = badgeCollection[i].badge; + // } else if (badgeOfType && badgeOfType.totalHrs > badgeCollection[i].badge.totalHrs) { + // removeDupBadge(personId, badgeCollection[i].badge._id); + // } else if (!badgeOfType) { + // badgeOfType = badgeCollection[i].badge; + // } + // } + // } + // const newCatg = category.charAt(0).toUpperCase() + category.slice(1); // Capitalize the first letter + // await badge + // .find({ type: 'Total Hrs in Category', category: newCatg }) + + // .sort({ totalHrs: -1 }) + // .then((results) => { + // if (!Array.isArray(results) || !results.length || !categoryHrs) { + // return; + // } + + // results.every((elem) => { + // if ( + // hoursByCategory[categoryHrs] >= 100 && + // hoursByCategory[categoryHrs] >= elem.totalHrs + // ) { + // let theBadge; + // for (let i = 0; i < badgesOfType.length; i += 1) { + // if (badgesOfType[i]._id.toString() === elem._id.toString()) { + // theBadge = badgesOfType[i]._id; + // break; + // } + // } + // if (theBadge) { + // // increaseBadgeCount(personId, mongoose.Types.ObjectId(theBadge)); + // return false; + // } + // if (badgeOfType) { + // if ( + // badgeOfType._id.toString() !== elem._id.toString() && + // badgeOfType.totalHrs < elem.totalHrs + // ) { + // replaceBadge( + // personId, + // mongoose.Types.ObjectId(badgeOfType._id), + // mongoose.Types.ObjectId(elem._id), + // ); + // } + // return false; + // } + // addBadge(personId, mongoose.Types.ObjectId(elem._id)); + // return false; + // } + // return true; + // }); + // }); + // }); + // }; + + const addBadgeCategory = async function (personId, newBadge, badgeCollection) { + console.log('adding new badge category --- ', newBadge.category); + // remove all badges of Type 'Total Hrs in Category' and category from the user + + for (let i = 0; i < badgeCollection.length; i += 1) { + if ( + badgeCollection[i].badge?.type === 'Total Hrs in Category' && + badgeCollection[i].badge?.category === newBadge.category + ) { + removeDupBadge(personId, badgeCollection[i].badge._id); + } + } + + // try { + // badgeCollection.forEach((badge) => { + // if ( + // badge.badge.type === 'Total Hrs in Category' && + // badge.badge.category === newBadge.category + // ) { + // removeDupBadge(personId, badge._id); + // } + // }); + // } catch (err) { + // logger.logException(err); + // } + + // add the new badge + addBadge(personId, newBadge._id); + }; + const checkTotalHrsInCat = async function (personId, user, badgeCollection) { + console.log('Category Badge'); const hoursByCategory = user.hoursByCategory || {}; + // categories in badges are capitalized + // categories in user.hoursByCategory are not capitalized const categories = [ - 'food', - 'energy', - 'housing', - 'education', - 'society', - 'economics', - 'stewardship', + 'Food', + 'Energy', + 'Housing', + 'Education', + 'Society', + 'Economics', + 'Stewardship', ]; const badgesOfType = badgeCollection @@ -1937,77 +2063,57 @@ const userHelper = function () { .map((object) => object.badge); categories.forEach(async (category) => { - const categoryHrs = Object.keys(hoursByCategory).find((elem) => elem === category); - - let badgeOfType; - for (let i = 0; i < badgeCollection.length; i += 1) { - if ( - badgeCollection[i].badge?.type === 'Total Hrs in Category' && - badgeCollection[i].badge?.category === category - ) { - if (badgeOfType && badgeOfType.totalHrs <= badgeCollection[i].badge.totalHrs) { - removeDupBadge(personId, badgeOfType._id); - badgeOfType = badgeCollection[i].badge; - } else if (badgeOfType && badgeOfType.totalHrs > badgeCollection[i].badge.totalHrs) { - removeDupBadge(personId, badgeCollection[i].badge._id); - } else if (!badgeOfType) { - badgeOfType = badgeCollection[i].badge; - } - } - } - - const newCatg = category.charAt(0).toUpperCase() + category.slice(1); + const categoryHrsKey = Object.keys(hoursByCategory).find( + (elem) => elem === category.toLowerCase(), + ); + console.log( + `checing category -- ${category} with hours -- ${hoursByCategory[categoryHrsKey]}`, + ); + // console.log('\nbadgesOfType ----- ', badgesOfType[0]); + // all badges of type 'Total Hrs in Category' in descending order of totalHrs await badge - .find({ type: 'Total Hrs in Category', category: newCatg }) - + .find({ type: 'Total Hrs in Category', category }) .sort({ totalHrs: -1 }) .then((results) => { - if (!Array.isArray(results) || !results.length || !categoryHrs) { - return; - } - - results.every((elem) => { - if ( - hoursByCategory[categoryHrs] >= 100 && - hoursByCategory[categoryHrs] >= elem.totalHrs - ) { - let theBadge; - for (let i = 0; i < badgesOfType.length; i += 1) { - if (badgesOfType[i]._id.toString() === elem._id.toString()) { - theBadge = badgesOfType[i]._id; - break; - } - } - if (theBadge) { - increaseBadgeCount(personId, mongoose.Types.ObjectId(theBadge)); - return false; - } - if (badgeOfType) { - if ( - badgeOfType._id.toString() !== elem._id.toString() && - badgeOfType.totalHrs < elem.totalHrs - ) { - replaceBadge( - personId, - mongoose.Types.ObjectId(badgeOfType._id), - mongoose.Types.ObjectId(elem._id), - ); - } - return false; + // loop results to check if user has earned the badge + + // eslint-disable-next-line guard-for-in + for (let i = 0; i < results.length; i += 1) { + const badge = results[i]; + // console.log( + // 'hoursByCategory[categoryHrsKey] >= badge.totalHrs --- ', + // hoursByCategory[categoryHrsKey] >= badge.totalHrs, + // ); + // console.log('category, badge hrs --- ', categoryHrsKey, badge.totalHrs); + // console.log('\n'); + if (hoursByCategory[categoryHrsKey] >= badge.totalHrs) { + console.log('if condition -- criteria met'); + // if condition -- criteria met + const badgeIndex = badgesOfType.findIndex( + (badgeItem) => badgeItem._id.toString() === badge._id.toString(), + ); + // user has not earned this badge + // check if user has earned any other badge of same type + if (badgeIndex === -1) { + console.log('adding badge --- ', badge); + addBadgeCategory(personId, badge, badgesOfType); + console.log('Badge added'); + break; } - addBadge(personId, mongoose.Types.ObjectId(elem._id)); - return false; } - return true; - }); + } }); }); }; const awardNewBadges = async () => { try { - const users = await userProfile.find({ isActive: true }).populate('badgeCollection.badge'); + // const users = await userProfile.find({ isActive: true }).populate('badgeCollection.badge'); + // for testing do 1 user with id = 672f294cccc3cc90b8215e17 + const users = await userProfile + .find({ _id: '672f294cccc3cc90b8215e17' }) + .populate('badgeCollection.badge'); for (let i = 0; i < users.length; i += 1) { const user = users[i]; const { _id, badgeCollection } = user; @@ -2066,10 +2172,10 @@ const userHelper = function () { sendThreeWeeks, followup, ) { - let subject; - let emailBody; - recipients.push('onecommunityglobal@gmail.com'); - recipients = recipients.toString(); + let subject; + let emailBody; + recipients.push('onecommunityglobal@gmail.com'); + recipients = recipients.toString(); if (reactivationDate) { subject = `IMPORTANT: ${firstName} ${lastName} has been PAUSED in the Highest Good Network`; emailBody = `

Management,

@@ -2094,7 +2200,6 @@ const userHelper = function () {

One Community

`; emailSender(email, subject, emailBody, null, recipients, email); - } else if (endDate && isSet && followup) { subject = `IMPORTANT: The last day for ${firstName} ${lastName} has been set in the Highest Good Network`; emailBody = `

Management,

@@ -2106,8 +2211,7 @@ const userHelper = function () {

One Community

`; emailSender(email, subject, emailBody, null, recipients, email); - - } else if (endDate && isSet ) { + } else if (endDate && isSet) { subject = `IMPORTANT: The last day for ${firstName} ${lastName} has been set in the Highest Good Network`; emailBody = `

Management,

@@ -2118,8 +2222,7 @@ const userHelper = function () {

One Community

`; emailSender(email, subject, emailBody, null, recipients, email); - - } else if(endDate){ + } else if (endDate) { subject = `IMPORTANT: ${firstName} ${lastName} has been deactivated in the Highest Good Network`; emailBody = `

Management,

@@ -2130,10 +2233,9 @@ const userHelper = function () {

One Community

`; emailSender(email, subject, emailBody, null, recipients, email); - }; - } - + }; + const deActivateUser = async () => { try { const emailReceivers = await userProfile.find( @@ -2149,14 +2251,18 @@ const userHelper = function () { const user = users[i]; const { endDate, finalEmailThreeWeeksSent } = user; endDate.setHours(endDate.getHours() + 7); - // notify reminder set final day before 2 weeks - if(finalEmailThreeWeeksSent && moment().isBefore(moment(endDate).subtract(2, 'weeks')) && moment().isAfter(moment(endDate).subtract(3, 'weeks'))){ + // notify reminder set final day before 2 weeks + if ( + finalEmailThreeWeeksSent && + moment().isBefore(moment(endDate).subtract(2, 'weeks')) && + moment().isAfter(moment(endDate).subtract(3, 'weeks')) + ) { const id = user._id; const person = await userProfile.findById(id); const lastDay = moment(person.endDate).format('YYYY-MM-DD'); logger.logInfo(`User with id: ${user._id}'s final Day is set at ${moment().format()}.`); person.teams.map(async (teamId) => { - const managementEmails = await userHelper.getTeamManagementEmail(teamId); + const managementEmails = await userHelper.getTeamManagementEmail(teamId); if (Array.isArray(managementEmails) && managementEmails.length > 0) { managementEmails.forEach((management) => { recipients.push(management.email); From 50c0d917db93ba82a1d20b9e656af176c0848151 Mon Sep 17 00:00:00 2001 From: ArtemisNyx3 Date: Sun, 17 Nov 2024 00:07:27 -0500 Subject: [PATCH 2/3] requirements met --- src/helpers/userHelper.js | 151 ++++++-------------------------------- 1 file changed, 24 insertions(+), 127 deletions(-) diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 23cb863da..986283c98 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -1919,126 +1919,37 @@ const userHelper = function () { }); }; - // 'Total Hrs in Category' - // const checkTotalHrsInCat = async function (personId, user, badgeCollection) { - // console.log('Category Badge'); - // const hoursByCategory = user.hoursByCategory || {}; - // const categories = [ - // 'food', - // 'energy', - // 'housing', - // 'education', - // 'society', - // 'economics', - // 'stewardship', - // ]; - // // const categories = [ - // // 'Food', - // // 'Energy', - // // 'Housing', - // // 'Education', - // // 'Society', - // // 'Economics', - // // 'Stewardship', - // // ]; - - // const badgesOfType = badgeCollection - // .filter((object) => object.badge.type === 'Total Hrs in Category') - // .map((object) => object.badge); - - // categories.forEach(async (category) => { - // const categoryHrs = Object.keys(hoursByCategory).find((elem) => elem === category); - - // let badgeOfType; - // for (let i = 0; i < badgeCollection.length; i += 1) { - // if ( - // badgeCollection[i].badge?.type === 'Total Hrs in Category' && - // badgeCollection[i].badge?.category === category - // ) { - // if (badgeOfType && badgeOfType.totalHrs <= badgeCollection[i].badge.totalHrs) { - // removeDupBadge(personId, badgeOfType._id); - // badgeOfType = badgeCollection[i].badge; - // } else if (badgeOfType && badgeOfType.totalHrs > badgeCollection[i].badge.totalHrs) { - // removeDupBadge(personId, badgeCollection[i].badge._id); - // } else if (!badgeOfType) { - // badgeOfType = badgeCollection[i].badge; - // } - // } - // } - // const newCatg = category.charAt(0).toUpperCase() + category.slice(1); // Capitalize the first letter - // await badge - // .find({ type: 'Total Hrs in Category', category: newCatg }) - - // .sort({ totalHrs: -1 }) - // .then((results) => { - // if (!Array.isArray(results) || !results.length || !categoryHrs) { - // return; - // } - - // results.every((elem) => { - // if ( - // hoursByCategory[categoryHrs] >= 100 && - // hoursByCategory[categoryHrs] >= elem.totalHrs - // ) { - // let theBadge; - // for (let i = 0; i < badgesOfType.length; i += 1) { - // if (badgesOfType[i]._id.toString() === elem._id.toString()) { - // theBadge = badgesOfType[i]._id; - // break; - // } - // } - // if (theBadge) { - // // increaseBadgeCount(personId, mongoose.Types.ObjectId(theBadge)); - // return false; - // } - // if (badgeOfType) { - // if ( - // badgeOfType._id.toString() !== elem._id.toString() && - // badgeOfType.totalHrs < elem.totalHrs - // ) { - // replaceBadge( - // personId, - // mongoose.Types.ObjectId(badgeOfType._id), - // mongoose.Types.ObjectId(elem._id), - // ); - // } - // return false; - // } - // addBadge(personId, mongoose.Types.ObjectId(elem._id)); - // return false; - // } - // return true; - // }); - // }); - // }); - // }; + const removeBadge = async function (personId, badgeId) { + userProfile.updateOne( + { _id: personId }, + { + $pull: { + badgeCollection: { badge: badgeId }, + }, + }, + (err) => { + if (err) { + throw new Error(err); + } + }, + ); + }; const addBadgeCategory = async function (personId, newBadge, badgeCollection) { - console.log('adding new badge category --- ', newBadge.category); + // console.log('adding new badge category --- ', newBadge.category); // remove all badges of Type 'Total Hrs in Category' and category from the user - for (let i = 0; i < badgeCollection.length; i += 1) { if ( - badgeCollection[i].badge?.type === 'Total Hrs in Category' && - badgeCollection[i].badge?.category === newBadge.category + badgeCollection[i].type === 'Total Hrs in Category' && + badgeCollection[i].category === newBadge.category ) { - removeDupBadge(personId, badgeCollection[i].badge._id); + removeBadge(personId, badgeCollection[i]._id); + // console.log( + // `Badge removed category ${badgeCollection[i].category} and totalHrs ${badgeCollection[i].totalHrs}`, + // ); } } - // try { - // badgeCollection.forEach((badge) => { - // if ( - // badge.badge.type === 'Total Hrs in Category' && - // badge.badge.category === newBadge.category - // ) { - // removeDupBadge(personId, badge._id); - // } - // }); - // } catch (err) { - // logger.logException(err); - // } - // add the new badge addBadge(personId, newBadge._id); }; @@ -2066,10 +1977,6 @@ const userHelper = function () { const categoryHrsKey = Object.keys(hoursByCategory).find( (elem) => elem === category.toLowerCase(), ); - console.log( - `checing category -- ${category} with hours -- ${hoursByCategory[categoryHrsKey]}`, - ); - // console.log('\nbadgesOfType ----- ', badgesOfType[0]); // all badges of type 'Total Hrs in Category' in descending order of totalHrs await badge @@ -2078,29 +1985,19 @@ const userHelper = function () { .then((results) => { // loop results to check if user has earned the badge - // eslint-disable-next-line guard-for-in for (let i = 0; i < results.length; i += 1) { const badge = results[i]; - // console.log( - // 'hoursByCategory[categoryHrsKey] >= badge.totalHrs --- ', - // hoursByCategory[categoryHrsKey] >= badge.totalHrs, - // ); - // console.log('category, badge hrs --- ', categoryHrsKey, badge.totalHrs); - // console.log('\n'); + if (hoursByCategory[categoryHrsKey] >= badge.totalHrs) { - console.log('if condition -- criteria met'); // if condition -- criteria met const badgeIndex = badgesOfType.findIndex( (badgeItem) => badgeItem._id.toString() === badge._id.toString(), ); // user has not earned this badge - // check if user has earned any other badge of same type if (badgeIndex === -1) { - console.log('adding badge --- ', badge); addBadgeCategory(personId, badge, badgesOfType); - console.log('Badge added'); - break; } + break; } } }); From 14eddc350d532fed42108442876d79eacb37687d Mon Sep 17 00:00:00 2001 From: ArtemisNyx3 Date: Sun, 17 Nov 2024 01:10:06 -0500 Subject: [PATCH 3/3] hotfix update all users and cronjob schedule updated to standard --- src/cronjobs/userProfileJobs.js | 5 ++--- src/helpers/userHelper.js | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/cronjobs/userProfileJobs.js b/src/cronjobs/userProfileJobs.js index b64610b66..1add019b9 100644 --- a/src/cronjobs/userProfileJobs.js +++ b/src/cronjobs/userProfileJobs.js @@ -6,9 +6,8 @@ const userhelper = require('../helpers/userHelper')(); const userProfileJobs = () => { console.log('User Profile Jobs Started'); const allUserProfileJobs = new CronJob( - // '*/5 * * * *', // Comment out for testing. Run Every 5 minutes. - '* * * * *', // Comment out for testing. Run Every minute. - // '1 0 * * 0', // Every Sunday, 1 minute past midnight. + // '* * * * *', // Comment out for testing. Run Every minute. + '1 0 * * 0', // Every Sunday, 1 minute past midnight. async () => { const SUNDAY = 0; // will change back to 0 after fix diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 986283c98..1939df030 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -2006,11 +2006,11 @@ const userHelper = function () { const awardNewBadges = async () => { try { - // const users = await userProfile.find({ isActive: true }).populate('badgeCollection.badge'); - // for testing do 1 user with id = 672f294cccc3cc90b8215e17 - const users = await userProfile - .find({ _id: '672f294cccc3cc90b8215e17' }) - .populate('badgeCollection.badge'); + const users = await userProfile.find({ isActive: true }).populate('badgeCollection.badge'); + // for testing do 1 user with id = x + // const users = await userProfile + // .find({ _id: 'x' }) + // .populate('badgeCollection.badge'); for (let i = 0; i < users.length; i += 1) { const user = users[i]; const { _id, badgeCollection } = user;