trabajo-afectivo/lib/transaction_dispatcher.rb

236 lines
5.7 KiB
Ruby
Raw Permalink Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2016-04-15 21:56:10 +00:00
class TransactionDispatcher
def self.reset
EventBuffer.reset('transaction')
end
def self.commit(params = {})
# add attribute of interface handle (e. g. to send (no) notifications if a agent
# is creating a ticket via application_server, but send it if it's created via
# postmaster)
params[:interface_handle] = ApplicationHandleInfo.current
2016-04-15 21:56:10 +00:00
# execute object transactions
TransactionDispatcher.perform(params)
2016-04-15 21:56:10 +00:00
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')
# get async backends
2016-05-03 00:36:44 +00:00
sync_backends = []
Setting.where(area: 'Transaction::Backend::Sync').order(:name).each do |setting|
2016-05-03 00:36:44 +00:00
backend = Setting.get(setting.name)
2017-11-23 08:09:44 +00:00
next if params[:disable]&.include?(backend)
sync_backends.push backend.constantize
end
2016-05-03 00:36:44 +00:00
2016-04-15 21:56:10 +00:00
# get uniq objects
list_objects = get_uniq_changes(list)
2017-11-23 08:09:44 +00:00
list_objects.each_value do |objects|
objects.each_value do |item|
2016-05-03 00:36:44 +00:00
# execute sync backends
sync_backends.each do |backend|
execute_single_backend(backend, item, params)
end
2016-05-03 00:36:44 +00:00
# execute async backends
TransactionJob.perform_later(item, params)
end
end
2016-04-15 21:56:10 +00:00
end
def self.execute_single_backend(backend, item, params)
Rails.logger.debug { "Execute single 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 e
2016-05-03 00:36:44 +00:00
end
end
2016-04-15 21:56:10 +00:00
=begin
result = get_uniq_changes(events)
result = {
'Ticket' =>
1 => {
object: 'Ticket',
type: 'create',
object_id: 123,
article_id: 123,
user_id: 123,
2016-07-10 22:47:21 +00:00
created_at: Time.zone.now,
},
9 => {
object: 'Ticket',
type: 'update',
object_id: 123,
changes: {
attribute1: [before, now],
attribute2: [before, now],
},
user_id: 123,
2016-07-10 22:47:21 +00:00
created_at: Time.zone.now,
},
2016-04-15 21:56:10 +00:00
},
}
result = {
'Ticket' =>
9 => {
object: 'Ticket',
type: 'update',
object_id: 123,
article_id: 123,
changes: {
attribute1: [before, now],
attribute2: [before, now],
},
user_id: 123,
2016-07-10 22:47:21 +00:00
created_at: Time.zone.now,
},
2016-04-15 21:56:10 +00:00
},
}
=end
def self.get_uniq_changes(events)
list_objects = {}
events.each do |event|
2016-04-15 21:56:10 +00:00
# simulate article create as ticket update
article = nil
if event[:object] == 'Ticket::Article'
article = Ticket::Article.find_by(id: event[:id])
2016-04-15 21:56:10 +00:00
next if !article
next if event[:type] == 'update'
# set new event infos
ticket = Ticket.find_by(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
# get current state of objects
object = event[:object].constantize.find_by(id: event[:id])
2016-04-15 21:56:10 +00:00
# next if object is already deleted
next if !object
2016-04-15 21:56: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
store[:user_id] = event[:user_id]
2016-07-10 22:47:21 +00:00
store[:created_at] = event[:created_at]
2016-04-15 21:56:10 +00:00
if !store[:type] || store[:type] == 'update'
store[:type] = event[:type]
end
2016-04-15 21:56:10 +00:00
# merge changes
if event[:changes]
if store[:changes]
event[:changes].each do |key, value|
if store[:changes][key]
store[:changes][key][1] = value[1]
else
store[:changes][key] = value
end
end
else
store[:changes] = event[:changes]
2016-04-15 21:56:10 +00:00
end
end
2016-04-15 21:56:10 +00:00
# remember article id if exists
if article
store[:article_id] = article.id
2016-04-15 21:56:10 +00:00
end
end
2016-04-15 21:56:10 +00:00
list_objects
end
# Used as ActiveRecord lifecycle callback on the class.
def self.after_create(record)
2016-04-15 21:56:10 +00:00
# return if we run import mode
return true if Setting.get('import_mode')
2016-04-15 21:56:10 +00:00
e = {
object: record.class.name,
type: 'create',
data: record,
id: record.id,
user_id: record.created_by_id,
2016-07-10 22:47:21 +00:00
created_at: Time.zone.now,
2016-04-15 21:56:10 +00:00
}
EventBuffer.add('transaction', e)
true
2016-04-15 21:56:10 +00:00
end
# Used as ActiveRecord lifecycle callback on the class.
def self.before_update(record)
2016-04-15 21:56:10 +00:00
# return if we run import mode
return true if Setting.get('import_mode')
2016-04-15 21:56:10 +00:00
# ignore certain attributes
real_changes = {}
record.changes_to_save.each do |key, value|
2016-04-15 21:56:10 +00:00
next if key == 'updated_at'
next if key == 'article_count'
next if key == 'create_article_type_id'
next if key == 'create_article_sender_id'
2016-04-15 21:56:10 +00:00
real_changes[key] = value
end
2016-04-15 21:56:10 +00:00
# do not send anything if nothing has changed
2017-11-23 08:09:44 +00:00
return true if real_changes.blank?
2016-07-07 05:16:13 +00:00
changed_by_id = if record.respond_to?('updated_by_id')
record.updated_by_id
else
record.created_by_id
end
2016-07-06 06:13:44 +00:00
2016-04-15 21:56:10 +00:00
e = {
object: record.class.name,
type: 'update',
data: record,
changes: real_changes,
id: record.id,
user_id: changed_by_id,
2016-07-10 22:47:21 +00:00
created_at: Time.zone.now,
2016-04-15 21:56:10 +00:00
}
EventBuffer.add('transaction', e)
true
end
end