Skip to content

Commit

Permalink
Merge pull request #982 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.77]
  • Loading branch information
one-community authored Jun 8, 2024
2 parents 88b804f + 34bf81e commit 4d4f1fc
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,44 @@ const userHelper = function () {
}
});
};

const getAllWeeksData = async (personId, user) => {
const userId = mongoose.Types.ObjectId(personId);
const weeksData = [];
const currentDate = moment().tz('America/Los_Angeles');
const startDate = moment(user.createdDate).tz('America/Los_Angeles');
const numWeeks = Math.ceil((currentDate.diff(startDate, 'days') / 7));

// iterate through weeks to get hours of each week
for (let week = 1; week <= numWeeks; week += 1) {
const pdtstart = startDate.clone().add(week - 1, 'weeks').startOf('week').format('YYYY-MM-DD');
const pdtend = startDate.clone().add(week, 'weeks').subtract(1, 'days').format('YYYY-MM-DD');
try {
const results = await dashboardHelper.laborthisweek(userId,pdtstart,pdtend,);
const { timeSpent_hrs: timeSpent } = results[0];
weeksData.push(timeSpent);
} catch (error) {
console.error(error);
throw error;
}
}
return weeksData;
};

const getMaxHrs = async (personId, user) => {
const weeksdata = await getAllWeeksData(personId, user);
return Math.max(...weeksdata);
};

const updatePersonalMax = async (personId, user) => {
try {
const MaxHrs = await getMaxHrs(personId, user);
user.personalBestMaxHrs = MaxHrs;
await user.save();
} catch (error) {
console.error(error);
}
};

// 'Personal Max',
const checkPersonalMax = async function (personId, user, badgeCollection) {
Expand All @@ -1422,17 +1460,20 @@ const userHelper = function () {
}
}
await badge.findOne({ type: 'Personal Max' }).then((results) => {
const currentDate = moment(moment().format("MM-DD-YYYY"), "MM-DD-YYYY").tz("America/Los_Angeles").format("MMM-DD-YY");
if (
user.lastWeekTangibleHrs &&
user.lastWeekTangibleHrs >= 1 &&
user.lastWeekTangibleHrs === user.personalBestMaxHrs
user.lastWeekTangibleHrs >= user.personalBestMaxHrs &&
!badgeOfType.earnedDate.includes(currentDate)

) {
if (badgeOfType) {
changeBadgeCount(
increaseBadgeCount(
personId,
mongoose.Types.ObjectId(badgeOfType._id),
user.personalBestMaxHrs,
mongoose.Types.ObjectId(badgeOfType.badge._id),
);
// Update the earnedDate array with the new date
badgeOfType.earnedDate.unshift(moment().format("MMM-DD-YYYY"));
} else {
addBadge(personId, mongoose.Types.ObjectId(results._id), user.personalBestMaxHrs);
}
Expand Down Expand Up @@ -1746,7 +1787,8 @@ const userHelper = function () {
const user = users[i];
const { _id, badgeCollection } = user;
const personId = mongoose.Types.ObjectId(_id);


await updatePersonalMax(personId, user);
await checkPersonalMax(personId, user, badgeCollection);
await checkMostHrsWeek(personId, user, badgeCollection);
await checkMinHoursMultiple(personId, user, badgeCollection);
Expand Down

0 comments on commit 4d4f1fc

Please sign in to comment.