Skip to content

Commit

Permalink
Merge pull request #761 from OneCommunityGlobal/shengwei_update_code_…
Browse files Browse the repository at this point in the history
…logic_for_badge_assignment_and_update

Shengwei Hotfix PR703
  • Loading branch information
one-community authored Feb 23, 2024
2 parents 3cd6b92 + fca8a04 commit 34b6fb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
12 changes: 0 additions & 12 deletions src/controllers/badgeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ const badgeController = function (Badge) {
if (element.count < 1) {
throw new Error('Badge count should be greater than 0.');
}
if (element.count !== element.earnedDate.length) {
element.earnedDate = fillEarnedDateToMatchCount(
element.earnedDate,
element.count,
);
element.lastModified = Date.now();
logger.logInfo(
`Badge count and earned dates mismatched found. ${Date.now()} was generated for user ${userToBeAssigned}. Badge record ID ${
element._id
}; Badge Type ID ${element.badge}`,
);
}
return element;
});
} catch (err) {
Expand Down
22 changes: 13 additions & 9 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,18 +985,21 @@ const userHelper = function () {
if (!recordToUpdate) {
throw new Error('Badge not found');
}
// If the count is the same, do nothing
if (recordToUpdate.count === count) {
return;
}
const copyOfEarnedDate = recordToUpdate.earnedDate;
if (copyOfEarnedDate.length < count) {
// if the EarnedDate count is less than the new count, add a earned date to the end of the collection
while (copyOfEarnedDate.length < count) {
// Update: We refrain from automatically correcting the mismatch problem as we intend to preserve the original
// earned date even when a badge is deleted. This approach ensures that a record of badges earned is maintained,
// preventing oversight of any mismatches caused by bugs.
if (recordToUpdate.count < count) {
let dateToAdd = count - recordToUpdate.count;
// if the EarnedDate count is less than the new count, add a earned date to the end of the collection
while (dateToAdd > 0) {
copyOfEarnedDate.push(earnedDateBadge());
dateToAdd -= 1;
}
} else {
// if the EarnedDate count is greater than the new count, remove the oldest earned date of the collection until it matches the new count - 1
while (copyOfEarnedDate.length >= count) {
copyOfEarnedDate.shift();
}
copyOfEarnedDate.push(earnedDateBadge());
}
newEarnedDate = [...copyOfEarnedDate];
userProfile.updateOne(
Expand All @@ -1006,6 +1009,7 @@ const userHelper = function () {
'badgeCollection.$.count': count,
'badgeCollection.$.lastModified': Date.now().toString(),
'badgeCollection.$.earnedDate': newEarnedDate,
'badgeCollection.$.hasBadgeDeletionImpact': recordToUpdate.count > count, // badge deletion impact set to true if the new count is less than the old count
},
},
(err) => {
Expand Down
1 change: 1 addition & 0 deletions src/models/userProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const userProfileSchema = new Schema({
count: { type: Number, default: 0 },
earnedDate: { type: Array, default: [] },
lastModified: { type: Date, required: true, default: new Date()},
hasBadgeDeletionImpact: { type: Boolean, default: false },
featured: {
type: Boolean,
required: true,
Expand Down

0 comments on commit 34b6fb3

Please sign in to comment.