summaryrefslogtreecommitdiffstats
path: root/server/api/groups.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/groups.js')
-rw-r--r--server/api/groups.js124
1 files changed, 90 insertions, 34 deletions
diff --git a/server/api/groups.js b/server/api/groups.js
index 8158d1c..02bfa25 100644
--- a/server/api/groups.js
+++ b/server/api/groups.js
@@ -2,41 +2,97 @@
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
-module.exports = {
- get: function (req, res) {
- var id = req.query.id > 0 ? req.query.id : null
- switch (req.query.action) {
- case 'getParents':
- db.group.findOne({ where: { id: req.query.id }, include: ['parents']}).then(group => {
- group.getParents().then(parents => {
- res.send(parents.map(x => ({ id: x.id, name: x.name })))
- })
- })
- break
- case 'getSubGroups':
- db.group.findAll({ where: { '$parents.id$': id }, include: ['parents'] }).then(result => {
- res.send(result)
- })
- break
- case 'getClients':
- db.client.findAll({ where: { '$groups.id$': id }, include: ['groups'] }).then(result => {
- res.send(result)
- })
- break
- default:
- res.end()
- }
+// 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'], order: [['name', 'ASC']] }).then(list => {
+ res.send(list)
+ })
+ },
+
+ // 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)
+ })
},
- post: function (req, res) {
- var id = req.body.id > 0 ? req.body.id : null
- switch (req.body.action) {
- case 'update':
- if (!id) res.end()
- db.group.update({ name: req.body.name }, { where: { id: id } })
- res.end()
- break
- default:
- res.end()
+
+ // 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()
+ })
+ }
+}
+
+// POST Requests
+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 (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}) })
+ } 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() }
+ })
}
}