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.

58 lines
1.9 KiB

  1. const _ = require('lodash')
  2. const graphHelper = require('../../helpers/graph')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async search() { return {} }
  7. },
  8. Mutation: {
  9. async search() { return {} }
  10. },
  11. SearchQuery: {
  12. async searchEngines(obj, args, context, info) {
  13. let searchEngines = await WIKI.models.searchEngines.getSearchEngines()
  14. searchEngines = searchEngines.map(searchEngine => {
  15. const searchEngineInfo = _.find(WIKI.data.searchEngines, ['key', searchEngine.key]) || {}
  16. return {
  17. ...searchEngineInfo,
  18. ...searchEngine,
  19. config: _.sortBy(_.transform(searchEngine.config, (res, value, key) => {
  20. const configData = _.get(searchEngineInfo.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) { searchEngines = graphHelper.filter(searchEngines, args.filter) }
  32. if (args.orderBy) { searchEngines = graphHelper.orderBy(searchEngines, args.orderBy) }
  33. return searchEngines
  34. }
  35. },
  36. SearchMutation: {
  37. async updateSearchEngines(obj, args, context) {
  38. try {
  39. for (let searchEngine of args.searchEngines) {
  40. await WIKI.models.searchEngines.query().patch({
  41. isEnabled: searchEngine.isEnabled,
  42. config: _.reduce(searchEngine.config, (result, value, key) => {
  43. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  44. return result
  45. }, {})
  46. }).where('key', searchEngine.key)
  47. }
  48. return {
  49. responseResult: graphHelper.generateSuccess('Search Engines updated successfully')
  50. }
  51. } catch (err) {
  52. return graphHelper.generateError(err)
  53. }
  54. }
  55. }
  56. }