added rename endpoint for groups
This commit is contained in:
parent
7d98ba89c5
commit
947dc720a5
|
@ -15,6 +15,7 @@
|
|||
"group id not found": "Group with such ID not found!",
|
||||
"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!",
|
||||
"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!",
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
"group id not found": "Группы с таким ID не существует!",
|
||||
"group not an owner": "Вы не владелец этой группы!",
|
||||
"group not a member": "Вы не участник этой группы!",
|
||||
"name not specified": "Не указано новое имя группы!",
|
||||
"abstract product not found": "Такой абстрактный продукт не найден!",
|
||||
"abstract product invalid syntax": "Неправильный синткасис в одном из параметров абстрактного продукта!",
|
||||
"barcode not found": "Такой штрихкод не найден!",
|
||||
|
|
|
@ -5,6 +5,7 @@ import config from '../../config.json' with {type: "json"};
|
|||
import log from '../utils/log.js';
|
||||
import translate from '../utils/translate.js';
|
||||
import responseCodes from '../response/responseCodes.js';
|
||||
import customError from '../response/customError.js';
|
||||
|
||||
const TAG = "/controllers/group.js";
|
||||
|
||||
|
@ -75,6 +76,17 @@ class GroupController {
|
|||
|
||||
return res.status(200).send(result.toString())
|
||||
}
|
||||
|
||||
async rename(req, res) {
|
||||
const groupId = req.params.groupId;
|
||||
const name = req.body.name;
|
||||
|
||||
if (!name) throw new customError(`New group name is not specified`, responseCodes.responses.groups.name_not_specified);
|
||||
|
||||
await GroupService.rename(groupId, name);
|
||||
|
||||
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
|
||||
}
|
||||
}
|
||||
|
||||
export default new GroupController();
|
||||
|
|
|
@ -23,7 +23,8 @@ const responses = {
|
|||
name_not_found: "group name not found",
|
||||
id_not_found: "group id not found",
|
||||
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"
|
||||
},
|
||||
abstractproducts: {
|
||||
not_found: "abstract product not found",
|
||||
|
@ -141,6 +142,8 @@ const getHTTPCode = (type) => {
|
|||
return 403
|
||||
case responses.groups.not_a_member:
|
||||
return 403
|
||||
case responses.groups.name_not_specified:
|
||||
return 400
|
||||
|
||||
case responses.abstractproducts.not_found:
|
||||
return 404
|
||||
|
|
|
@ -13,5 +13,6 @@ GroupRouter.get('/byName/:groupName', tryHandler(auth.authenticate), tryHandler(
|
|||
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));
|
||||
|
||||
export default GroupRouter;
|
|
@ -37,6 +37,10 @@ class GroupService {
|
|||
async getUsersInGroup(groupId) {
|
||||
return (await db.query("SELECT id FROM users WHERE $1 = ANY(groups)", [groupId])).rows.map((group) => group.id)
|
||||
}
|
||||
|
||||
async rename(groupId, newName) {
|
||||
await db.query("UPDATE groups SET name = $1 WHERE id = $2", [newName, groupId]).catch((e) => errorHandler(e, "groups"));;
|
||||
}
|
||||
};
|
||||
|
||||
export default new GroupService();
|
Loading…
Reference in New Issue