storage.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, {})
  24. res.push({
  25. key,
  26. value: JSON.stringify({
  27. ...configData,
  28. value
  29. })
  30. })
  31. }, []), 'key')
  32. }
  33. })
  34. if (args.filter) { targets = graphHelper.filter(targets, args.filter) }
  35. if (args.orderBy) { targets = graphHelper.orderBy(targets, args.orderBy) }
  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. }
  48. })
  49. }
  50. },
  51. StorageMutation: {
  52. async updateTargets(obj, args, context) {
  53. try {
  54. for (let tgt of args.targets) {
  55. await WIKI.models.storage.query().patch({
  56. isEnabled: tgt.isEnabled,
  57. mode: tgt.mode,
  58. syncInterval: tgt.syncInterval,
  59. config: _.reduce(tgt.config, (result, value, key) => {
  60. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  61. return result
  62. }, {}),
  63. state: {
  64. status: 'pending',
  65. message: 'Initializing...'
  66. }
  67. }).where('key', tgt.key)
  68. }
  69. await WIKI.models.storage.initTargets()
  70. return {
  71. responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
  72. }
  73. } catch (err) {
  74. return graphHelper.generateError(err)
  75. }
  76. }
  77. }
  78. }