summaryrefslogtreecommitdiffstats
path: root/server/api/groups.js
diff options
context:
space:
mode:
authorUdo Walter2018-08-07 05:25:44 +0200
committerUdo Walter2018-08-07 05:25:44 +0200
commit8f64402237a74ddb380b7e9650fa5897505a665f (patch)
tree692971f50fa7d442bf6fa3be959e72f588626774 /server/api/groups.js
parentmerge (diff)
downloadbas-8f64402237a74ddb380b7e9650fa5897505a665f.tar.gz
bas-8f64402237a74ddb380b7e9650fa5897505a665f.tar.xz
bas-8f64402237a74ddb380b7e9650fa5897505a665f.zip
[groups] add ability to show all nested children and not just direkt children + bug fixes
Diffstat (limited to 'server/api/groups.js')
-rw-r--r--server/api/groups.js62
1 files changed, 40 insertions, 22 deletions
diff --git a/server/api/groups.js b/server/api/groups.js
index 64f49fc..e8efd4f 100644
--- a/server/api/groups.js
+++ b/server/api/groups.js
@@ -2,6 +2,27 @@
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
+function includeAllChilds (group, groupIdChain = []) {
+ const newIdChain = [...groupIdChain, group.id]
+ return new Promise((resolve, reject) => {
+ group.getSubgroups({ include: ['subgroups', 'clients'] }).then(subgroups => {
+ const subgroupPromises = []
+ subgroups.forEach(subgroup => {
+ if (!groupIdChain.includes(subgroup.id)) {
+ subgroupPromises.push(includeAllChilds(subgroup, newIdChain))
+ }
+ })
+ Promise.all(subgroupPromises).then(result => {
+ result.forEach(resolvedSubgroup => {
+ resolvedSubgroup.subgroups.forEach(x => { if (!group.subgroups.includes(x)) group.subgroups.push(x) })
+ resolvedSubgroup.clients.forEach(x => { if (!group.clients.includes(x)) group.clients.push(x) })
+ })
+ resolve(group)
+ })
+ })
+ })
+}
+
// GET Requests
module.exports.get = {
// get a list containing id and name of all groups
@@ -11,26 +32,24 @@ module.exports.get = {
})
},
- // get all groups
- getAll: function (req, res) {
- db.group.findAll().then(list => {
- res.send(list)
- })
- },
-
- // get all groups that have no parents
- getTopLevel: function (req, res) {
- db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
- res.send(groups)
- })
- },
-
// get name, description, parents, subgroups and clients of a group (by id)
getGroup: function (req, res) {
- db.group.findOne({ where: { id: req.query.id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
- if (group) res.send(group)
- else res.status(404).end()
- })
+ const all = req.query.all === 'true'
+ if (req.query.id > 0) {
+ db.group.findOne({ where: { id: req.query.id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
+ if (group) {
+ if (all) includeAllChilds(group).then(x => res.send(x))
+ else res.send(group)
+ } else {
+ res.status(404).end()
+ }
+ })
+ } else {
+ Promise.all([db.group.findAll(all ? {} : { where: { '$parents.id$': null }, include: ['parents'] }),
+ db.client.findAll(all ? {} : { where: { '$groups.id$': null }, include: ['groups'] })]).then(result => {
+ res.send({ id: 0, subgroups: result[0], clients: result[1] })
+ })
+ }
}
}
@@ -38,14 +57,13 @@ module.exports.get = {
module.exports.post = {
// create group or update information of a group (returns id)
save: function (req, res) {
- const id = req.body.id > 0 ? req.body.id : null
- if (id) {
- db.group.findOne({ where: { id } }).then(group => {
+ if (req.body.id > 0) {
+ db.group.findOne({ where: { id: req.body.id } }).then(group => {
if (group) {
var promises = []
if (req.body.info) promises.push(group.update(req.body.info))
if (req.body.parentIds) promises.push(group.setParents(req.body.parentIds))
- Promise.all(promises).then(() => { res.send({id}) })
+ Promise.all(promises).then(() => { res.send({ id: req.body.id }) })
} else { res.status(404).end() }
})
} else {