suggestions.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright (C) 2012-2025 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. argument :group_id, GraphQL::Types::ID, loads: Gql::Types::GroupType, required: false, description: 'Group to filter by'
  8. argument :ticket_id, GraphQL::Types::ID, loads: Gql::Types::TicketType, required: false, description: 'Optional ticket this is going to be inserted into'
  9. type [Gql::Types::TextModuleType], null: false
  10. def self.authorize(_obj, ctx)
  11. ctx.current_user.permissions?('ticket.agent')
  12. end
  13. def resolve(query:, group: nil, ticket: nil, template_render_context: nil, limit: 10)
  14. permission = ticket.present? ? :read : :create
  15. scope = TextModulePolicy::Scope
  16. .new(context.current_user, ::TextModule)
  17. .resolve(context: permission)
  18. if group
  19. scope = scope.available_in_groups(group)
  20. end
  21. scope.where('((text_modules.name LIKE :query) OR (text_modules.keywords LIKE :query))', query: "%#{SqlHelper.quote_like(query.strip)}%")
  22. .limit(limit || 10)
  23. .reorder(:name)
  24. end
  25. end
  26. end