/* 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 } } // GET Requests module.exports.get = { // get a list containing id and name of all groups getList: function (req, res) { db.group.findAll({ attributes: ['id', 'name', 'description'], order: [['name', 'ASC']] }).then(list => { res.send(list) }) }, // get name, description, parents, subgroups and clients of a group (by id) getGroup: function (req, res) { const all = req.query.all === 'true' if (req.query.id > 0) { db.group.findOne({ where: { id: req.query.id }, include: ['parents', 'subgroups', 'clients'] }).then(async group => { if (group) { if (all) res.send({ ...group.get({ plain: true }), ...await getAllChildren([group]) }) 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] }) }) } } } // POST Requests module.exports.post = { // create group or update information of a group (returns id) save: function (req, res) { 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: req.body.id }) }) } else { res.status(404).end() } }) } else { db.group.create(req.body.info).then(group => { if (req.body.parentIds) group.setParents(req.body.parentIds).then(() => { res.send({ id: group.id }) }) }) } }, // delete groups delete: function (req, res) { db.group.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) }) }, // remove subgroups from a group removeSubgroups: function (req, res) { db.group.findOne({ where: { id: req.body.id } }).then(group => { if (group) { group.removeSubgroups(req.body.ids).then(() => { res.end() }) } else { res.status(404).end() } }) }, // remove clients from a group removeClients: function (req, res) { db.group.findOne({ where: { id: req.body.id } }).then(group => { if (group) { group.removeClients(req.body.ids).then(() => { res.end() }) } else { res.status(404).end() } }) }, // add subgroups to a group addSubgroups: function (req, res) { db.group.findOne({ where: { id: req.body.id } }).then(group => { if (group) { group.addSubgroups(req.body.ids).then(() => { res.end() }) } else { res.status(404).end() } }) }, // add clients to a group addClients: function (req, res) { db.group.findOne({ where: { id: req.body.id } }).then(group => { if (group) { group.addClients(req.body.ids).then(() => { res.end() }) } else { res.status(404).end() } }) } }