From 9bffdf4923a20a443f28c74aa4b130a5bbf1bb34 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Wed, 25 Mar 2020 12:29:17 +0000 Subject: [webapp] show part of the path (full path on hover) in the select boxes for selecting groups and clients --- server/api/clients.js | 8 +++++-- server/api/groups.js | 5 ++++- server/lib/grouphelper.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 4 deletions(-) (limited to 'server') diff --git a/server/api/clients.js b/server/api/clients.js index 15e3b8d..4222f49 100644 --- a/server/api/clients.js +++ b/server/api/clients.js @@ -8,13 +8,17 @@ const { decorateApp } = require('@awaitjs/express') var router = decorateApp(express.Router()) const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse')) const log = require(path.join(__appdir, 'lib', 'log')) +const groupHelper = require(path.join(__appdir, 'lib', 'grouphelper')) // ############################################################################ // ########################### GET requests ################################# router.getAsync('', async (req, res) => { - const clients = await db.client.findAll({ order: [['name', 'ASC']] }) - res.status(200).send(clients) + const includePaths = req.query.path !== undefined && req.query.path !== 'false' + const include = includePaths ? ['groups'] : [] + let clients = await db.client.findAll({ order: [['name', 'ASC']], include }) + if (includePaths) clients = await groupHelper.addPathsToClients(clients, false) + res.send(clients) }) router.getAsync('/:id', async (req, res) => { diff --git a/server/api/groups.js b/server/api/groups.js index 88e7da8..64351cf 100644 --- a/server/api/groups.js +++ b/server/api/groups.js @@ -14,7 +14,10 @@ const log = require(path.join(__appdir, 'lib', 'log')) // ########################### GET requests ################################# router.getAsync('', async (req, res) => { - const groups = await db.group.findAll({ order: [['name', 'ASC']] }) + const includePaths = req.query.path !== undefined && req.query.path !== 'false' + const include = includePaths ? ['parents'] : [] + let groups = await db.group.findAll({ order: [['name', 'ASC']], include }) + if (includePaths) groups = await groupHelper.addPathsToGroups(groups, false) res.send(groups) }) diff --git a/server/lib/grouphelper.js b/server/lib/grouphelper.js index 117dd79..fe94b50 100644 --- a/server/lib/grouphelper.js +++ b/server/lib/grouphelper.js @@ -36,4 +36,57 @@ async function getAllChildrenByIds (groupIds, knownGroupMap, knownClientMap) { return { subgroups, clients } } -module.exports = { getAllChildren } +async function addPathsToGroups (groups, queryParents = true) { + const allGroups = queryParents ? await db.group.findAll({ order: [['name', 'ASC']], include: ['parents'] }) : groups + const parentMap = {} + allGroups.forEach(group => { + parentMap[group.id] = group.parents + }) + const groupsWithPaths = [] + groups.forEach(group => { + let g = group.toJSON() + delete g.parents + g.paths = _buildPaths(parentMap, group) + groupsWithPaths.push(g) + }) + return groupsWithPaths +} + +async function addPathsToClients (clients) { + const allGroups = await db.group.findAll({ order: [['name', 'ASC']], include: ['parents'] }) + const parentMap = {} + allGroups.forEach(group => { + parentMap[group.id] = group.parents + }) + const clientsWithPaths = [] + clients.forEach(client => { + let c = client.toJSON() + delete c.groups + + c.paths = [] + client.groups.forEach(group => { + c.paths.push(..._buildPaths(parentMap, group)) + }) + + clientsWithPaths.push(c) + }) + return clientsWithPaths +} + +function _buildPaths (parentMap, group, knownIds = []) { + let parents = parentMap[group.id] + let paths = [] + if (parents && parents.length > 0) { + parents.forEach(parent => { + if (!knownIds.includes(parent.id)) { + paths.push(..._buildPaths(parentMap, parent, [...knownIds, group.id])) + } + }) + if (knownIds.length) paths.forEach(path => path.push(group.name)) + } else { + if (knownIds.length) paths.push([group.name]) + } + return paths +} + +module.exports = { getAllChildren, addPathsToGroups, addPathsToClients } -- cgit v1.2.3-55-g7522