From a080d6261132b944d845bbce7d3e469fe5ed28d3 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 4 Feb 2016 00:35:49 +0100 Subject: [PATCH] Updated test result check. Added postgresql support. Do not import articles a second time on reimport (use message_id for identifier). --- lib/import/zendesk.rb | 28 ++++++++++++++++++------- test/integration/zendesk_import_test.rb | 12 +++++------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/import/zendesk.rb b/lib/import/zendesk.rb index 2486935a0..a4e9869ae 100644 --- a/lib/import/zendesk.rb +++ b/lib/import/zendesk.rb @@ -677,7 +677,13 @@ module Import::Zendesk end end - local_ticket = Ticket.create_or_update( local_ticket_fields ) + local_ticket = Ticket.find_by(id: local_ticket_fields[:id]) + if local_ticket + local_ticket.update_attributes(local_ticket_fields) + else + local_ticket = Ticket.create(local_ticket_fields) + _reset_pk('tickets') + end zendesk_ticket_tags = [] zendesk_ticket.tags.each { |tag| @@ -704,6 +710,7 @@ module Import::Zendesk ticket_id: local_ticket.id, body: zendesk_article.html_body, internal: !zendesk_article.public, + message_id: zendesk_article.id, updated_by_id: @zendesk_user_mapping[ zendesk_article.author_id ] || 1, created_by_id: @zendesk_user_mapping[ zendesk_article.author_id ] || 1, } @@ -719,19 +726,14 @@ module Import::Zendesk end if zendesk_article.via.channel == 'web' - local_article_fields[:message_id] = zendesk_article.id local_article_fields[:type_id] = article_type_web.id elsif zendesk_article.via.channel == 'email' local_article_fields[:from] = zendesk_article.via.source.from.address local_article_fields[:to] = zendesk_article.via.source.to.address # Notice zendesk_article.via.from.original_recipients=[\"another@gmail.com\", \"support@example.zendesk.com\"] - local_article_fields[:message_id] = zendesk_article.id local_article_fields[:type_id] = article_type_email.id elsif zendesk_article.via.channel == 'sample_ticket' - local_article_fields[:message_id] = zendesk_article.id local_article_fields[:type_id] = article_type_note.id elsif zendesk_article.via.channel == 'twitter' - local_article_fields[:message_id] = zendesk_article.id - local_article_fields[:type_id] = if zendesk_article.via.source.rel == 'mention' article_type_twitter_status.id else @@ -742,7 +744,6 @@ module Import::Zendesk local_article_fields[:from] = zendesk_article.via.source.from.facebook_id local_article_fields[:to] = zendesk_article.via.source.to.facebook_id - local_article_fields[:message_id] = zendesk_article.id local_article_fields[:type_id] = if zendesk_article.via.source.rel == 'post' article_type_facebook_feed_post.id @@ -752,7 +753,12 @@ module Import::Zendesk end # create article - local_article = Ticket::Article.create( local_article_fields ) + local_article = Ticket::Article.find_by(message_id: local_article_fields[:message_id]) + if local_article + local_article.update_attributes(local_article_fields) + else + local_article = Ticket::Article.create( local_article_fields ) + end zendesk_attachments = zendesk_article.attachments @@ -1029,4 +1035,10 @@ module Import::Zendesk } end + # reset primary key sequences + def self._reset_pk(table) + return if ActiveRecord::Base.connection_config[:adapter] != 'postgresql' + ActiveRecord::Base.connection.reset_pk_sequence!(table) + end + end diff --git a/test/integration/zendesk_import_test.rb b/test/integration/zendesk_import_test.rb index 26724d333..db4e3a201 100644 --- a/test/integration/zendesk_import_test.rb +++ b/test/integration/zendesk_import_test.rb @@ -279,15 +279,15 @@ class ZendeskImportTest < ActiveSupport::TestCase data: { title: 'Bob Smith, here is the test ticket you requested', note: 'test email', - create_article_type_id: 1, + create_article_type_id: 10, create_article_sender_id: 2, - article_count: 2, + article_count: 4, state_id: 3, group_id: 3, - priority_id: 3, + priority_id: 1, owner_id: 1, - customer_id: 6, - organization_id: 2, + customer_id: 7, + organization_id: nil, }, }, { @@ -318,7 +318,7 @@ class ZendeskImportTest < ActiveSupport::TestCase group_id: 3, priority_id: 2, owner_id: 1, - customer_id: 90, + customer_id: 91, organization_id: nil, }, },