diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index c6fee8ab4..4816bd1e2 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -17,9 +17,10 @@ To test this backend PR you need to checkout the #XXX frontend PR. ## How to test: 1. check into current branch 2. do `npm install` and `...` to run this PR locally -3. log as admin user -4. go to dashboard→ Tasks→ task→… -5. verify function “A” (feel free to include screenshot here) +3. Clear site data/cache +4. log as admin user +5. go to dashboard→ Tasks→ task→… +6. verify function “A” (feel free to include screenshot here) ## Screenshots or videos of changes: diff --git a/src/cronjobs/userProfileJobs.js b/src/cronjobs/userProfileJobs.js index f3903c057..1e4c6392a 100644 --- a/src/cronjobs/userProfileJobs.js +++ b/src/cronjobs/userProfileJobs.js @@ -18,12 +18,12 @@ const userProfileJobs = () => { await userhelper.awardNewBadges(); await userhelper.reActivateUser(); await userhelper.deActivateUser(); + await userhelper.oneTimeLocationUpdate(); }, null, false, 'America/Los_Angeles', ); - allUserProfileJobs.start(); }; module.exports = userProfileJobs; diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 8bf04eb5f..1e349245c 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -105,8 +105,8 @@ const userHelper = function () {

Date Assigned: ${infringement.date}

Description: ${infringement.description}

Total Infringements: This is your ${moment - .localeData() - .ordinal(totalInfringements)} blue square of 5.

+ .localeData() + .ordinal(totalInfringements)} blue square of 5.

${final_paragraph}

Thank you,
One Community

`; @@ -197,8 +197,8 @@ const userHelper = function () {
Weekly Summary (for the week ending on ${moment(dueDate) - .tz("America/Los_Angeles") - .format("YYYY-MMM-DD")}): + .tz("America/Los_Angeles") + .format("YYYY-MMM-DD")}):
${summary} @@ -220,24 +220,24 @@ const userHelper = function () { Media URL: ${ mediaUrlLink || 'Not provided!' - } + }

${ weeklySummariesCount === 8 - ? `

Total Valid Weekly Summaries: ${weeklySummariesCount}

` - : `

Total Valid Weekly Summaries: ${ + ? `

Total Valid Weekly Summaries: ${weeklySummariesCount}

` + : `

Total Valid Weekly Summaries: ${ weeklySummariesCount || "No valid submissions yet!" - }

` + }

` } ${ hoursLogged >= weeklycommittedHours - ? `

Hours logged: ${hoursLogged.toFixed( - 2 - )} / ${weeklycommittedHours}

` - : `

Hours logged: ${hoursLogged.toFixed( - 2 - )} / ${weeklycommittedHours}

` + ? `

Hours logged: ${hoursLogged.toFixed( + 2 + )} / ${weeklycommittedHours}

` + : `

Hours logged: ${hoursLogged.toFixed( + 2 + )} / ${weeklycommittedHours}

` } ${weeklySummaryMessage}
`; @@ -298,6 +298,69 @@ const userHelper = function () { }) .catch((error) => logger.logException(error)); }; + async function wait(ms) { + return new Promise((r) => setTimeout(r, ms)); + } + const oneTimeLocationUpdate = async () => { + const users = await userProfile.find({}, '_id'); + + for (let i = 0; i < users.length; i += 1) { + const user = users[i]; + const person = await userProfile.findById(user._id); + if (!person.location.coords && !person.location.country && !person.location.city && !person.location.userProvided) { + const personLoc = person.location || ''; + let location; + if (personLoc) { + try { + const res = await fetch(`https://api.opencagedata.com/geocode/v1/json?key=d093a5e0eee34aea8043f4e6edb0e9f7&q=${encodeURIComponent(personLoc)}&pretty=1&limit=1`); + if (!res) { + throw new Error(); + } else { + const data = await res.json(); + location = { + userProvided: personLoc || '', + coords: { + lat: data.results[0].geometry.lat || '', + lng: data.results[0].geometry.lng || '', + }, + country: data.results[0].components.country || '', + city: data.results[0].components.city || '', + }; + } + } catch (err) { + console.log(err); + location = { + userProvided: personLoc, + coords: { + lat: 'err', + lng: '', + }, + country: '', + city: '', + } + } + } else { + location = { + userProvided: personLoc || '', + coords: { + lat: '', + lng: '', + }, + country: '', + city: '', + }; + } + await userProfile.findOneAndUpdate({ _id: user._id }, { + $set: { + location, + }, + }); + } + + await wait(2000); + } + }; + /** * This function is called by a cron job to do 3 things to all active users: @@ -936,7 +999,7 @@ const userHelper = function () { for (let i = 0; i < badgeCollection.length; i += 1) { if ( badgeCollection[i].badge?.type === - "X Hours for X Week Streak" && + "X Hours for X Week Streak" && badgeCollection[i].badge?.weeks === bdge.weeks && bdge.hrs === hrs && !removed @@ -1041,7 +1104,7 @@ const userHelper = function () { true ) ) >= - elem.months - 12 + elem.months - 12 ) { if (badgeOfType) { if (badgeOfType._id.toString() !== elem._id.toString()) { @@ -1092,9 +1155,9 @@ const userHelper = function () { ); return theBadge ? increaseBadgeCount( - personId, - mongoose.Types.ObjectId(theBadge._id) - ) + personId, + mongoose.Types.ObjectId(theBadge._id) + ) : addBadge(personId, mongoose.Types.ObjectId(elem._id)); } } @@ -1227,7 +1290,7 @@ const userHelper = function () { for (let i = 0; i < badgeCollection.length; i += 1) { if ( badgeCollection[i].badge?.type === - "X Hours for X Week Streak" && + "X Hours for X Week Streak" && badgeCollection[i].badge?.weeks === bdge.weeks ) { if ( @@ -1619,6 +1682,7 @@ const userHelper = function () { awardNewBadges, getTangibleHoursReportedThisWeekByUserId, deleteExpiredTokens, + oneTimeLocationUpdate, }; };