Skip to content

Commit

Permalink
Merge pull request #759 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.47]
  • Loading branch information
one-community authored Feb 21, 2024
2 parents cf46c08 + 5db3d27 commit e2cfdb9
Show file tree
Hide file tree
Showing 78 changed files with 969 additions and 920 deletions.
21 changes: 15 additions & 6 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/node": "^8.10.61",
"eslint": "^8.47.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-import-resolver-babel-module": "^5.3.1",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-jsx-a11y": "^6.7.1",
Expand Down
5 changes: 1 addition & 4 deletions src/controllers/actionItemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const actionItemController = function (ActionItem) {
_actionItem.assignedTo = req.body.assignedTo;
_actionItem.createdBy = req.body.requestor.requestorId;


_actionItem.save()
.then((result) => {
notificationhelper.notificationcreated(requestorId, assignedTo, _actionItem.description);
Expand All @@ -58,7 +57,6 @@ const actionItemController = function (ActionItem) {
const deleteactionItem = async function (req, res) {
const actionItemId = mongoose.Types.ObjectId(req.params.actionItemId);


const _actionItem = await ActionItem.findById(actionItemId)
.catch((error) => {
res.status(400).send(error);
Expand Down Expand Up @@ -109,10 +107,9 @@ 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 {
getactionItem,
postactionItem,
Expand Down
73 changes: 38 additions & 35 deletions src/controllers/badgeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ const cache = require('../utilities/nodeCache')();
const logger = require('../startup/logger');

const badgeController = function (Badge) {
/**
* getAllBadges handles badges retrieval.
* @param {Object} req - Request object.
* @returns {Array<Object>} List containing badge records.
*/
const getAllBadges = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'seeBadges'))) {
res.status(403).send('You are not authorized to view all badge data.');
return;
}
// Add cache to reduce database query and optimize performance
if (cache.hasCache('allBadges')) {
res.status(200).send(cache.getCache('allBadges'));
return;
}

Badge.find(
{},
Expand All @@ -25,16 +35,19 @@ const badgeController = function (Badge) {
ranking: 1,
badgeName: 1,
})
.then(results => res.status(200).send(results))
.catch(error => res.status(404).send(error));
.then((results) => {
cache.setCache('allBadges', results);
res.status(200).send(results);
})
.catch((error) => res.status(500).send(error));
};

/**
* Updated Date: 12/06/2023
* Updated Date: 01/12/2024
* Updated By: Shengwei
* Function added:
* - Added data validation for earned date and badge count mismatch.
* - Added fillEarnedDateToMatchCount function to resolve earned date and badge count mismatch.
* - Added fillEarnedDateToMatchCount function to resolve earned date and badge count mismatch. (Deleted due to new requirement)
* - 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.
Expand All @@ -45,14 +58,6 @@ const badgeController = function (Badge) {
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 @@ -75,32 +80,16 @@ const badgeController = function (Badge) {
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;
}
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 (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) {
res
.status(500)
.send(`Internal Error: Badge Collection. ${err.message}`);
logger.logException(`Internal Error: Badge Collection. ${err.message} User ID: ${userToBeAssigned} Badge Collection: ${JSON.stringify(req.body.badgeCollection)}`);
res.status(500).send(`Internal Error: Badge Collection. ${ err.message}`);
return;
}
record.badgeCollection = newBadgeCollection;
Expand Down Expand Up @@ -158,8 +147,14 @@ const badgeController = function (Badge) {

badge
.save()
.then(results => res.status(201).send(results))
.catch(errors => res.status(500).send(errors));
.then((results) => {
// remove cache after new badge is saved
if (cache.getCache('allBadges')) {
cache.removeCache('allBadges');
}
res.status(201).send(results);
})
.catch((errors) => res.status(500).send(errors));
});
};

Expand All @@ -183,11 +178,15 @@ const badgeController = function (Badge) {
const deleteRecord = record.remove();

Promise.all([removeBadgeFromProfile, deleteRecord])
.then(
.then(() => {
// remove cache after new badge is deleted
if (cache.getCache('allBadges')) {
cache.removeCache('allBadges');
}
res.status(200).send({
message: 'Badge successfully deleted and user profiles updated',
}),
)
});
})
.catch((errors) => {
res.status(500).send(errors);
});
Expand Down Expand Up @@ -234,6 +233,10 @@ const badgeController = function (Badge) {
res.status(400).send({ error: 'No valid records found' });
return;
}
// remove cache after new badge is updated
if (cache.getCache('allBadges')) {
cache.removeCache('allBadges');
}
res.status(200).send({ message: 'Badge successfully updated' });
});
};
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
17 changes: 8 additions & 9 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 @@ -26,8 +26,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 @@ -55,7 +55,6 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
}
};


async function addMaterialType(req, res) {
const {
name,
Expand Down Expand Up @@ -118,7 +117,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 @@ -142,8 +141,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 @@ -182,7 +181,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
14 changes: 7 additions & 7 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const bmMaterialsController = function (BuildingMaterial) {
},
])
.exec()
.then(results => res.status(200).send(results))
.catch(error => res.status(500).send(error));
.then((results) => res.status(200).send(results))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ const bmMaterialsController = function (BuildingMaterial) {
BuildingMaterial
.create(newDoc)
.then(() => res.status(201).send())
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
return;
}
BuildingMaterial
Expand All @@ -84,7 +84,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 Expand Up @@ -132,7 +132,7 @@ const bmMaterialsController = function (BuildingMaterial) {

)
.then((results) => { res.status(200).send(results); })
.catch(error => res.status(500).send({ message: error }));
.catch((error) => res.status(500).send({ message: error }));
}
};

Expand Down Expand Up @@ -183,7 +183,7 @@ const bmMaterialsController = function (BuildingMaterial) {
res.status(500).send('Stock quantities submitted seems to be invalid');
return;
}
const updatePromises = updateRecordsToBeAdded.map(updateItem => BuildingMaterial.updateOne(
const updatePromises = updateRecordsToBeAdded.map((updateItem) => BuildingMaterial.updateOne(
{ _id: updateItem.updateId },
{
$set: updateItem.set,
Expand All @@ -194,7 +194,7 @@ const bmMaterialsController = function (BuildingMaterial) {
.then((results) => {
res.status(200).send({ result: `Successfully posted log for ${results.length} Material records.` });
})
.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 e2cfdb9

Please sign in to comment.