state_factory.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. module StateFactory
  5. extend Import::TransactionFactory
  6. # rubocop:disable Style/ModuleFunction
  7. extend self
  8. def pre_import_hook(_records, *_args)
  9. backup
  10. end
  11. def backup
  12. # rename states to handle not uniq issues
  13. ::Ticket::State.all.each do |state|
  14. state.name = "#{state.name}_tmp"
  15. state.save
  16. end
  17. end
  18. def import_loop(records, *_args, &import_block)
  19. super
  20. update_attribute_settings
  21. end
  22. def update_attribute_settings
  23. return if Import::OTRS.diff?
  24. update_attribute
  25. update_ticket_attributes
  26. end
  27. def update_attribute
  28. update_default_create
  29. update_default_follow_up
  30. end
  31. def update_default_create
  32. state = ::Ticket::State.find_by(
  33. name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:state_default_create),
  34. active: true
  35. )
  36. return if !state
  37. state.default_create = true
  38. state.save!
  39. end
  40. def update_default_follow_up
  41. state = ::Ticket::State.find_by(
  42. name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:state_default_follow_up),
  43. active: true
  44. )
  45. return if !state
  46. state.default_follow_up = true
  47. state.save!
  48. end
  49. def update_ticket_attributes
  50. update_ticket_state
  51. update_ticket_pending_time
  52. reseed_dependent_objects
  53. end
  54. def update_ticket_state
  55. agent_new = ::Ticket::State.where(
  56. state_type_id: ::Ticket::StateType.where.not(name: %w[merged removed])
  57. ).pluck(:id)
  58. agent_edit = ::Ticket::State.where(
  59. state_type_id: ::Ticket::StateType.where.not(name: %w[new merged removed])
  60. ).pluck(:id)
  61. customer_new = ::Ticket::State.where(
  62. state_type_id: ::Ticket::StateType.where.not(name: %w[new closed])
  63. ).pluck(:id)
  64. customer_edit = ::Ticket::State.where(
  65. state_type_id: ::Ticket::StateType.where.not(name: %w[open closed])
  66. ).pluck(:id)
  67. ticket_state_id = ::ObjectManager::Attribute.get(
  68. object: 'Ticket',
  69. name: 'state_id',
  70. )
  71. ticket_state_id[:data_option][:filter] = agent_new
  72. ticket_state_id[:screens][:create_middle][:Customer] = customer_new
  73. ticket_state_id[:screens][:edit][:Agent] = agent_edit
  74. ticket_state_id[:screens][:edit][:Customer] = customer_edit
  75. update_ticket_attribute(ticket_state_id)
  76. end
  77. def update_ticket_pending_time
  78. pending_state_ids = ::Ticket::State.where(
  79. state_type_id: ::Ticket::StateType.where(name: ['pending reminder', 'pending action'])
  80. ).pluck(:id)
  81. ticket_pending_time = ::ObjectManager::Attribute.get(
  82. object: 'Ticket',
  83. name: 'pending_time',
  84. )
  85. ticket_pending_time[:data_option][:required_if][:state_id] = pending_state_ids
  86. ticket_pending_time[:data_option][:required_if][:state_id] = pending_state_ids
  87. update_ticket_attribute(ticket_pending_time)
  88. end
  89. def update_ticket_attribute(attribute)
  90. ::ObjectManager::Attribute.add(
  91. object_lookup_id: attribute[:object_lookup_id],
  92. name: attribute[:name],
  93. display: attribute[:display],
  94. data_type: attribute[:data_type],
  95. data_option: attribute[:data_option],
  96. active: attribute[:active],
  97. screens: attribute[:screens],
  98. force: true # otherwise _id as a name is not permitted
  99. )
  100. end
  101. def reseed_dependent_objects
  102. Overview.reseed
  103. Trigger.reseed
  104. Macro.reseed
  105. # we don't have to re-seed the ObjectManager
  106. # Attributes since they contain the already
  107. # imported DynamicFields which will be lost
  108. ObjectManager::Attribute.seed
  109. end
  110. end
  111. end
  112. end