added 2 new endpoints
This commit is contained in:
parent
07ffeb8de4
commit
317fba81aa
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
|
@ -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;
|
|
@ -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;
|
|
@ -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();
|
|
@ -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();
|
|
@ -6,4 +6,8 @@ const genToken = (login) => {
|
|||
return jwt.sign(payload, config.secret, { expiresIn: "7d" });
|
||||
};
|
||||
|
||||
export default genToken;
|
||||
const getUserIdFromToken = (token) => {
|
||||
return jwt.decode(token).login.id;
|
||||
}
|
||||
|
||||
export default { genToken, getUserIdFromToken };
|
Loading…
Reference in New Issue