state_factory.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 { |state|
  13. state.name = state.name + '_tmp'
  14. state.save
  15. }
  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.callback_loop = 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.callback_loop = true
  48. state.save
  49. end
  50. def update_ticket_attributes
  51. update_ticket_state
  52. update_ticket_pending_time
  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. end
  102. end
  103. end