state_factory.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (C) 2012-2022 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, &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. 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_attribute(attribute)
  77. ::ObjectManager::Attribute.add(
  78. object_lookup_id: attribute[:object_lookup_id],
  79. name: attribute[:name],
  80. display: attribute[:display],
  81. data_type: attribute[:data_type],
  82. data_option: attribute[:data_option],
  83. active: attribute[:active],
  84. screens: attribute[:screens],
  85. force: true # otherwise _id as a name is not permitted
  86. )
  87. end
  88. def reseed_dependent_objects
  89. Overview.reseed
  90. Trigger.reseed
  91. Macro.reseed
  92. # we don't have to re-seed the ObjectManager
  93. # Attributes since they contain the already
  94. # imported DynamicFields which will be lost
  95. ObjectManager::Attribute.seed
  96. end
  97. end
  98. end
  99. end