-
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 5 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 | ||
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const db = require("../models") | ||
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. 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 |
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 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import axios from 'axios'; | ||
import React, { useEffect, useState } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import DashboardLayout from '../../layout/dashboardLayout/DashboardLayout' | ||
import { getApi, getCommunity, putApi } from '../../utils/apiFunc' | ||
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. there was already another function to get current community |
||
import './CommunityMemberAdmin.scss'; | ||
|
||
const ComMemberAdmin = () => { | ||
const [data, setData] = useState([]); | ||
const [success, setSuccess] = useState(null); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
// getting current community from localstorage | ||
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) => { | ||
// const {data} = await axios.put(`${process.env.REACT_APP_API_BASE_URL}/api/communities-users/${id}/community/${currentCommunity.id}`, | ||
// {role: "manager"}, config) | ||
const {data} = await putApi( | ||
dispatch, | ||
`${process.env.REACT_APP_API_BASE_URL}/api/communities-users/${id}/community/${currentCommunity.id}`, | ||
{role: 'manager'} | ||
) | ||
setSuccess(data) | ||
} | ||
|
||
return ( | ||
<DashboardLayout title={currentCommunity.name}> | ||
<div className="com-member-container"> | ||
<div className="com-member-header"> | ||
{ | ||
["firstName", "lastName", "email", "phone", "dateOfBirth", "role", "options"].map(el => { | ||
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. do manual lint fix |
||
return ( | ||
<p>{el}</p> | ||
) | ||
}) | ||
} | ||
</div> | ||
{ | ||
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. use table.jsx from main branch |
||
const {firstName, lastName, email, phone, dateOfBirth, role} = item | ||
return ( | ||
<div className="com-member-item"> | ||
<p>{firstName ? firstName : 'name'}</p> | ||
<p>{lastName ? lastName : 'lastname'}</p> | ||
<p>{email ? email : 'email'}</p> | ||
<p>{phone ? phone : 'phone'}</p> | ||
<p>{dateOfBirth ? dateOfBirth : 'dateOfBirth'}</p> | ||
<p>{role ? role : 'role'}</p> | ||
<p className="access-btn"><img src="/img/edit-icon.svg" onClick={() => allowAccess(item.id)} /></p> | ||
</div> | ||
) | ||
}) | ||
} | ||
</div> | ||
</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; | ||
} |
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