Skip to content

Commit

Permalink
Merge pull request #807 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.55]
  • Loading branch information
one-community authored Mar 22, 2024
2 parents 5eb490a + 200e358 commit 2c3087c
Show file tree
Hide file tree
Showing 44 changed files with 372 additions and 322 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/controllers/actionItemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const actionItemController = function (ActionItem) {

_actionItem.save()
.then(res.status(200).send('Saved'))
.catch(error => res.status(400).send(error));
.catch((error) => res.status(400).send(error));
};

return {
Expand Down
119 changes: 64 additions & 55 deletions src/controllers/badgeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,18 @@ const badgeController = function (Badge) {
cache.setCache('allBadges', results);
res.status(200).send(results);
})
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
};

/**
* 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 Expand Up @@ -166,7 +175,7 @@ const badgeController = function (Badge) {
}
res.status(201).send(results);
})
.catch(errors => res.status(500).send(errors));
.catch((errors) => res.status(500).send(errors));
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/bmdashboard/bmConsumableController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const bmConsumableController = function (BuildingConsumable) {
.then((result) => {
res.status(200).send(result);
})
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down
16 changes: 8 additions & 8 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
MatType
.find()
.exec()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand All @@ -38,8 +38,8 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
ToolType
.find()
.exec()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down Expand Up @@ -129,7 +129,7 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
});
}
})
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (error) {
res.status(500).send(error);
}
Expand All @@ -153,8 +153,8 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
SelectedType
.find()
.exec()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
});
}
})
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (error) {
res.status(500).send(error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const bmMaterialsController = function (BuildingMaterial) {
)
.exec()
.then(() => res.status(201).send())
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (error) {
res.status(500).send(error);
}
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/bmdashboard/bmNewLessonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const bmNewLessonController = function (BuildingNewLesson) {
BuildingNewLesson
.find()
.populate()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};
const bmPostLessonList = async (req, res) => {
try {
const newLesson = BuildingNewLesson.create(req.body)
.then(result => res.status(201).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(201).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down Expand Up @@ -47,7 +47,7 @@ const bmNewLessonController = function (BuildingNewLesson) {
// Extract only allowed fields (content, tag, relatedProject and title)
const allowedFields = ['content', 'tags', 'relatedProject', 'title', 'allowedRoles', 'files'];
const filteredUpdateData = Object.keys(updateData)
.filter(key => allowedFields.includes(key))
.filter((key) => allowedFields.includes(key))
.reduce((obj, key) => {
obj[key] = updateData[key];
return obj;
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/bmdashboard/bmProjectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const bmMProjectController = function (BuildingProject) {
});
res.status(200).send(results);
})
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (err) {
res.status(500).send(err);
}
Expand All @@ -105,7 +105,7 @@ const bmMProjectController = function (BuildingProject) {
},
])
.exec()
.then(project => res.status(200).send(project))
.then((project) => res.status(200).send(project))
// TODO: uncomment this block to execute the auth check
// authenticate request by comparing userId param with buildingManager id field
// Note: _id has type object and must be converted to string
Expand All @@ -117,7 +117,7 @@ const bmMProjectController = function (BuildingProject) {
// }
// return res.status(200).send(project);
// })
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/bmdashboard/bmReusableController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');
const {
reusableType: ReusableType
reusableType: ReusableType,
} = require('../../models/bmdashboard/buildingInventoryType');

function isValidDate(dateString) {
Expand Down Expand Up @@ -41,7 +41,7 @@ const bmReusableController = function (BuildingReusable) {
.then((result) => {
res.status(200).send(result);
})
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down
Loading

0 comments on commit 2c3087c

Please sign in to comment.