summaryrefslogblamecommitdiffstats
path: root/server/api/backends.js
blob: b744bd225eb83cea1b18d73773f0e5d7118bf55f (plain) (tree)
1
2
3
4
5
6
                     


                                                                                                         

                  






                                                                                                                           

                                                   
 











                                                                      
 


                                           
 



                                            
 

                               
 






                                        
 














                                                                             




                                                                        
 





                                                                                                                                                         
     




                                   

                                       
 
                                                                        


                                     
 
/* 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'))

module.exports = {
  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())
  },

  getBackendInfoById: 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)
    })
  },

  getBackendTypes: 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)
  },

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

  checkConnection: function (req, res) {
    var x = Math.floor(Math.random() * Math.floor(2))
    var y = ''
    if (x === 0) {
      y = 'success'
    } else {
      y = 'error'
    }
    var w = 'Oh noes! An error occurred.'
    var z = Math.floor(Math.random() * Math.floor(5000))
    
    
    setTimeout(function() { res.status(200).send({ status: y, msg: w }) }, 0)
  },

  // POST REQUESTS
  saveBackend: 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')
  },

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

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