summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends/backendhelper.js
blob: d8d4b24bf2e70cf3103605dad552149e22c3a689 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* global __appdir */
const path = require('path')
const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
const db = require(path.join(__appdir, 'lib', 'sequelize'))
const log = require(path.join(__appdir, 'lib', 'log'))

module.exports = { addClient, updateClient, deleteClients, uploadFiles }

async function addClient (client) {
  // Get all backends and call addClient for each instance.
  const backends = await db.backend.findAll({ include: [{ association: 'mappedGroups', where: { id: client.parents }, required: false }] })
  var result = []

  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
      if (elements.length > 1) {
        // Conflict occured!
        const conflict = await db.conflict.create({ description: 'Multiple parents found' })

        // Add backend to the conflict.
        conflict.createObject({ objectType: 'BACKEND', objectId: backend.id })

        // Add the groups to the conflict.
        for (let element of elements) {
          conflict.createObject({ objectType: 'GROUP', objectId: element.id })
        }
      } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId
    }

    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: 'ERROR_BACKEND', description: `[${addClient.backendId}] ${addClient.error}: ${addClient.message}`, clientId: client.id })
    result.push(addClient)
  }
  return result
}

async function updateClient (client) {
  // Get all backends and call addClient for each instance.
  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) {
    const backend = backends[b]
    const ba = new ExternalBackends()
    const instance = ba.getInstance(backend.type)
    var tmpClient = JSON.parse(JSON.stringify(client))

    // Get the external clientid.
    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
      if (elements.length > 1) {
        // Conflict occured!
        const conflict = await db.conflict.create({ description: 'Multiple parents found' })

        // Add backend to the conflict.
        conflict.createObject({ objectType: 'BACKEND', objectId: backend.id })

        // Add the groups to the conflict.
        for (let element of elements) {
          conflict.createObject({ objectType: 'GROUP', objectId: element.id })
        }
      } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId
    }

    let updateClient = await instance.updateClient(backend.credentials, tmpClient)
    updateClient.backendId = backend.id
    result.push(updateClient)
  }
  return result
}

async function deleteClients (clientids) {
  // Get all backends and call deleteClient for each instance.
  const backends = await db.backend.findAll({ where: { '$clientMappings.clientid$': clientids }, include: ['clientMappings'] })

  backends.forEach(backend => {
    const ba = new ExternalBackends()
    const instance = ba.getInstance(backend.type)
    var objectsToDelete = []
    backend.clientMappings.forEach(mapping => {
      objectsToDelete.push(mapping.externalId)
    })
    // If there are objects to delete -> delete them.
    if (objectsToDelete.length > 0) instance.deleteObjects(backend.credentials, objectsToDelete)
  })
}

async function uploadFiles (clientId, files) {
  const backends = await db.backend.findAll({ include: ['mappedClients'] })
  let results = []
  for (let b in backends) {
    const backend = backends[b]
    const ba = new ExternalBackends()
    const instance = ba.getInstance(backend.type)

    let exid = backend.mappedClients.find(y => y.id === parseInt(clientId))
    if (exid) exid = exid.backend_x_client.externalId
    results.push(await instance.uploadFiles(backend.credentials, exid, files))
  }
  return results
}