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.

48 lines
1.4 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.db.storage.getTargets()
  14. targets = targets.map(tgt => ({
  15. ...tgt,
  16. config: _.sortBy(_.transform(tgt.config, (res, value, key) => {
  17. res.push({ key, value: JSON.stringify(value) })
  18. }, []), 'key')
  19. }))
  20. if (args.filter) { targets = graphHelper.filter(targets, args.filter) }
  21. if (args.orderBy) { targets = graphHelper.orderBy(targets, args.orderBy) }
  22. return targets
  23. }
  24. },
  25. StorageMutation: {
  26. async updateTargets(obj, args, context) {
  27. try {
  28. for (let tgt of args.targets) {
  29. await WIKI.db.storage.query().patch({
  30. isEnabled: tgt.isEnabled,
  31. mode: tgt.mode,
  32. config: _.reduce(tgt.config, (result, value, key) => {
  33. _.set(result, `${value.key}.value`, value.value)
  34. return result
  35. }, {})
  36. }).where('key', tgt.key)
  37. }
  38. return {
  39. responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
  40. }
  41. } catch (err) {
  42. return graphHelper.generateError(err)
  43. }
  44. }
  45. }
  46. }