suggestions.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class TextModule::Suggestions < BaseQuery
  4. description 'Search for text modules and return them with variable interpolation'
  5. argument :query, String, description: 'Query from the autocomplete field'
  6. argument :limit, Integer, required: false, description: 'Limit for the amount of entries'
  7. type [Gql::Types::TextModuleType], null: false
  8. def self.authorize(_obj, ctx)
  9. ctx.current_user.permissions?(['ticket.agent'])
  10. end
  11. def resolve(query:, template_render_context: nil, limit: 10)
  12. find_text_modules(query: query, limit: limit || 10)
  13. end
  14. def find_text_modules(query:, limit:)
  15. ::TextModule.joins('LEFT OUTER JOIN groups_text_modules ON groups_text_modules.text_module_id = text_modules.id')
  16. .distinct
  17. .where('((text_modules.name LIKE :query) OR (text_modules.keywords LIKE :query))', query: "%#{query.strip}%")
  18. .where(active: true)
  19. .where(where_agent_having_groups)
  20. .limit(limit)
  21. .order(:name)
  22. end
  23. private
  24. def where_agent_having_groups
  25. no_assigned_groups = 'groups_text_modules.group_id IS NULL'
  26. groups = context.current_user.groups.access(:read)
  27. if groups.any?
  28. groups_matcher = groups.map(&:id).join(',')
  29. return " (#{no_assigned_groups} OR (groups_text_modules.group_id IN (#{groups_matcher})))"
  30. end
  31. no_assigned_groups
  32. end
  33. end
  34. end