last_owner_update.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Observer::Ticket::LastOwnerUpdate < ActiveRecord::Observer
  3. observe 'ticket'
  4. def before_create(record)
  5. _check(record)
  6. end
  7. def before_update(record)
  8. _check(record)
  9. end
  10. private
  11. def _check(record)
  12. # return if we run import mode
  13. return true if Setting.get('import_mode')
  14. # check if owner, state or group has changed
  15. return true if record.changes_to_save['owner_id'].blank? && record.changes_to_save['state_id'].blank? && record.changes_to_save['group_id'].blank? && record.changes_to_save['last_contact_agent_at'].blank?
  16. # check if owner is nobody
  17. if record.changes_to_save['owner_id'].present? && record.changes_to_save['owner_id'][1] == 1
  18. record.last_owner_update_at = nil
  19. return true
  20. end
  21. # check if group is change
  22. if record.changes_to_save['group_id'].present?
  23. group = Group.lookup(id: record.changes_to_save['group_id'][1])
  24. return true if !group
  25. if group.assignment_timeout.blank? || group.assignment_timeout.zero?
  26. record.last_owner_update_at = nil
  27. return true
  28. end
  29. end
  30. # check if state is not new/open
  31. if record.changes_to_save['state_id'].present?
  32. state_ids = Ticket::State.by_category(:work_on).pluck(:id)
  33. if !state_ids.include?(record.changes_to_save['state_id'][1])
  34. record.last_owner_update_at = nil
  35. return true
  36. end
  37. end
  38. record.last_owner_update_at = Time.zone.now
  39. end
  40. end