From 519033174d743b2d97891c1cbded493d95efb43e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 30 Oct 2015 11:05:51 +0100 Subject: [PATCH] Improved reset of last_contact/last_contact_customer (only reset on first customer follow up - needed for update time escalation). --- app/models/observer/ticket/article_changes.rb | 12 +- test/unit/ticket_test.rb | 104 +++++++++++++++--- 2 files changed, 98 insertions(+), 18 deletions(-) diff --git a/app/models/observer/ticket/article_changes.rb b/app/models/observer/ticket/article_changes.rb index a4512a759..922dcb2ab 100644 --- a/app/models/observer/ticket/article_changes.rb +++ b/app/models/observer/ticket/article_changes.rb @@ -70,12 +70,18 @@ class Observer::Ticket::ArticleChanges < ActiveRecord::Observer # if sender is customer sender = Ticket::Article::Sender.lookup(id: record.sender_id) + ticket = record.ticket if sender.name == 'Customer' + # if customer is sending agains, ignore update of last contact (usecase of update escalation) + return true if ticket.last_contact_customer && + ticket.last_contact && + ticket.last_contact_customer == ticket.last_contact + # check if last communication is done by agent, else do not set last_contact_customer - if record.ticket.last_contact_customer.nil? || - record.ticket.last_contact_agent.nil? || - record.ticket.last_contact_agent.to_i > record.ticket.last_contact_customer.to_i + if ticket.last_contact_customer.nil? || + ticket.last_contact_agent.nil? || + ticket.last_contact_agent.to_i > ticket.last_contact_customer.to_i # set last_contact customer record.ticket.last_contact_customer = record.created_at diff --git a/test/unit/ticket_test.rb b/test/unit/ticket_test.rb index 926fe111f..d8300c9ae 100644 --- a/test/unit/ticket_test.rb +++ b/test/unit/ticket_test.rb @@ -18,26 +18,51 @@ class TicketTest < ActiveSupport::TestCase assert_equal( ticket.group.name, 'Users', 'ticket.group verify' ) assert_equal( ticket.state.name, 'new', 'ticket.state verify' ) - # create inbound article - article_inbound = Ticket::Article.create( + # create inbound article #1 + article_inbound1 = Ticket::Article.create( ticket_id: ticket.id, from: 'some_sender@example.com', to: 'some_recipient@example.com', subject: 'some subject', message_id: 'some@id', - body: 'some message article_inbound 😍😍😍', + body: 'some message article_inbound1 😍😍😍', internal: false, sender: Ticket::Article::Sender.where(name: 'Customer').first, type: Ticket::Article::Type.where(name: 'email').first, updated_by_id: 1, created_by_id: 1, ) - assert_equal( article_inbound.body, 'some message article_inbound 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound' ) + assert_equal( article_inbound1.body, 'some message article_inbound1 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound' ) ticket = Ticket.find(ticket.id) assert_equal( ticket.article_count, 1, 'ticket.article_count verify - inbound' ) - assert_equal( ticket.last_contact.to_s, article_inbound.created_at.to_s, 'ticket.last_contact verify - inbound' ) - assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - inbound' ) + assert_equal( ticket.last_contact.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer verify - inbound' ) + assert_equal( ticket.last_contact_agent, nil, 'ticket.last_contact_agent verify - inbound' ) + assert_equal( ticket.first_response, nil, 'ticket.first_response verify - inbound' ) + assert_equal( ticket.close_time, nil, 'ticket.close_time verify - inbound' ) + + # create inbound article #2 + sleep 2 + article_inbound2 = Ticket::Article.create( + ticket_id: ticket.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message article_inbound2 😍😍😍', + internal: false, + sender: Ticket::Article::Sender.where(name: 'Customer').first, + type: Ticket::Article::Type.where(name: 'email').first, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal( article_inbound2.body, 'some message article_inbound2 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound' ) + + ticket = Ticket.find(ticket.id) + assert_equal( ticket.article_count, 2, 'ticket.article_count verify - inbound' ) + assert_equal( ticket.last_contact.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer verify - inbound' ) assert_equal( ticket.last_contact_agent, nil, 'ticket.last_contact_agent verify - inbound' ) assert_equal( ticket.first_response, nil, 'ticket.first_response verify - inbound' ) assert_equal( ticket.close_time, nil, 'ticket.close_time verify - inbound' ) @@ -58,15 +83,15 @@ class TicketTest < ActiveSupport::TestCase assert_equal( article_note.body, "some\n message", 'article_note.body verify - inbound' ) ticket = Ticket.find(ticket.id) - assert_equal( ticket.article_count, 2, 'ticket.article_count verify - note' ) - assert_equal( ticket.last_contact.to_s, article_inbound.created_at.to_s, 'ticket.last_contact verify - note' ) - assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - note' ) + assert_equal( ticket.article_count, 3, 'ticket.article_count verify - note' ) + assert_equal( ticket.last_contact.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - note' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer verify - note' ) assert_equal( ticket.last_contact_agent, nil, 'ticket.last_contact_agent verify - note' ) assert_equal( ticket.first_response, nil, 'ticket.first_response verify - note' ) assert_equal( ticket.close_time, nil, 'ticket.close_time verify - note' ) # create outbound article - sleep 10 + sleep 2 article_outbound = Ticket::Article.create( ticket_id: ticket.id, from: 'some_recipient@example.com', @@ -82,9 +107,58 @@ class TicketTest < ActiveSupport::TestCase ) ticket = Ticket.find(ticket.id) - assert_equal( ticket.article_count, 3, 'ticket.article_count verify - outbound' ) + assert_equal( ticket.article_count, 4, 'ticket.article_count verify - outbound' ) assert_equal( ticket.last_contact.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - outbound' ) - assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - outbound' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer verify - outbound' ) + assert_equal( ticket.last_contact_agent.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent verify - outbound' ) + assert_equal( ticket.first_response.to_s, article_outbound.created_at.to_s, 'ticket.first_response verify - outbound' ) + assert_equal( ticket.close_time, nil, 'ticket.close_time verify - outbound' ) + + # create inbound article #3 + article_inbound3 = Ticket::Article.create( + ticket_id: ticket.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message article_inbound3 😍😍😍', + internal: false, + sender: Ticket::Article::Sender.where(name: 'Customer').first, + type: Ticket::Article::Type.where(name: 'email').first, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal( article_inbound3.body, 'some message article_inbound3 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound' ) + + ticket = Ticket.find(ticket.id) + assert_equal( ticket.article_count, 5, 'ticket.article_count verify - inbound' ) + assert_equal( ticket.last_contact.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer verify - inbound' ) + assert_equal( ticket.last_contact_agent.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent verify - outbound' ) + assert_equal( ticket.first_response.to_s, article_outbound.created_at.to_s, 'ticket.first_response verify - outbound' ) + assert_equal( ticket.close_time, nil, 'ticket.close_time verify - outbound' ) + + # create inbound article #4 + sleep 2 + article_inbound4 = Ticket::Article.create( + ticket_id: ticket.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message article_inbound4 😍😍😍', + internal: false, + sender: Ticket::Article::Sender.where(name: 'Customer').first, + type: Ticket::Article::Type.where(name: 'email').first, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal( article_inbound4.body, 'some message article_inbound4 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound' ) + + ticket = Ticket.find(ticket.id) + assert_equal( ticket.article_count, 6, 'ticket.article_count verify - inbound' ) + assert_equal( ticket.last_contact.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer verify - inbound' ) assert_equal( ticket.last_contact_agent.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent verify - outbound' ) assert_equal( ticket.first_response.to_s, article_outbound.created_at.to_s, 'ticket.first_response verify - outbound' ) assert_equal( ticket.close_time, nil, 'ticket.close_time verify - outbound' ) @@ -93,9 +167,9 @@ class TicketTest < ActiveSupport::TestCase ticket.save ticket = Ticket.find(ticket.id) - assert_equal( ticket.article_count, 3, 'ticket.article_count verify - state update' ) - assert_equal( ticket.last_contact.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - state update' ) - assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - state update' ) + assert_equal( ticket.article_count, 6, 'ticket.article_count verify - state update' ) + assert_equal( ticket.last_contact.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - state update' ) + assert_equal( ticket.last_contact_customer.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer verify - state update' ) assert_equal( ticket.last_contact_agent.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent verify - state update' ) assert_equal( ticket.first_response.to_s, article_outbound.created_at.to_s, 'ticket.first_response verify - state update' ) assert( ticket.close_time, 'ticket.close_time verify - state update' )