summaryrefslogtreecommitdiffstats
path: root/server/api/groups.js
diff options
context:
space:
mode:
authorUdo Walter2018-11-15 23:28:38 +0100
committerUdo Walter2018-11-15 23:28:38 +0100
commiteb28dc41376b41f6643bda3728adf6cede4e3299 (patch)
tree6450dd840ad407c6dc7f2a31e3eae792913be140 /server/api/groups.js
parent[groups] fix api method to get a group taking way to long with all=true (diff)
downloadbas-eb28dc41376b41f6643bda3728adf6cede4e3299.tar.gz
bas-eb28dc41376b41f6643bda3728adf6cede4e3299.tar.xz
bas-eb28dc41376b41f6643bda3728adf6cede4e3299.zip
[groups] API method getGroup: forgot to include clients with all=true
Diffstat (limited to 'server/api/groups.js')
-rw-r--r--server/api/groups.js22
1 files changed, 13 insertions, 9 deletions
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()