diff --git a/src/controllers/badgeController.js b/src/controllers/badgeController.js index 55c661b2c..84d9bccaf 100644 --- a/src/controllers/badgeController.js +++ b/src/controllers/badgeController.js @@ -19,7 +19,7 @@ const badgeController = function (Badge) { // }; const getAllBadges = async function (req, res) { - console.log(req.body.requestor); // Retain logging from development branch for debugging + // console.log(req.body.requestor); // Retain logging from development branch for debugging // Check if the user has any of the following permissions if ( @@ -29,7 +29,7 @@ const badgeController = function (Badge) { !(await helper.hasPermission(req.body.requestor, 'updateBadges')) && !(await helper.hasPermission(req.body.requestor, 'deleteBadges')) ) { - console.log('in if statement'); // Retain logging from development branch for debugging + // console.log('in if statement'); // Retain logging from development branch for debugging res.status(403).send('You are not authorized to view all badge data.'); return; } @@ -129,12 +129,10 @@ const badgeController = function (Badge) { 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) + grouped[badge].earnedDate = timestampArray.map((timestamp) => new Date(timestamp) .toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: '2-digit' }) .replace(/ /g, '-') - .replace(',', ''), - ); + .replace(',', '')); } } if (existingBadges[badge]) { diff --git a/src/controllers/bmdashboard/bmConsumableController.js b/src/controllers/bmdashboard/bmConsumableController.js index d501c5a47..ee44513ce 100644 --- a/src/controllers/bmdashboard/bmConsumableController.js +++ b/src/controllers/bmdashboard/bmConsumableController.js @@ -80,7 +80,12 @@ const bmConsumableController = function (BuildingConsumable) { const bmPostConsumableUpdateRecord = function (req, res) { const { - quantityUsed, quantityWasted, qtyUsedLogUnit, qtyWastedLogUnit, stockAvailable, consumable, + quantityUsed, + quantityWasted, + qtyUsedLogUnit, + qtyWastedLogUnit, + stockAvailable, + consumable, } = req.body; let unitsUsed = quantityUsed; let unitsWasted = quantityWasted; @@ -91,36 +96,48 @@ const bmConsumableController = function (BuildingConsumable) { if (quantityWasted >= 0 && qtyWastedLogUnit === 'percent') { unitsWasted = (stockAvailable / 100) * quantityWasted; } - if (unitsUsed > stockAvailable || unitsWasted > stockAvailable || (unitsUsed + unitsWasted) > stockAvailable) { - return res.status(500).send({ message: 'Please check the used and wasted stock values. Either individual values or their sum exceeds the total stock available.' }); - } if (unitsUsed < 0 || unitsWasted < 0) { - return res.status(500).send({ message: 'Please check the used and wasted stock values. Negative numbers are invalid.' }); + if ( + unitsUsed > stockAvailable || + unitsWasted > stockAvailable || + unitsUsed + unitsWasted > stockAvailable + ) { + return res + .status(500) + .send({ + message: + 'Please check the used and wasted stock values. Either individual values or their sum exceeds the total stock available.', + }); + } + if (unitsUsed < 0 || unitsWasted < 0) { + return res + .status(500) + .send({ + message: 'Please check the used and wasted stock values. Negative numbers are invalid.', + }); } + const newStockUsed = parseFloat((consumable.stockUsed + unitsUsed).toFixed(4)); + const newStockWasted = parseFloat((consumable.stockWasted + unitsWasted).toFixed(4)); + const newAvailable = parseFloat((stockAvailable - (unitsUsed + unitsWasted)).toFixed(4)); - const newStockUsed = parseFloat((consumable.stockUsed + unitsUsed).toFixed(4)); - const newStockWasted = parseFloat((consumable.stockWasted + unitsWasted).toFixed(4)); - const newAvailable = parseFloat((stockAvailable - (unitsUsed + unitsWasted)).toFixed(4)); - - BuildingConsumable.updateOne( - { _id: consumable._id }, - { - $set: { - stockUsed: newStockUsed, - stockWasted: newStockWasted, - stockAvailable: newAvailable, - }, - $push: { - updateRecord: { - date: req.body.date, - createdBy: req.body.requestor.requestorId, - quantityUsed: unitsUsed, - quantityWasted: unitsWasted, - }, + BuildingConsumable.updateOne( + { _id: consumable._id }, + { + $set: { + stockUsed: newStockUsed, + stockWasted: newStockWasted, + stockAvailable: newAvailable, + }, + $push: { + updateRecord: { + date: req.body.date, + createdBy: req.body.requestor.requestorId, + quantityUsed: unitsUsed, + quantityWasted: unitsWasted, }, - }, - ) + }, + ) .then((results) => { res.status(200).send(results); }) @@ -128,13 +145,13 @@ const bmConsumableController = function (BuildingConsumable) { console.log('error: ', error); res.status(500).send({ message: error }); }); -}; + }; return { fetchBMConsumables, bmPurchaseConsumables, - bmPostConsumableUpdateRecord + bmPostConsumableUpdateRecord, }; }; -module.exports = bmConsumableController; \ No newline at end of file +module.exports = bmConsumableController; diff --git a/src/controllers/bmdashboard/bmInventoryTypeController.js b/src/controllers/bmdashboard/bmInventoryTypeController.js index 175d948b4..d52ce6c77 100644 --- a/src/controllers/bmdashboard/bmInventoryTypeController.js +++ b/src/controllers/bmdashboard/bmInventoryTypeController.js @@ -32,39 +32,36 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp } const fetchToolTypes = async (req, res) => { - try { - ToolType - .find() + ToolType.find() .populate([ - { - path: 'available', - select: '_id code project', - populate: { - path: 'project', - select: '_id name' - } - }, - { - path: 'using', - select: '_id code project', - populate: { - path: 'project', - select: '_id name' - } - } + { + path: 'available', + select: '_id code project', + populate: { + path: 'project', + select: '_id name', + }, + }, + { + path: 'using', + select: '_id code project', + populate: { + path: 'project', + select: '_id name', + }, + }, ]) .exec() - .then(result => { + .then((result) => { res.status(200).send(result); }) - .catch(error => { - console.error("fetchToolTypes error: ", error); + .catch((error) => { + console.error('fetchToolTypes error: ', error); res.status(500).send(error); }); - } catch (err) { - console.log("error: ", err) + console.log('error: ', err); res.json(err); } }; @@ -269,7 +266,6 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp } } - async function fetchInventoryByType(req, res) { const { type } = req.params; let SelectedType = InvType; @@ -409,4 +405,4 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp }; } -module.exports = bmInventoryTypeController; \ No newline at end of file +module.exports = bmInventoryTypeController; diff --git a/src/controllers/reasonSchedulingController.js b/src/controllers/reasonSchedulingController.js index 67162abe1..a227e7f3e 100644 --- a/src/controllers/reasonSchedulingController.js +++ b/src/controllers/reasonSchedulingController.js @@ -395,4 +395,4 @@ module.exports = { getSingleReason, patchReason, deleteReason, -}; \ No newline at end of file +}; diff --git a/src/controllers/teamController.js b/src/controllers/teamController.js index 42d9d8d25..9ce719a8c 100644 --- a/src/controllers/teamController.js +++ b/src/controllers/teamController.js @@ -290,8 +290,6 @@ const teamcontroller = function (Team) { }); }; const updateTeamVisibility = async (req, res) => { - console.log('==============> 9 '); - const { visibility, teamId, userId } = req.body; try { diff --git a/src/controllers/userProfileController.js b/src/controllers/userProfileController.js index d89e26f41..04805be15 100644 --- a/src/controllers/userProfileController.js +++ b/src/controllers/userProfileController.js @@ -484,7 +484,6 @@ const userProfileController = function (UserProfile, Project) { let originalRecord = {}; if (PROTECTED_EMAIL_ACCOUNT.includes(record.email)) { originalRecord = objectUtils.deepCopyMongooseObjectWithLodash(record); - // console.log('originalRecord', originalRecord); } // validate userprofile pic @@ -1577,6 +1576,43 @@ const userProfileController = function (UserProfile, Project) { } }; + const toggleInvisibility = async function (req, res) { + const { userId } = req.params; + const { isVisible } = req.body; + + if (!mongoose.Types.ObjectId.isValid(userId)) { + res.status(400).send({ + error: 'Bad Request', + }); + return; + } + if (!(await hasPermission(req.body.requestor, 'toggleInvisibility'))) { + res.status(403).send('You are not authorized to change user visibility'); + return; + } + + cache.removeCache(`user-${userId}`); + UserProfile.findByIdAndUpdate(userId, { $set: { isVisible } }, (err, _) => { + if (err) { + return res.status(500).send(`Could not Find user with id ${userId}`); + } + // Check if there's a cache for all users and update it accordingly + const isUserInCache = cache.hasCache('allusers'); + if (isUserInCache) { + const allUserData = JSON.parse(cache.getCache('allusers')); + const userIdx = allUserData.findIndex((users) => users._id === userId); + const userData = allUserData[userIdx]; + userData.isVisible = isVisible; + allUserData.splice(userIdx, 1, userData); + cache.setCache('allusers', JSON.stringify(allUserData)); + } + + return res.status(200).send({ + message: 'User visibility updated successfully', + isVisible, + }); + })} + const addInfringements = async function (req, res) { if (!(await hasPermission(req.body.requestor, 'addInfringements'))) { res.status(403).send('You are not authorized to add blue square'); @@ -1690,7 +1726,6 @@ const userProfileController = function (UserProfile, Project) { return; } const { userId, blueSquareId } = req.params; - // console.log(userId, blueSquareId); UserProfile.findById(userId, async (err, record) => { if (err || !record) { @@ -1853,7 +1888,7 @@ const userProfileController = function (UserProfile, Project) { console.log(error) return res.status(500) } - } + }; return { postUserProfile, @@ -1877,6 +1912,7 @@ const userProfileController = function (UserProfile, Project) { getUserByFullName, changeUserRehireableStatus, authorizeUser, + toggleInvisibility, addInfringements, editInfringements, deleteInfringements, diff --git a/src/routes/userProfileRouter.js b/src/routes/userProfileRouter.js index 2d68d2da1..5babb9384 100644 --- a/src/routes/userProfileRouter.js +++ b/src/routes/userProfileRouter.js @@ -73,6 +73,10 @@ const routes = function (userProfile, project) { .route('/userProfile/:userId/rehireable') .patch(controller.changeUserRehireableStatus); + userProfileRouter + .route('/userProfile/:userId/toggleInvisibility') + .patch(controller.toggleInvisibility); + userProfileRouter .route('/userProfile/singleName/:singleName') .get(controller.getUserBySingleName); diff --git a/src/utilities/createInitialPermissions.js b/src/utilities/createInitialPermissions.js index 062679cdd..7344c6016 100644 --- a/src/utilities/createInitialPermissions.js +++ b/src/utilities/createInitialPermissions.js @@ -52,6 +52,7 @@ const permissionsRoles = [ 'changeUserRehireableStatus', 'updatePassword', 'deleteUserProfile', + 'toggleInvisibility', 'addInfringements', 'editInfringements', 'deleteInfringements', @@ -256,9 +257,9 @@ const permissionsRoles = [ 'seeUsersInDashboard', 'changeUserRehireableStatus', - + 'toggleInvisibility', + 'manageAdminLinks', 'removeUserFromTask', - 'editHeaderMessage', ], },