state_factory.rb 4.0 KB

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