2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
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
|
|
|
|
2018-01-09 07:39:50 +00:00
|
|
|
result = Ticket::Overviews.all(current_user: User.find(3))
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2019-11-28 06:59:22 +00:00
|
|
|
certain overviews by user
|
|
|
|
|
|
|
|
result = Ticket::Overviews.all(current_user: User.find(3), links: ['all_unassigned', 'my_assigned'])
|
|
|
|
|
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)
|
2021-07-23 13:07:16 +00:00
|
|
|
Ticket::OverviewsPolicy::Scope.new(data[:current_user], Overview).resolve
|
|
|
|
.where({ link: data[:links] }.compact)
|
|
|
|
.distinct
|
|
|
|
.order(:prio, :name)
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 20:04:57 +00:00
|
|
|
=begin
|
|
|
|
|
2019-11-28 06:59:22 +00:00
|
|
|
index of all overviews by user
|
|
|
|
|
|
|
|
result = Ticket::Overviews.index(User.find(3))
|
|
|
|
|
|
|
|
index of certain overviews by user
|
|
|
|
|
|
|
|
result = Ticket::Overviews.index(User.find(3), ['all_unassigned', 'my_assigned'])
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
overview: {
|
|
|
|
id: 123,
|
2018-07-03 14:39:42 +00:00
|
|
|
name: 'some name',
|
|
|
|
view: 'some_view',
|
2016-03-03 01:51:24 +00:00
|
|
|
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
|
|
|
|
|
2019-11-28 06:59:22 +00:00
|
|
|
def self.index(user, links = nil)
|
2016-03-03 01:51:24 +00:00
|
|
|
overviews = Ticket::Overviews.all(
|
|
|
|
current_user: user,
|
2019-11-28 06:59:22 +00:00
|
|
|
links: links,
|
2016-03-03 01:51:24 +00:00
|
|
|
)
|
2016-12-21 00:00:25 +00:00
|
|
|
return [] if overviews.blank?
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2016-11-21 14:59:31 +00:00
|
|
|
ticket_attributes = Ticket.new.attributes
|
2021-07-21 14:04:17 +00:00
|
|
|
overviews.map do |overview|
|
2019-03-26 00:17:17 +00:00
|
|
|
query_condition, bind_condition, tables = Ticket.selector2sql(overview.condition, current_user: user)
|
2018-02-28 06:42:02 +00:00
|
|
|
direction = overview.order[:direction]
|
|
|
|
order_by = overview.order[:by]
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2016-11-21 14:59:31 +00:00
|
|
|
# validate direction
|
2021-05-12 11:37:44 +00:00
|
|
|
raise "Invalid order direction '#{direction}'" if direction && direction !~ %r{^(ASC|DESC)$}i
|
2016-11-21 14:59:31 +00:00
|
|
|
|
|
|
|
# check if order by exists
|
|
|
|
if !ticket_attributes.key?(order_by)
|
|
|
|
order_by = if ticket_attributes.key?("#{order_by}_id")
|
|
|
|
"#{order_by}_id"
|
|
|
|
else
|
|
|
|
'created_at'
|
|
|
|
end
|
|
|
|
end
|
2018-06-28 10:33:07 +00:00
|
|
|
order_by = "#{ActiveRecord::Base.connection.quote_table_name('tickets')}.#{ActiveRecord::Base.connection.quote_column_name(order_by)}"
|
2016-11-21 14:59:31 +00:00
|
|
|
|
|
|
|
# check if group by exists
|
2017-11-16 21:54:31 +00:00
|
|
|
if overview.group_by.present?
|
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
|
2018-06-28 10:33:07 +00:00
|
|
|
order_by = "#{ActiveRecord::Base.connection.quote_table_name('tickets')}.#{ActiveRecord::Base.connection.quote_column_name(group_by)}, #{order_by}"
|
2016-11-21 14:59:31 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2021-07-20 17:35:02 +00:00
|
|
|
scope = TicketPolicy::OverviewScope
|
2021-03-30 07:41:23 +00:00
|
|
|
if overview.condition['ticket.mention_user_ids'].present?
|
2021-07-20 17:35:02 +00:00
|
|
|
scope = TicketPolicy::ReadScope
|
2021-03-30 07:41:23 +00:00
|
|
|
end
|
2021-07-20 17:35:02 +00:00
|
|
|
ticket_result = scope.new(user).resolve
|
|
|
|
.distinct
|
|
|
|
.where(query_condition, *bind_condition)
|
|
|
|
.joins(tables)
|
|
|
|
.order(Arel.sql("#{order_by} #{direction}"))
|
|
|
|
.limit(2000)
|
|
|
|
.pluck(:id, :updated_at, Arel.sql(order_by))
|
2016-03-03 01:51:24 +00:00
|
|
|
|
2018-02-09 15:46:55 +00:00
|
|
|
tickets = ticket_result.map do |ticket|
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
id: ticket[0],
|
2018-02-28 06:42:02 +00:00
|
|
|
updated_at: ticket[1],
|
2016-03-03 01:51:24 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2018-02-09 15:46:55 +00:00
|
|
|
|
2021-07-20 17:35:02 +00:00
|
|
|
count = TicketPolicy::OverviewScope.new(user).resolve
|
|
|
|
.distinct
|
|
|
|
.where(query_condition, *bind_condition)
|
|
|
|
.joins(tables)
|
|
|
|
.count()
|
|
|
|
|
2021-07-21 14:04:17 +00:00
|
|
|
{
|
2016-03-03 01:51:24 +00:00
|
|
|
overview: {
|
2018-12-19 17:31:51 +00:00
|
|
|
name: overview.name,
|
|
|
|
id: overview.id,
|
|
|
|
view: overview.link,
|
2016-03-03 01:51:24 +00:00
|
|
|
updated_at: overview.updated_at,
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
tickets: tickets,
|
|
|
|
count: count,
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
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
|