storage.js 3.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const _ = require('lodash')
const graphHelper = require('../../helpers/graph')

/* global WIKI */

module.exports = {
  Query: {
    async storage() { return {} }
  },
  Mutation: {
    async storage() { return {} }
  },
  StorageQuery: {
    async targets(obj, args, context, info) {
15
      let targets = await WIKI.models.storage.getTargets()
16
      targets = _.sortBy(targets.map(tgt => {
17 18
        const targetInfo = _.find(WIKI.data.storage, ['key', tgt.key]) || {}
        return {
19
          ...targetInfo,
20
          ...tgt,
Nick's avatar
Nick committed
21
          hasSchedule: (targetInfo.schedule !== false),
22
          syncInterval: tgt.syncInterval || targetInfo.schedule || 'P0D',
Nick's avatar
Nick committed
23
          syncIntervalDefault: targetInfo.schedule,
24
          config: _.sortBy(_.transform(tgt.config, (res, value, key) => {
25 26 27 28 29 30
            const configData = _.get(targetInfo.props, key, false)
            if (configData) {
              res.push({
                key,
                value: JSON.stringify({
                  ...configData,
31
                  value: (configData.sensitive && value.length > 0) ? '********' : value
32
                })
33
              })
34
            }
35 36
          }, []), 'key')
        }
37
      }), ['title', 'key'])
38
      return targets
Nick's avatar
Nick committed
39 40 41 42 43 44 45 46 47
    },
    async status(obj, args, context, info) {
      let activeTargets = await WIKI.models.storage.query().where('isEnabled', true)
      return activeTargets.map(tgt => {
        const targetInfo = _.find(WIKI.data.storage, ['key', tgt.key]) || {}
        return {
          key: tgt.key,
          title: targetInfo.title,
          status: _.get(tgt, 'state.status', 'pending'),
Nick's avatar
Nick committed
48 49
          message: _.get(tgt, 'state.message', 'Initializing...'),
          lastAttempt: _.get(tgt, 'state.lastAttempt', null)
Nick's avatar
Nick committed
50 51
        }
      })
52 53 54 55 56
    }
  },
  StorageMutation: {
    async updateTargets(obj, args, context) {
      try {
57
        let dbTargets = await WIKI.models.storage.getTargets()
58
        for (let tgt of args.targets) {
59 60 61 62
          const currentDbTarget = _.find(dbTargets, ['key', tgt.key])
          if (!currentDbTarget) {
            continue
          }
63
          await WIKI.models.storage.query().patch({
64 65
            isEnabled: tgt.isEnabled,
            mode: tgt.mode,
Nick's avatar
Nick committed
66
            syncInterval: tgt.syncInterval,
67
            config: _.reduce(tgt.config, (result, value, key) => {
68 69 70 71 72
              let configValue = _.get(JSON.parse(value.value), 'v', null)
              if (configValue === '********') {
                configValue = _.get(currentDbTarget.config, value.key, '')
              }
              _.set(result, `${value.key}`, configValue)
73
              return result
Nick's avatar
Nick committed
74 75 76
            }, {}),
            state: {
              status: 'pending',
Nick's avatar
Nick committed
77 78
              message: 'Initializing...',
              lastAttempt: null
Nick's avatar
Nick committed
79
            }
80 81
          }).where('key', tgt.key)
        }
Nick's avatar
Nick committed
82
        await WIKI.models.storage.initTargets()
83 84 85 86 87 88
        return {
          responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
89 90 91 92 93 94 95 96 97 98
    },
    async executeAction(obj, args, context) {
      try {
        await WIKI.models.storage.executeAction(args.targetKey, args.handler)
        return {
          responseResult: graphHelper.generateSuccess('Action completed.')
        }
      } catch (err) {
        return graphHelper.generateError(err)
      }
99 100 101
    }
  }
}