2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-02-01 08:53:14 +00:00
|
|
|
class Sla < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksClientNotification
|
|
|
|
include ChecksConditionValidation
|
2021-02-15 13:55:00 +00:00
|
|
|
include HasEscalationCalculationImpact
|
2016-03-20 19:09:52 +00:00
|
|
|
include Sla::Assets
|
|
|
|
|
2021-09-23 07:52:08 +00:00
|
|
|
belongs_to :calendar, optional: true
|
|
|
|
|
|
|
|
# workflow checks should run after before_create and before_update callbacks
|
|
|
|
include ChecksCoreWorkflow
|
|
|
|
|
|
|
|
validates :name, presence: true
|
|
|
|
|
2021-10-27 09:38:45 +00:00
|
|
|
validate :cannot_have_response_and_update
|
|
|
|
|
2015-12-11 16:00:04 +00:00
|
|
|
store :condition
|
|
|
|
store :data
|
2021-02-15 13:55:00 +00:00
|
|
|
|
|
|
|
def condition_matches?(ticket)
|
|
|
|
query_condition, bind_condition, tables = Ticket.selector2sql(condition)
|
|
|
|
Ticket.where(query_condition, *bind_condition).joins(tables).exists?(ticket.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.for_ticket(ticket)
|
|
|
|
fallback = nil
|
2021-11-30 13:04:17 +00:00
|
|
|
all.order(:name, :created_at).as_batches(size: 10) do |record|
|
2021-02-15 13:55:00 +00:00
|
|
|
if record.condition.present?
|
|
|
|
return record if record.condition_matches?(ticket)
|
|
|
|
else
|
|
|
|
fallback = record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fallback
|
|
|
|
end
|
2021-10-27 09:38:45 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cannot_have_response_and_update
|
|
|
|
return if response_time.blank? || update_time.blank?
|
|
|
|
|
|
|
|
errors.add :base, 'cannot have both response time and update time'
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|