From 6adb48aeab1b36d3b7e3056c1b4297bb9abd1085 Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 31 Oct 2024 10:06:49 +0300 Subject: [PATCH] check for group name --- src/middlewares/existance.js | 12 +++++++++++- src/routers/group.js | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/middlewares/existance.js b/src/middlewares/existance.js index 2b95efb..73c2c88 100644 --- a/src/middlewares/existance.js +++ b/src/middlewares/existance.js @@ -11,6 +11,15 @@ const groupExists = async (req, res, next) => { next(); }; +const groupNameExists = async (req, res, next) => { + let groupName = req.params.groupName || req.body.groupName; + + let group = await GroupService.getByName(groupName); + + if (!group) throw new customError(`group does not exists`, responseCodes.responses.groups.name_not_found); + next(); +}; + const localIdExists = async (req, res, next) => { let localId = req.params.localId || req.body.localId; @@ -18,4 +27,5 @@ const localIdExists = async (req, res, next) => { next(); } -export default { groupExists, localIdExists } \ No newline at end of file + +export default { groupExists, localIdExists, groupNameExists } \ No newline at end of file diff --git a/src/routers/group.js b/src/routers/group.js index f486630..3147968 100644 --- a/src/routers/group.js +++ b/src/routers/group.js @@ -9,6 +9,6 @@ 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.groupExists), tryHandler(GroupController.getByName)) +GroupRouter.get('/byName/:groupName', tryHandler(auth.authenticate), tryHandler(existance.groupNameExists), tryHandler(GroupController.getByName)) export default GroupRouter; \ No newline at end of file