Skip to content

Commit

Permalink
Merge pull request #666 from OneCommunityGlobal/Roberto_Fix_Assigning…
Browse files Browse the repository at this point in the history
…_Duplicate_Badges

Roberto fix assigning duplicate badges
  • Loading branch information
one-community authored Mar 16, 2024
2 parents fb79bcb + 72351ec commit a253daf
Showing 1 changed file with 62 additions and 53 deletions.
115 changes: 62 additions & 53 deletions src/controllers/badgeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,14 @@ const badgeController = function (Badge) {
};

/**
* Updated Date: 12/06/2023
* Updated By: Shengwei
* Updated Date: 12/17/2023
* Updated By: Roberto
* Function added:
* - Added data validation for earned date and badge count mismatch.
* - Added fillEarnedDateToMatchCount function to resolve earned date and badge count mismatch.
* - Refactored data validation for duplicate badge id.
* - Added data validation for badge count should greater than 0.
* - Added formatDate function to format date to MMM-DD-YY.
* - Added logic to combine duplicate badges into one with updated properties.
*/

const formatDate = () => {
const currentDate = new Date(Date.now());
return moment(currentDate).tz('America/Los_Angeles').format('MMM-DD-YY');
};

const fillEarnedDateToMatchCount = (earnedDate, count) => {
const result = [...earnedDate];
while (result.length < count) {
result.push(formatDate());
}
return result;
};

const assignBadges = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'assignBadges'))) {
res.status(403).send('You are not authorized to assign badges.');
Expand All @@ -79,48 +64,72 @@ const badgeController = function (Badge) {
res.status(400).send('Can not find the user to be assigned.');
return;
}
const badgeCounts = {};
let newBadgeCollection = [];
// This line is using the forEach function to group badges in the badgeCollection
// array in the request body.
// Validation: No duplicate badge id;
try {
newBadgeCollection = req.body.badgeCollection.map((element) => {
if (badgeCounts[element.badge]) {
throw new Error('Duplicate badges sent in.');
// res.status(500).send('Duplicate badges sent in.');
// return;

const badgeGroups = req.body.badgeCollection.reduce((grouped, item) => {
const { badge } = item;

if (typeof item.count !== 'number') {
item.count = Number(item.count);
if (Number.isNaN(item.count)) {
return grouped;
}
badgeCounts[element.badge] = element.count;
// Validation: count should be greater than 0
if (element.count < 1) {
throw new Error('Badge count should be greater than 0.');
}
// if count is 0, skip
if (item.count === 0) {
return grouped;
}

if (!grouped[badge]) {
// If the badge is not in the grouped object, add a new entry
grouped[badge] = {
count: item.count,
lastModified: item.lastModified ? item.lastModified : Date.now(),
featured: item.featured || false,
earnedDate: item.earnedDate,
};
} else {
// If the badge is already in the grouped object, update properties
grouped[badge].count += item.count;
grouped[badge].lastModified = Date.now();
grouped[badge].featured = grouped[badge].featured || item.featured || false;

// Combine and sort earnedDate arrays
if (Array.isArray(item.earnedDate)) {
const combinedEarnedDate = [...grouped[badge].earnedDate, ...item.earnedDate];
const timestampArray = combinedEarnedDate.map((date) => new Date(date).getTime());
timestampArray.sort((a, b) => a - b);
grouped[badge].earnedDate = timestampArray.map((timestamp) => new Date(timestamp).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: '2-digit' })
.replace(/ /g, '-')
.replace(',', ''));
}
return element;
});
} catch (err) {
res
.status(500)
.send(`Internal Error: Badge Collection. ${err.message}`);
return;
}
record.badgeCollection = newBadgeCollection;
}

return grouped;
}, {});

// Convert badgeGroups object to array
const badgeGroupsArray = Object.entries(badgeGroups).map(([badge, data]) => ({
badge,
count: data.count,
lastModified: data.lastModified,
featured: data.featured,
earnedDate: data.earnedDate,
}));

record.badgeCollection = badgeGroupsArray;

if (cache.hasCache(`user-${userToBeAssigned}`)) {
cache.removeCache(`user-${userToBeAssigned}`);
}
// Save Updated User Profile

record
.save()
.then((result) => {
// TO-DO - add user back to cache. For some reason, the saved records lead to badge img loading failure in frontend.
// cache.setCache(`user-${userToBeAssigned}`, JSON.stringify(result));
res.status(201).send(result._id);
})
.catch((err) => {
logger.logException(err);
res.status(500).send('Internal Error: Unable to save the record.');
});
.save()
.then((results) => {
res.status(201).send(results._id);
})
.catch((err) => {
res.status(500).send(`Internal Error: Badge Collection. ${err.message}`);
});
});
};

Expand Down

0 comments on commit a253daf

Please sign in to comment.