From 7dc2875a49ba33dd367e590cbe560a2b2273d85e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Feb 2018 12:56:51 +0100 Subject: [PATCH] Enhancement to issue #1816 - also reset owner if agent isn't agent anymore. --- app/models/ticket.rb | 13 ++++-- ...> ticket_owner_reset_on_follow_up_test.rb} | 42 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) rename test/unit/{ticket_update_test.rb => ticket_owner_reset_on_follow_up_test.rb} (81%) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 83b6723d2..e2a061518 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -1209,9 +1209,16 @@ result end def check_owner_active - return true if owner.login == '-' # return when ticket is unassigned - return true if owner.groups.any?(&:active?) && owner.active? # return if user in any groups assigned is active and user account is active - self.owner = User.find_by(login: '-') # else set the owner of the ticket to the default user as unassigned + + # return when ticket is unassigned + return true if owner_id.blank? + return true if owner_id == 1 + + # return if owner is active, is agent and has access to group of ticket + return true if owner.active? && owner.permissions?('ticket.agent') && owner.group_access?(group_id, 'full') + + # else set the owner of the ticket to the default user as unassigned + self.owner_id = 1 true end end diff --git a/test/unit/ticket_update_test.rb b/test/unit/ticket_owner_reset_on_follow_up_test.rb similarity index 81% rename from test/unit/ticket_update_test.rb rename to test/unit/ticket_owner_reset_on_follow_up_test.rb index 21744b74a..1c162c14d 100644 --- a/test/unit/ticket_update_test.rb +++ b/test/unit/ticket_owner_reset_on_follow_up_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class TicketUpdateTest < ActiveSupport::TestCase +class TicketOwnerResetOnFollowUpTest < ActiveSupport::TestCase setup do UserInfo.current_user_id = 1 @@ -179,6 +179,46 @@ Some Text" To: customer@example.com Subject: #{ticket.subject_build('some new subject')} +Some Text" + + ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw) + assert_equal(ticket.id, ticket_p.id) + + assert_equal('open', ticket_p.state.name) + assert_equal('-', ticket_p.owner.login) + end + + test 'check if ticket is unassigned on follow up via email if current owner is customer now' do + + ticket = Ticket.create!( + title: 'follow up check for invalid owner is customer now', + group: Group.lookup(name: 'Users'), + customer: @customer1, + owner: @agent1, + state: Ticket::State.lookup(name: 'closed'), + updated_by_id: 1, + created_by_id: 1, + ) + article = Ticket::Article.create!( + ticket_id: ticket.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'follow up check', + body: 'some message article', + internal: false, + sender: Ticket::Article::Sender.lookup(name: 'Agent'), + type: Ticket::Article::Type.lookup(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + @agent1.roles = Role.where(name: 'Customer') + @agent1.save! + + email_raw = "From: me@example.com +To: customer@example.com +Subject: #{ticket.subject_build('some new subject')} + Some Text" ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw)