added endpoint for group ownership transfer

This commit is contained in:
leca 2024-11-18 13:34:39 +03:00
parent 947dc720a5
commit 63aaa30d92
6 changed files with 26 additions and 6 deletions

View File

@ -16,6 +16,7 @@
"group not an owner": "You are not an owner of this group!", "group not an owner": "You are not an owner of this group!",
"group not a member": "You are not a member 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!", "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 not found": "Such abstract product is not found!",
"abstract product invalid syntax": "Invalid syntax in one of user's parameters!", "abstract product invalid syntax": "Invalid syntax in one of user's parameters!",
"barcode not found": "Such barcode not found!", "barcode not found": "Such barcode not found!",

View File

@ -16,6 +16,7 @@
"group not an owner": "Вы не владелец этой группы!", "group not an owner": "Вы не владелец этой группы!",
"group not a member": "Вы не участник этой группы!", "group not a member": "Вы не участник этой группы!",
"name not specified": "Не указано новое имя группы!", "name not specified": "Не указано новое имя группы!",
"new owner not specified": "Не указан ID нового владельца группы!",
"abstract product not found": "Такой абстрактный продукт не найден!", "abstract product not found": "Такой абстрактный продукт не найден!",
"abstract product invalid syntax": "Неправильный синткасис в одном из параметров абстрактного продукта!", "abstract product invalid syntax": "Неправильный синткасис в одном из параметров абстрактного продукта!",
"barcode not found": "Такой штрихкод не найден!", "barcode not found": "Такой штрихкод не найден!",

View File

@ -87,6 +87,17 @@ class GroupController {
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); 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(); export default new GroupController();

View File

@ -24,7 +24,8 @@ const responses = {
id_not_found: "group id not found", id_not_found: "group id not found",
not_an_owner: "group not an owner", not_an_owner: "group not an owner",
not_a_member: "group not a member", 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: { abstractproducts: {
not_found: "abstract product not found", not_found: "abstract product not found",
@ -144,6 +145,8 @@ const getHTTPCode = (type) => {
return 403 return 403
case responses.groups.name_not_specified: case responses.groups.name_not_specified:
return 400 return 400
case responses.new_owner_not_specified:
return 400
case responses.abstractproducts.not_found: case responses.abstractproducts.not_found:
return 404 return 404

View File

@ -9,10 +9,10 @@ const GroupRouter = new Router();
GroupRouter.post('/create/:groupName', tryHandler(auth.authenticate), tryHandler(GroupController.create)); 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('/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.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('/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('/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('/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('/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('/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; export default GroupRouter;

View File

@ -41,6 +41,10 @@ class GroupService {
async rename(groupId, newName) { async rename(groupId, newName) {
await db.query("UPDATE groups SET name = $1 WHERE id = $2", [newName, groupId]).catch((e) => errorHandler(e, "groups"));; 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(); export default new GroupService();