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

Anoushka fix most hrs week badge #1150

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1602,35 +1602,44 @@ const userHelper = function () {
};

// 'Most Hrs in Week'

const checkMostHrsWeek = async function (personId, user, badgeCollection) {
if (user.weeklycommittedHours > 0 && user.lastWeekTangibleHrs > user.weeklycommittedHours) {
const badgeOfType = badgeCollection
.filter((object) => object.badge.type === 'Most Hrs in Week')
.map((object) => object.badge);
await badge.findOne({ type: 'Most Hrs in Week' }).then((results) => {
userProfile
.aggregate([
{ $match: { isActive: true } },
{ $group: { _id: 1, maxHours: { $max: '$lastWeekTangibleHrs' } } },
])
.then((userResults) => {
if (badgeOfType.length > 1) {
removeDupBadge(user._id, badgeOfType[0]._id);
}
try {
if (user.weeklycommittedHours > 0 && user.lastWeekTangibleHrs > user.weeklycommittedHours) {
// Getting badge of type 'Most Hrs in Week'
const results = await badge.findOne({ type: 'Most Hrs in Week' });
if (!results) {
console.error('No badge found for type "Most Hrs in Week"');
return;
}

if (user.lastWeekTangibleHrs && user.lastWeekTangibleHrs >= userResults[0].maxHours) {
if (badgeOfType.length) {
increaseBadgeCount(personId, mongoose.Types.ObjectId(badgeOfType[0]._id));
} else {
addBadge(personId, mongoose.Types.ObjectId(results._id));
}
}
});
});
// Getting the max hours of all active users
const userResults = await userProfile.aggregate([
{ $match: { isActive: true } },
{ $group: { _id: null, maxHours: { $max: '$lastWeekTangibleHrs' } } },
]);

if (!userResults || userResults.length === 0) {
console.error('No user results found');
return;
}

const maxHours = userResults[0].maxHours;

if (user.lastWeekTangibleHrs && user.lastWeekTangibleHrs >= maxHours) {
const existingBadge = badgeCollection.find((object) => object.badge.type === 'Most Hrs in Week');
if (existingBadge) {
// console.log('Increasing badge count');
await increaseBadgeCount(personId, mongoose.Types.ObjectId(existingBadge.badge._id));
} else {
// console.log('Adding badge');
await addBadge(personId, mongoose.Types.ObjectId(results._id));
}
}
}
} catch (error) {
console.error('Error in checkMostHrsWeek:', error);
}
};

// 'X Hours in one week',
const checkXHrsInOneWeek = async function (personId, user, badgeCollection) {
const badgesOfType = [];
Expand Down Expand Up @@ -2007,7 +2016,7 @@ const userHelper = function () {

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 (let i = 0; i < users.length; i += 1) {
const user = users[i];
const { _id, badgeCollection } = user;
Expand Down
Loading