2016-04-15 21:56:10 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Observer::Transaction < ActiveRecord::Observer
|
2016-04-22 06:55:10 +00:00
|
|
|
observe :ticket, 'ticket::_article', :user, :organization
|
2016-04-14 07:17:13 +00:00
|
|
|
|
|
|
|
def self.commit(params = {})
|
|
|
|
|
2016-04-15 21:56:10 +00:00
|
|
|
# add attribute if execution is via web
|
|
|
|
params[:via_web] = false
|
|
|
|
if ENV['RACK_ENV'] || Rails.configuration.webserver_is_active
|
|
|
|
params[:via_web] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# execute object transactions
|
|
|
|
Observer::Transaction.perform(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.perform(params)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
# get buffer
|
|
|
|
list = EventBuffer.list('transaction')
|
|
|
|
|
|
|
|
# reset buffer
|
|
|
|
EventBuffer.reset('transaction')
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
# get asyn backends
|
|
|
|
sync_backends = []
|
|
|
|
Setting.where(area: 'Transaction::Backend::Sync').order(:name).each {|setting|
|
|
|
|
backend = Setting.get(setting.name)
|
|
|
|
sync_backends.push Kernel.const_get(backend)
|
|
|
|
}
|
|
|
|
|
2016-04-15 21:56:10 +00:00
|
|
|
# get uniq objects
|
|
|
|
list_objects = get_uniq_changes(list)
|
2016-04-22 06:55:10 +00:00
|
|
|
list_objects.each {|_object, objects|
|
|
|
|
objects.each {|_id, item|
|
2016-05-03 00:36:44 +00:00
|
|
|
|
|
|
|
# execute sync backends
|
|
|
|
sync_backends.each {|backend|
|
|
|
|
execute_singel_backend(backend, item, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
# execute async backends
|
2016-04-22 06:55:10 +00:00
|
|
|
Delayed::Job.enqueue(Transaction::BackgroundJob.new(item, params))
|
|
|
|
}
|
2016-04-15 21:56:10 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.execute_singel_backend(backend, item, params)
|
2016-05-05 19:04:51 +00:00
|
|
|
Rails.logger.debug "Execute singel backend #{backend}"
|
2016-05-03 00:36:44 +00:00
|
|
|
begin
|
|
|
|
UserInfo.current_user_id = nil
|
|
|
|
integration = backend.new(item, params)
|
|
|
|
integration.perform
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error 'ERROR: ' + backend.inspect
|
|
|
|
Rails.logger.error 'ERROR: ' + e.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-15 21:56:10 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
result = get_uniq_changes(events)
|
|
|
|
|
|
|
|
result = {
|
2016-04-22 06:55:10 +00:00
|
|
|
'Ticket' =>
|
|
|
|
1 => {
|
|
|
|
object: 'Ticket',
|
|
|
|
type: 'create',
|
|
|
|
object_id: 123,
|
|
|
|
article_id: 123,
|
2016-04-27 07:31:11 +00:00
|
|
|
user_id: 123,
|
2016-04-22 06:55:10 +00:00
|
|
|
},
|
|
|
|
9 => {
|
|
|
|
object: 'Ticket',
|
|
|
|
type: 'update',
|
|
|
|
object_id: 123,
|
|
|
|
changes: {
|
|
|
|
attribute1: [before, now],
|
|
|
|
attribute2: [before, now],
|
|
|
|
},
|
2016-04-27 07:31:11 +00:00
|
|
|
user_id: 123,
|
2016-04-22 06:55:10 +00:00
|
|
|
},
|
2016-04-15 21:56:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result = {
|
2016-04-22 06:55:10 +00:00
|
|
|
'Ticket' =>
|
|
|
|
9 => {
|
|
|
|
object: 'Ticket',
|
|
|
|
type: 'update',
|
|
|
|
object_id: 123,
|
|
|
|
article_id: 123,
|
|
|
|
changes: {
|
|
|
|
attribute1: [before, now],
|
|
|
|
attribute2: [before, now],
|
|
|
|
},
|
2016-04-27 07:31:11 +00:00
|
|
|
user_id: 123,
|
2016-04-22 06:55:10 +00:00
|
|
|
},
|
2016-04-15 21:56:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.get_uniq_changes(events)
|
|
|
|
list_objects = {}
|
|
|
|
events.each { |event|
|
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
# simulate article create as ticket update
|
|
|
|
article = nil
|
|
|
|
if event[:object] == 'Ticket::Article'
|
2016-04-15 21:56:10 +00:00
|
|
|
article = Ticket::Article.lookup(id: event[:id])
|
|
|
|
next if !article
|
2016-04-22 06:55:10 +00:00
|
|
|
next if event[:type] == 'update'
|
|
|
|
|
|
|
|
# set new event infos
|
|
|
|
ticket = Ticket.lookup(id: article.ticket_id)
|
|
|
|
event[:object] = 'Ticket'
|
|
|
|
event[:id] = ticket.id
|
|
|
|
event[:type] = 'update'
|
|
|
|
event[:changes] = nil
|
|
|
|
end
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
# get current state of objects
|
|
|
|
object = Kernel.const_get(event[:object]).lookup(id: event[:id])
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
# next if object is already deleted
|
|
|
|
next if !object
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
if !list_objects[event[:object]]
|
|
|
|
list_objects[event[:object]] = {}
|
|
|
|
end
|
|
|
|
if !list_objects[event[:object]][object.id]
|
|
|
|
list_objects[event[:object]][object.id] = {}
|
|
|
|
end
|
|
|
|
store = list_objects[event[:object]][object.id]
|
|
|
|
store[:object] = event[:object]
|
|
|
|
store[:object_id] = object.id
|
2016-04-27 07:31:11 +00:00
|
|
|
store[:user_id] = event[:user_id]
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
if !store[:type] || store[:type] == 'update'
|
|
|
|
store[:type] = event[:type]
|
|
|
|
end
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
# merge changes
|
|
|
|
if event[:changes]
|
|
|
|
if !store[:changes]
|
|
|
|
store[:changes] = event[:changes]
|
|
|
|
else
|
|
|
|
event[:changes].each {|key, value|
|
|
|
|
if !store[:changes][key]
|
|
|
|
store[:changes][key] = value
|
|
|
|
else
|
|
|
|
store[:changes][key][1] = value[1]
|
|
|
|
end
|
|
|
|
}
|
2016-04-15 21:56:10 +00:00
|
|
|
end
|
2016-04-22 06:55:10 +00:00
|
|
|
end
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
# remember article id if exists
|
|
|
|
if article
|
|
|
|
store[:article_id] = article.id
|
2016-04-15 21:56:10 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
list_objects
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create(record)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
e = {
|
2016-04-22 06:55:10 +00:00
|
|
|
object: record.class.name,
|
2016-04-15 21:56:10 +00:00
|
|
|
type: 'create',
|
|
|
|
data: record,
|
|
|
|
id: record.id,
|
2016-04-27 07:31:11 +00:00
|
|
|
user_id: record.created_by_id,
|
2016-04-15 21:56:10 +00:00
|
|
|
}
|
|
|
|
EventBuffer.add('transaction', e)
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_update(record)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
# ignore certain attributes
|
|
|
|
real_changes = {}
|
|
|
|
record.changes.each {|key, value|
|
|
|
|
next if key == 'updated_at'
|
|
|
|
next if key == 'first_response'
|
|
|
|
next if key == 'close_time'
|
|
|
|
next if key == 'last_contact_agent'
|
|
|
|
next if key == 'last_contact_customer'
|
|
|
|
next if key == 'last_contact'
|
|
|
|
next if key == 'article_count'
|
|
|
|
next if key == 'create_article_type_id'
|
|
|
|
next if key == 'create_article_sender_id'
|
|
|
|
real_changes[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
# do not send anything if nothing has changed
|
|
|
|
return if real_changes.empty?
|
2016-04-14 07:17:13 +00:00
|
|
|
|
2016-04-15 21:56:10 +00:00
|
|
|
e = {
|
2016-04-22 06:55:10 +00:00
|
|
|
object: record.class.name,
|
2016-04-15 21:56:10 +00:00
|
|
|
type: 'update',
|
|
|
|
data: record,
|
|
|
|
changes: real_changes,
|
|
|
|
id: record.id,
|
2016-04-27 07:31:11 +00:00
|
|
|
user_id: record.updated_by_id,
|
2016-04-15 21:56:10 +00:00
|
|
|
}
|
|
|
|
EventBuffer.add('transaction', e)
|
2016-04-14 07:17:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|