diff --git a/src/controllers/group.js b/src/controllers/group.js index 2d6593a..da439b5 100644 --- a/src/controllers/group.js +++ b/src/controllers/group.js @@ -46,7 +46,16 @@ class GroupController { let group = await GroupService.getByName(groupName); - return res.status(200).send(group.id.toString()); + return res.status(200).send(group["id"].toString()); + } + + async getUsersInGroup(req, res) { + + const groupId = req.params.groupId; + + let result = await GroupService.getUsersInGroup(groupId); + + return res.status(200).send(result); } } diff --git a/src/controllers/user.js b/src/controllers/user.js index 584ef8c..ef22565 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -1,7 +1,7 @@ import UserService from '../services/user.js'; import log from '../utils/log.js'; import bcrypt from 'bcrypt'; -import genToken from '../utils/jwt.js'; +import jwtutils from '../utils/jwt.js'; import AbstractProductService from '../services/abstractproduct.js'; import ProductService from '../services/product.js'; import translate from '../utils/translate.js'; @@ -27,7 +27,7 @@ class UserController { const user = await UserService.getByUsername(username); if (!bcrypt.compareSync(password, user.password)) throw new customError(`Wrong user password`, responseCodes.responses.passwords.invalid); - const token = genToken(user); + const token = jwtutils.genToken(user); return res.status(200).send(token); } @@ -43,7 +43,7 @@ class UserController { } async changeUsername(req, res) { - const userId = jwt.decode(req.headers.authorization.split(' ')[1]).login.id + const userId = jwtutils.getUserIdFromToken(req.headers.authorization.split(' ')[1]); const { username } = req.body; await UserService.changeUsername(userId, username); @@ -52,12 +52,20 @@ class UserController { } async changePassword(req, res) { - const userId = jwt.decode(req.headers.authorization.split(' ')[1]).login.id + const userId = jwtutils.getUserIdFromToken(req.headers.authorization.split(' ')[1]); const { password } = req.body; await UserService.changePassword(userId, password); return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)) } + + async getAllGroupsForUser(req, res) { + const userId = jwtutils.getUserIdFromToken(req.headers.authorization.split(' ')[1]); + + let result = await UserService.getAllGroupsForUser(userId); + + return res.status(200).send(result); + } } export default new UserController() \ No newline at end of file diff --git a/src/routers/group.js b/src/routers/group.js index 3147968..8de36dc 100644 --- a/src/routers/group.js +++ b/src/routers/group.js @@ -10,5 +10,5 @@ GroupRouter.post('/create/:groupName', tryHandler(auth.authenticate), tryHandler GroupRouter.post('/join/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.requirePassword), tryHandler(auth.checkGroupPassword), tryHandler(GroupController.join)); GroupRouter.post('/password/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.authorizeGroupOwner), tryHandler(auth.requirePassword), tryHandler(GroupController.updatePassword)); GroupRouter.get('/byName/:groupName', tryHandler(auth.authenticate), tryHandler(existance.groupNameExists), tryHandler(GroupController.getByName)) - +GroupRouter.get('/getUsers/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getUsersInGroup)) export default GroupRouter; \ No newline at end of file diff --git a/src/routers/user.js b/src/routers/user.js index 3f72e4d..8a91f39 100644 --- a/src/routers/user.js +++ b/src/routers/user.js @@ -10,5 +10,6 @@ UserRouter.post('/login', tryHandler(auth.requireUsername), tryHandler(auth.requ UserRouter.get('/synchronize/:groupId', tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(UserController.synchronize)); UserRouter.post('/changeUsername', tryHandler(auth.authenticate), tryHandler(auth.requireUsername), tryHandler(UserController.changeUsername)); UserRouter.post('/changePassword', tryHandler(auth.authenticate), tryHandler(auth.requirePassword), tryHandler(UserController.changePassword)); +UserRouter.get('/myGroups', tryHandler(auth.authenticate), tryHandler(UserController.getAllGroupsForUser)); export default UserRouter; \ No newline at end of file diff --git a/src/services/group.js b/src/services/group.js index c258254..3a4536d 100644 --- a/src/services/group.js +++ b/src/services/group.js @@ -29,10 +29,14 @@ class GroupService { } async getByName(name) { - let res = (await db.query("SELECT * FROM groups WHERE name = $1", [name])); + let res = (await db.query("SELECT id FROM groups WHERE name = $1", [name])); if (res.rowCount == 0) throw new customError(`getByName group not found`, responseCodes.responses.groups.name_not_found); return res.rows[0]; } + + async getUsersInGroup(groupId) { + return (await db.query("SELECT id FROM users WHERE $1 = ANY(groups)", [groupId])).rows.map((group) => group.id) + } }; export default new GroupService(); \ No newline at end of file diff --git a/src/services/user.js b/src/services/user.js index 7c903d5..545178a 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -39,6 +39,10 @@ class UserService { async changePassword(userId, password) { await db.query("UPDATE users SET password = $1 WHERE id = $2", [bcrypt.hashSync(password, 12), userId]) } + + async getAllGroupsForUser(userId) { + return (await db.query("SELECT groups FROM users WHERE id = $1", [userId])).rows[0].groups + } } export default new UserService(); \ No newline at end of file diff --git a/src/utils/jwt.js b/src/utils/jwt.js index 99902b7..bd2ead5 100644 --- a/src/utils/jwt.js +++ b/src/utils/jwt.js @@ -6,4 +6,8 @@ const genToken = (login) => { return jwt.sign(payload, config.secret, { expiresIn: "7d" }); }; -export default genToken; \ No newline at end of file +const getUserIdFromToken = (token) => { + return jwt.decode(token).login.id; +} + +export default { genToken, getUserIdFromToken }; \ No newline at end of file