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.

57 lines
1.6 KiB

  1. const _ = require('lodash')
  2. const graphHelper = require('../../helpers/graph')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async analytics() { return {} }
  7. },
  8. Mutation: {
  9. async analytics() { return {} }
  10. },
  11. AnalyticsQuery: {
  12. async providers(obj, args, context, info) {
  13. let providers = await WIKI.models.analytics.getProviders(args.isEnabled)
  14. providers = providers.map(stg => {
  15. const providerInfo = _.find(WIKI.data.analytics, ['key', stg.key]) || {}
  16. return {
  17. ...providerInfo,
  18. ...stg,
  19. config: _.sortBy(_.transform(stg.config, (res, value, key) => {
  20. const configData = _.get(providerInfo.props, key, {})
  21. res.push({
  22. key,
  23. value: JSON.stringify({
  24. ...configData,
  25. value
  26. })
  27. })
  28. }, []), 'key')
  29. }
  30. })
  31. return providers
  32. }
  33. },
  34. AnalyticsMutation: {
  35. async updateProviders(obj, args, context) {
  36. try {
  37. for (let str of args.providers) {
  38. await WIKI.models.analytics.query().patch({
  39. isEnabled: str.isEnabled,
  40. config: _.reduce(str.config, (result, value, key) => {
  41. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  42. return result
  43. }, {})
  44. }).where('key', str.key)
  45. await WIKI.cache.del('analytics')
  46. }
  47. return {
  48. responseResult: graphHelper.generateSuccess('Providers updated successfully')
  49. }
  50. } catch (err) {
  51. return graphHelper.generateError(err)
  52. }
  53. }
  54. }
  55. }