From 63aaa30d927a2ad7f6a8aec19fb01426e2d4e427 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 18 Nov 2024 13:34:39 +0300 Subject: [PATCH] added endpoint for group ownership transfer --- messages/en-US/msgs.json | 1 + messages/ru-RU/msgs.json | 1 + src/controllers/group.js | 11 +++++++++++ src/response/responseCodes.js | 5 ++++- src/routers/group.js | 10 +++++----- src/services/group.js | 4 ++++ 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/messages/en-US/msgs.json b/messages/en-US/msgs.json index df4060a..23f4878 100644 --- a/messages/en-US/msgs.json +++ b/messages/en-US/msgs.json @@ -16,6 +16,7 @@ "group not an owner": "You are not an owner of this group!", "group not a member": "You are not a member of this group!", "name not specified": "New name of a group is not specified!", + "new owner not specified": "ID of a new group owner is not specified!", "abstract product not found": "Such abstract product is not found!", "abstract product invalid syntax": "Invalid syntax in one of user's parameters!", "barcode not found": "Such barcode not found!", diff --git a/messages/ru-RU/msgs.json b/messages/ru-RU/msgs.json index 371d466..54494b2 100644 --- a/messages/ru-RU/msgs.json +++ b/messages/ru-RU/msgs.json @@ -16,6 +16,7 @@ "group not an owner": "Вы не владелец этой группы!", "group not a member": "Вы не участник этой группы!", "name not specified": "Не указано новое имя группы!", + "new owner not specified": "Не указан ID нового владельца группы!", "abstract product not found": "Такой абстрактный продукт не найден!", "abstract product invalid syntax": "Неправильный синткасис в одном из параметров абстрактного продукта!", "barcode not found": "Такой штрихкод не найден!", diff --git a/src/controllers/group.js b/src/controllers/group.js index 5d14179..30300f0 100644 --- a/src/controllers/group.js +++ b/src/controllers/group.js @@ -87,6 +87,17 @@ class GroupController { return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); } + + async transferOwnership(req, res) { + const groupId = req.params.groupId; + const userId = req.body.userId; + + if (!userId) throw new customError(`New owner id is not specified`, responseCodes.responses.groups.new_owner_not_specified); + + await GroupService.transferOwnership(groupId, userId); + + return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); + } } export default new GroupController(); diff --git a/src/response/responseCodes.js b/src/response/responseCodes.js index d2f8258..7600b2c 100644 --- a/src/response/responseCodes.js +++ b/src/response/responseCodes.js @@ -24,7 +24,8 @@ const responses = { id_not_found: "group id not found", not_an_owner: "group not an owner", not_a_member: "group not a member", - name_not_specified: "name not specified" + name_not_specified: "name not specified", + new_owner_not_specified: "new owner not specified" }, abstractproducts: { not_found: "abstract product not found", @@ -144,6 +145,8 @@ const getHTTPCode = (type) => { return 403 case responses.groups.name_not_specified: return 400 + case responses.new_owner_not_specified: + return 400 case responses.abstractproducts.not_found: return 404 diff --git a/src/routers/group.js b/src/routers/group.js index 3074df7..ff3c29c 100644 --- a/src/routers/group.js +++ b/src/routers/group.js @@ -9,10 +9,10 @@ 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.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)) -GroupRouter.get('/getUsers/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getUsersInGroup)) -GroupRouter.get('/adminId/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getAdminId)) +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)); +GroupRouter.get('/getUsers/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getUsersInGroup)); +GroupRouter.get('/adminId/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getAdminId)); GroupRouter.post('/rename/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.authorizeGroupOwner), tryHandler(GroupController.rename)); - +GroupRouter.post('/transferOwnership/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.authorizeGroupOwner), tryHandler(GroupController.transferOwnership)); export default GroupRouter; \ No newline at end of file diff --git a/src/services/group.js b/src/services/group.js index 27d79f8..4b7ad87 100644 --- a/src/services/group.js +++ b/src/services/group.js @@ -41,6 +41,10 @@ class GroupService { async rename(groupId, newName) { await db.query("UPDATE groups SET name = $1 WHERE id = $2", [newName, groupId]).catch((e) => errorHandler(e, "groups"));; } + + async transferOwnership(groupId, userId) { + await db.query("UPDATE groups SET admin_id = $1 WHERE id = $2", [userId, groupId]) + } }; export default new GroupService(); \ No newline at end of file