2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2021-05-20 06:59:02 +00:00
|
|
|
class TransactionDispatcher
|
2016-04-14 07:17:13 +00:00
|
|
|
|
2017-07-26 18:46:31 +00:00
|
|
|
def self.reset
|
|
|
|
EventBuffer.reset('transaction')
|
|
|
|
end
|
|
|
|
|
2016-04-14 07:17:13 +00:00
|
|
|
def self.commit(params = {})
|
|
|
|
|
2016-08-20 19:29:22 +00:00
|
|
|
# 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
|
2021-05-20 06:59:02 +00:00
|
|
|
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')
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# get async backends
|
2016-05-03 00:36:44 +00:00
|
|
|
sync_backends = []
|
2017-10-01 12:25:52 +00:00
|
|
|
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)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-01-06 18:41:29 +00:00
|
|
|
sync_backends.push backend.constantize
|
2017-10-01 12:25:52 +00:00
|
|
|
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
|
2017-10-01 12:25:52 +00:00
|
|
|
sync_backends.each do |backend|
|
2021-05-20 06:59:02 +00:00
|
|
|
execute_single_backend(backend, item, params)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-05-03 00:36:44 +00:00
|
|
|
|
|
|
|
# execute async backends
|
2020-10-27 15:36:38 +00:00
|
|
|
TransactionJob.perform_later(item, params)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-15 21:56:10 +00:00
|
|
|
end
|
|
|
|
|
2021-05-20 06:59:02 +00:00
|
|
|
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
|
2018-11-27 09:56:23 +00:00
|
|
|
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 = {
|
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-07-10 22:47:21 +00:00
|
|
|
created_at: Time.zone.now,
|
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-07-10 22:47:21 +00:00
|
|
|
created_at: Time.zone.now,
|
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-07-10 22:47:21 +00:00
|
|
|
created_at: Time.zone.now,
|
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 = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
events.each do |event|
|
2016-04-15 21:56:10 +00:00
|
|
|
|
2016-04-22 06:55:10 +00:00
|
|
|
# simulate article create as ticket update
|
|
|
|
article = nil
|
|
|
|
if event[:object] == 'Ticket::Article'
|
2018-06-05 10:37:52 +00:00
|
|
|
article = Ticket::Article.find_by(id: event[:id])
|
2016-04-15 21:56:10 +00:00
|
|
|
next if !article
|
2016-04-22 06:55:10 +00:00
|
|
|
next if event[:type] == 'update'
|
|
|
|
|
|
|
|
# set new event infos
|
2018-06-05 10:37:52 +00:00
|
|
|
ticket = Ticket.find_by(id: article.ticket_id)
|
2016-04-22 06:55:10 +00:00
|
|
|
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
|
2019-01-06 18:41:29 +00:00
|
|
|
object = event[:object].constantize.find_by(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-07-10 22:47:21 +00:00
|
|
|
store[:created_at] = event[:created_at]
|
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]
|
2020-11-05 16:31:00 +00:00
|
|
|
if store[:changes]
|
2017-10-01 12:25:52 +00:00
|
|
|
event[:changes].each do |key, value|
|
2020-11-05 16:31:00 +00:00
|
|
|
if store[:changes][key]
|
2016-04-22 06:55:10 +00:00
|
|
|
store[:changes][key][1] = value[1]
|
2020-11-05 16:31:00 +00:00
|
|
|
else
|
|
|
|
store[:changes][key] = value
|
2016-04-22 06:55:10 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2020-11-05 16:31:00 +00:00
|
|
|
else
|
|
|
|
store[:changes] = event[:changes]
|
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
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-04-15 21:56:10 +00:00
|
|
|
list_objects
|
|
|
|
end
|
|
|
|
|
2021-05-20 06:59:02 +00:00
|
|
|
# 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
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if Setting.get('import_mode')
|
2016-04-15 21:56:10 +00:00
|
|
|
|
|
|
|
e = {
|
2018-12-19 17:31:51 +00:00
|
|
|
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)
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2016-04-15 21:56:10 +00:00
|
|
|
end
|
|
|
|
|
2021-05-20 06:59:02 +00:00
|
|
|
# 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
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if Setting.get('import_mode')
|
2016-04-15 21:56:10 +00:00
|
|
|
|
|
|
|
# ignore certain attributes
|
|
|
|
real_changes = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
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'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-04-15 21:56:10 +00:00
|
|
|
real_changes[key] = value
|
2017-10-01 12:25:52 +00:00
|
|
|
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-04-14 07:17:13 +00:00
|
|
|
|
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 = {
|
2018-12-19 17:31:51 +00:00
|
|
|
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)
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2016-04-14 07:17:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|