summaryrefslogtreecommitdiffstats
path: root/server/api/groups.js
blob: ff29799bbd62fbc212f81268b3c373b5e2eb389b (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
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))

module.exports = {
  get: {

    getParents: function (req, res) {
      const id = req.query.id > 0 ? req.query.id : null
      db.group.findOne({ where: { id: id }, include: ['parents'] }).then(group => {
        group.getParents().then(parents => {
          res.send(parents.map(x => ({ id: x.id, name: x.name })))
        })
      })
    },

    getSubGroups: function (req, res) {
      const id = req.query.id > 0 ? req.query.id : null
      db.group.findAll({ where: { '$parents.id$': id }, include: ['parents'] }).then(result => {
        res.send(result)
      })
    },

    getClients: function (req, res) {
      const id = req.query.id > 0 ? req.query.id : null
      db.client.findAll({ where: { '$groups.id$': id }, include: ['groups'] }).then(result => {
        res.send(result)
      })
    }

  },
  post: {

    update: function (req, res) {
      const id = req.body.id > 0 ? req.body.id : null
      if (!id) res.end()
      db.group.update({ name: req.body.name }, { where: { id: id } })
      res.end()
    }

  }
}