summaryrefslogblamecommitdiffstats
path: root/server/api/backends.js
blob: e3e6014f66745106e1554181d489acd0b3a68e43 (plain) (tree)










































































                                                                                                                                                             
/* 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);
        res.status(200).send(instance.getCredentials());
    },

    getBackendInfoById: function(req, res) {
        // TODO: Test (Not used for now) needed for edit // TODO: // TODO: 
        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);
        });
    },

    // 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');
    },

    deleteBackendById: function(req, res) {
        const backendId = req.body.id;
        
        db.backend.destroy({ where: { id: backendId } }).then(function() {
            res.status(200).send('success');
        });
    }
}