summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends/backendhelper.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/external-backends/backendhelper.js')
-rw-r--r--server/lib/external-backends/backendhelper.js21
1 files changed, 14 insertions, 7 deletions
diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js
index 6892f7c..a088074 100644
--- a/server/lib/external-backends/backendhelper.js
+++ b/server/lib/external-backends/backendhelper.js
@@ -8,18 +8,18 @@ module.exports = { addClient, updateClient, deleteClients, uploadFiles }
async function addClient (client) {
// Get all backends and call addClient for each instance.
- var backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] })
+ const backends = await db.backend.findAll({ include: [{ association: 'mappedGroups', where: { id: client.parents }, required: false }] })
var result = []
- for (var b in backends) {
- var backend = backends[b]
+ for (let b in backends) {
+ const backend = backends[b]
const ba = new ExternalBackends()
const instance = ba.getInstance(backend.type)
var tmpClient = JSON.parse(JSON.stringify(client))
// Convert the parent group ids to the external backend parentIds. If multiple -> conflict
if (client.parents) {
- const elements = backend.mappedGroups.filter(x => client.parents.includes(x.id))
+ const elements = backend.mappedGroups
if (elements.length > 1) {
// Conflict occured!
const conflict = await db.conflict.create({ description: 'Multiple parents found' })
@@ -35,12 +35,14 @@ async function addClient (client) {
}
let addClient = await instance.addClient(backend.credentials, tmpClient)
+
addClient.backendId = backend.id
if (addClient.succes) {
// If the object was created we need to make the objectid / external id mapping.
const clientDb = await db.client.findOne({ where: { id: client.id } })
backend.addMappedClients(clientDb, { through: { externalId: addClient.id, externalType: addClient.type } })
}
+
if (addClient.error && addClient.error !== 'NOT_IMPLEMENTED_EXCEPTION') log({ category: 'BACKEND_ERROR', description: `[${addClient.backendId}] ${addClient.error}: ${addClient.message}`, clientId: client.id })
result.push(addClient)
}
@@ -49,7 +51,12 @@ async function addClient (client) {
async function updateClient (client) {
// Get all backends and call addClient for each instance.
- const backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] })
+ const backends = await db.backend.findAll({
+ include: [
+ { association: 'mappedGroups', where: { id: client.parents }, required: false },
+ { association: 'mappedClients', where: { id: client.id }, required: false }
+ ]
+ })
let result = []
for (let b in backends) {
@@ -59,12 +66,12 @@ async function updateClient (client) {
var tmpClient = JSON.parse(JSON.stringify(client))
// Get the external clientid.
- var exid = backend.mappedClients.find(y => y.id === parseInt(client.id))
+ const exid = backend.mappedClients[0]
if (exid) tmpClient.id = exid.backend_x_client.externalId
// Convert the parent group ids to the external backend parentIds. If multiple -> conflict
if (client.parents) {
- const elements = backend.mappedGroups.filter(x => client.parents.includes(x.id))
+ const elements = backend.mappedGroups
if (elements.length > 1) {
// Conflict occured!
const conflict = await db.conflict.create({ description: 'Multiple parents found' })