From 010475bca2bff4c26891e78aab8871e568ea73da Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 18 Nov 2024 13:54:05 +0300 Subject: [PATCH] added endpoint for group leaving --- messages/en-US/msgs.json | 1 + messages/ru-RU/msgs.json | 1 + src/controllers/group.js | 19 +++++++++++++++---- src/response/responseCodes.js | 5 ++++- src/routers/group.js | 1 + src/services/user.js | 6 +++++- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/messages/en-US/msgs.json b/messages/en-US/msgs.json index 23f4878..fa217c0 100644 --- a/messages/en-US/msgs.json +++ b/messages/en-US/msgs.json @@ -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!", diff --git a/messages/ru-RU/msgs.json b/messages/ru-RU/msgs.json index 54494b2..ab852ba 100644 --- a/messages/ru-RU/msgs.json +++ b/messages/ru-RU/msgs.json @@ -5,6 +5,7 @@ "user invalid syntax": "Неправильный синткасис в одном из параметров пользователя!", "user already in group": "Пользователь уже в группе!", "user not found": "Пользователь не существует!", + "admin leave": "Вы являетесь администратором группы. Перед выходом передайте владение группой!", "username taken": "Имя пользователя занято!", "username not found": "Такое имя пользователя не найдено!", "username required": "Требуется имя пользователя!", diff --git a/src/controllers/group.js b/src/controllers/group.js index 30300f0..3aba1df 100644 --- a/src/controllers/group.js +++ b/src/controllers/group.js @@ -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)); } diff --git a/src/response/responseCodes.js b/src/response/responseCodes.js index 7600b2c..380abba 100644 --- a/src/response/responseCodes.js +++ b/src/response/responseCodes.js @@ -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: diff --git a/src/routers/group.js b/src/routers/group.js index ff3c29c..67dea72 100644 --- a/src/routers/group.js +++ b/src/routers/group.js @@ -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)); diff --git a/src/services/user.js b/src/services/user.js index af2be6d..e899b4e 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -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) {