summaryrefslogtreecommitdiffstats
path: root/server/api/backends.js
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-06 03:04:54 +0200
committerJannik Schönartz2018-08-06 03:04:54 +0200
commit311a686f3672a00c8def9190c874d6f83a006e35 (patch)
tree29d05513d8c1e96dca50aae4445845177bbf5d3f /server/api/backends.js
parent[webapp/searchtable] bugfix (diff)
downloadbas-311a686f3672a00c8def9190c874d6f83a006e35.tar.gz
bas-311a686f3672a00c8def9190c874d6f83a006e35.tar.xz
bas-311a686f3672a00c8def9190c874d6f83a006e35.zip
[server/external-backends] Implemented import Objects from iDoIT
renamed external-backends.js in index.js so its return when requireing the folder Added new tables for the external id mapping for clients / groups iDoIT method for importing Objects and adding them in the db with all neccessary constraints
Diffstat (limited to 'server/api/backends.js')
-rw-r--r--server/api/backends.js138
1 files changed, 133 insertions, 5 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 5e98a9c..2fae6bd 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -1,6 +1,6 @@
/* global __appdir */
const path = require('path')
-const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends', 'external-backends.js'))
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
var db = require(path.join(__appdir, 'lib', 'sequelize'))
// GET requests
@@ -128,8 +128,8 @@ module.exports.get = {
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.groups = JSON.parse(backend.groupTypes)
+ types.clients = JSON.parse(backend.clientTypes)
types.sync = backend.sync
res.status(200).send(types)
})
@@ -147,7 +147,136 @@ module.exports.get = {
const instance = ba.getInstance(backend.type)
res.status(200).send(instance.getSyncTypes())
})
+ },
+
+ /*
+ * ?id: <BACKEND_ID>
+ * id: <BACKEND_ID>
+ */
+ importObjects: function (req, res) {
+ const id = req.query.id
+ // const id = req.body.id
+
+ // Get the backend where the objects are importet from.
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ if (backend) {
+ var endRequest = []
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const groups = JSON.parse(backend.groupTypes).map(x => x.id)
+ const clients = JSON.parse(backend.clientTypes).map(x => x.id)
+
+ // Get a list with all objects in the backend.
+ const objectPromise = new Promise(function (resolve, reject) {
+ resolve(instance.getObjects(backend.credentials))
+ })
+
+ objectPromise.then(result => {
+ // Check for the not implemented exception
+ if (result.status) res.status(501).send(result)
+
+ // Filter those objects in groups / clients
+ var groupObjects = []
+ var clientObjects = []
+ result.objects.filter(obj => {
+ if (groups.find(x => x === obj.type)) groupObjects.push({ id: obj.id, name: obj.title, type: obj.type, typeName: obj.type_title, sysid: obj.sysid })
+ else if (clients.find(y => y === obj.type)) clientObjects.push({ id: obj.id, name: obj.title, type: obj.type, typeName: obj.type_title, sysid: obj.sysid })
+ })
+
+ var promises = []
+ var promises2 = []
+
+ // Add all groups in the database.
+ groupObjects.forEach(group => {
+ // Insert the group.
+ promises.push(db.group.create({ name: group.name, description: group.typeName }).then(g => {
+ // Insert the backend_x_group relation.
+ promises2.push(backend.addMappedGroups(g, { through: { externalId: group.id, externalType: group.type } }))
+ }))
+ })
+
+ // Add all clients in the databse.
+ clientObjects.forEach(client => {
+ // Insert the client.
+ promises.push(db.client.create({ name: client.name, description: client.typeName }).then(c => {
+ // Insert the backend_x_client relation.
+ promises2.push(backend.addMappedClients(c, { through: { externalId: client.id, externalType: client.type } }))
+ }))
+ })
+
+ // Wait till all clients / groups are created and all mapping operations are done. Then add childs.
+ Promise.all(promises).then(() => {
+ Promise.all(promises2).then(() => {
+ // Get the backend with all the mapped groups. ! Only groups can have childs !
+ db.backend.findOne({ where: { id: backend.id }, include: ['mappedGroups'] }).then(b => {
+ var objectData = []
+ // Put all groups in the array to make a one session call which returns all needed informations.
+ b.mappedGroups.forEach(mGroup => {
+ const mG = mGroup.backend_x_group
+ const eid = mG.externalId
+ const gid = mGroup.id
+ objectData.push({ eid: eid, gid: gid })
+ })
+
+ // Get all the information needed from the backend instance. (For all object ids in the array)
+ var promise = new Promise(function (resolve) {
+ resolve(instance.getDataTree(backend.credentials, objectData))
+ })
+
+ promise.then(data => {
+ // Check for the not implemented exception
+ if (data.status) res.status(501).send(data)
+
+ data.forEach(obj => {
+ var groupChildsToAdd = []
+ var clientChildsToAdd = []
+ var prom = []
+
+ // Put all clientChilds in the clientList and all groupChilds in the groupList.
+ obj.childs.forEach(child => {
+ if (groups.find(x => x === child.type)) {
+ // Get the group id out of the externalId.
+ prom.push(db.backend.findOne({ where: { id: backend.id, '$mappedGroups.backend_x_group.externalId$': child.id }, include: ['mappedGroups'] }).then(ba => {
+ // The externalId should only be once in the db.
+ if (ba.mappedGroups.length === 1) {
+ groupChildsToAdd.push(ba.mappedGroups[0].backend_x_group.groupId)
+ }
+ }))
+ } else if (clients.find(x => x === child.type)) {
+ // Get the client id out of the externalId.
+ prom.push(db.backend.findOne({ where: { id: backend.id, '$mappedClients.backend_x_client.externalId$': child.id }, include: ['mappedClients'] }).then(ba => {
+ // The externalId should only be once in the db.
+ if (ba.mappedClients.length === 1) {
+ clientChildsToAdd.push(ba.mappedClients[0].backend_x_client.clientId)
+ }
+ }))
+ }
+ })
+
+ // After all the group and client ids are collected. Add them as subgroup / client
+ Promise.all(prom).then(() => {
+ endRequest.push(db.group.findOne({ where: { id: obj.gid } }).then(group => {
+ if (group) {
+ group.addSubgroups(groupChildsToAdd)
+ group.addClients(clientChildsToAdd)
+ }
+ }))
+ })
+ })
+ })
+ })
+ })
+ })
+
+ // If all requests are fullfilled. End the request.
+ Promise.all(endRequest).then(() => {
+ res.status(200).send({ status: 'SUCCESS' })
+ })
+ })
+ } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+ })
}
+
}
// POST requests
@@ -173,7 +302,6 @@ module.exports.post = {
// 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')
},
@@ -232,7 +360,7 @@ module.exports.post = {
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(() => {
+ db.backend.update({ groupTypes: groups, clientTypes: clients, sync: sync }, { where: { id: id } }).then(() => {
res.status(200).send()
})
})