diff --git a/app/models/channel/filter/identify_sender.rb b/app/models/channel/filter/identify_sender.rb index 1b67d1709..7818f251f 100644 --- a/app/models/channel/filter/identify_sender.rb +++ b/app/models/channel/filter/identify_sender.rb @@ -22,9 +22,9 @@ module Channel::Filter::IdentifySender if !customer_user && mail[ 'x-zammad-customer-email'.to_sym ].present? customer_user = User.find_by(email: mail[ 'x-zammad-customer-email'.to_sym ]) end - if !customer_user - # get correct customer + # get correct customer + if !customer_user && Setting.get('postmaster_sender_is_agent_search_for_customer') == true if mail[ 'x-zammad-ticket-create-article-sender'.to_sym ] == 'Agent' # get first recipient and set customer @@ -46,18 +46,21 @@ module Channel::Filter::IdentifySender end end rescue => e - Rails.logger.error 'ERROR: SenderIsSystemAddress: ' + e.inspect + Rails.logger.error "SenderIsSystemAddress: ##{e.inspect}" end end - if !customer_user - customer_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 + + # take regular from as customer + if !customer_user + customer_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 + create_recipients(mail) mail[ 'x-zammad-ticket-customer_id'.to_sym ] = customer_user.id diff --git a/db/migrate/20171123000001_email_process_customer_selection_based_on_sender_recipient.rb b/db/migrate/20171123000001_email_process_customer_selection_based_on_sender_recipient.rb new file mode 100644 index 000000000..54f04157f --- /dev/null +++ b/db/migrate/20171123000001_email_process_customer_selection_based_on_sender_recipient.rb @@ -0,0 +1,34 @@ +class EmailProcessCustomerSelectionBasedOnSenderRecipient < ActiveRecord::Migration[4.2] + def up + + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + Setting.create_if_not_exists( + title: 'Customer selection based on sender and receiver list', + name: 'postmaster_sender_is_agent_search_for_customer', + area: 'Email::Base', + description: 'If the sender is an agent, set the first user in the recipient list as a customer.', + options: { + form: [ + { + display: '', + null: true, + name: 'postmaster_sender_is_agent_search_for_customer', + tag: 'boolean', + options: { + true => 'yes', + false => 'no', + }, + }, + ], + }, + state: true, + preferences: { + permission: ['admin.channel_email'], + }, + frontend: false + ) + end + +end diff --git a/db/seeds/settings.rb b/db/seeds/settings.rb index 25155d67a..b2a8c8521 100644 --- a/db/seeds/settings.rb +++ b/db/seeds/settings.rb @@ -2041,6 +2041,32 @@ Setting.create_if_not_exists( frontend: false ) +Setting.create_if_not_exists( + title: 'Customer selection based on sender and receiver list', + name: 'postmaster_sender_is_agent_search_for_customer', + area: 'Email::Base', + description: 'If the sender is an agent, set the first user in the recipient list as a customer.', + options: { + form: [ + { + display: '', + null: true, + name: 'postmaster_sender_is_agent_search_for_customer', + tag: 'boolean', + options: { + true => 'yes', + false => 'no', + }, + }, + ], + }, + state: true, + preferences: { + permission: ['admin.channel_email'], + }, + frontend: false +) + Setting.create_if_not_exists( title: 'Notification Sender', name: 'notification_sender', diff --git a/test/unit/email_process_customer_selection_based_on_sender_recipient_test.rb b/test/unit/email_process_customer_selection_based_on_sender_recipient_test.rb new file mode 100644 index 000000000..81fcfa24d --- /dev/null +++ b/test/unit/email_process_customer_selection_based_on_sender_recipient_test.rb @@ -0,0 +1,79 @@ + +require 'test_helper' + +class EmailProcessCustomerSelectionBasedOnSenderRecipient < ActiveSupport::TestCase + + setup do + groups = Group.all + roles = Role.where(name: 'Agent') + @agent1 = User.create_or_update( + login: 'user-customer-selection-agent1@example.com', + firstname: 'UserOutOfOffice', + lastname: 'Agent1', + email: 'user-customer-selection-agent1@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + @customer1 = User.create_or_update( + login: 'user-customer-selection-customer1@example.com', + firstname: 'UserOutOfOffice', + lastname: 'customer1', + email: 'user-customer-selection-customer1@example.com', + password: 'customerpw', + active: true, + roles: roles, + updated_by_id: 1, + created_by_id: 1, + ) + end + + test 'customer need to be customer' do + + email_raw_string = "From: #{@agent1.email} +To: #{@customer1.email} +Subject: test + +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('test', ticket.title) + assert_equal('new', ticket.state.name) + assert_equal('Agent', ticket.create_article_sender.name) + assert_equal('Agent', article.sender.name) + assert_equal(@customer1.email, ticket.customer.email) + assert_equal(@customer1.firstname, ticket.customer.firstname) + assert_equal(@customer1.lastname, ticket.customer.lastname) + + end + + test 'agent need to be customer' do + + Setting.set('postmaster_sender_is_agent_search_for_customer', false) + + email_raw_string = "From: #{@agent1.email} +To: #{@customer1.email} +Subject: test + +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('test', ticket.title) + assert_equal('new', ticket.state.name) + assert_equal('Agent', ticket.create_article_sender.name) + assert_equal('Agent', article.sender.name) + assert_equal(@agent1.email, ticket.customer.email) + assert_equal(@agent1.firstname, ticket.customer.firstname) + assert_equal(@agent1.lastname, ticket.customer.lastname) + + end + +end