-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
41bbb9c
30786be
249f11c
421343d
b9bb95d
b8cd916
e6fbf8d
a61e39c
b9e18da
bc6a177
cd597f4
b2d31b3
9a94711
3b98b1f
41b81e6
dd29d00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'], | ||
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) => { | ||
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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is not required can be used directly below in place of rest.datavalues |
||
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 }, { 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 } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
const permit = (role) => { | ||
return (req, res, next) => { | ||
if (checkRole(req, role)) { | ||
const db = require('../models') | ||
|
||
function checkRole (dbRole, roles) { | ||
return roles.some(el => el === dbRole) | ||
} | ||
|
||
// async function checkMemberRole(req, roles) { | ||
// const member = await db.CommunityUser.findOne({ where: { userId: req.user.id, communityId: req.params.id }, attributes: ['role'] }) | ||
// console.log(member.dataValues.role); | ||
// console.log(checkRole(member.dataValues.role, roles)) | ||
// if (checkRole(member.dataValues.role, roles)) { | ||
// return true; | ||
// } else { | ||
// return false; | ||
// } | ||
// } | ||
|
||
const permit = (roles) => { | ||
return async (req, res, next) => { | ||
// getting member role | ||
const member = await db.CommunityUser.findOne({ where: { userId: req.user.id, communityId: req.params.id }, attributes: ['role'] }) | ||
|
||
if (checkRole(member.dataValues.role, roles) || checkRole(req.user.role, roles)) { | ||
next() | ||
} else { | ||
res.json({ error: 'Sorry, You don\'t have permission' }) | ||
} | ||
} | ||
} | ||
|
||
function checkRole (req, role) { | ||
return role.some(el => el === req.user.role) | ||
} | ||
|
||
module.exports = permit |
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') | ||
} | ||
} |
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 permit = require('../middleware/permission') | ||
const router = require('express').Router() | ||
|
||
router.get('/', getCommunityUsers) | ||
router.post('/follow', protect, followCommunity) | ||
router.get('/community/:id', getAllMembers) | ||
router.get('/community/:id', protect, permit(['manager']), getAllMembers) | ||
router.get('/community/:id/search', searchMemberName) | ||
router.put('/:memberId/community/:id/', protect, permit(['manager']), updateMemberRole) | ||
// router.put('/:id', updateCommunityUsers); | ||
|
||
router.get('/community/:id/details', protect, permit(['manager']), getAllMemberDetails) | ||
module.exports = router |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import Table from '../../components/table/Table' | ||
import DashboardLayout from '../../layout/dashboardLayout/DashboardLayout' | ||
import { getApi, putApi } from '../../utils/apiFunc' | ||
import { getCommunity } from '../../utils/getCommunity' | ||
import './CommunityMemberAdmin.scss' | ||
|
||
const ComMemberAdmin = () => { | ||
const [data, setData] = useState([]) | ||
const [success, setSuccess] = useState(null) | ||
|
||
const dispatch = useDispatch() | ||
|
||
// fetching current community | ||
const currentCommunity = getCommunity() | ||
|
||
useEffect(() => { | ||
getMemberDetails() | ||
}, [success]) | ||
|
||
const getMemberDetails = async () => { | ||
const { data } = await getApi( | ||
dispatch, | ||
`${process.env.REACT_APP_API_BASE_URL}/api/communities-users/community/${currentCommunity.id}/details` | ||
) | ||
setData(data.results) | ||
} | ||
|
||
const allowAccess = async (id) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be able to switch between roles not always make manager |
||
const { data } = await putApi( | ||
dispatch, | ||
`${process.env.REACT_APP_API_BASE_URL}/api/communities-users/${id}/community/${currentCommunity.id}`, | ||
{ role: 'manager' } | ||
) | ||
setSuccess(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is also not reloading table so have to manually do refresh |
||
} | ||
|
||
return ( | ||
<DashboardLayout title={currentCommunity.name}> | ||
{data.length && <Table | ||
addSymbolNumber | ||
data={{ | ||
tblData: data, | ||
tblProperty: ['firstName', 'lastName', 'email', 'phone', 'dateOfBirth', 'role', 'options'], | ||
tblHeader: ['First Name', 'Last Name', 'Email', 'Phone', 'Date of Birth', 'Role', 'options'] | ||
}} | ||
options={ | ||
[ | ||
{ | ||
img: '/img/edit-icon.svg', | ||
action: allowAccess | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be list of roles for now lets take [manager, member, coach] and should show list which is not current role of member as dropdown |
||
} | ||
] | ||
} | ||
/>} | ||
</DashboardLayout> | ||
) | ||
} | ||
|
||
export default ComMemberAdmin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.com-member-container { | ||
width: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
color: #fff; | ||
} | ||
|
||
.com-member-header { | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
background: var(--primary-color); | ||
padding: 12px; | ||
text-align: center; | ||
|
||
p { | ||
flex: 1; | ||
} | ||
} | ||
|
||
.com-member-item { | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding: 8px; | ||
text-align: center; | ||
background: var(--dropdowns); | ||
|
||
p { | ||
flex: 1; | ||
} | ||
} | ||
|
||
.access-btn { | ||
cursor: pointer; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const getCommunity = () => { | ||
return localStorage.getItem('currentCommunity') | ||
? JSON.parse(localStorage.getItem('currentCommunity')) | ||
: null | ||
} |
There was a problem hiding this comment.
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