2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
class Ticket::State < ApplicationModel
|
2019-01-28 06:04:05 +00:00
|
|
|
include CanBeImported
|
2021-04-12 09:49:26 +00:00
|
|
|
include ChecksHtmlSanitized
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksLatestChangeObserved
|
2020-02-20 13:34:03 +00:00
|
|
|
include HasCollectionUpdate
|
2021-01-27 12:55:22 +00:00
|
|
|
include HasSearchIndexBackend
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :state_type, class_name: 'Ticket::StateType', inverse_of: :states, optional: true
|
|
|
|
belongs_to :next_state, class_name: 'Ticket::State', optional: true
|
2018-04-12 14:57:37 +00:00
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
after_create :ensure_defaults
|
|
|
|
after_update :ensure_defaults
|
|
|
|
after_destroy :ensure_defaults
|
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
validates :name, presence: true
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2021-04-12 09:49:26 +00:00
|
|
|
sanitized_html :note
|
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
attr_accessor :callback_loop
|
|
|
|
|
2021-07-21 14:04:17 +00:00
|
|
|
TYPES = {
|
|
|
|
open: ['new', 'open', 'pending reminder', 'pending action'],
|
|
|
|
pending_reminder: ['pending reminder'],
|
|
|
|
pending_action: ['pending action'],
|
|
|
|
pending: ['pending reminder', 'pending action'],
|
|
|
|
work_on: %w[new open],
|
|
|
|
work_on_all: ['new', 'open', 'pending reminder'],
|
|
|
|
viewable: ['new', 'open', 'pending reminder', 'pending action', 'closed', 'removed'],
|
|
|
|
viewable_agent_new: ['new', 'open', 'pending reminder', 'pending action', 'closed'],
|
|
|
|
viewable_agent_edit: ['open', 'pending reminder', 'pending action', 'closed'],
|
|
|
|
viewable_customer_new: %w[new closed],
|
|
|
|
viewable_customer_edit: %w[open closed],
|
|
|
|
closed: %w[closed],
|
|
|
|
merged: %w[merged],
|
|
|
|
}.freeze
|
|
|
|
|
2013-08-06 09:23:25 +00:00
|
|
|
=begin
|
|
|
|
|
2017-03-27 14:06:18 +00:00
|
|
|
looks up states for a given category
|
2013-08-06 09:23:25 +00:00
|
|
|
|
2017-03-31 11:56:42 +00:00
|
|
|
states = Ticket::State.by_category(:open) # :open|:closed|:work_on|:work_on_all|:viewable|:viewable_agent_new|:viewable_agent_edit|:viewable_customer_new|:viewable_customer_edit|:pending_reminder|:pending_action|:pending|:merged
|
2013-08-06 09:23:25 +00:00
|
|
|
|
2013-08-06 09:49:56 +00:00
|
|
|
returns:
|
|
|
|
|
2017-03-27 14:06:18 +00:00
|
|
|
state object list
|
2013-08-06 09:49:56 +00:00
|
|
|
|
2013-08-06 09:23:25 +00:00
|
|
|
=end
|
|
|
|
|
2021-07-21 14:04:17 +00:00
|
|
|
def self.by_category(*categories)
|
|
|
|
state_types = TYPES.slice(*categories.map(&:to_sym)).values.uniq
|
|
|
|
raise ArgumentError, "No such categories (#{categories.join(', ')})" if state_types.empty?
|
2017-03-27 14:06:18 +00:00
|
|
|
|
2021-07-21 14:04:17 +00:00
|
|
|
Ticket::State.joins(:state_type).where(ticket_state_types: { name: state_types })
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-06 09:49:56 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check if state is ignored for escalation
|
|
|
|
|
2015-09-10 19:09:50 +00:00
|
|
|
state = Ticket::State.lookup(name: 'state name')
|
2013-08-06 09:49:56 +00:00
|
|
|
|
2014-06-08 22:01:20 +00:00
|
|
|
result = state.ignore_escalation?
|
2013-08-06 09:49:56 +00:00
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
true/false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def ignore_escalation?
|
2015-09-10 19:09:50 +00:00
|
|
|
return true if ignore_escalation
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
false
|
2013-08-06 09:49:56 +00:00
|
|
|
end
|
2017-02-12 17:21:03 +00:00
|
|
|
|
|
|
|
def ensure_defaults
|
|
|
|
return if callback_loop
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%w[default_create default_follow_up].each do |default_field|
|
2017-02-12 17:21:03 +00:00
|
|
|
states_with_default = Ticket::State.where(default_field => true)
|
|
|
|
next if states_with_default.count == 1
|
|
|
|
|
|
|
|
if states_with_default.count.zero?
|
|
|
|
state = Ticket::State.where(active: true).order(id: :asc).first
|
|
|
|
state[default_field] = true
|
|
|
|
state.callback_loop = true
|
|
|
|
state.save!
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
Ticket::State.all.each do |local_state|
|
2017-02-12 17:21:03 +00:00
|
|
|
next if local_state.id == id
|
|
|
|
next if local_state[default_field] == false
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
local_state[default_field] = false
|
|
|
|
local_state.callback_loop = true
|
|
|
|
local_state.save!
|
|
|
|
next
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-02-12 17:21:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|