2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Overviews
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
all overviews by user
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2013-08-19 10:21:31 +00:00
|
|
|
result = Ticket::Overviews.all(
|
2016-03-03 01:51:24 +00:00
|
|
|
current_user: User.find(123),
|
2013-08-17 20:04:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [overview1, overview2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
def self.all(data)
|
2016-12-21 00:00:25 +00:00
|
|
|
current_user = data[:current_user]
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get customer overviews
|
2017-04-19 19:54:04 +00:00
|
|
|
role_ids = User.joins(:roles).where(users: { id: current_user.id, active: true }, roles: { active: true }).pluck('roles.id')
|
2016-12-21 00:00:25 +00:00
|
|
|
if current_user.permissions?('ticket.customer')
|
|
|
|
overviews = if current_user.organization_id && current_user.organization.shared
|
2017-04-19 19:54:04 +00:00
|
|
|
Overview.joins(:roles).where(overviews_roles: { role_id: role_ids }, overviews: { active: true }).distinct('overview.id').order(:prio)
|
2016-01-15 17:22:57 +00:00
|
|
|
else
|
2017-04-19 19:54:04 +00:00
|
|
|
Overview.joins(:roles).where(overviews_roles: { role_id: role_ids }, overviews: { active: true, organization_shared: false }).distinct('overview.id').order(:prio)
|
2016-01-15 17:22:57 +00:00
|
|
|
end
|
2016-03-14 15:05:52 +00:00
|
|
|
overviews_list = []
|
2016-06-30 20:04:48 +00:00
|
|
|
overviews.each { |overview|
|
2016-03-14 15:05:52 +00:00
|
|
|
user_ids = overview.user_ids
|
2016-12-21 00:00:25 +00:00
|
|
|
next if !user_ids.empty? && !user_ids.include?(current_user.id)
|
2016-03-14 15:05:52 +00:00
|
|
|
overviews_list.push overview
|
|
|
|
}
|
|
|
|
return overviews_list
|
2015-04-27 21:27:51 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get agent overviews
|
2016-12-21 00:00:25 +00:00
|
|
|
return [] if !current_user.permissions?('ticket.agent')
|
2017-04-19 19:54:04 +00:00
|
|
|
overviews = Overview.joins(:roles).where(overviews_roles: { role_id: role_ids }, overviews: { active: true }).distinct('overview.id').order(:prio)
|
2016-03-14 15:05:52 +00:00
|
|
|
overviews_list = []
|
2016-06-30 20:04:48 +00:00
|
|
|
overviews.each { |overview|
|
2016-03-14 15:05:52 +00:00
|
|
|
user_ids = overview.user_ids
|
2016-12-21 00:00:25 +00:00
|
|
|
next if !user_ids.empty? && !user_ids.include?(current_user.id)
|
2016-03-14 15:05:52 +00:00
|
|
|
overviews_list.push overview
|
|
|
|
}
|
|
|
|
overviews_list
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 20:04:57 +00:00
|
|
|
=begin
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
result = Ticket::Overviews.index(User.find(123))
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
overview: {
|
|
|
|
id: 123,
|
|
|
|
updated_at: ...,
|
|
|
|
},
|
|
|
|
count: 3,
|
|
|
|
tickets: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
updated_at: ...,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
updated_at: ...,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 3,
|
|
|
|
updated_at: ...,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
2016-03-03 01:51:24 +00:00
|
|
|
]
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
def self.index(user)
|
|
|
|
overviews = Ticket::Overviews.all(
|
|
|
|
current_user: user,
|
|
|
|
)
|
2016-12-21 00:00:25 +00:00
|
|
|
return [] if overviews.blank?
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get only tickets with permissions
|
2016-03-03 01:51:24 +00:00
|
|
|
access_condition = Ticket.access_condition(user)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2016-11-21 14:59:31 +00:00
|
|
|
ticket_attributes = Ticket.new.attributes
|
2016-03-03 01:51:24 +00:00
|
|
|
list = []
|
2016-06-30 20:04:48 +00:00
|
|
|
overviews.each { |overview|
|
2016-10-28 12:04:08 +00:00
|
|
|
query_condition, bind_condition, tables = Ticket.selector2sql(overview.condition, user)
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2016-11-21 14:59:31 +00:00
|
|
|
# validate direction
|
|
|
|
raise "Invalid order direction '#{overview.order[:direction]}'" if overview.order[:direction] && overview.order[:direction] !~ /^(ASC|DESC)$/i
|
|
|
|
|
|
|
|
# check if order by exists
|
|
|
|
order_by = overview.order[:by]
|
|
|
|
if !ticket_attributes.key?(order_by)
|
|
|
|
order_by = if ticket_attributes.key?("#{order_by}_id")
|
|
|
|
"#{order_by}_id"
|
|
|
|
else
|
|
|
|
'created_at'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
order_by = "tickets.#{order_by} #{overview.order[:direction]}"
|
|
|
|
|
|
|
|
# check if group by exists
|
2016-03-03 01:51:24 +00:00
|
|
|
if overview.group_by && !overview.group_by.empty?
|
2016-11-21 14:59:31 +00:00
|
|
|
group_by = overview.group_by
|
|
|
|
if !ticket_attributes.key?(group_by)
|
|
|
|
group_by = if ticket_attributes.key?("#{group_by}_id")
|
|
|
|
"#{group_by}_id"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if group_by
|
|
|
|
order_by = "tickets.#{group_by}, #{order_by}"
|
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
ticket_result = Ticket.select('id, updated_at')
|
|
|
|
.where(access_condition)
|
|
|
|
.where(query_condition, *bind_condition)
|
2016-10-28 12:04:08 +00:00
|
|
|
.joins(tables)
|
2016-03-03 01:51:24 +00:00
|
|
|
.order(order_by)
|
|
|
|
.limit(500)
|
2016-08-31 07:10:25 +00:00
|
|
|
.pluck(:id, :updated_at)
|
2016-03-03 01:51:24 +00:00
|
|
|
|
|
|
|
tickets = []
|
|
|
|
ticket_result.each { |ticket|
|
|
|
|
ticket_item = {
|
2016-08-31 07:10:25 +00:00
|
|
|
id: ticket[0],
|
|
|
|
updated_at: ticket[1],
|
2016-03-03 01:51:24 +00:00
|
|
|
}
|
|
|
|
tickets.push ticket_item
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
2016-11-21 14:59:31 +00:00
|
|
|
count = Ticket.where(access_condition).where(query_condition, *bind_condition).joins(tables).count()
|
2016-03-03 01:51:24 +00:00
|
|
|
item = {
|
|
|
|
overview: {
|
2016-11-21 14:59:31 +00:00
|
|
|
name: overview.name,
|
2016-03-03 01:51:24 +00:00
|
|
|
id: overview.id,
|
|
|
|
view: overview.link,
|
|
|
|
updated_at: overview.updated_at,
|
|
|
|
},
|
|
|
|
tickets: tickets,
|
|
|
|
count: count,
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
list.push item
|
2015-04-27 23:19:26 +00:00
|
|
|
}
|
2016-03-03 01:51:24 +00:00
|
|
|
list
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|