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.

59 lines
1.7 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. config: _.sortBy(_.transform(tgt.config, (res, value, key) => {
  20. const configData = _.get(targetInfo.props, key, {})
  21. res.push({
  22. key,
  23. value: JSON.stringify({
  24. ...configData,
  25. value
  26. })
  27. })
  28. }, []), 'key')
  29. }
  30. })
  31. if (args.filter) { targets = graphHelper.filter(targets, args.filter) }
  32. if (args.orderBy) { targets = graphHelper.orderBy(targets, args.orderBy) }
  33. return targets
  34. }
  35. },
  36. StorageMutation: {
  37. async updateTargets(obj, args, context) {
  38. try {
  39. for (let tgt of args.targets) {
  40. await WIKI.models.storage.query().patch({
  41. isEnabled: tgt.isEnabled,
  42. mode: tgt.mode,
  43. config: _.reduce(tgt.config, (result, value, key) => {
  44. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  45. return result
  46. }, {})
  47. }).where('key', tgt.key)
  48. }
  49. return {
  50. responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
  51. }
  52. } catch (err) {
  53. return graphHelper.generateError(err)
  54. }
  55. }
  56. }
  57. }