sla.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 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. core_workflow_screens 'create', 'edit'
  11. validates :name, presence: true, uniqueness: { case_sensitive: false }
  12. validate :cannot_have_response_and_update
  13. store :condition
  14. store :data
  15. def condition_matches?(ticket)
  16. query_condition, bind_condition, tables = Ticket.selector2sql(condition)
  17. Ticket.where(query_condition, *bind_condition).joins(tables).exists?(ticket.id)
  18. end
  19. def self.for_ticket(ticket)
  20. fallback = nil
  21. all.reorder(:name, :created_at).as_batches(size: 10) do |record|
  22. if record.condition.present?
  23. return record if record.condition_matches?(ticket)
  24. else
  25. fallback = record
  26. end
  27. end
  28. fallback
  29. end
  30. private
  31. def cannot_have_response_and_update
  32. return if response_time.blank? || update_time.blank?
  33. errors.add :base, __('Cannot have both response time and update time.')
  34. end
  35. end