From 8270b5c6b35302611eedde263f09535c66bee626 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Sat, 4 Aug 2018 02:25:05 +0000 Subject: [server/groups] add 404 status code if group / client id not found --- server/api/groups.js | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) (limited to 'server/api/groups.js') diff --git a/server/api/groups.js b/server/api/groups.js index a41666a..526f3ee 100644 --- a/server/api/groups.js +++ b/server/api/groups.js @@ -28,7 +28,8 @@ module.exports.get = { // get name, description, parents, subgroups and clients of a group (by id) getGroup: function (req, res) { db.group.findOne({ where: { id: req.query.id }, include: ['parents', 'subgroups', 'clients'] }).then(group => { - res.send(group) + if (group) res.send(group) + else res.status(404).end() }) } } @@ -40,10 +41,12 @@ module.exports.post = { const id = req.body.id > 0 ? req.body.id : null if (id) { db.group.findOne({ where: { id } }).then(group => { - var promises = [] - if (req.body.info) promises.push([group.update(req.body.info)]) - if (req.body.parentIds) promises.push(group.setParents(req.body.parentIds)) - Promise.all(promises).then(() => { res.send({id}) }) + if (group) { + var promises = [] + if (req.body.info) promises.push([group.update(req.body.info)]) + if (req.body.parentIds) promises.push(group.setParents(req.body.parentIds)) + Promise.all(promises).then(() => { res.send({id}) }) + } else { res.status(404).end() } }) } else { db.group.create(req.body.info).then(group => { @@ -54,46 +57,42 @@ module.exports.post = { // delete groups delete: function (req, res) { - db.group.destroy({ where: { id: req.body.ids } }).then(() => { res.end() }) + db.group.destroy({ where: { id: req.body.ids } }).then(count => { res.end(count) }) }, // remove subgroups from a group removeSubgroups: function (req, res) { - const id = req.body.id > 0 ? req.body.id : null - if (id) { - db.group.findOne({ where: { id } }).then(group => { + db.group.findOne({ where: { id: req.body.id } }).then(group => { + if (group) { group.removeSubgroups(req.body.ids).then(() => { res.end() }) - }) - } else { res.status(404).end() } + } else { res.status(404).end() } + }) }, // remove clients from a group removeClients: function (req, res) { - const id = req.body.id > 0 ? req.body.id : null - if (id) { - db.group.findOne({ where: { id } }).then(group => { + db.group.findOne({ where: { id: req.body.id } }).then(group => { + if (group) { group.removeClients(req.body.ids).then(() => { res.end() }) - }) - } else { res.status(404).end() } + } else { res.status(404).end() } + }) }, // add subgroups to a group addSubgroups: function (req, res) { - const id = req.body.id > 0 ? req.body.id : null - if (id) { - db.group.findOne({ where: { id } }).then(group => { + db.group.findOne({ where: { id: req.body.id } }).then(group => { + if (group) { group.addSubgroups(req.body.ids).then(() => { res.end() }) - }) - } else { res.status(404).end() } + } else { res.status(404).end() } + }) }, // add clients to a group addClients: function (req, res) { - const id = req.body.id > 0 ? req.body.id : null - if (id) { - db.group.findOne({ where: { id } }).then(group => { + db.group.findOne({ where: { id: req.body.id } }).then(group => { + if (group) { group.addClients(req.body.ids).then(() => { res.end() }) - }) - } else { res.status(404).end() } + } else { res.status(404).end() } + }) } } -- cgit v1.2.3-55-g7522