custom_fields.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Kayako::Mapping::CustomFields < Sequencer::Unit::Base
  3. include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
  4. uses :resource, :field_map, :model_class, :default_language
  5. def process
  6. provide_mapped do
  7. custom_fields || {}
  8. end
  9. end
  10. private
  11. def custom_fields
  12. resource['custom_fields']&.each_with_object({}) do |item, result|
  13. field = item['field']
  14. local_name = custom_fields_map[field['key']]
  15. next if local_name.nil? || item['value'].empty?
  16. field_type_instance = attribute_type_instance(field)
  17. result[ local_name.to_sym ] = local_value(local_name, field_type_instance, item['value'])
  18. end
  19. end
  20. def local_value(local_name, field_type_instance, value)
  21. begin
  22. field_type_instance.local_value(value)
  23. rescue => e
  24. logger.error "Error when setting local value for custom field (#{local_name}) for case: #{resource['id']}."
  25. logger.error e
  26. nil
  27. end
  28. end
  29. def custom_fields_map
  30. @custom_fields_map ||= field_map[model_class.name]
  31. end
  32. def attribute_type_instance(field)
  33. "Sequencer::Unit::Import::Kayako::ObjectAttribute::AttributeType::#{field['type'].capitalize}".constantize.new(field, default_language)
  34. rescue
  35. nil
  36. end
  37. end