summaryrefslogtreecommitdiffstats
path: root/server/lib/grouphelper.js
diff options
context:
space:
mode:
authorUdo Walter2019-04-15 16:37:33 +0200
committerUdo Walter2019-04-15 16:37:33 +0200
commit95ae9a051d7cab018a4ccfef4cd6140000076bb8 (patch)
tree8684430ee58ab9bb5022019301f80e8aeca9a4a8 /server/lib/grouphelper.js
parent[configurator] add ability to mark a config as default (diff)
downloadbas-95ae9a051d7cab018a4ccfef4cd6140000076bb8.tar.gz
bas-95ae9a051d7cab018a4ccfef4cd6140000076bb8.tar.xz
bas-95ae9a051d7cab018a4ccfef4cd6140000076bb8.zip
[server] grouputils -> grouphelper
Diffstat (limited to 'server/lib/grouphelper.js')
-rw-r--r--server/lib/grouphelper.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/server/lib/grouphelper.js b/server/lib/grouphelper.js
new file mode 100644
index 0000000..417c1d1
--- /dev/null
+++ b/server/lib/grouphelper.js
@@ -0,0 +1,43 @@
+/* global __appdir */
+var path = require('path')
+var db = require(path.join(__appdir, 'lib', 'sequelize'))
+
+async function getAllChildren (groups, groupBlacklist, clientBlacklist) {
+ const knownGroupMap = {}
+ const knownClientMap = {}
+ if (groupBlacklist) groupBlacklist.forEach(group => {
+ knownGroupMap[group.id] = true
+ })
+ if (clientBlacklist) clientBlacklist.forEach(client => {
+ knownGroupMap[client.id] = true
+ })
+ return getAllChildrenByIds(groups.map(x => x.id), {}, {})
+}
+
+async function getAllChildrenByIds (groupIds, knownGroupMap, knownClientMap) {
+ let [subgroups, clients] = await Promise.all([
+ db.group.findAll({ where: { '$parents.id$': groupIds }, include: ['parents'] }),
+ db.client.findAll({ where: { '$groups.id$': groupIds }, include: ['groups'] })
+ ])
+
+ subgroups = subgroups.filter(subgroup => !knownGroupMap[subgroup.id])
+ clients = clients.filter(client => !knownClientMap[client.id])
+
+ if (subgroups.length > 0) {
+ let groupIds = subgroups.map(x => x.id)
+ for (let i in groupIds) {
+ knownGroupMap[groupIds[i]] = true
+ }
+ let clientIds = clients.map(x => x.id)
+ for (let i in clientIds) {
+ knownClientMap[clientIds[i]] = true
+ }
+ let subChildren = await getAllChildrenByIds(groupIds, knownGroupMap, knownClientMap)
+ subgroups = [...subgroups, ...subChildren.subgroups]
+ clients = [...clients, ...subChildren.clients]
+ }
+
+ return { subgroups, clients }
+}
+
+module.exports = { getAllChildren }