Skip to content

Commit

Permalink
Merge pull request #619 from OneCommunityGlobal/Bailey-new-personalMa…
Browse files Browse the repository at this point in the history
…x-backend

Bailey new personal max backend
  • Loading branch information
one-community authored Jan 27, 2024
2 parents f379f27 + 830d23b commit 560c57a
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,11 @@ const userHelper = function () {
personId,
{
$pull: {
badgeCollection: { badge: badgeId },
},
badgeCollection: { _id: mongoose.Types.ObjectId(badgeId) }
}
},
(err) => {
{ new: true },
err => {
if (err) {
throw new Error(err);
}
Expand Down Expand Up @@ -1107,31 +1108,37 @@ const changeBadgeCount = async function (personId, badgeId, count) {
// 'Personal Max',
const checkPersonalMax = async function (personId, user, badgeCollection) {
let badgeOfType;
let duplicateBadges = [];

for (let i = 0; i < badgeCollection.length; i += 1) {
if (badgeCollection[i].badge?.type === 'Personal Max') {
if (badgeOfType) {
removeDupBadge(personId, badgeOfType._id);
if (badgeCollection[i].badge?.type === "Personal Max") {
if (!badgeOfType) {
badgeOfType = badgeCollection[i];
} else {
duplicateBadges.push(badgeCollection[i]);
}
}
for (let badge of duplicateBadges) {
await removeDupBadge(personId, badge._id);
}
}
await badge.findOne({ type: 'Personal Max' }).then((results) => {
if (
user.lastWeekTangibleHrs
&& user.lastWeekTangibleHrs >= 1
&& user.lastWeekTangibleHrs === user.personalBestMaxHrs
) {

user.lastWeekTangibleHrs &&
user.lastWeekTangibleHrs >= 1 &&
user.lastWeekTangibleHrs === user.personalBestMaxHrs
)
{
if (badgeOfType) {
changeBadgeCount(
personId,
mongoose.Types.ObjectId(badgeOfType._id),
user.personalBestMaxHrs,
);
} else {
addBadge(
personId,
mongoose.Types.ObjectId(results._id),
user.personalBestMaxHrs,
);
addBadge(personId, mongoose.Types.ObjectId(results._id), user.personalBestMaxHrs);

}
}
});
Expand Down Expand Up @@ -1489,17 +1496,13 @@ const changeBadgeCount = async function (personId, badgeId, count) {
};

const awardNewBadges = async () => {
console.log('Awarding');
try {
// This will be used in production to run task on all users
const users = await userProfile
.find({ isActive: true })
.populate('badgeCollection.badge');

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 checkPersonalMax(personId, user, badgeCollection);
await checkMostHrsWeek(personId, user, badgeCollection);
await checkMinHoursMultiple(personId, user, badgeCollection);
Expand All @@ -1519,15 +1522,10 @@ const changeBadgeCount = async function (personId, badgeId, count) {

const getTangibleHoursReportedThisWeekByUserId = function (personId) {
const userId = mongoose.Types.ObjectId(personId);

const pdtstart = moment().tz('America/Los_Angeles').startOf('week').format('YYYY-MM-DD');
const pdtend = moment().tz('America/Los_Angeles').endOf('week').format('YYYY-MM-DD');

const pdtstart = moment()
.tz('America/Los_Angeles')
.startOf('week')
.format('YYYY-MM-DD');
const pdtend = moment()
.tz('America/Los_Angeles')
.endOf('week')
.format('YYYY-MM-DD');

return timeEntries
.find(
Expand Down

0 comments on commit 560c57a

Please sign in to comment.