ticket_preferences.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Escalation
  3. class TicketPreferences
  4. KEYS = %i[escalation_disabled
  5. first_response_at last_update_at close_at escalation_at
  6. sla_id sla_updated_at
  7. calendar_id calendar_updated_at last_contact_at].freeze
  8. attr_reader :hash
  9. def initialize(ticket)
  10. @hash = {}
  11. KEYS.each do |key|
  12. @hash[key] = ticket.preferences.dig(:escalation_calculation, key) || nil
  13. end
  14. end
  15. def escalation_became_enabled?(escalation_disabled)
  16. !escalation_disabled && @hash[:escalation_disabled] != escalation_disabled
  17. end
  18. def sla_changed?(sla)
  19. @hash[:sla_id] != sla&.id || @hash[:sla_updated_at] != sla&.updated_at
  20. end
  21. def calendar_changed?(calendar)
  22. @hash[:calendar_id] != calendar&.id || @hash[:calendar_updated_at] != calendar&.updated_at
  23. end
  24. def first_response_at_changed?(ticket)
  25. @hash[:first_response_at] != ticket.first_response_at
  26. end
  27. def last_update_at_changed?(ticket)
  28. @hash[:last_update_at] != ticket.last_original_update_at || ticket.saved_change_to_state_id?
  29. end
  30. def close_at_changed?(ticket)
  31. @hash[:close_at] != ticket.close_at
  32. end
  33. def last_contact_at_changed?(ticket)
  34. @hash[:last_contact_at] != ticket.last_contact_at
  35. end
  36. def property_changes?(ticket)
  37. %i[first_response_at last_update_at close_at].any? { |elem| send("#{elem}_changed?", ticket) }
  38. end
  39. def any_changes?(ticket, sla, escalation_disabled)
  40. property_changes?(ticket) || sla_changed?(sla) || calendar_changed?(sla&.calendar) || @hash[:escalation_disabled] != escalation_disabled
  41. end
  42. def update_preferences(ticket, sla, escalation_disabled)
  43. new_hash = hash_of(ticket, sla, escalation_disabled)
  44. return if new_hash == { escalation_disabled: false } && !@hash[:escalation_disabled] # do not update when update not necessary
  45. ticket.preferences[:escalation_calculation] = new_hash
  46. end
  47. def hash_of(ticket, sla, escalation_disabled)
  48. {
  49. first_response_at: ticket.first_response_at,
  50. last_update_at: ticket.last_original_update_at,
  51. close_at: ticket.close_at,
  52. last_contact_at: ticket.last_contact_at,
  53. sla_id: sla&.id,
  54. sla_updated_at: sla&.updated_at,
  55. calendar_id: sla&.calendar&.id,
  56. calendar_updated_at: sla&.calendar&.updated_at,
  57. escalation_disabled: escalation_disabled,
  58. }.compact
  59. end
  60. end
  61. end