From 6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Tue, 4 Dec 2018 16:41:15 +0000 Subject: [groups,clients,configurator] api rework to the new format --- server/lib/grouputil.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 server/lib/grouputil.js (limited to 'server/lib/grouputil.js') diff --git a/server/lib/grouputil.js b/server/lib/grouputil.js new file mode 100644 index 0000000..311eb7e --- /dev/null +++ b/server/lib/grouputil.js @@ -0,0 +1,21 @@ +/* global __appdir */ +var path = require('path') +var db = require(path.join(__appdir, 'lib', 'sequelize')) + +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 subChildren = await getAllChildren(subgroups, knownGroupIds) + subgroups = [...subgroups, ...subChildren.subgroups] + clients = [...clients, ...subChildren.clients] + } + return { subgroups, clients } +} + +module.exports.getAllChildren = getAllChildren \ No newline at end of file -- cgit v1.2.3-55-g7522