config.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Freshdesk::ObjectAttribute::Config < Sequencer::Unit::Base
  3. prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
  4. skip_any_action
  5. uses :resource, :sanitized_name, :model_class
  6. provides :config, :action
  7. def process
  8. if !data_type
  9. state.provide(:action, :skipped)
  10. return
  11. end
  12. state.provide(:config, provide_config)
  13. end
  14. private
  15. DATA_TYPE_MAP = {
  16. 'custom_date' => 'date',
  17. 'custom_date_time' => 'datetime',
  18. 'custom_checkbox' => 'boolean',
  19. 'custom_dropdown' => 'select',
  20. 'custom_text' => 'input',
  21. 'custom_number' => 'integer',
  22. 'custom_paragraph' => 'input',
  23. 'custom_decimal' => 'input', # Don't use 'integer' as it would cut off the fractional part.
  24. 'custom_url' => 'input',
  25. 'custom_phone_number' => 'input',
  26. 'default_ticket_type' => 'select',
  27. }.freeze
  28. DEFAULT_FIELD_NAME_MAP = {
  29. 'ticket_type' => 'type',
  30. }.freeze
  31. def provide_config
  32. {
  33. object: model_class.to_s,
  34. name: DEFAULT_FIELD_NAME_MAP[resource['name']] || sanitized_name,
  35. display: resource['label'],
  36. data_type: data_type,
  37. data_option: data_option,
  38. editable: true,
  39. active: true,
  40. screens: screens,
  41. position: resource['position'],
  42. created_by_id: 1,
  43. updated_by_id: 1,
  44. }
  45. end
  46. def data_type
  47. @data_type ||= DATA_TYPE_MAP[resource['type']]
  48. if !@data_type
  49. Rails.logger.debug { "The custom field type '#{resource['type']}' cannot be mapped to an internal field, skipping." }
  50. return
  51. end
  52. @data_type
  53. end
  54. def data_option
  55. {
  56. null: true,
  57. note: '',
  58. }.merge(data_type_options)
  59. end
  60. def data_type_options
  61. case data_type
  62. when 'date', 'datetime'
  63. {
  64. future: true,
  65. past: true,
  66. diff: 0,
  67. }
  68. when 'boolean'
  69. {
  70. default: false,
  71. options: {
  72. true => 'yes',
  73. false => 'no',
  74. },
  75. }
  76. when 'select'
  77. {
  78. default: '',
  79. options: options,
  80. }
  81. when 'input'
  82. case resource['type']
  83. when 'custom_phone_number'
  84. {
  85. type: 'tel',
  86. maxlength: 100,
  87. }
  88. when 'custom_url'
  89. {
  90. type: 'url',
  91. maxlength: 250,
  92. }
  93. else
  94. {
  95. type: 'text',
  96. maxlength: 255,
  97. }
  98. end
  99. when 'integer'
  100. {
  101. min: 0,
  102. max: 999_999_999,
  103. }
  104. else
  105. {}
  106. end
  107. end
  108. def screens
  109. {
  110. view: {
  111. '-all-' => {
  112. shown: true,
  113. null: true,
  114. },
  115. Customer: {
  116. shown: false,
  117. null: true,
  118. },
  119. },
  120. edit: {
  121. '-all-' => {
  122. shown: true,
  123. null: true,
  124. },
  125. Customer: {
  126. shown: false,
  127. null: true,
  128. },
  129. }
  130. }
  131. end
  132. def options
  133. resource['choices'].each_with_object({}) do |choice, result|
  134. result[choice] = choice
  135. end
  136. end
  137. end