2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
module Ticket::Article::HasTicketContactAttributesImpact
|
|
|
|
extend ActiveSupport::Concern
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
included do
|
|
|
|
after_create :update_ticket_article_attributes
|
|
|
|
after_destroy :update_ticket_count_attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_ticket_article_attributes
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = false
|
2021-02-15 13:55:00 +00:00
|
|
|
if article_count_update
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
if first_response_at_update
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
if sender_type_update
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
if last_contact_update_at
|
2016-05-04 09:45:05 +00:00
|
|
|
changed = true
|
|
|
|
end
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# save ticket
|
2016-05-04 16:12:53 +00:00
|
|
|
if !changed
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.touch # rubocop:disable Rails/SkipsModelValidations
|
2016-12-15 16:05:54 +00:00
|
|
|
return
|
|
|
|
end
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.save!
|
2016-12-15 16:05:54 +00:00
|
|
|
end
|
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
def update_ticket_count_attribute
|
2016-12-15 16:05:54 +00:00
|
|
|
changed = false
|
2021-02-15 13:55:00 +00:00
|
|
|
if article_count_update
|
2016-12-15 16:05:54 +00:00
|
|
|
changed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# save ticket
|
|
|
|
if !changed
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.touch # rubocop:disable Rails/SkipsModelValidations
|
2016-05-04 16:12:53 +00:00
|
|
|
return
|
|
|
|
end
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.save!
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get article count
|
2021-02-15 13:55:00 +00:00
|
|
|
def article_count_update
|
|
|
|
current_count = ticket.article_count
|
2016-05-04 09:45:05 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(name: 'System')
|
2021-02-15 13:55:00 +00:00
|
|
|
count = Ticket::Article.where(ticket_id: ticket_id).where.not(sender_id: sender.id).count
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if current_count == count
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.article_count = count
|
2016-05-04 09:45:05 +00:00
|
|
|
true
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# set first response
|
2021-02-15 13:55:00 +00:00
|
|
|
def first_response_at_update
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# return if we run import mode
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if Setting.get('import_mode')
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article in internal
|
2021-02-15 13:55:00 +00:00
|
|
|
return false if internal
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if sender is not agent
|
2021-02-15 13:55:00 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: sender_id)
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if sender.name != 'Agent'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article is a message to customer
|
2021-02-15 13:55:00 +00:00
|
|
|
type = Ticket::Article::Type.lookup(id: type_id)
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if !type.communication
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# check if first_response_at is already set
|
2021-02-15 13:55:00 +00:00
|
|
|
return false if ticket.first_response_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# set first_response_at
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.first_response_at = created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# set sender type
|
2021-02-15 13:55:00 +00:00
|
|
|
def sender_type_update
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# ignore if create channel is already set
|
2021-02-15 13:55:00 +00:00
|
|
|
count = Ticket::Article.where(ticket_id: ticket_id).count
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if count > 1
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2021-02-15 13:55:00 +00:00
|
|
|
ticket.create_article_type_id = type_id
|
|
|
|
ticket.create_article_sender_id = sender_id
|
2016-05-04 09:45:05 +00:00
|
|
|
true
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# set last contact
|
2021-02-15 13:55:00 +00:00
|
|
|
def last_contact_update_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article in internal
|
2021-02-15 13:55:00 +00:00
|
|
|
return false if internal
|
2016-05-04 09:45:05 +00:00
|
|
|
|
|
|
|
# if sender is system
|
2021-02-15 13:55:00 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: sender_id)
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if sender.name == 'System'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if article is a message to customer
|
2021-02-15 13:55:00 +00:00
|
|
|
return false if !Ticket::Article::Type.lookup(id: type_id).communication
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# if sender is customer
|
2021-02-15 13:55:00 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: sender_id)
|
|
|
|
ticket = self.ticket
|
2015-05-01 15:24:17 +00:00
|
|
|
if sender.name == 'Customer'
|
|
|
|
|
2019-08-16 15:39:31 +00:00
|
|
|
# in case, update last_contact_customer_at on any customer follow-up
|
2018-05-30 08:49:54 +00:00
|
|
|
if Setting.get('ticket_last_contact_behaviour') == 'based_on_customer_reaction'
|
|
|
|
|
|
|
|
# set last_contact_at customer
|
2021-02-15 13:55:00 +00:00
|
|
|
self.ticket.last_contact_customer_at = created_at
|
2018-05-30 08:49:54 +00:00
|
|
|
|
|
|
|
# set last_contact
|
2021-02-15 13:55:00 +00:00
|
|
|
self.ticket.last_contact_at = created_at
|
2018-05-30 08:49:54 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# if customer is sending again, ignore update of last contact (use case of update escalation)
|
2016-09-14 07:15:30 +00:00
|
|
|
return false if ticket.last_contact_customer_at &&
|
|
|
|
ticket.last_contact_at &&
|
|
|
|
ticket.last_contact_customer_at == ticket.last_contact_at
|
2015-10-30 10:05:51 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# check if last communication is done by agent, else do not set last_contact_customer_at
|
|
|
|
if ticket.last_contact_customer_at.nil? ||
|
|
|
|
ticket.last_contact_agent_at.nil? ||
|
|
|
|
ticket.last_contact_agent_at.to_i > ticket.last_contact_customer_at.to_i
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# set last_contact_at customer
|
2021-02-15 13:55:00 +00:00
|
|
|
self.ticket.last_contact_customer_at = created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# set last_contact
|
2021-02-15 13:55:00 +00:00
|
|
|
self.ticket.last_contact_at = created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# if sender is not agent
|
2016-05-04 09:45:05 +00:00
|
|
|
return false if sender.name != 'Agent'
|
2015-05-01 15:24:17 +00:00
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
# set last_contact_agent_at
|
2021-02-15 13:55:00 +00:00
|
|
|
self.ticket.last_contact_agent_at = created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
# set last_contact
|
2021-02-15 13:55:00 +00:00
|
|
|
self.ticket.last_contact_at = created_at
|
2015-05-01 15:24:17 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2021-02-15 13:55:00 +00:00
|
|
|
|
2015-05-01 15:24:17 +00:00
|
|
|
end
|