Skip to content

Commit

Permalink
Edited Permission Change Logs controller + router
Browse files Browse the repository at this point in the history
Edited controller and router related to PermissionChangeLog to handle
getting permissionChangeLogs from the database.
  • Loading branch information
jerryren527 committed Sep 21, 2023
1 parent c2cd1e5 commit d516ccc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
23 changes: 19 additions & 4 deletions src/controllers/permissionChangeLogsController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
const UserProfile = require('../models/userProfile');

const permissionChangeLogController = function (PermissionChangeLog) {

const getPermissionChangeLogs = async function (req, res) {
// Check if user is Owner here. Skipped for now.

const changeLogs = await PermissionChangeLog.find({})
console.log("πŸš€ ~ file: permissionChangeLogsController.js:8 ~ getPermissionChangeLogs ~ changeLogs:", changeLogs)
res.status(200).send(changeLogs)
try {
const userProfile = await UserProfile.findOne({ _id: req.params.userId }).exec()

if (userProfile) {
if (userProfile.role !== 'Owner') {
res.status(204).send([])
} else {
const changeLogs = await PermissionChangeLog.find({})
res.status(200).send(changeLogs)
}
} else {
res.status(403).send(`User (${req.params.userId}) not found.`)
}
} catch (err) {
console.error(err)
}
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/permissionChangeLogsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const routes = function (permissionChangeLog) {

const permissionChangeLogRouter = express.Router()

permissionChangeLogRouter.route("/permissionChangeLogs")
permissionChangeLogRouter.route("/permissionChangeLogs/:userId")
.get(controller.getPermissionChangeLogs)

return permissionChangeLogRouter
Expand Down
8 changes: 0 additions & 8 deletions src/utilities/logPermissionChangeByAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const findLatestRelatedLog = (roleId) => {
reject(err);
return;
}
console.log("πŸš€ ~ file: logPermissionChangeByAccount.js:20 ~ .exec ~ document:", document);
resolve(document);
});
})
Expand All @@ -36,15 +35,10 @@ const logPermissionChangeByAccount = async (requestBody) => {

// Find the latest log related to permission
const document = await findLatestRelatedLog(roleId)
console.log("πŸš€ ~ file: logPermissionChangeByAccount.js:36 ~ logPermissionChangeByAccount ~ document:", document)

if (document) {
// console.log(document);
console.log("πŸš€ ~ file: logPermissionChangeByAccount.js:29 ~ .exec ~ document:", document)
permissionsRemoved = document.permissions.filter(item => !(permissions.includes(item)))
console.log("πŸš€ ~ file: logPermissionChangeByAccount.js:31 ~ .exec ~ permissionsRemoved:", permissionsRemoved)
permissionsAdded = permissions.filter(item => !(document.permissions.includes(item)))
console.log("πŸš€ ~ file: logPermissionChangeByAccount.js:33 ~ .exec ~ permissionsAdded:", permissionsAdded)
} else {
// else this is the first permissions change log for this particular role
permissionsAdded = permissions
Expand All @@ -62,8 +56,6 @@ const logPermissionChangeByAccount = async (requestBody) => {
requestorRole: role,
requestorEmail: email,
})

console.log("πŸš€ ~ file: logPermissionChangeByAccount.js:46 ~ .exec ~ logEntry:", logEntry)

await logEntry.save()

Expand Down

0 comments on commit d516ccc

Please sign in to comment.