summaryrefslogtreecommitdiffstats
path: root/server/api/backends.js
blob: e4d3ff6082db6e2b4656c784dccd405bd2875df2 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* global __appdir */
const path = require('path')
const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends', 'external-backends.js'))
var db = require(path.join(__appdir, 'lib', 'sequelize'))

// GET requests
module.exports.get = {
  getCredentialsByType: function (req, res) {
    const backendType = req.query.type
    const b = new ExternalBackends()
    const instance = b.getInstance(backendType)
    if (instance === null) {
      res.status(500).send({ auth: false, status: 'TYPE_INVALID', error_message: 'The provided backend type is invalid.' })
    }
    res.status(200).send(instance.getCredentials())
  },

  getInfoById: function (req, res) {
    const backendId = req.query.id
    db.backend.findOne({ where: { id: backendId } }).then(backend => {
      const b = {
        backendId: backendId,
        backendName: backend.name,
        backendType: backend.type,
        backendCredentials: backend.credentials
      }
      res.status(200).send(b)
    })
  },

  getTypes: function (req, res) {
    const backends = new ExternalBackends()
    var files = backends.getBackends()

    for (var i = 0; i < files.length; i++) {
      // Cut off -backends.js
      files[i] = files[i].slice(0, -11)
    }

    res.status(200).send(files)
  },

  getList: function (req, res) {
    db.backend.findAll({
      attributes: ['id', 'name', 'type']
    }).then(function (backends) {
      res.status(200).send(backends)
    })
  },

  checkConnectionById: function (req, res) {
    const backendId = req.query.id
    db.backend.findOne({ where: { id: backendId } }).then(backend => {
      const bCredentials = {
        id: backendId,
        name: backend.name,
        type: backend.type,
        credentials: backend.credentials
      }
      const b = new ExternalBackends()
      const instance = b.getInstance(bCredentials.type)
      instance.checkConnection(bCredentials).then(connection => {
        res.status(200).send({ success: connection.success, msg: connection.msg })
      })
    })
  }
}

// POST requests
module.exports.post = {
  save: function (req, res) {
    // Save credentials in the db.
    const formData = req.body
    const credentialString = JSON.stringify(formData.backendCredentials)

    if (formData.backendId === 0) {
      // Insert new backend in the db.
      db.backend.create({ name: formData.backendName, type: formData.backendType, credentials: credentialString })
    } else {
      // Update an existing backend in the db.
      db.backend.update({ name: formData.backendName, type: formData.backendType, credentials: credentialString }, { where: { id: formData.backendId } })
    }
    // db.backend.findOne({})

    res.status(200).send('success')
  },

  delete: function (req, res) {
    const backendIds = req.body.id

    db.backend.destroy({ where: { id: backendIds } }).then(function () {
      res.status(200).send('success')
    })
  },

  // If id is set the backend connection in the db is testest.
  // Else backendinfo has to be set manually, to test unsaved connections.
  checkConnection: function (req, res) {
    const id = req.body.id

  	var getBackend = null
    if (id) getBackend = db.backend.findOne({ where: { id: id } })
  	else getBackend = new Promise(resolve => resolve(req.body))

  	getBackend.then(backend => {
      var bCredentials = {
        type: backend.type,
        credentials: backend.credentials
      }
      // Creating the backend instance and calling the specific checkConnection.
  		const b = new ExternalBackends()
  		const instance = b.getInstance(bCredentials.type)
  		instance.checkConnection(bCredentials).then(connection => {
  		  res.status(200).send({ success: connection.success, msg: connection.msg })
  		})
  	})
  }
}