sla.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sla < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksConditionValidation
  5. include HasEscalationCalculationImpact
  6. include Sla::Assets
  7. belongs_to :calendar, optional: true
  8. # workflow checks should run after before_create and before_update callbacks
  9. include ChecksCoreWorkflow
  10. validates :name, presence: true
  11. validate :cannot_have_response_and_update
  12. store :condition
  13. store :data
  14. def condition_matches?(ticket)
  15. query_condition, bind_condition, tables = Ticket.selector2sql(condition)
  16. Ticket.where(query_condition, *bind_condition).joins(tables).exists?(ticket.id)
  17. end
  18. def self.for_ticket(ticket)
  19. fallback = nil
  20. all.order(:name, :created_at).as_batches(size: 10) do |record|
  21. if record.condition.present?
  22. return record if record.condition_matches?(ticket)
  23. else
  24. fallback = record
  25. end
  26. end
  27. fallback
  28. end
  29. private
  30. def cannot_have_response_and_update
  31. return if response_time.blank? || update_time.blank?
  32. errors.add :base, 'cannot have both response time and update time'
  33. end
  34. end