state_factory.rb 4.0 KB

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