You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

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