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.

94 lines
3.0 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 = 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
  30. })
  31. })
  32. }
  33. }, []), 'key')
  34. }
  35. })
  36. // if (args.filter) { targets = graphHelper.filter(targets, args.filter) }
  37. if (args.orderBy) { targets = _.sortBy(targets, [args.orderBy]) }
  38. return targets
  39. },
  40. async status(obj, args, context, info) {
  41. let activeTargets = await WIKI.models.storage.query().where('isEnabled', true)
  42. return activeTargets.map(tgt => {
  43. const targetInfo = _.find(WIKI.data.storage, ['key', tgt.key]) || {}
  44. return {
  45. key: tgt.key,
  46. title: targetInfo.title,
  47. status: _.get(tgt, 'state.status', 'pending'),
  48. message: _.get(tgt, 'state.message', 'Initializing...'),
  49. lastAttempt: _.get(tgt, 'state.lastAttempt', null)
  50. }
  51. })
  52. }
  53. },
  54. StorageMutation: {
  55. async updateTargets(obj, args, context) {
  56. try {
  57. for (let tgt of args.targets) {
  58. await WIKI.models.storage.query().patch({
  59. isEnabled: tgt.isEnabled,
  60. mode: tgt.mode,
  61. syncInterval: tgt.syncInterval,
  62. config: _.reduce(tgt.config, (result, value, key) => {
  63. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  64. return result
  65. }, {}),
  66. state: {
  67. status: 'pending',
  68. message: 'Initializing...',
  69. lastAttempt: null
  70. }
  71. }).where('key', tgt.key)
  72. }
  73. await WIKI.models.storage.initTargets()
  74. return {
  75. responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
  76. }
  77. } catch (err) {
  78. return graphHelper.generateError(err)
  79. }
  80. },
  81. async executeAction(obj, args, context) {
  82. try {
  83. await WIKI.models.storage.executeAction(args.targetKey, args.handler)
  84. return {
  85. responseResult: graphHelper.generateSuccess('Action completed.')
  86. }
  87. } catch (err) {
  88. return graphHelper.generateError(err)
  89. }
  90. }
  91. }
  92. }