Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member role add (fixes: #405) #406

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Next Next commit
member-role
SujalLama committed Jul 28, 2021
commit 41bbb9c497cc3254a333abe26877245c4004aa11
52 changes: 50 additions & 2 deletions api/src/controllers/communityUserController.js
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ const getAllMembers = async (req, res) => {
const data = await db.CommunityUser.findAll(
{
where: { communityId: req.params.id, active: true },
attributes: ['userId'],
attributes: ['userId', "role"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linting issue

include: [{
model: db.User,
attributes: ['firstName']
@@ -101,6 +101,54 @@ const getAllMembers = async (req, res) => {
}
}

// @desc Get the community-users
// @route GET /api/community-users/community/:id/details
// @access Public

const getAllMemberDetails = async (req, res) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like getAllMemberDetails, getAllMembers, getCommunityUsers can be single function

try {
const data = await db.CommunityUser.findAll(
{
where: { communityId: req.params.id, active: true },
attributes: ['id', 'userId', 'role'],
include: [{
model: db.User,
attributes: ['firstName', 'lastName', 'email', "phone", "dateOfBirth"]
}],
required: true
}
)

// flattening the array to show only one object
const newArray = data.map(item => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could just be nested to after findall

const {userId, role, id} = item.dataValues
const {...rest} = item.user
return {id, userId, role, ...rest.dataValues}
}
)

res.json({
results: newArray
})
} catch (error) {
res.status(400).json({ error })
}
}

// @desc Update the community users
// @route PUT /api/community-users/:memberId/community/:id/
// @access Public

const updateMemberRole = async (req, res) => {
try {
const {role} = req.body
await db.CommunityUser.update({role: role}, {where: {id: parseInt(req.params.memberId)}})
res.json({message: 'Successfully role updated'})
} catch (error) {
res.status(400).json({error})
}
}

// @desc Search Name
// @route POST /api/news/community/:id/search
// @access Private
@@ -129,4 +177,4 @@ const searchMemberName = (req, res) => {
.catch(err => res.json({ error: err }).status(400))
}

module.exports = { getCommunityUsers, followCommunity, getAllMembers, searchMemberName }
module.exports = { getCommunityUsers, followCommunity, getAllMembers, searchMemberName, getAllMemberDetails, updateMemberRole }
15 changes: 15 additions & 0 deletions api/src/middleware/memberPermit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const db = require("../models")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to permission file so that we don't have lot of permission files


const memberPermit = (role) => {

return async (req, res, next) => {
const member = await db.CommunityUser.findOne({where: {userId: req.user.id, communityId: req.params.id}, attributes: ['role']});
if (member.dataValues.role === role) {
next()
} else {
res.json({ error: 'Sorry, You don\'t have permission' })
}
}
}

module.exports = memberPermit
14 changes: 14 additions & 0 deletions api/src/migrations/20210728085949-alter_community_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
queryInterface.addColumn('communities_users', 'role', {
type: Sequelize.STRING,
defaultValue: 'member'
})
},

down: async (queryInterface, Sequelize) => {
queryInterface.removeColumn('communities_users', 'role')
}
};
4 changes: 4 additions & 0 deletions api/src/models/communityUserModel.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,10 @@ module.exports = (sequelize, DataTypes) => {
active: {
type: DataTypes.INTEGER,
defaultValue: true
},
role: {
type: DataTypes.STRING,
defaultValue: 'manager'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value between model and migration seems to have conflict

}
},
{ timestamps: true }
8 changes: 5 additions & 3 deletions api/src/routes/communityUserRouter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { getCommunityUsers, followCommunity, getAllMembers, searchMemberName } = require('../controllers/communityUserController')
const { getCommunityUsers, followCommunity, getAllMembers, searchMemberName, getAllMemberDetails, updateMemberRole } = require('../controllers/communityUserController')
const protect = require('../middleware/authMiddleware')
const memberPermit = require('../middleware/memberPermit')
const router = require('express').Router()

router.get('/', getCommunityUsers)
router.post('/follow', protect, followCommunity)
router.get('/community/:id', getAllMembers)
router.get('/community/:id', protect, memberPermit('manager'), getAllMembers)
router.get('/community/:id/search', searchMemberName)
router.put('/:memberId/community/:id/', protect, memberPermit('manager'), updateMemberRole)
// router.put('/:id', updateCommunityUsers);

router.get('/community/:id/details', protect, memberPermit('manager'), getAllMemberDetails)
module.exports = router