generic.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. Service::Search
  13. .new(current_user: context.current_user,
  14. query: query,
  15. objects: input[:only_in] || Gql::Types::SearchResultType.searchable_models,
  16. options: { limit: limit })
  17. .execute
  18. .flattened
  19. .map { |object| coerce_to_result(object) }
  20. end
  21. def coerce_to_result(object)
  22. {
  23. value: object.id,
  24. label: label(object),
  25. heading: heading(object),
  26. heading_placeholder: heading_placeholder(object),
  27. object: object,
  28. }
  29. end
  30. private
  31. def label(object)
  32. case object
  33. when ::User
  34. label_user(object)
  35. when ::Organization
  36. object.name
  37. when ::Ticket
  38. "##{object.number} - #{object.title}"
  39. end
  40. end
  41. def label_user(user)
  42. user.fullname.presence || user.phone.presence || user.login
  43. end
  44. def heading(object)
  45. case object
  46. when ::User
  47. object.organization&.name
  48. when ::Organization
  49. __('%s people')
  50. when ::Ticket
  51. label_user(object.customer)
  52. end
  53. end
  54. def heading_placeholder(object)
  55. case object
  56. when ::Organization
  57. [object.all_members.size]
  58. else
  59. []
  60. end
  61. end
  62. end
  63. end