From 136713c45f76ed75eb598b10ed1a66837d319f91 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 19 Aug 2016 11:07:53 +0200 Subject: [PATCH] Added check if sender is system address (show sender as agent in ticket zoom). --- app/models/channel/filter/identify_sender.rb | 37 ++++++-- ...00001_update_setting_postmaster_filter2.rb | 15 +++ db/seeds.rb | 9 ++ ...l_process_sender_is_system_address_test.rb | 92 +++++++++++++++++++ 4 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20160819000001_update_setting_postmaster_filter2.rb create mode 100644 test/unit/email_process_sender_is_system_address_test.rb diff --git a/app/models/channel/filter/identify_sender.rb b/app/models/channel/filter/identify_sender.rb index 6ed30cc05..a89f77f91 100644 --- a/app/models/channel/filter/identify_sender.rb +++ b/app/models/channel/filter/identify_sender.rb @@ -27,12 +27,36 @@ module Channel::Filter::IdentifySender user = User.find_by(email: mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email]) end if !user - user = user_create( - login: mail[ 'x-zammad-customer-login'.to_sym ] || mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email], - firstname: mail[ 'x-zammad-customer-firstname'.to_sym ] || mail[:from_display_name], - lastname: mail[ 'x-zammad-customer-lastname'.to_sym ], - email: mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email], - ) + + # get correct customer + if mail[ 'x-zammad-ticket-create-article-sender'.to_sym ] == 'Agent' + + # get first recipient and set customer + begin + to = 'raw-to'.to_sym + if mail[to] && mail[to].addrs + items = mail[to].addrs + items.each { |item| + user = user_create( + login: item.address, + firstname: item.display_name, + email: item.address, + ) + break + } + end + rescue => e + Rails.logger.error 'ERROR: SenderIsSystemAddress: ' + e.inspect + end + end + if !user + user = user_create( + login: mail[ 'x-zammad-customer-login'.to_sym ] || mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email], + firstname: mail[ 'x-zammad-customer-firstname'.to_sym ] || mail[:from_display_name], + lastname: mail[ 'x-zammad-customer-lastname'.to_sym ], + email: mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email], + ) + end end create_recipients(mail) @@ -77,7 +101,6 @@ module Channel::Filter::IdentifySender ) } end - } end diff --git a/db/migrate/20160819000001_update_setting_postmaster_filter2.rb b/db/migrate/20160819000001_update_setting_postmaster_filter2.rb new file mode 100644 index 000000000..d204ec165 --- /dev/null +++ b/db/migrate/20160819000001_update_setting_postmaster_filter2.rb @@ -0,0 +1,15 @@ +class UpdateSettingPostmasterFilter2 < ActiveRecord::Migration + def up + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + Setting.create_if_not_exists( + title: 'Define postmaster filter.', + name: '0012_postmaster_filter_sender_is_system_address', + area: 'Postmaster::PreFilter', + description: 'Define postmaster filter to check if email got created via email as Zammad.', + options: {}, + state: 'Channel::Filter::SenderIsSystemAddress', + frontend: false + ) + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 2ce3a0120..4ab4d21d4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1875,6 +1875,15 @@ Setting.create_if_not_exists( state: 'Channel::Filter::Trusted', frontend: false ) +Setting.create_if_not_exists( + title: 'Define postmaster filter.', + name: '0012_postmaster_filter_sender_is_system_address', + area: 'Postmaster::PreFilter', + description: 'Define postmaster filter to check if email got created via email as Zammad.', + options: {}, + state: 'Channel::Filter::SenderIsSystemAddress', + frontend: false +) Setting.create_if_not_exists( title: 'Define postmaster filter.', name: '0015_postmaster_filter_identify_sender', diff --git a/test/unit/email_process_sender_is_system_address_test.rb b/test/unit/email_process_sender_is_system_address_test.rb new file mode 100644 index 000000000..a60bf88cc --- /dev/null +++ b/test/unit/email_process_sender_is_system_address_test.rb @@ -0,0 +1,92 @@ +# encoding: utf-8 +require 'test_helper' + +class EmailProcessSenderIsSystemAddress < ActiveSupport::TestCase + + test 'process with ticket creates and system address check' do + + EmailAddress.create_or_update( + channel_id: 1, + realname: 'My System', + email: 'my@system.test', + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + subject = "some new subject #{rand(9_999_999)}" + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: #{subject} + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + ticket = Ticket.find(ticket_p.id) + article = Ticket::Article.find(article_p.id) + assert_equal(subject, ticket.title) + assert_equal('new', ticket.state.name) + assert_equal('Customer', ticket.create_article_sender.name) + assert_equal('Customer', article.sender.name) + assert_equal('me@example.com', ticket.customer.email) + + # check article sender + customer of ticket + subject = "some new subject #{rand(9_999_999)}" + email_raw_string = "From: my@system.test +To: me@example.com, customer@example.com +Subject: #{subject} +Message-ID: <123456789-1@linuxhotel.de> + + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + ticket = Ticket.find(ticket_p.id) + article = Ticket::Article.find(article_p.id) + p "ticket: #{ticket.inspect}" + assert_equal(subject, ticket.title) + assert_equal('open', ticket.state.name) + assert_equal('Agent', ticket.create_article_sender.name) + assert_equal('Agent', article.sender.name) + assert_equal('me@example.com', ticket.customer.email) + + # check if follow up based on inital system sender address + setting_orig = Setting.get('postmaster_follow_up_search_in') + Setting.set('postmaster_follow_up_search_in', []) + + # follow up possible because same subject + email_raw_string = "From: me@example.com +To: my@system.test +Subject: #{subject} +Message-ID: <123456789-2@linuxhotel.de> +References: <123456789-1@linuxhotel.de> + +Some Text" + + ticket_p2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + ticket2 = Ticket.find(ticket_p2.id) + article = Ticket::Article.find(article_p.id) + assert_equal(subject, ticket2.title) + assert_equal(ticket.id, ticket2.id) + + # follow up not possible because subject has changed + subject = "new subject without ticket ref #{rand(9_999_999)}" + email_raw_string = "From: me@example.com +To: my@system.test +Subject: #{subject} +Message-ID: <123456789-3@linuxhotel.de> +References: <123456789-1@linuxhotel.de> + +Some Text" + + ticket_p2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + ticket2 = Ticket.find(ticket_p2.id) + article = Ticket::Article.find(article_p.id) + assert_not_equal(ticket.id, ticket2.id) + assert_equal(subject, ticket2.title) + assert_equal('new', ticket2.state.name) + + Setting.set('postmaster_follow_up_search_in', setting_orig) + + end + +end