summaryrefslogtreecommitdiffstats
path: root/server/api/clients.js
blob: d72207bf604ed872d41eabb5c4335e433d0c01ff (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
/* 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) => {
  if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
  const client = await db.client.findOne({ where: { id: req.params.id }, include: ['groups', 'config'] })
  if (client) res.status(200).send(client)
  else HttpResponse.notFound(req.params.id).send(res)
})

// ############################################################################
// ##########################  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('ids', 'an array').send(res)
    await backendHelper.deleteClients(req.body.ids, req.body.backendIds)
    const count = await db.client.destroy({ where: { id: req.body.ids } })
    HttpResponse.successBatch('deleted', 'client', count).send(res)
  } else {
    let client
    let action = 'updated'

    let backendClient = {
      id: req.params.id,
      name: req.body.data.name,
      type: 'CLIENT',
      uuid: req.body.data.uuid,
      parents: req.body.groupIds,
      networks: [
        { ip: req.body.data.ip, mac: req.body.data.mac }
      ]
    }

    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!' })

      // Add client to the backends
      backendClient.id = client.id
      backendHelper.addClient(backendClient)
    } else if (req.params.id > 0) {
      client = await db.client.findOne({ where: { id: req.params.id } })
      if (!client) return HttpResponse.notFound(req.params.id).send(res)
      else await client.update(req.body.data)
      backendHelper.updateClient(backendClient)
    } else {
      return HttpResponse.invalidId().send(res)
    }
    await client.setGroups(req.body.groupIds)
    HttpResponse.success(action, 'client', client.id).send(res)
  }
})

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

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

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

module.exports.router = router