generic.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class AutocompleteSearch::Generic < BaseQuery
  4. description 'Generic autocomplete search'
  5. argument :input, Gql::Types::Input::AutocompleteSearch::GenericInputType, required: true, description: 'The input object for the autocomplete search'
  6. type [Gql::Types::AutocompleteSearch::GenericEntryType], null: false
  7. def resolve(input:)
  8. input = input.to_h
  9. query = input[:query]
  10. limit = input[:limit] || 50
  11. return [] if query.blank?
  12. objects = Service::Search.new(current_user: context.current_user).execute(
  13. term: query,
  14. objects: input[:only_in] || Gql::Types::SearchResultType.searchable_models,
  15. options: { limit: limit }
  16. )
  17. post_process(objects, input:)
  18. end
  19. def post_process(results, input:)
  20. results.map { |object| coerce_to_result(object) }
  21. end
  22. def coerce_to_result(object)
  23. {
  24. value: object.id,
  25. label: label(object),
  26. heading: heading(object),
  27. heading_placeholder: heading_placeholder(object),
  28. object: object,
  29. }
  30. end
  31. private
  32. def label(object)
  33. case object
  34. when ::User
  35. label_user(object)
  36. when ::Organization
  37. object.name
  38. when ::Ticket
  39. "##{object.number} - #{object.title}"
  40. end
  41. end
  42. def label_user(user)
  43. user.fullname.presence || user.phone.presence || user.login
  44. end
  45. def heading(object)
  46. case object
  47. when ::User
  48. object.organization&.name
  49. when ::Organization
  50. __('%s people')
  51. when ::Ticket
  52. label_user(object.customer)
  53. end
  54. end
  55. def heading_placeholder(object)
  56. case object
  57. when ::Organization
  58. [object.members.size]
  59. else
  60. []
  61. end
  62. end
  63. end
  64. end