wikijs-fork/server/graph/resolvers/storage.js

102 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2018-06-26 02:04:47 +00:00
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) {
let targets = await WIKI.models.storage.getTargets()
targets = _.sortBy(targets.map(tgt => {
const targetInfo = _.find(WIKI.data.storage, ['key', tgt.key]) || {}
return {
2018-08-04 21:27:55 +00:00
...targetInfo,
...tgt,
2019-02-17 06:32:35 +00:00
hasSchedule: (targetInfo.schedule !== false),
syncInterval: tgt.syncInterval || targetInfo.schedule || 'P0D',
2019-02-17 06:32:35 +00:00
syncIntervalDefault: targetInfo.schedule,
config: _.sortBy(_.transform(tgt.config, (res, value, key) => {
const configData = _.get(targetInfo.props, key, false)
if (configData) {
res.push({
key,
value: JSON.stringify({
...configData,
value: (configData.sensitive && value.length > 0) ? '********' : value
})
})
}
}, []), 'key')
}
}), ['title', 'key'])
2018-06-26 02:04:47 +00:00
return targets
2019-02-17 06:32:35 +00:00
},
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'),
2019-02-25 04:48:28 +00:00
message: _.get(tgt, 'state.message', 'Initializing...'),
lastAttempt: _.get(tgt, 'state.lastAttempt', null)
2019-02-17 06:32:35 +00:00
}
})
2018-06-26 02:04:47 +00:00
}
},
StorageMutation: {
async updateTargets(obj, args, context) {
try {
let dbTargets = await WIKI.models.storage.getTargets()
2018-06-26 02:04:47 +00:00
for (let tgt of args.targets) {
const currentDbTarget = _.find(dbTargets, ['key', tgt.key])
if (!currentDbTarget) {
continue
}
await WIKI.models.storage.query().patch({
2018-06-26 02:04:47 +00:00
isEnabled: tgt.isEnabled,
mode: tgt.mode,
2019-02-17 06:32:35 +00:00
syncInterval: tgt.syncInterval,
2018-06-26 02:04:47 +00:00
config: _.reduce(tgt.config, (result, value, key) => {
let configValue = _.get(JSON.parse(value.value), 'v', null)
if (configValue === '********') {
configValue = _.get(currentDbTarget.config, value.key, '')
}
_.set(result, `${value.key}`, configValue)
2018-06-26 02:04:47 +00:00
return result
2019-02-17 06:32:35 +00:00
}, {}),
state: {
status: 'pending',
2019-02-25 04:48:28 +00:00
message: 'Initializing...',
lastAttempt: null
2019-02-17 06:32:35 +00:00
}
2018-06-26 02:04:47 +00:00
}).where('key', tgt.key)
}
2019-02-03 07:48:30 +00:00
await WIKI.models.storage.initTargets()
2018-06-26 02:04:47 +00:00
return {
responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
}
} catch (err) {
return graphHelper.generateError(err)
}
},
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)
}
2018-06-26 02:04:47 +00:00
}
}
}