Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix personal max record badge #1193

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
35 changes: 21 additions & 14 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ const userHelper = function () {
});
};

const getAllWeeksData = async (personId, user) => {
const getAllWeeksData = async (personId, user) => { // gets time entries values (week)
const userId = mongoose.Types.ObjectId(personId);
const weeksData = [];
const currentDate = moment().tz('America/Los_Angeles');
Expand All @@ -1548,16 +1548,21 @@ const userHelper = function () {
return weeksData;
};

const getMaxHrs = async (personId, user) => {
const weeksdata = await getAllWeeksData(personId, user);
return Math.max(...weeksdata);
};
function mergeArrays(array1, array2) {
// create a new array and merge array1 and array2
const tempArray = [...array1, ...array2];
return tempArray; // return new array
}

const updatePersonalMax = async (personId, user) => {
const updatePersonalMax = async (personId, user) => { //
try {
const MaxHrs = await getMaxHrs(personId, user);
user.personalBestMaxHrs = MaxHrs;
await user.save();
const weeksData = await getAllWeeksData(personId, user); // get `Time Entries` tangible hours
const savedHours = user.savedTangibleHrs;
const result = mergeArrays(savedHours, weeksData);
const MaxHrs = Math.max(...result);
user.personalBestMaxHrs = MaxHrs; // updates userProfile's personalBestMaxHrs with the max hours.
await user.save(); // this will update user.personalBestMaxHrs with the new max hours.

} catch (error) {
console.error(error);
}
Expand All @@ -1581,10 +1586,10 @@ const userHelper = function () {
await removeDupBadge(personId, b._id);
}
}

await updatePersonalMax(personId, user);
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');
const currentDate = moment().tz('America/Los_Angeles').format('MMM-DD-YY'); // fixed format
if (
user.lastWeekTangibleHrs &&
user.lastWeekTangibleHrs >= user.personalBestMaxHrs &&
Expand All @@ -1593,7 +1598,7 @@ const userHelper = function () {
if (badgeOfType) {
increaseBadgeCount(personId, mongoose.Types.ObjectId(badgeOfType.badge._id));
// Update the earnedDate array with the new date
badgeOfType.earnedDate.unshift(moment().format('MMM-DD-YYYY'));
badgeOfType.earnedDate.unshift(moment().format('MMM-DD-YYYY'));
} else {
addBadge(personId, mongoose.Types.ObjectId(results._id), user.personalBestMaxHrs);
}
Expand Down Expand Up @@ -1631,6 +1636,8 @@ const userHelper = function () {
}
};



// 'X Hours in one week',
const checkXHrsInOneWeek = async function (personId, user, badgeCollection) {
const badgesOfType = [];
Expand Down Expand Up @@ -2008,12 +2015,12 @@ const userHelper = function () {
const awardNewBadges = async () => {
try {
const users = await userProfile.find({ isActive: true }).populate('badgeCollection.badge');

for (let i = 0; i < users.length; i += 1) {
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
Loading