summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorUdo Walter2020-03-25 13:29:17 +0100
committerUdo Walter2020-03-25 13:29:17 +0100
commit9bffdf4923a20a443f28c74aa4b130a5bbf1bb34 (patch)
tree904f39a8a13782e9cf51d8d5e9ee059d1149bce1 /server
parentmerge (diff)
downloadbas-9bffdf4923a20a443f28c74aa4b130a5bbf1bb34.tar.gz
bas-9bffdf4923a20a443f28c74aa4b130a5bbf1bb34.tar.xz
bas-9bffdf4923a20a443f28c74aa4b130a5bbf1bb34.zip
[webapp] show part of the path (full path on hover) in the select boxes for selecting groups and clients
Diffstat (limited to 'server')
-rw-r--r--server/api/clients.js8
-rw-r--r--server/api/groups.js5
-rw-r--r--server/lib/grouphelper.js55
3 files changed, 64 insertions, 4 deletions
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 }