summaryrefslogtreecommitdiffstats
path: root/server/lib/grouphelper.js
blob: 8139cd6c82900045ed840526741a136dd536a184 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* 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 => { knownClientMap[client.id] = true })
  return getAllChildrenByIds(groups.map(x => x.id), knownGroupMap, knownClientMap)
}

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 }
}

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, getAllChildrenByIds, addPathsToGroups, addPathsToClients }