trabajo-afectivo/app/models/transaction/trigger.rb

125 lines
3.3 KiB
Ruby
Raw Normal View History

2016-05-03 00:36:44 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Transaction::Trigger
=begin
{
object: 'Ticket',
type: 'update',
object_id: 123,
interface_handle: 'application_server', # application_server|websocket|scheduler
2016-05-03 00:36:44 +00:00
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],
2016-07-16 21:43:08 +00:00
},
created_at: Time.zone.now,
2016-05-03 00:36:44 +00:00
user_id: 123,
},
=end
def initialize(item, params = {})
@item = item
@params = params
end
def perform
# return if we run import mode
return if Setting.get('import_mode')
return if @item[:object] != 'Ticket'
triggers = Trigger.where(active: true)
return if triggers.empty?
ticket = Ticket.lookup(id: @item[:object_id])
return if !ticket
if @item[:article_id]
article = Ticket::Article.lookup(id: @item[:article_id])
end
original_user_id = UserInfo.current_user_id
2016-05-03 00:36:44 +00:00
UserInfo.current_user_id = 1
2016-06-30 20:04:48 +00:00
triggers.each { |trigger|
2016-05-03 00:36:44 +00:00
condition = trigger.condition
# check action
if condition['ticket.action']
next if condition['ticket.action']['operator'] == 'is' && condition['ticket.action']['value'] != @item[:type]
next if condition['ticket.action']['operator'] != 'is' && condition['ticket.action']['value'] == @item[:type]
condition.delete('ticket.action')
end
2016-07-16 21:43:08 +00:00
2016-06-30 20:04:48 +00:00
# check "has changed" options
2016-07-16 21:43:08 +00:00
has_changed_condition_exists = false
has_changed = false
condition.each do |key, value|
2016-06-30 20:04:48 +00:00
next if !value
next if !value['operator']
next if !value['operator']['has changed']
2016-07-16 21:43:08 +00:00
has_changed_condition_exists = true
2016-06-30 20:04:48 +00:00
# next if has changed? && !@item[:changes][attribute]
(object_name, attribute) = key.split('.', 2)
2016-05-03 00:36:44 +00:00
2016-06-30 20:04:48 +00:00
# remove condition item, because it has changed
if @item[:changes][attribute]
2016-07-16 21:43:08 +00:00
has_changed = true
condition.delete(key)
2016-06-30 20:04:48 +00:00
next
end
break
end
2016-07-16 21:43:08 +00:00
next if has_changed_condition_exists && !has_changed
2016-05-03 13:51:07 +00:00
# check if selector is matching
2016-05-03 00:36:44 +00:00
condition['ticket.id'] = {
operator: 'is',
value: ticket.id,
}
2016-05-03 13:51:07 +00:00
if article
condition['article.id'] = {
operator: 'is',
value: article.id,
}
end
2016-05-03 00:36:44 +00:00
ticket_count, tickets = Ticket.selectors(condition, 1)
2016-07-26 22:02:28 +00:00
next if ticket_count.zero?
2016-05-03 00:36:44 +00:00
next if tickets.first.id != ticket.id
2016-05-03 13:51:07 +00:00
# check if min one article attribute is used
article_selector = false
trigger.condition.each do |key, _value|
(object_name, attribute) = key.split('.', 2)
next if object_name != 'article'
next if attribute == 'id'
article_selector = true
end
2016-05-03 00:36:44 +00:00
# check in min one attribute has changed
2016-05-03 13:51:07 +00:00
if @item[:type] == 'update' && !article_selector
2016-05-03 00:36:44 +00:00
match = false
2016-07-16 21:43:08 +00:00
if has_changed_condition_exists && has_changed
2016-05-03 00:36:44 +00:00
match = true
2016-07-16 21:43:08 +00:00
else
trigger.condition.each do |key, _value|
(object_name, attribute) = key.split('.', 2)
next if object_name != 'ticket'
next if !@item[:changes][attribute]
match = true
break
end
2016-05-03 00:36:44 +00:00
end
next if !match
end
ticket.perform_changes(trigger.perform, 'trigger', @item)
2016-05-03 00:36:44 +00:00
}
UserInfo.current_user_id = original_user_id
2016-05-03 00:36:44 +00:00
end
end