idoit_object_search.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class Ticket::ExternalReferences::IdoitObjectSearch < BaseQuery
  4. description 'Search for idoit objects belonging to a selected type'
  5. argument :idoit_type_id, String, required: false, description: 'Selected idoit object type id to search in'
  6. argument :query, String, required: false, description: 'Query for searching the idoit objects'
  7. argument :limit, Integer, required: false, description: 'Limit for the amount of entries'
  8. type [Gql::Types::Ticket::ExternalReferences::IdoitObjectType], null: false
  9. def self.authorize(_obj, ctx)
  10. Setting.get('idoit_integration') && ctx.current_user.permissions?('ticket.agent')
  11. end
  12. def resolve(query: '', limit: 10, idoit_type_id: nil)
  13. Idoit.query('cmdb.objects', build_filter(idoit_type_id:, query:))['result']&.first(limit)
  14. end
  15. private
  16. def build_filter(idoit_type_id:, query:)
  17. {}.tap do |filter|
  18. if idoit_type_id
  19. filter['type'] = idoit_type_id
  20. end
  21. search_query = normalize_query(query)
  22. if search_query.present?
  23. filter['title'] = search_query
  24. end
  25. end
  26. end
  27. def normalize_query(query)
  28. query = query.strip
  29. return '' if query.blank? || query == '*'
  30. "%#{query.delete_prefix('*').delete_suffix('*')}%"
  31. end
  32. end
  33. end