Browse Source

Working on issue #689 - Ticket state_id and pending_time attributes won't get synced with new OTRS data.

Thorsten Eckel 8 years ago
parent
commit
37e98b1a81

+ 65 - 0
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

+ 10 - 0
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"
+}

+ 10 - 0
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"
+}

+ 10 - 0
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"
+}

+ 10 - 0
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"
+}

+ 10 - 0
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"
+}

+ 10 - 0
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-"
+}

+ 10 - 0
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+"
+}

+ 10 - 0
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"
+}

+ 10 - 0
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"
+}

Some files were not shown because too many files changed in this diff