recipient.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class AutocompleteSearch::Recipient < AutocompleteSearch::User
  4. description 'Search for recipients'
  5. argument :input, Gql::Types::Input::AutocompleteSearch::RecipientInputType, required: true, description: 'The input object for the recipient autocomplete search'
  6. type [Gql::Types::AutocompleteSearch::RecipientEntryType], null: false
  7. def post_process(results, input:)
  8. results.flat_map do |user|
  9. case input[:contact]
  10. when 'phone'
  11. user_phone_contacts(user)
  12. else
  13. user_email_contact(user)
  14. end
  15. end.map { |user| coerce_to_result(user) }
  16. end
  17. def coerce_to_result(contact)
  18. {
  19. value: contact[:contact],
  20. label: contact[:contact],
  21. heading: contact[:name],
  22. }
  23. end
  24. private
  25. def user_phone_contacts(user)
  26. contacts = []
  27. if user.mobile.present?
  28. contacts.push({
  29. name: user.fullname,
  30. contact: user.mobile,
  31. })
  32. end
  33. if user.phone.present?
  34. contacts.push({
  35. name: user.fullname,
  36. contact: user.phone,
  37. })
  38. end
  39. contacts
  40. end
  41. def user_email_contact(user)
  42. return [] if user.email.empty?
  43. {
  44. name: user.fullname,
  45. contact: user.email,
  46. }
  47. end
  48. end
  49. end