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

Bailey new personal max backend #619

Merged
merged 5 commits into from
Jan 27, 2024
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
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
Loading