user_autocomplete.rb 994 B

12345678910111213141516171819202122232425262728
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::ApplyValue::UserAutocomplete < FormUpdater::ApplyValue::Base
  3. def can_handle_field?(field:, field_attribute:)
  4. field_attribute&.data_option&.[]('relation') == 'User'
  5. end
  6. def map_value(field:, config:)
  7. user = User.find_by(id: config['value'])
  8. return if !user
  9. user_obj = user.attributes
  10. .slice('active', 'email', 'firstname', 'fullname', 'image', 'lastname', 'mobile', 'out_of_office', 'out_of_office_end_at', 'out_of_office_start_at', 'phone', 'source', 'vip')
  11. .merge({
  12. '__typename' => 'User',
  13. 'id' => Gql::ZammadSchema.id_from_internal_id('User', user.id),
  14. })
  15. result[field][:value] = user.id
  16. result[field][:options] = [{
  17. value: user.id,
  18. label: user.fullname.presence || user.phone.presence || user.login,
  19. heading: user.organization&.name,
  20. object: user_obj,
  21. }]
  22. end
  23. end