sets_last_owner_update_time.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. # Adds a last_owner_update time on ticket changes.
  3. module Ticket::SetsLastOwnerUpdateTime
  4. extend ActiveSupport::Concern
  5. included do
  6. before_create :ticket_set_last_owner_update_time
  7. before_update :ticket_set_last_owner_update_time
  8. end
  9. private
  10. def ticket_set_last_owner_update_time
  11. # return if we run import mode
  12. return true if Setting.get('import_mode')
  13. # check if owner, state or group has changed
  14. return true if changes_to_save['owner_id'].blank? && changes_to_save['state_id'].blank? && changes_to_save['group_id'].blank? && changes_to_save['last_contact_agent_at'].blank?
  15. # check if owner is nobody
  16. if changes_to_save['owner_id'].present? && changes_to_save['owner_id'][1] == 1
  17. self.last_owner_update_at = nil
  18. return true
  19. end
  20. # check if group is change
  21. if changes_to_save['group_id'].present?
  22. group = Group.lookup(id: changes_to_save['group_id'][1])
  23. return true if !group
  24. if group.assignment_timeout.blank? || group.assignment_timeout.zero?
  25. self.last_owner_update_at = nil
  26. return true
  27. end
  28. end
  29. # check if state is not new/open
  30. if changes_to_save['state_id'].present?
  31. state_ids = Ticket::State.by_category(:work_on).pluck(:id)
  32. if state_ids.exclude?(changes_to_save['state_id'][1])
  33. self.last_owner_update_at = nil
  34. return true
  35. end
  36. end
  37. self.last_owner_update_at = Time.zone.now
  38. end
  39. end