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


                                                                                                         
 

                      





                                                                             






                                                                                                                           

                                                   
 




                                                   
                                    










                                                                      
 


                                                            
                                 

                                           
 



                                            
 

                               
 


                                                             
                                





                                        
 























                                                                                                                 



                                                               






                                                                                                                 
      

    




                                                                                                            




                                                               





                                                                   




                                                        
                                        




                                                               
                               

                                 

    




                                                                






                                                               

   
 

                       








                                       
                             
                                  

                                                                
 
                           
                                      
                                                                                                  

                                              
                                                                                                                                 
     




                                   




                                         
                               
                                  
 
                                                                        

                                     

    









                                                        
                                        
                          
 
                         
                                                                  
                                                               
 
                                
                                                                                
                                      

                                                                        
                                                                                      

        

    







                                                                                   
                                         


                                                    
                              
                                                               
                                                                                                             


                              
   
 
/* 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 = {

  /*
   * ?id=<BACKEND_ID>
   *
   * @return: Returns the credentials structure and fields of a backend type.
   */
  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())
  },

  /*
   * ?id=<BACKEND_ID>
   *
   * @return: Returns the information of a backend.
   */
  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)
    })
  },

  /*
   * @return: Returns a list of all available backend types.
   */
  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)
  },

  /*
   * @return: Returns a list of all backends saved in the db.
   */
  getList: function (req, res) {
    db.backend.findAll({
      attributes: ['id', 'name', 'type']
    }).then(function (backends) {
      res.status(200).send(backends)
    })
  },

  /*
   * ?id=<Backend_ID>
   *
   * @return: Returns a list with all objects of the backend.
   */
  getObjects: function (req, res) {
    const id = req.query.id
    db.backend.findOne({ where: { id: id } }).then(backend => {
      if (backend) {
        const ba = new ExternalBackends()
        const instance = ba.getInstance(backend.type)
        instance.getObjects(backend.credentials).then(result => {
          res.status(200).send(result)
        })
      } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
    })
  },

  /*
   * ?id=<Backend_ID>
   * ?oid=<OBJECT_ID>
   *
   * @return: Returns information about a given object and all childs.
   */
  getObject: function (req, res) {
    const id = req.query.id
    const oid = req.query.oid
    db.backend.findOne({ where: { id: id } }).then(backend => {
      if (backend) {
        const ba = new ExternalBackends()
        const instance = ba.getInstance(backend.type)
        instance.getObject(backend.credentials, oid).then(result => {
          res.status(200).send(result)
        })
      } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
    })
  },

  /*
   * ?id=<Backend_ID>
   *
   * @return: Returns a list of all the object types of the given backend. [{id: <id>, title: <title>}, ...]
   */
  getObjectTypes: function (req, res) {
    const id = req.query.id
    db.backend.findOne({ where: { id: id } }).then(backend => {
      const ba = new ExternalBackends()
      const instance = ba.getInstance(backend.type)
      instance.getObjectTypes(backend.credentials).then(result => {
        res.status(200).send(result)
      })
    })
  },

  /*
   * ?id=<Backend_ID>
   *
   * @return: Returns the sync settings saved in the db.
   */
  getSyncSettings: function (req, res) {
    const id = req.query.id
    var types = {}
    db.backend.findOne({ where: { id: id } }).then(backend => {
      types.groups = JSON.parse(backend.groups)
      types.clients = JSON.parse(backend.clients)
      types.sync = backend.sync
      res.status(200).send(types)
    })
  },

  /*
   * ?id=<Backend_ID>
   *
   * @return: Returns a list of sync types the backend supports.
   */
  getSyncTypes: function (req, res) {
    const id = req.query.id
    db.backend.findOne({ where: { id: id } }).then(backend => {
      const ba = new ExternalBackends()
      const instance = ba.getInstance(backend.type)
      res.status(200).send(instance.getSyncTypes())
    })
  }
}

// POST requests
module.exports.post = {

  /*
   * id: <BACKEND_ID>
   * name: <BACKEND_NAME>
   * type: <BACKEND_TYPE>
   * credentials: <BACKEND_CREDENTIALS>
   *
   * Creates or updates the backend.
   */
  save: function (req, res) {
    // Save credentials in the db.
    const backend = req.body
    const credentialString = JSON.stringify(backend.credentials)

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

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

  /*
   * id: <BACKEND_ID>
   *
   * Deletes the backend to the given id.
   */
  delete: function (req, res) {
    const backendIds = req.body.id

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

  /*
   * id: <BACKEND_ID>
   * <OR>
   * name: <BACKEND_NAME>
   * type: <BACKEND_TYPE>
   * credentials: <BACKEND_CREDENTIALS>
   *
   * If the id is set, the backend in the db ist testet.
   * Else the backend is postet in the request.
   */
  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 => {
      // Creating the backend instance and calling the specific checkConnection.
      const b = new ExternalBackends()
      const instance = b.getInstance(backend.type)
      instance.checkConnection(backend.credentials).then(connection => {
        res.status(200).send({ success: connection.success, error: connection.error })
      })
    })
  },

  /*
   * id: <BACKEND_ID>
   * groups: <JSON_OF_ASSIGNED_GROUPS> (list of backend types)
   * clients: <JSON_OF_ASSIGNED_CLIENTS> (list of backend types)
   * sync: <SYNC_OPTION>
   *
   * Saves the group / clients assigned object types in the database and sync type.
   */
  saveSyncSettings: function (req, res) {
    const id = req.body.id
    const groups = JSON.stringify(req.body.groups)
    const clients = JSON.stringify(req.body.clients)
    const sync = req.body.sync
    db.backend.findOne({ where: { id: id } }).then(backend => {
      db.backend.update({ groups: groups, clients: clients, sync: sync }, { where: { id: id } }).then(() => {
        res.status(200).send()
      })
    })
  }
}