diff --git a/lib/import/otrs/state_factory.rb b/lib/import/otrs/state_factory.rb index 6da18c9b1..b19802ae9 100644 --- a/lib/import/otrs/state_factory.rb +++ b/lib/import/otrs/state_factory.rb @@ -17,6 +17,71 @@ module Import state.save } end + + def import_loop(records, *_args, &import_block) + super + update_ticket_state + update_ticket_pending_time + end + + def update_ticket_state + agent_new = ::Ticket::State.where( + state_type_id: ::Ticket::StateType.where.not(name: %w(merged removed)) + ).pluck(:id) + + agent_edit = ::Ticket::State.where( + state_type_id: ::Ticket::StateType.where.not(name: %w(new merged removed)) + ).pluck(:id) + + customer_new = ::Ticket::State.where( + state_type_id: ::Ticket::StateType.where.not(name: %w(new closed)) + ).pluck(:id) + + customer_edit = ::Ticket::State.where( + state_type_id: ::Ticket::StateType.where.not(name: %w(open closed)) + ).pluck(:id) + + ticket_state_id = ::ObjectManager::Attribute.get( + object: 'Ticket', + name: 'state_id', + ) + + ticket_state_id[:data_option][:filter] = agent_new + ticket_state_id[:screens][:create_middle][:Customer] = customer_new + ticket_state_id[:screens][:edit][:Agent] = agent_edit + ticket_state_id[:screens][:edit][:Customer] = customer_edit + + update_ticket_attribute(ticket_state_id) + end + + def update_ticket_pending_time + pending_state_ids = ::Ticket::State.where( + state_type_id: ::Ticket::StateType.where(name: ['pending reminder', 'pending action']) + ).pluck(:id) + + ticket_pending_time = ::ObjectManager::Attribute.get( + object: 'Ticket', + name: 'pending_time', + ) + + ticket_pending_time[:data_option][:required_if][:state_id] = pending_state_ids + ticket_pending_time[:data_option][:required_if][:state_id] = pending_state_ids + + update_ticket_attribute(ticket_pending_time) + end + + def update_ticket_attribute(attribute) + ::ObjectManager::Attribute.add( + object_lookup_id: attribute[:object_lookup_id], + name: attribute[:name], + display: attribute[:display], + data_type: attribute[:data_type], + data_option: attribute[:data_option], + active: attribute[:active], + screens: attribute[:screens], + force: true # otherwise _id as a name is not permitted + ) + end end end end diff --git a/spec/fixtures/import/otrs/state/closed_successful.json b/spec/fixtures/import/otrs/state/closed_successful.json new file mode 100644 index 000000000..5b9ca8f04 --- /dev/null +++ b/spec/fixtures/import/otrs/state/closed_successful.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "2", + "ValidID": "1", + "TypeID": "3", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "closed", + "Comment": "Ticket is closed successful.", + "Name": "closed successful" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/closed_unsuccessful.json b/spec/fixtures/import/otrs/state/closed_unsuccessful.json new file mode 100644 index 000000000..f32d4a20b --- /dev/null +++ b/spec/fixtures/import/otrs/state/closed_unsuccessful.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "3", + "ValidID": "1", + "TypeID": "3", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "closed", + "Comment": "Ticket is closed unsuccessful.", + "Name": "closed unsuccessful" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/merged.json b/spec/fixtures/import/otrs/state/merged.json new file mode 100644 index 000000000..05ac43a20 --- /dev/null +++ b/spec/fixtures/import/otrs/state/merged.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "9", + "ValidID": "1", + "TypeID": "7", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "merged", + "Comment": "State for merged tickets.", + "Name": "merged" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/new.json b/spec/fixtures/import/otrs/state/new.json new file mode 100644 index 000000000..b7a302014 --- /dev/null +++ b/spec/fixtures/import/otrs/state/new.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "1", + "ValidID": "1", + "TypeID": "1", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "new", + "Comment": "New ticket created by customer.", + "Name": "new" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/open.json b/spec/fixtures/import/otrs/state/open.json new file mode 100644 index 000000000..44242b947 --- /dev/null +++ b/spec/fixtures/import/otrs/state/open.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "4", + "ValidID": "1", + "TypeID": "2", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "open", + "Comment": "Open tickets.", + "Name": "open" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/pending_auto_close_n.json b/spec/fixtures/import/otrs/state/pending_auto_close_n.json new file mode 100644 index 000000000..ae91bc2d9 --- /dev/null +++ b/spec/fixtures/import/otrs/state/pending_auto_close_n.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "8", + "ValidID": "1", + "TypeID": "5", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "pending auto", + "Comment": "Ticket is pending for automatic close.", + "Name": "pending auto close-" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/pending_auto_close_p.json b/spec/fixtures/import/otrs/state/pending_auto_close_p.json new file mode 100644 index 000000000..41db2766a --- /dev/null +++ b/spec/fixtures/import/otrs/state/pending_auto_close_p.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "7", + "ValidID": "1", + "TypeID": "5", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "pending auto", + "Comment": "Ticket is pending for automatic close.", + "Name": "pending auto close+" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/pending_reminder.json b/spec/fixtures/import/otrs/state/pending_reminder.json new file mode 100644 index 000000000..54c6712f4 --- /dev/null +++ b/spec/fixtures/import/otrs/state/pending_reminder.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "6", + "ValidID": "1", + "TypeID": "4", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "pending reminder", + "Comment": "Ticket is pending for agent reminder.", + "Name": "pending reminder" +} \ No newline at end of file diff --git a/spec/fixtures/import/otrs/state/removed.json b/spec/fixtures/import/otrs/state/removed.json new file mode 100644 index 000000000..202aed347 --- /dev/null +++ b/spec/fixtures/import/otrs/state/removed.json @@ -0,0 +1,10 @@ +{ + "ChangeTime": "2014-04-28 10:53:18", + "ID": "5", + "ValidID": "1", + "TypeID": "6", + "CreateTime": "2014-04-28 10:53:18", + "TypeName": "removed", + "Comment": "Customer removed ticket.", + "Name": "removed" +} \ No newline at end of file diff --git a/spec/import/otrs/state_factory_spec.rb b/spec/import/otrs/state_factory_spec.rb index 40764dca7..06a5d8c9f 100644 --- a/spec/import/otrs/state_factory_spec.rb +++ b/spec/import/otrs/state_factory_spec.rb @@ -8,4 +8,41 @@ RSpec.describe Import::OTRS::StateFactory do expect(described_class).to receive(:backup) described_class.pre_import_hook([]) end + + def load_state_json(file) + json_fixture("import/otrs/state/#{file}") + end + + it 'updates ObjectManager Ticket state_id and pending_time filter' do + + states = %w(new open merged pending_reminder pending_auto_close_p pending_auto_close_n pending_auto_close_p closed_successful closed_unsuccessful closed_successful removed) + + state_backend_param = [] + states.each do |state| + state_backend_param.push(load_state_json(state)) + end + + ticket_state_id = ::ObjectManager::Attribute.get( + object: 'Ticket', + name: 'state_id', + ) + ticket_pending_time = ::ObjectManager::Attribute.get( + object: 'Ticket', + name: 'pending_time', + ) + + expect { + described_class.import(state_backend_param) + + # sync changes + ticket_state_id.reload + ticket_pending_time.reload + }.to change { + ticket_state_id.data_option + }.and change { + ticket_state_id.screens + }.and change { + ticket_pending_time.data_option + } + end end