summaryrefslogblamecommitdiffstats
path: root/server/api/groups.js
blob: ff29799bbd62fbc212f81268b3c373b5e2eb389b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12




                                                         






                                                                                   
          














                                                                                                
     
 
    






                                                                     
     
 

   
/* 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()
    }

  }
}