summaryrefslogtreecommitdiffstats
path: root/server/api/groups.js
blob: a41666a131ccfb3770b02019c21da1532024f38f (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
93
94
95
96
97
98
99
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))

// 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)
    })
  },

  // 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 => {
      res.send(group)
    })
  }
}

// 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 => {
        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 {
      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(() => { res.end() })
  },

  // remove subgroups from a group
  removeSubgroups: function (req, res) {
    const id = req.body.id > 0 ? req.body.id : null
    if (id) {
      db.group.findOne({ where: { id } }).then(group => {
        group.removeSubgroups(req.body.ids).then(() => { res.end() })
      })
    } else { res.status(404).end() }
  },

  // remove clients from a group
  removeClients: function (req, res) {
    const id = req.body.id > 0 ? req.body.id : null
    if (id) {
      db.group.findOne({ where: { id } }).then(group => {
        group.removeClients(req.body.ids).then(() => { res.end() })
      })
    } else { res.status(404).end() }
  },

  // add subgroups to a group
  addSubgroups: function (req, res) {
    const id = req.body.id > 0 ? req.body.id : null
    if (id) {
      db.group.findOne({ where: { id } }).then(group => {
        group.addSubgroups(req.body.ids).then(() => { res.end() })
      })
    } else { res.status(404).end() }
  },

  // add clients to a group
  addClients: function (req, res) {
    const id = req.body.id > 0 ? req.body.id : null
    if (id) {
      db.group.findOne({ where: { id } }).then(group => {
        group.addClients(req.body.ids).then(() => { res.end() })
      })
    } else { res.status(404).end() }
  }
}