diff --git a/lib/import/otrs/diff.rb b/lib/import/otrs/diff.rb index 316457953..40104614b 100644 --- a/lib/import/otrs/diff.rb +++ b/lib/import/otrs/diff.rb @@ -9,6 +9,11 @@ module Import diff end + def diff? + return true if @diff + false + end + private def diff_import_possible? @@ -20,6 +25,8 @@ module Import def diff log 'Start diff...' + @diff = true + check_import_mode updateable_objects diff --git a/lib/import/otrs/priority_factory.rb b/lib/import/otrs/priority_factory.rb index ebd405775..7122dfa8f 100644 --- a/lib/import/otrs/priority_factory.rb +++ b/lib/import/otrs/priority_factory.rb @@ -2,6 +2,32 @@ module Import module OTRS module PriorityFactory extend Import::Factory + + # rubocop:disable Style/ModuleFunction + extend self + + def import_loop(records, *_args, &import_block) + super + update_attribute_settings + end + + def update_attribute_settings + return if Import::OTRS.diff? + update_attribute + end + + def update_attribute + priority = ::Ticket::Priority.find_by( + name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:priority_default_create), + active: true + ) + return if !priority + + priority.default_create = true + priority.callback_loop = true + + priority.save + end end end end diff --git a/lib/import/otrs/state_factory.rb b/lib/import/otrs/state_factory.rb index b19802ae9..b9dcb9eec 100644 --- a/lib/import/otrs/state_factory.rb +++ b/lib/import/otrs/state_factory.rb @@ -20,6 +20,48 @@ module Import def import_loop(records, *_args, &import_block) super + update_attribute_settings + end + + def update_attribute_settings + return if Import::OTRS.diff? + + update_attribute + update_ticket_attributes + end + + def update_attribute + update_default_create + update_default_follow_up + end + + def update_default_create + state = ::Ticket::State.find_by( + name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:state_default_create), + active: true + ) + return if !state + + state.default_create = true + state.callback_loop = true + + state.save + end + + def update_default_follow_up + state = ::Ticket::State.find_by( + name: Import::OTRS::SysConfigFactory.postmaster_default_lookup(:state_default_follow_up), + active: true + ) + return if !state + + state.default_follow_up = true + state.callback_loop = true + + state.save + end + + def update_ticket_attributes update_ticket_state update_ticket_pending_time end diff --git a/lib/import/otrs/sys_config_factory.rb b/lib/import/otrs/sys_config_factory.rb index 886dc4d17..a17abbe06 100644 --- a/lib/import/otrs/sys_config_factory.rb +++ b/lib/import/otrs/sys_config_factory.rb @@ -9,9 +9,15 @@ module Import settings.each do |setting| next if direct_copy?(setting) next if number_generator?(setting) + next if postmaster_default?(setting) end end + def postmaster_default_lookup(key) + @postmaster_defaults ||= {} + @postmaster_defaults[key] + end + private def direct_settings @@ -46,6 +52,22 @@ module Import true end + + def postmaster_default?(setting) + + relevant_configs = %w(PostmasterDefaultPriority PostmasterDefaultState PostmasterFollowUpState) + return false if !relevant_configs.include?(setting['Key']) + + map = { + 'PostmasterDefaultPriority' => :priority_default_create, + 'PostmasterDefaultState' => :state_default_create, + 'PostmasterFollowUpState' => :state_default_follow_up, + } + + @postmaster_defaults ||= {} + @postmaster_defaults[ map[setting['Key']] ] = setting['Value'] + true + end end end end diff --git a/spec/import/otrs/priority_factory_spec.rb b/spec/import/otrs/priority_factory_spec.rb index 0f3d82288..54ada125f 100644 --- a/spec/import/otrs/priority_factory_spec.rb +++ b/spec/import/otrs/priority_factory_spec.rb @@ -12,4 +12,32 @@ RSpec.describe Import::OTRS::PriorityFactory do expect(::Import::OTRS::Priority).to receive(:new).with(import_data) described_class.import([import_data]) end + + it 'sets default create Priority' do + priority = ::Ticket::Priority.first + priority.default_create = false + priority.callback_loop = true + priority.save + + expect(Import::OTRS::SysConfigFactory).to receive(:postmaster_default_lookup).with(:priority_default_create).and_return(priority.name) + + described_class.update_attribute_settings + priority.reload + + expect(priority.default_create).to be true + end + + it "doesn't set default create Priority in diff import" do + priority = ::Ticket::Priority.first + priority.default_create = false + priority.callback_loop = true + priority.save + + expect(Import::OTRS).to receive(:diff?).and_return(true) + + described_class.update_attribute_settings + priority.reload + + expect(priority.default_create).to be false + end end diff --git a/spec/import/otrs/state_factory_spec.rb b/spec/import/otrs/state_factory_spec.rb index 06a5d8c9f..3601a6547 100644 --- a/spec/import/otrs/state_factory_spec.rb +++ b/spec/import/otrs/state_factory_spec.rb @@ -1,6 +1,8 @@ require 'rails_helper' require 'import/transaction_factory_examples' +RSpec::Matchers.define_negated_matcher :not_change, :change + RSpec.describe Import::OTRS::StateFactory do it_behaves_like 'Import::TransactionFactory' @@ -45,4 +47,65 @@ RSpec.describe Import::OTRS::StateFactory do ticket_pending_time.data_option } end + + it "doesn't update ObjectManager Ticket state_id and pending_time filter in diff import" do + + ticket_state_id = ::ObjectManager::Attribute.get( + object: 'Ticket', + name: 'state_id', + ) + ticket_pending_time = ::ObjectManager::Attribute.get( + object: 'Ticket', + name: 'pending_time', + ) + + expect(Import::OTRS).to receive(:diff?).and_return(true) + + expect { + described_class.update_attribute_settings + + # sync changes + ticket_state_id.reload + ticket_pending_time.reload + }.to not_change { + ticket_state_id.data_option + }.and not_change { + ticket_state_id.screens + }.and not_change { + ticket_pending_time.data_option + } + end + + it 'sets default create and update State' do + state = ::Ticket::State.first + state.default_create = false + state.default_follow_up = false + state.callback_loop = true + state.save + + expect(Import::OTRS::SysConfigFactory).to receive(:postmaster_default_lookup).with(:state_default_create).and_return(state.name) + expect(Import::OTRS::SysConfigFactory).to receive(:postmaster_default_lookup).with(:state_default_follow_up).and_return(state.name) + + described_class.update_attribute + state.reload + + expect(state.default_create).to be true + expect(state.default_follow_up).to be true + end + + it "doesn't set default create and update State in diff import" do + state = ::Ticket::State.first + state.default_create = false + state.default_follow_up = false + state.callback_loop = true + state.save + + expect(Import::OTRS).to receive(:diff?).and_return(true) + + described_class.update_attribute_settings + state.reload + + expect(state.default_create).to be false + expect(state.default_follow_up).to be false + end end diff --git a/spec/import/otrs/sys_config_factory_spec.rb b/spec/import/otrs/sys_config_factory_spec.rb index 119568af4..cf646cb3b 100644 --- a/spec/import/otrs/sys_config_factory_spec.rb +++ b/spec/import/otrs/sys_config_factory_spec.rb @@ -3,4 +3,20 @@ require 'import/import_factory_examples' RSpec.describe Import::OTRS::SysConfigFactory do it_behaves_like 'Import factory' + + it 'stores default postmaster values' do + + value = 'new' + + settings = [ + { + 'Key' => 'PostmasterDefaultState', + 'Value' => value + } + ] + + described_class.import(settings) + + expect(described_class.postmaster_default_lookup(:state_default_create)).to eq(value) + end end