From d96d02c83f1069d1b2f3f05cdffe883ed7ba038e Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 20 Dec 2016 16:17:27 +0100 Subject: [PATCH] Fixed issue: Zendesk attachments might get imported multiple times for single article. --- .../ticket/comment/attachment_factory.rb | 18 ++++++++++++++++++ test/integration/zendesk_import_test.rb | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/import/zendesk/ticket/comment/attachment_factory.rb b/lib/import/zendesk/ticket/comment/attachment_factory.rb index 474e21501..4ccc1da85 100644 --- a/lib/import/zendesk/ticket/comment/attachment_factory.rb +++ b/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) diff --git a/test/integration/zendesk_import_test.rb b/test/integration/zendesk_import_test.rb index 13be8dd44..9aa3ec719 100644 --- a/test/integration/zendesk_import_test.rb +++ b/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')