recipient_autocomplete.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::ApplyValue::RecipientAutocomplete < FormUpdater::ApplyValue::Base
  3. def can_handle_field?(field:, field_attribute:)
  4. %w[to cc].include?(field)
  5. end
  6. def map_value(field:, config:)
  7. metadata = config['value']
  8. .split(',')
  9. .each_with_object({ value: [], options: [] }) do |elem, memo|
  10. (value, options) = user_or_email(elem.strip)
  11. memo[:value].push value
  12. memo[:options].push options
  13. end
  14. result[field].merge!(metadata)
  15. end
  16. private
  17. def user_or_email(recipient)
  18. user = find_user_by_recipient(recipient)
  19. if !user
  20. return [
  21. recipient,
  22. {
  23. value: recipient,
  24. label: recipient,
  25. }
  26. ]
  27. end
  28. [
  29. user.email,
  30. {
  31. value: user.email,
  32. label: user.email,
  33. heading: user.fullname,
  34. }
  35. ]
  36. end
  37. def find_user_by_recipient(recipient)
  38. ::User.search(
  39. query: recipient,
  40. limit: 1,
  41. current_user: context[:current_user],
  42. ).first
  43. end
  44. end