state_factory.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. module StateFactory # rubocop:disable Metrics/ModuleLength
  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, &)
  19. super
  20. update_pending_auto_states
  21. update_attribute_settings
  22. end
  23. def post_all_import_hook
  24. ::Ticket::State.where(id: 5, name: 'merged_tmp').each(&:destroy!)
  25. end
  26. def update_pending_auto_states
  27. ::Ticket::State.where(state_type_id: ::Ticket::StateType.where(name: 'pending action').map(&:id)).each do |state|
  28. close_state_name = state.name == 'pending auto close-' ? 'closed unsuccessful' : 'closed successful'
  29. update_state_with_next_state_id(state, close_state_name)
  30. end
  31. end
  32. def update_state_with_next_state_id(state, close_state_name)
  33. state.next_state_id = ::Ticket::State.find_by(name: close_state_name)&.id
  34. if state.next_state_id.blank?
  35. state.next_state_id = ::Ticket::State.by_category(:closed)&.first&.id
  36. end
  37. state.save
  38. end
  39. def update_attribute_settings
  40. return if Import::OTRS.diff?
  41. update_attribute
  42. update_ticket_attributes
  43. end
  44. def update_attribute
  45. update_default_create
  46. update_default_follow_up
  47. end
  48. def update_default_create
  49. state = ::Ticket::State.find_by(
  50. name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:state_default_create),
  51. active: true
  52. )
  53. return if !state
  54. state.default_create = true
  55. state.save!
  56. end
  57. def update_default_follow_up
  58. state = ::Ticket::State.find_by(
  59. name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:state_default_follow_up),
  60. active: true
  61. )
  62. return if !state
  63. state.default_follow_up = true
  64. state.save!
  65. end
  66. def update_ticket_attributes
  67. update_ticket_state
  68. reseed_dependent_objects
  69. end
  70. def update_ticket_state
  71. agent_new = fetch_ticket_states(%w[merged])
  72. agent_edit = fetch_ticket_states(%w[new merged])
  73. customer_new = fetch_ticket_states(%w[new closed])
  74. customer_edit = fetch_ticket_states(%w[open closed])
  75. ticket_state_id = ::ObjectManager::Attribute.get(
  76. object: 'Ticket',
  77. name: 'state_id',
  78. )
  79. ticket_state_id[:data_option][:filter] = agent_new
  80. ticket_state_id[:screens][:create_middle][:Customer] = customer_new
  81. ticket_state_id[:screens][:edit][:Agent] = agent_edit
  82. ticket_state_id[:screens][:edit][:Customer] = customer_edit
  83. update_ticket_attribute(ticket_state_id)
  84. end
  85. def fetch_ticket_states(ignore_state_names)
  86. ::Ticket::State.where(
  87. state_type_id: ::Ticket::StateType.where.not(name: ignore_state_names)
  88. ).pluck(:id)
  89. end
  90. def update_ticket_attribute(attribute)
  91. ::ObjectManager::Attribute.add(
  92. object_lookup_id: attribute[:object_lookup_id],
  93. name: attribute[:name],
  94. display: attribute[:display],
  95. data_type: attribute[:data_type],
  96. data_option: attribute[:data_option],
  97. active: attribute[:active],
  98. screens: attribute[:screens],
  99. force: true # otherwise _id as a name is not permitted
  100. )
  101. end
  102. def reseed_dependent_objects
  103. Overview.reseed
  104. Trigger.reseed
  105. Macro.reseed
  106. # we don't have to re-seed the ObjectManager
  107. # Attributes since they contain the already
  108. # imported DynamicFields which will be lost
  109. ObjectManager::Attribute.seed
  110. end
  111. end
  112. end
  113. end