From 24da432aecf5ed211c2cfa6cb62a11ef5288821e Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 16 Nov 2016 11:46:38 +0100 Subject: [PATCH] WIP issue #368 - OTRS tickets are created without an assigned customer user. --- lib/import/otrs.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index 0bc0fd7f2..50f8582b5 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -632,13 +632,20 @@ module Import::OTRS ticket_new.delete(:owner) end + record['Articles'].each { |article| + # utf8 encode + _utf8_encode(article) + # lookup customers to create first + _article_based_customers(article) + } + # find customer if ticket_new[:customer] user = User.lookup(login: ticket_new[:customer].downcase) ticket_new[:customer_id] = if user user.id else - 1 + _first_customer_id(record['Articles']) end ticket_new.delete(:customer) else @@ -664,16 +671,6 @@ module Import::OTRS end end - # utf8 encode - record['Articles'].each { |article| - _utf8_encode(article) - } - - # lookup customers to create first - record['Articles'].each { |article| - _article_based_customers(article) - } - record['Articles'].each do |article| retries = 3 @@ -1598,4 +1595,17 @@ module Import::OTRS %w(ProcessManagementProcessID ProcessManagementActivityID ZammadMigratorChanged ZammadMigratorChangedOld) end + def self._first_customer_id(articles) + user_id = 1 + articles.each { |article| + next if article['sender'] != 'customer' + next if article['created_by_id'].to_i != 1 + next if article['from'].empty? + + user_id = article['created_by_id'] + break + } + + user_id + end end