added endpoint for group leaving

This commit is contained in:
leca 2024-11-18 13:54:05 +03:00
parent 63aaa30d92
commit 010475bca2
6 changed files with 27 additions and 6 deletions

View File

@ -5,6 +5,7 @@
"user invalid syntax": "Invalid syntax in one of user's parameters!",
"user already in group": "User is already in group!",
"user not found": "User does not exists!",
"admin leave": "You are an admin of that group. You must transfer ownership of that group first!",
"username taken": "Username is taken!",
"username not found": "Such username not found!",
"username required": "Username is required!",

View File

@ -5,6 +5,7 @@
"user invalid syntax": "Неправильный синткасис в одном из параметров пользователя!",
"user already in group": "Пользователь уже в группе!",
"user not found": "Пользователь не существует!",
"admin leave": "Вы являетесь администратором группы. Перед выходом передайте владение группой!",
"username taken": "Имя пользователя занято!",
"username not found": "Такое имя пользователя не найдено!",
"username required": "Требуется имя пользователя!",

View File

@ -16,10 +16,7 @@ class GroupController {
let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
let group = await GroupService.create(groupName, user.login.id);
log.info(`New group with name ${groupName} was just created by user ${user.login.username}`);
await UserService.joinGroup(user.login.id, group.id);
// return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
return res.status(200).send(group.id.toString())
}
async join(req, res) {
@ -28,7 +25,21 @@ class GroupController {
let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
await UserService.joinGroup(user.login.id, groupId);
log.info(`User ${user.login.username} has just joined group with ID ${groupId}`);
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
}
async leave(req, res) {
let groupId = req.params.groupId;
let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
if (await GroupService.getAdminId(groupId) == user.login.id) {
let code = responseCodes.responses.user.admin_leave
return res.status(responseCodes.getHTTPCode(code)).send(translate(req.headers["accept-language"], code))
}
await UserService.leaveGroup(user.login.id, groupId);
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
}

View File

@ -7,7 +7,8 @@ const responses = {
duplicate: "user duplicate",
invalid_syntax: "user invalid syntax",
already_in_group: "user already in group",
not_found: "user not found"
not_found: "user not found",
admin_leave: "admin leave"
},
usernames: {
duplicate: "username taken",
@ -119,6 +120,8 @@ const getHTTPCode = (type) => {
return 400
case responses.user.not_found:
return 404
case responses.user.admin_leave:
return 409
case responses.usernames.duplicate:

View File

@ -8,6 +8,7 @@ const GroupRouter = new Router();
GroupRouter.post('/create/:groupName', tryHandler(auth.authenticate), tryHandler(GroupController.create));
GroupRouter.post('/join/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.requirePassword), tryHandler(auth.checkGroupPassword), tryHandler(GroupController.join));
GroupRouter.get('/leave/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.leave));
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('/byId/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getById));

View File

@ -31,7 +31,11 @@ class UserService {
async joinGroup(userId, groupId) {
if (await (this.isInGroup(userId, groupId))) throw new customError(`joinGroup user already in group`, responseCodes.responses.user.already_in_group);
await db.query("UPDATE Users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
await db.query("UPDATE users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
}
async leaveGroup(userId, groupId) {
await db.query("UPDATE users SET groups = array_remove(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
}
async changeUsername(userId, username) {