has_ticket_contact_attributes_impact.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::Article::HasTicketContactAttributesImpact
  3. extend ActiveSupport::Concern
  4. included do
  5. after_create :update_ticket_article_attributes
  6. after_destroy :update_ticket_count_attribute
  7. end
  8. private
  9. def update_ticket_article_attributes
  10. changed = false
  11. if article_count_update
  12. changed = true
  13. end
  14. if first_response_at_update
  15. changed = true
  16. end
  17. if sender_type_update
  18. changed = true
  19. end
  20. if last_contact_update_at
  21. changed = true
  22. end
  23. if !changed
  24. ticket.touch # rubocop:disable Rails/SkipsModelValidations
  25. return
  26. end
  27. ticket.save!
  28. end
  29. def update_ticket_count_attribute
  30. changed = false
  31. if article_count_update
  32. changed = true
  33. end
  34. # save ticket
  35. if !changed
  36. ticket.touch # rubocop:disable Rails/SkipsModelValidations
  37. return
  38. end
  39. ticket.save!
  40. end
  41. # get article count
  42. def article_count_update
  43. current_count = ticket.article_count
  44. sender = Ticket::Article::Sender.lookup(name: 'System')
  45. count = Ticket::Article.where(ticket_id: ticket_id).where.not(sender_id: sender.id).count
  46. return false if current_count == count
  47. ticket.article_count = count
  48. true
  49. end
  50. # set first response
  51. def first_response_at_update
  52. # return if we run import mode
  53. return false if Setting.get('import_mode')
  54. # if article in internal
  55. return false if internal
  56. # if sender is not agent
  57. sender = Ticket::Article::Sender.lookup(id: sender_id)
  58. return false if sender.name != 'Agent'
  59. # if article is a message to customer
  60. type = Ticket::Article::Type.lookup(id: type_id)
  61. return false if !type.communication
  62. # check if first_response_at is already set
  63. return false if ticket.first_response_at
  64. # set first_response_at
  65. ticket.first_response_at = created_at
  66. true
  67. end
  68. # set sender type
  69. def sender_type_update
  70. # ignore if create channel is already set
  71. count = Ticket::Article.where(ticket_id: ticket_id).count
  72. return false if count > 1
  73. ticket.create_article_type_id = type_id
  74. ticket.create_article_sender_id = sender_id
  75. true
  76. end
  77. # set last contact
  78. def last_contact_update_at
  79. # if article in internal
  80. return false if internal
  81. # if sender is system
  82. sender = Ticket::Article::Sender.lookup(id: sender_id)
  83. return false if sender.name == 'System'
  84. # if article is a message to customer
  85. return false if !Ticket::Article::Type.lookup(id: type_id).communication
  86. # if sender is customer
  87. sender = Ticket::Article::Sender.lookup(id: sender_id)
  88. ticket = self.ticket
  89. if sender.name == 'Customer'
  90. # in case, update last_contact_customer_at on any customer follow-up
  91. if Setting.get('ticket_last_contact_behaviour') == 'based_on_customer_reaction'
  92. # set last_contact_at customer
  93. self.ticket.last_contact_customer_at = created_at
  94. # set last_contact
  95. self.ticket.last_contact_at = created_at
  96. return true
  97. end
  98. # if customer is sending again, ignore update of last contact (use case of update escalation)
  99. return false if ticket.last_contact_customer_at &&
  100. ticket.last_contact_at &&
  101. ticket.last_contact_customer_at == ticket.last_contact_at
  102. # check if last communication is done by agent, else do not set last_contact_customer_at
  103. if ticket.last_contact_customer_at.nil? ||
  104. ticket.last_contact_agent_at.nil? ||
  105. ticket.last_contact_agent_at.to_i > ticket.last_contact_customer_at.to_i
  106. # set last_contact_at customer
  107. self.ticket.last_contact_customer_at = created_at
  108. # set last_contact
  109. self.ticket.last_contact_at = created_at
  110. end
  111. return true
  112. end
  113. # if sender is not agent
  114. return false if sender.name != 'Agent'
  115. # set last_contact_agent_at
  116. self.ticket.last_contact_agent_at = created_at
  117. # set last_contact
  118. self.ticket.last_contact_at = created_at
  119. true
  120. end
  121. end