summaryrefslogtreecommitdiffstats
path: root/server/api/groups.js
diff options
context:
space:
mode:
authorUdo Walter2018-12-04 17:41:15 +0100
committerUdo Walter2018-12-04 17:41:15 +0100
commit6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b (patch)
tree5edcf1f2608624dd61d677fef8a2017edca01241 /server/api/groups.js
parenteslint fix :) (diff)
downloadbas-6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b.tar.gz
bas-6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b.tar.xz
bas-6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b.zip
[groups,clients,configurator] api rework to the new format
Diffstat (limited to 'server/api/groups.js')
-rw-r--r--server/api/groups.js177
1 files changed, 84 insertions, 93 deletions
diff --git a/server/api/groups.js b/server/api/groups.js
index ddaed18..f1825cc 100644
--- a/server/api/groups.js
+++ b/server/api/groups.js
@@ -1,111 +1,102 @@
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
+var groupUtil = require(path.join(__appdir, 'lib', 'grouputil'))
+var express = require('express')
+const { decorateApp } = require('@awaitjs/express');
+var router = decorateApp(express.Router())
-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]
+// ############################################################################
+// ########################### GET requests #################################
+
+router.getAsync('', async (req, res) => {
+ const groups = await db.group.findAll({ order: [['name', 'ASC']] })
+ res.send(groups)
+})
+
+router.getAsync('/:id', async (req, res) => {
+ const all = req.query.all !== undefined && req.query.all !== 'false'
+ if (req.params.id > 0) {
+ const group = await db.group.findOne({ where: { id: req.params.id }, include: ['parents', 'subgroups', 'clients'] })
+ if (group) {
+ if (all) res.status(200).send({ ...group.get({ plain: true }), ...await groupUtil.getAllChildren([group]) })
+ else res.status(200).send(group)
+ } else {
+ res.status(404).end()
+ }
+ } else {
+ const [subgroups, clients] = await Promise.all([
+ db.group.findAll(all ? {} : { where: { '$parents.id$': null }, include: ['parents'] }),
+ db.client.findAll(all ? {} : { where: { '$groups.id$': null }, include: ['groups'] })
+ ])
+ res.send({ id: 0, subgroups, 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)
- })
- },
+// ############################################################################
+// ########################## POST requests #################################
- // 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()
- }
- })
+router.postAsync(['', '/:id'], async (req, res) => {
+ if (req.query.delete !== undefined && req.query.delete !== 'false') {
+ const count = await db.group.destroy({ where: { id: req.body.ids } })
+ res.status(200).send({ count })
+ } else {
+ let group
+ if (req.params.id === undefined) group = await db.group.create(req.body.data)
+ else {
+ group = await db.group.findOne({ where: { id: req.params.id } })
+ if (group) await group.update(req.body.data)
+ }
+ if (group) {
+ await group.setParents(req.body.parentIds)
+ res.status(200).send({ id: group.id })
} 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] })
- })
+ res.status(404).end()
}
}
-}
+})
-// 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() }
- })
+router.postAsync('/:id/subgroups', async (req, res) => {
+ const group = await db.group.findOne({ where: { id: req.params.id } })
+ if (group) {
+ if (req.query.delete !== undefined && req.query.delete !== 'false') {
+ const count = await group.removeSubgroups(req.body.ids)
+ res.status(200).send({ count })
} else {
- db.group.create(req.body.info).then(group => {
- if (req.body.parentIds) group.setParents(req.body.parentIds).then(() => { res.send({ id: group.id }) })
- })
+ const count = await group.addSubgroups(req.body.ids)
+ res.status(200).send({ count })
}
- },
+ } else {
+ res.status(404).end()
+ }
+})
- // delete groups
- delete: function (req, res) {
- db.group.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) })
- },
+router.postAsync('/:id/clients', async (req, res) => {
+ const group = await db.group.findOne({ where: { id: req.params.id } })
+ if (group) {
+ if (req.query.delete !== undefined && req.query.delete !== 'false') {
+ const count = await group.removeClients(req.body.ids)
+ res.status(200).send({ count })
+ } else {
+ const count = await group.addClients(req.body.ids)
+ res.status(200).send({ count })
+ }
+ } else {
+ res.status(404).end()
+ }
+})
- // 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() }
- })
- },
+// ############################################################################
+// ########################## DELETE requests ###############################
- // 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() }
- })
- },
+router.delete('/:id', async (req, res) => {
+ const count = await db.group.destroy({ where: { id: req.params.id } })
+ if (count) res.status(200).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() }
- })
- }
-}
+module.exports.router = router