From 87ec273b5736613e82b649d66c1544e97c37eb74 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Tue, 25 Apr 2017 16:06:23 +0200 Subject: [PATCH] Fixed issue #206 - Wrong recipient on reply to Received call. --- .../ticket_zoom/article_view.coffee | 2 +- .../concerns/creates_ticket_articles.rb | 1 + .../article/fillup_from_origin_by_id.rb | 25 +++++++++++ config/application.rb | 1 + db/migrate/20120101000010_create_ticket.rb | 1 + db/migrate/20170421110000_add_origin_by_id.rb | 9 ++++ .../ticket_articles_controller_test.rb | 45 +++++++++++++++++++ 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/models/observer/ticket/article/fillup_from_origin_by_id.rb create mode 100644 db/migrate/20170421110000_add_origin_by_id.rb diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee index 84ffe5d2f..61f101602 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee @@ -157,7 +157,7 @@ class ArticleViewItem extends App.ObserverController new App.WidgetAvatar( el: @$('.js-avatar') - object_id: article.created_by_id + object_id: article.origin_by_id || article.created_by_id size: 40 ) diff --git a/app/controllers/concerns/creates_ticket_articles.rb b/app/controllers/concerns/creates_ticket_articles.rb index 5414eb7b3..8d0e06390 100644 --- a/app/controllers/concerns/creates_ticket_articles.rb +++ b/app/controllers/concerns/creates_ticket_articles.rb @@ -34,6 +34,7 @@ module CreatesTicketArticles if !current_user.permissions?('ticket.agent') clean_params[:sender_id] = Ticket::Article::Sender.lookup(name: 'Customer').id clean_params.delete(:sender) + clean_params.delete(:origin_by_id) type = Ticket::Article::Type.lookup(id: clean_params[:type_id]) if type.name !~ /^(note|web)$/ clean_params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id diff --git a/app/models/observer/ticket/article/fillup_from_origin_by_id.rb b/app/models/observer/ticket/article/fillup_from_origin_by_id.rb new file mode 100644 index 000000000..fa77cf3d5 --- /dev/null +++ b/app/models/observer/ticket/article/fillup_from_origin_by_id.rb @@ -0,0 +1,25 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Observer::Ticket::Article::FillupFromOriginById < ActiveRecord::Observer + observe 'ticket::_article' + + def before_create(record) + + # return if we run import mode + return if Setting.get('import_mode') + + # only do fill of from if article got created via application_server (e. g. not + # if article and sender type is set via *.postmaster) + return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster' + + # check if origin_by_id exists + return if record.origin_by_id.present? + return if !record.ticket.customer_id + return if record.sender.name != 'Customer' + return if record.type.name != 'phone' + + record.origin_by_id = record.ticket.customer_id + user = User.find(record.origin_by_id) + record.from = "#{user.firstname} #{user.lastname} <#{user.email}>" + end +end diff --git a/config/application.rb b/config/application.rb index 32fed4b20..28aaa09c4 100644 --- a/config/application.rb +++ b/config/application.rb @@ -27,6 +27,7 @@ module Zammad 'observer::_ticket::_article_changes', 'observer::_ticket::_article::_fillup_from_general', 'observer::_ticket::_article::_fillup_from_email', + 'observer::_ticket::_article::_fillup_from_origin_by_id', 'observer::_ticket::_article::_communicate_email', 'observer::_ticket::_article::_communicate_facebook', 'observer::_ticket::_article::_communicate_twitter', diff --git a/db/migrate/20120101000010_create_ticket.rb b/db/migrate/20120101000010_create_ticket.rb index d9bb29bd5..6cca4ada3 100644 --- a/db/migrate/20120101000010_create_ticket.rb +++ b/db/migrate/20120101000010_create_ticket.rb @@ -166,6 +166,7 @@ class CreateTicket < ActiveRecord::Migration t.column :preferences, :text, limit: 500.kilobytes + 1, null: true t.column :updated_by_id, :integer, null: false t.column :created_by_id, :integer, null: false + t.column :origin_by_id, :integer t.timestamps limit: 3, null: false end add_index :ticket_articles, [:ticket_id] diff --git a/db/migrate/20170421110000_add_origin_by_id.rb b/db/migrate/20170421110000_add_origin_by_id.rb new file mode 100644 index 000000000..72887d8c6 --- /dev/null +++ b/db/migrate/20170421110000_add_origin_by_id.rb @@ -0,0 +1,9 @@ +class AddOriginById < ActiveRecord::Migration + def up + + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + add_column :ticket_articles, :origin_by_id, :integer + end +end diff --git a/test/controllers/ticket_articles_controller_test.rb b/test/controllers/ticket_articles_controller_test.rb index b32ed6120..78c054446 100644 --- a/test/controllers/ticket_articles_controller_test.rb +++ b/test/controllers/ticket_articles_controller_test.rb @@ -280,4 +280,49 @@ AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO end + test '03.01 create phone ticket for customer and expected origin_by_id' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw') + + params = { + title: 'a new ticket #1', + group: 'Users', + customer_id: @customer_without_org.id, + article: { + body: 'some body', + sender: 'Customer', + type: 'phone', + } + } + post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials) + assert_response(201) + result = JSON.parse(@response.body) + assert_equal(Hash, result.class) + + article = Ticket::Article.find_by( ticket_id: result['id'] ) + assert_equal(@customer_without_org.id, article.origin_by_id) + assert_equal('Tickets Customer1 ', article.from) + end + + test '03.02 create phone ticket by customer and manipulate origin_by_id' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw') + + params = { + title: 'a new ticket #1', + group: 'Users', + customer_id: @customer_without_org.id, + article: { + body: 'some body', + sender: 'Customer', + type: 'phone', + origin_by_id: 1, + } + } + post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials) + assert_response(201) + result = JSON.parse(@response.body) + assert_equal(Hash, result.class) + + article = Ticket::Article.find_by( ticket_id: result['id'] ) + assert_nil(article.origin_by_id) + end end