From 95ae9a051d7cab018a4ccfef4cd6140000076bb8 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Mon, 15 Apr 2019 14:37:33 +0000 Subject: [server] grouputils -> grouphelper --- server/lib/grouphelper.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 server/lib/grouphelper.js (limited to 'server/lib/grouphelper.js') 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 } -- cgit v1.2.3-55-g7522