user.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class AutocompleteSearch::User < BaseQuery
  4. description 'Search for users'
  5. argument :input, Gql::Types::Input::AutocompleteSearch::UserInputType, required: true, description: 'The input object for the autocomplete search'
  6. type [Gql::Types::AutocompleteSearch::UserEntryType], null: false
  7. def resolve(input:)
  8. input = input.to_h
  9. query = input[:query]
  10. limit = input[:limit] || 50
  11. return [] if query.strip.empty?
  12. users = find_users(query:, limit:)
  13. users = reject_user(users, input:)
  14. post_process(users, input:)
  15. end
  16. def find_users(query:, limit:)
  17. ::User.search(
  18. query:,
  19. limit:,
  20. current_user: context.current_user,
  21. )
  22. end
  23. def reject_user(results, input:)
  24. return results if input[:except_internal_id].blank?
  25. results.reject { |user| user.id == input[:except_internal_id] }
  26. end
  27. def post_process(results, input:)
  28. results.map { |user| coerce_to_result(user) }
  29. end
  30. def coerce_to_result(user)
  31. {
  32. value: user.id,
  33. label: label(user),
  34. heading: user.organization&.name,
  35. user: user,
  36. }
  37. end
  38. private
  39. def label(user)
  40. return user.fullname if user.fullname.present?
  41. return user.phone if user.phone.present?
  42. user.login
  43. end
  44. end
  45. end