summaryrefslogblamecommitdiffstats
path: root/server/api/clients.js
blob: 6cd34a2b922d4b59bc4e05534ac2e71bdc9205d9 (plain) (tree)
1
2
3
4
5
6
7
8
9
                     

                                                         
                                                        
                                                                                               
                                
                                                   
                                          
                                                                        
 




                                                                               
                               
  
 

                                                                                               
                                          
                                                






                                                                               
                                                                                                  
                                                   
                                                                          
                                                              

              
                          

                                                    
                        
                                                                                                        
                                   
                                                                        

                                                                   
            
                                        
     

                                                          
   






                                                                               

                                                                          





                                                                               
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var io = require(path.join(__appdir, 'lib', 'socketio'))
const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper'))
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
const httpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))

// ############################################################################
// ###########################  GET requests  #################################

router.getAsync('', async (req, res) => {
  const clients = await db.client.findAll({ order: [['name', 'ASC']] })
  res.status(200).send(clients)
})

router.getAsync('/:id', async (req, res) => {
  const client = await db.client.findOne({ where: { id: req.params.id }, include: ['groups'] })
  if (client) res.status(200).send(client)
  else httpResponse.notFound(res, req.params.id)
})

// ############################################################################
// ##########################  POST requests  #################################

router.postAsync(['', '/:id'], async (req, res) => {
  if (req.query.delete !== undefined && req.query.delete !== 'false') {
    if (!Array.isArray(req.body.ids)) return httpResponse.invalidBodyValue(res, 'ids', 'an array')
    await backendHelper.deleteClients(req.body.ids)
    const count = await db.client.destroy({ where: { id: req.body.ids } })
    httpResponse.successBatch(res, 'deleted', 'client', count)
  } else {
    let client
    let action = 'updated'
    if (req.params.id === undefined) {
      client = await db.client.create(req.body.data)
      action = 'created'
      io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' })
    } else if (req.params.id > 0) {
      client = await db.client.findOne({ where: { id: req.params.id } })
      if (!client) return httpResponse.notFound(res, req.params.id)
      else await client.update(req.body.data)
    } else {
      return httpResponse.invalidId(res)
    }
    await client.setGroups(req.body.groupIds)
    httpResponse.success(res, action, 'client', client.id)
  }
})

// ############################################################################
// ##########################  DELETE requests  ###############################

router.delete('/:id', async (req, res) => {
  const count = await db.client.destroy({ where: { id: req.params.id } })
  if (count) httpResponse.success(res, 'deleted', 'client', req.params.id)
  else httpResponse.notFound(res, req.params.id)
})

// ############################################################################
// ############################################################################

module.exports.router = router