Added asset support for jobs, overviews and slas.

This commit is contained in:
Martin Edenhofer 2016-03-20 20:09:52 +01:00
parent 96fbc9034e
commit 30e28e71f6
12 changed files with 490 additions and 6 deletions

View file

@ -378,6 +378,21 @@ class ApplicationController < ActionController::Base
def model_index_render (object, _params) def model_index_render (object, _params)
generic_objects = object.all generic_objects = object.all
if params[:full]
assets = {}
item_ids = []
generic_objects.each {|item|
item_ids.push item.id
assets = item.assets(assets)
}
render json: {
record_ids: item_ids,
assets: assets,
}, status: :ok
return
end
generic_objects_with_associations = [] generic_objects_with_associations = []
generic_objects.each {|item| generic_objects.each {|item|
generic_objects_with_associations.push item.attributes_with_associations generic_objects_with_associations.push item.attributes_with_associations

View file

@ -6,9 +6,8 @@ class CalendarsController < ApplicationController
def index def index
return if deny_if_not_role(Z_ROLENAME_ADMIN) return if deny_if_not_role(Z_ROLENAME_ADMIN)
assets = {}
# calendars # calendars
assets = {}
calendar_ids = [] calendar_ids = []
Calendar.all.order(:name, :created_at).each {|calendar| Calendar.all.order(:name, :created_at).each {|calendar|
calendar_ids.push calendar.id calendar_ids.push calendar.id

View file

@ -1131,6 +1131,48 @@ get assets of object list
assets assets
end end
=begin
get assets and record_ids of selector
model = Model.find(123)
assets = model.assets_of_selector('attribute_name_of_selector', assets)
=end
def assets_of_selector(selector, assets = {})
# get assets of condition
models = Models.all
send(selector).each {|item, content|
attribute = item.split(/\./)
next if !attribute[1]
attribute_class = attribute[0].to_classname.constantize
reflection = attribute[1].sub(/_id$/, '')
#reflection = reflection.to_sym
next if !models[attribute_class]
next if !models[attribute_class][:reflections]
next if !models[attribute_class][:reflections][reflection]
next if !models[attribute_class][:reflections][reflection].klass
attribute_ref_class = models[attribute_class][:reflections][reflection].klass
if content['value'].class == Array
content['value'].each {|item_id|
attribute_object = attribute_ref_class.find_by(id: item_id)
if attribute_object
assets = attribute_object.assets(assets)
end
}
else
attribute_object = attribute_ref_class.find_by(id: content['value'])
if attribute_object
assets = attribute_object.assets(assets)
end
end
}
assets
end
private private
def attachments_buffer def attachments_buffer

View file

@ -10,6 +10,8 @@ class Calendar < ApplicationModel
after_update :sync_default, :min_one_check after_update :sync_default, :min_one_check
after_destroy :min_one_check after_destroy :min_one_check
notify_clients_support
=begin =begin
set inital default calendar set inital default calendar

View file

@ -1,6 +1,9 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Job < ApplicationModel class Job < ApplicationModel
load 'job/assets.rb'
include Job::Assets
store :timeplan store :timeplan
store :condition store :condition
store :perform store :perform

47
app/models/job/assets.rb Normal file
View file

@ -0,0 +1,47 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Job
module Assets
=begin
get all assets / related models for this job
job = Job.find(123)
result = job.assets(assets_if_exists)
returns
result = {
:jobs => {
123 => job_model_123,
1234 => job_model_1234,
}
}
=end
def assets (data)
if !data[ Job.to_app_model ]
data[ Job.to_app_model ] = {}
end
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ Job.to_app_model ][ id ]
data[ Job.to_app_model ][ id ] = attributes_with_associations
data = assets_of_selector('condition', data)
data = assets_of_selector('perform', data)
end
%w(created_by_id updated_by_id).each {|local_user_id|
next if !self[ local_user_id ]
next if data[ User.to_app_model ][ self[ local_user_id ] ]
user = User.lookup(id: self[ local_user_id ])
next if !user
data = user.assets(data)
}
data
end
end
end

View file

@ -1,6 +1,9 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Overview < ApplicationModel class Overview < ApplicationModel
load 'overview/assets.rb'
include Overview::Assets
has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update
store :condition store :condition
store :order store :order

View file

@ -0,0 +1,56 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Overview
module Assets
=begin
get all assets / related models for this overview
overview = Overview.find(123)
result = overview.assets(assets_if_exists)
returns
result = {
:overviews => {
123 => overview_model_123,
1234 => overview_model_1234,
}
}
=end
def assets (data)
if !data[ Overview.to_app_model ]
data[ Overview.to_app_model ] = {}
end
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ Overview.to_app_model ][ id ]
data[ Overview.to_app_model ][ id ] = attributes_with_associations
if user_ids
user_ids.each {|local_user_id|
next if data[ User.to_app_model ][ local_user_id ]
user = User.lookup(id: local_user_id)
next if !user
data = user.assets(data)
}
end
data = assets_of_selector('condition', data)
end
%w(created_by_id updated_by_id).each {|local_user_id|
next if !self[ local_user_id ]
next if data[ User.to_app_model ][ self[ local_user_id ] ]
user = User.lookup(id: self[ local_user_id ])
next if !user
data = user.assets(data)
}
data
end
end
end

View file

@ -1,8 +1,14 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Sla < ApplicationModel class Sla < ApplicationModel
load 'sla/assets.rb'
include Sla::Assets
store :condition store :condition
store :data store :data
validates :name, presence: true validates :name, presence: true
belongs_to :calendar belongs_to :calendar
notify_clients_support
end end

52
app/models/sla/assets.rb Normal file
View file

@ -0,0 +1,52 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Sla
module Assets
=begin
get all assets / related models for this sla
sla = Sla.find(123)
result = sla.assets(assets_if_exists)
returns
result = {
:slas => {
123 => sla_model_123,
1234 => sla_model_1234,
}
}
=end
def assets (data)
if !data[ Sla.to_app_model ]
data[ Sla.to_app_model ] = {}
end
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ Sla.to_app_model ][ id ]
data[ Sla.to_app_model ][ id ] = attributes_with_associations
data = assets_of_selector('condition', data)
if calendar_id
calendar = Calendar.lookup(id: calendar_id)
if calendar
data = calendar.assets(data)
end
end
end
%w(created_by_id updated_by_id).each {|local_user_id|
next if !self[ local_user_id ]
next if data[ User.to_app_model ][ self[ local_user_id ] ]
user = User.lookup(id: self[ local_user_id ])
next if !user
data = user.assets(data)
}
data
end
end
end

View file

@ -278,4 +278,263 @@ class AssetsTest < ActiveSupport::TestCase
#puts "ERROR: difference \n1: #{o1.inspect}\n2: #{o2.inspect}\ndiff: #{(o1.to_a - o2.to_a).inspect}" #puts "ERROR: difference \n1: #{o1.inspect}\n2: #{o2.inspect}\ndiff: #{(o1.to_a - o2.to_a).inspect}"
false false
end end
test 'overview' do
UserInfo.current_user_id = 1
roles = Role.where(name: %w(Customer))
user1 = User.create_or_update(
login: 'assets_overview1@example.org',
firstname: 'assets_overview1',
lastname: 'assets_overview1',
email: 'assets_overview1@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user2 = User.create_or_update(
login: 'assets_overview2@example.org',
firstname: 'assets_overview2',
lastname: 'assets_overview2',
email: 'assets_overview2@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user3 = User.create_or_update(
login: 'assets_overview3@example.org',
firstname: 'assets_overview3',
lastname: 'assets_overview3',
email: 'assets_overview3@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user4 = User.create_or_update(
login: 'assets_overview4@example.org',
firstname: 'assets_overview4',
lastname: 'assets_overview4',
email: 'assets_overview4@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user5 = User.create_or_update(
login: 'assets_overview5@example.org',
firstname: 'assets_overview5',
lastname: 'assets_overview5',
email: 'assets_overview5@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
ticket_state1 = Ticket::State.find_by(name: 'new')
ticket_state2 = Ticket::State.find_by(name: 'open')
overview_role = Role.find_by(name: 'Agent')
overview = Overview.create_or_update(
name: 'my asset test',
link: 'my_asset_test',
prio: 1000,
role_id: overview_role.id,
user_ids: [ user4.id, user5.id ],
condition: {
'ticket.state_id' => {
operator: 'is',
value: [ ticket_state1.id, ticket_state2.id ],
},
'ticket.owner_id' => {
operator: 'is',
pre_condition: 'specific',
value: user1.id,
value_completion: 'John Smith <john.smith@example.com>'
},
},
order: {
by: 'created_at',
direction: 'ASC',
},
view: {
d: %w(title customer group created_at),
s: %w(title customer group created_at),
m: %w(number title customer group created_at),
view_mode_default: 's',
},
)
assets = overview.assets({})
assert(assets[:User][user1.id], 'check assets')
assert_not(assets[:User][user2.id], 'check assets')
assert_not(assets[:User][user3.id], 'check assets')
assert(assets[:User][user4.id], 'check assets')
assert(assets[:User][user5.id], 'check assets')
assert(assets[:TicketState][ticket_state1.id], 'check assets')
assert(assets[:TicketState][ticket_state2.id], 'check assets')
overview = Overview.create_or_update(
name: 'my asset test',
link: 'my_asset_test',
prio: 1000,
role_id: overview_role.id,
user_ids: [ user4.id ],
condition: {
'ticket.state_id' => {
operator: 'is',
value: ticket_state1.id,
},
'ticket.owner_id' => {
operator: 'is',
pre_condition: 'specific',
value: [user1.id, user2.id],
},
},
order: {
by: 'created_at',
direction: 'ASC',
},
view: {
d: %w(title customer group created_at),
s: %w(title customer group created_at),
m: %w(number title customer group created_at),
view_mode_default: 's',
},
)
assets = overview.assets({})
assert(assets[:User][user1.id], 'check assets')
assert(assets[:User][user2.id], 'check assets')
assert_not(assets[:User][user3.id], 'check assets')
assert(assets[:User][user4.id], 'check assets')
assert_not(assets[:User][user5.id], 'check assets')
assert(assets[:TicketState][ticket_state1.id], 'check assets')
assert_not(assets[:TicketState][ticket_state2.id], 'check assets')
end
test 'sla' do
UserInfo.current_user_id = 1
roles = Role.where(name: %w(Customer))
user1 = User.create_or_update(
login: 'assets_sla1@example.org',
firstname: 'assets_sla1',
lastname: 'assets_sla1',
email: 'assets_sla1@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user2 = User.create_or_update(
login: 'assets_sla2@example.org',
firstname: 'assets_sla2',
lastname: 'assets_sla2',
email: 'assets_sla2@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
calendar1 = Calendar.first
ticket_state1 = Ticket::State.find_by(name: 'new')
ticket_state2 = Ticket::State.find_by(name: 'open')
sla = Sla.create_or_update(
name: 'my asset test',
calendar_id: calendar1.id,
condition: {
'ticket.state_id' => {
operator: 'is',
value: [ ticket_state1.id, ticket_state2.id ],
},
'ticket.owner_id' => {
operator: 'is',
pre_condition: 'specific',
value: user1.id,
value_completion: 'John Smith <john.smith@example.com>'
},
},
)
assets = sla.assets({})
assert(assets[:User][user1.id], 'check assets')
assert_not(assets[:User][user2.id], 'check assets')
assert(assets[:TicketState][ticket_state1.id], 'check assets')
assert(assets[:TicketState][ticket_state2.id], 'check assets')
assert(assets[:Calendar][calendar1.id], 'check assets')
end
test 'job' do
UserInfo.current_user_id = 1
roles = Role.where(name: %w(Customer))
user1 = User.create_or_update(
login: 'assets_job1@example.org',
firstname: 'assets_job1',
lastname: 'assets_job1',
email: 'assets_job1@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user2 = User.create_or_update(
login: 'assets_job2@example.org',
firstname: 'assets_job2',
lastname: 'assets_job2',
email: 'assets_job2@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
user3 = User.create_or_update(
login: 'assets_job3@example.org',
firstname: 'assets_job3',
lastname: 'assets_job3',
email: 'assets_job3@example.org',
password: 'some_pass',
active: true,
roles: roles,
)
ticket_state1 = Ticket::State.find_by(name: 'new')
ticket_state2 = Ticket::State.find_by(name: 'open')
ticket_priority2 = Ticket::Priority.find_by(name: '2 normal')
job = Job.create_or_update(
name: 'my job',
timeplan: {
mon: true,
},
condition: {
'ticket.state_id' => {
operator: 'is',
value: [ ticket_state1.id, ticket_state2.id ],
},
'ticket.owner_id' => {
operator: 'is',
pre_condition: 'specific',
value: user1.id,
value_completion: 'John Smith <john.smith@example.com>'
},
},
perform: {
'ticket.priority_id' => {
value: ticket_priority2.id,
},
'ticket.owner_id' => {
pre_condition: 'specific',
value: user2.id,
value_completion: 'metest123@znuny.com <metest123@znuny.com>'
},
},
disable_notification: true,
)
assets = job.assets({})
assert(assets[:User][user1.id], 'check assets')
assert(assets[:User][user2.id], 'check assets')
assert_not(assets[:User][user3.id], 'check assets')
assert(assets[:TicketState][ticket_state1.id], 'check assets')
assert(assets[:TicketState][ticket_state2.id], 'check assets')
assert(assets[:TicketPriority][ticket_priority2.id], 'check assets')
end
end end