Merged article changes on ticket into one observer to do ticket.save only once.

This commit is contained in:
Martin Edenhofer 2015-05-01 17:24:17 +02:00
parent ac9a5b1164
commit 9965a7587a
6 changed files with 104 additions and 117 deletions

View file

@ -0,0 +1,103 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::Ticket::ArticleChanges < ActiveRecord::Observer
observe 'ticket::_article'
def after_create(record)
article_count_update(record)
first_response_update(record)
sender_type_update(record)
last_contact_update(record)
# save ticket
record.ticket.save
end
# get article count
def article_count_update(record)
record.ticket.article_count = record.ticket.articles.count
end
# set frist response
def first_response_update(record)
# puts 'check first response'
# return if we run import mode
return if Setting.get('import_mode')
# if article in internal
return true if record.internal
# if sender is not agent
sender = Ticket::Article::Sender.lookup( id: record.sender_id )
type = Ticket::Article::Type.lookup( id: record.type_id )
return true if sender.name != 'Agent' && type.name !~ /^phone/
# if article is a message to customer
return true if !type.communication
# check if first_response is already set
return true if record.ticket.first_response
# set first_response
record.ticket.first_response = record.created_at
true
end
# set sender type
def sender_type_update(record)
# ignore if create channel is already set
count = Ticket::Article.where( ticket_id: record.ticket_id ).count
return if count > 1
record.ticket.create_article_type_id = record.type_id
record.ticket.create_article_sender_id = record.sender_id
end
# set last contact
def last_contact_update(record)
# puts 'check last contact'
# if article in internal
return true if record.internal
# if article is a message to customer
return true if !Ticket::Article::Type.lookup( id: record.type_id ).communication
# if sender is customer
sender = Ticket::Article::Sender.lookup( id: record.sender_id )
if sender.name == 'Customer'
# 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
# set last_contact customer
record.ticket.last_contact_customer = record.created_at
# set last_contact
record.ticket.last_contact = record.created_at
end
return true
end
# if sender is not agent
return if sender.name != 'Agent'
# set last_contact_agent
record.ticket.last_contact_agent = record.created_at
# set last_contact
record.ticket.last_contact = record.created_at
true
end
end

View file

@ -1,14 +0,0 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::Ticket::ArticleCounter < ActiveRecord::Observer
observe 'ticket::_article'
def after_create(record)
# get article count
record.ticket.article_count = record.ticket.articles.count
# save ticket
record.ticket.save
end
end

View file

@ -1,18 +0,0 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::Ticket::ArticleSenderType < ActiveRecord::Observer
observe 'ticket::_article'
def after_create(record)
# get article count
count = Ticket::Article.where( ticket_id: record.ticket_id ).count
return if count > 1
record.ticket.create_article_type_id = record.type_id
record.ticket.create_article_sender_id = record.sender_id
# save ticket
record.ticket.save
end
end

View file

@ -1,34 +0,0 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::Ticket::FirstResponse < ActiveRecord::Observer
observe 'ticket::_article'
def after_create(record)
# puts 'check first response'
# return if we run import mode
return if Setting.get('import_mode')
# if article in internal
return true if record.internal
# if sender is not agent
sender = Ticket::Article::Sender.lookup( id: record.sender_id )
type = Ticket::Article::Type.lookup( id: record.type_id )
if sender.name != 'Agent' && type.name !~ /^phone/
return true
end
# if article is a message to customer
return true if !type.communication
# check if first_response is already set
return true if record.ticket.first_response
# set first_response
record.ticket.first_response = record.created_at
# save ticket
record.ticket.save
end
end

View file

@ -1,47 +0,0 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::Ticket::LastContact < ActiveRecord::Observer
observe 'ticket::_article'
def after_create(record)
# puts 'check last contact'
# if article in internal
return true if record.internal
# if article is a message to customer
return true if !Ticket::Article::Type.lookup( id: record.type_id ).communication
# if sender is not customer
sender = Ticket::Article::Sender.lookup( id: record.sender_id )
if sender.name == 'Customer'
# 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
# set last_contact customer
record.ticket.last_contact_customer = record.created_at
# set last_contact
record.ticket.last_contact = record.created_at
# save ticket
record.ticket.save
end
end
# if sender is not agent
return if sender.name != 'Agent'
# set last_contact_agent
record.ticket.last_contact_agent = record.created_at
# set last_contact
record.ticket.last_contact = record.created_at
# save ticket
record.ticket.save
end
end

View file

@ -26,12 +26,9 @@ module Zammad
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
config.active_record.observers = config.active_record.observers =
'observer::_session', 'observer::_session',
'observer::_ticket::_first_response',
'observer::_ticket::_last_contact',
'observer::_ticket::_close_time', 'observer::_ticket::_close_time',
'observer::_ticket::_user_ticket_counter', 'observer::_ticket::_user_ticket_counter',
'observer::_ticket::_article_counter', 'observer::_ticket::_article_changes',
'observer::_ticket::_article_sender_type',
'observer::_ticket::_article::_fillup_from_general', 'observer::_ticket::_article::_fillup_from_general',
'observer::_ticket::_article::_fillup_from_email', 'observer::_ticket::_article::_fillup_from_email',
'observer::_ticket::_article::_communicate_email', 'observer::_ticket::_article::_communicate_email',