Browse Source

Fixed issue: Zendesk attachments might get imported multiple times for single article.

Thorsten Eckel 8 years ago
parent
commit
d96d02c83f

+ 18 - 0
lib/import/zendesk/ticket/comment/attachment_factory.rb

@@ -7,8 +7,26 @@ module Import
           # so we can use the default import factory here
           extend Import::Factory
 
+          # rubocop:disable Style/ModuleFunction
+          extend self
+
           private
 
+          # special handling which only starts import if needed
+          # Attention: skip? method can't be used since it (currently)
+          # only checks for single records - not all
+          def import_loop(records, *args, &import_block)
+            local_article     = args[0]
+            local_attachments = local_article.attachments
+
+            return if local_attachments.count == records.count
+            # get a common ground
+            local_attachments.each(&:delete)
+            return if records.empty?
+
+            records.each(&import_block)
+          end
+
           def create_instance(record, *args)
             local_article = args[0]
             backend_class(record).new(record, local_article)

+ 3 - 3
test/integration/zendesk_import_test.rb

@@ -384,7 +384,7 @@ If you\'re reading this message in your email, click the ticket number link that
 
     checks = [
       {
-        id: 5,
+        message_id: 39_984_258_725,
         data: {
           count: 1,
           1 => {
@@ -396,7 +396,7 @@ If you\'re reading this message in your email, click the ticket number link that
         },
       },
       {
-        id: 7,
+        message_id: 32_817_827_921,
         data: {
           count: 1,
           1 => {
@@ -410,7 +410,7 @@ If you\'re reading this message in your email, click the ticket number link that
     ]
 
     checks.each { |check|
-      article = Ticket::Article.find(check[:id])
+      article = Ticket::Article.find_by(message_id: check[:message_id])
 
       assert_equal(check[:data][:count], article.attachments.count, 'attachemnt count')