From eb28dc41376b41f6643bda3728adf6cede4e3299 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Thu, 15 Nov 2018 22:28:38 +0000 Subject: [groups] API method getGroup: forgot to include clients with all=true --- server/api/groups.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'server/api/groups.js') diff --git a/server/api/groups.js b/server/api/groups.js index 44516f7..ddaed18 100644 --- a/server/api/groups.js +++ b/server/api/groups.js @@ -2,16 +2,20 @@ var path = require('path') var db = require(path.join(__appdir, 'lib', 'sequelize')) -async function getAllSubgroups (groups, knownGroupIds = []) { - var subgroups = (await db.group.findAll({ - where: { '$parents.id$': groups.map(x => x.id) }, - include: ['parents', 'subgroups', 'clients'] - })).filter(subgroup => !knownGroupIds.includes(subgroup.id)) +async function getAllChildren (groups, knownGroupIds = []) { + groups = groups.filter(subgroup => !knownGroupIds.includes(subgroup.id)) + var groupIds = groups.map(g => g.id) + knownGroupIds = [...knownGroupIds, ...groupIds] + var [subgroups, clients] = await Promise.all([ + db.group.findAll({ where: { '$parents.id$': groupIds }, include: ['parents'] }), + db.client.findAll({ where: { '$groups.id$': groupIds }, include: ['groups'] }) + ]) if (subgroups.length > 0) { - var subSubgroups = await getAllSubgroups(subgroups, [...knownGroupIds, ...subgroups.map(g => g.id)]) - return [...subgroups, ...subSubgroups] + var subChildren = await getAllChildren(subgroups, knownGroupIds) + subgroups = [...subgroups, ...subChildren.subgroups] + clients = [...clients, ...subChildren.clients] } - return [] + return { subgroups, clients } } // GET Requests @@ -29,7 +33,7 @@ module.exports.get = { if (req.query.id > 0) { db.group.findOne({ where: { id: req.query.id }, include: ['parents', 'subgroups', 'clients'] }).then(async group => { if (group) { - if (all) res.send({ ...group.get({ plain: true }), subgroups: await getAllSubgroups(group.subgroups) }) + if (all) res.send({ ...group.get({ plain: true }), ...await getAllChildren([group]) }) else res.send(group) } else { res.status(404).end() -- cgit v1.2.3-55-g7522