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
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 )
2020-08-20 07:10:08 +00:00
current_user = data [ :current_user ]
overview_filter = { }
overview_filter_not = { out_of_office : true }
return [ ] if ! current_user . permissions? ( 'ticket.customer' ) && ! current_user . permissions? ( 'ticket.agent' )
2013-08-17 20:04:57 +00:00
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' )
2013-08-17 20:04:57 +00:00
2020-08-20 07:10:08 +00:00
if data [ :links ] . present?
overview_filter [ :link ] = data [ :links ]
end
2018-10-09 06:17:41 +00:00
2020-08-20 07:10:08 +00:00
overview_filter [ :active ] = true
2017-09-05 09:49:32 +00:00
if User . where ( 'out_of_office = ? AND out_of_office_start_at <= ? AND out_of_office_end_at >= ? AND out_of_office_replacement_id = ? AND active = ?' , true , Time . zone . today , Time . zone . today , current_user . id , true ) . count . positive?
overview_filter_not = { }
end
2020-08-20 07:10:08 +00:00
if ! current_user . organization_id || ! current_user . organization . shared
overview_filter [ :organization_shared ] = false
2019-11-28 06:59:22 +00:00
end
2020-08-20 07:10:08 +00:00
2018-04-27 11:40:55 +00:00
Overview . joins ( :roles ) . left_joins ( :users ) . where ( overviews_roles : { role_id : role_ids } , overviews_users : { user_id : nil } , overviews : overview_filter ) . or ( Overview . joins ( :roles ) . left_joins ( :users ) . where ( overviews_roles : { role_id : role_ids } , overviews_users : { user_id : current_user . id } , overviews : overview_filter ) ) . where . not ( overview_filter_not ) . distinct ( 'overview.id' ) . 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
2015-04-27 23:19:26 +00:00
# get only tickets with permissions
2021-03-30 07:41:23 +00:00
access_condition = Ticket . access_condition ( user , 'overview' )
access_condition_read = Ticket . access_condition ( user , 'read' )
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 = [ ]
2017-10-01 12:25:52 +00:00
overviews . each 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
2018-02-28 06:42:02 +00:00
raise " Invalid order direction ' #{ direction } ' " if direction && direction !~ / ^(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-03-30 07:41:23 +00:00
overview_access_condition = access_condition
if overview . condition [ 'ticket.mention_user_ids' ] . present?
overview_access_condition = access_condition_read
end
2018-02-09 15:46:55 +00:00
ticket_result = Ticket . distinct
2021-03-30 07:41:23 +00:00
. where ( overview_access_condition )
2016-03-03 01:51:24 +00:00
. where ( query_condition , * bind_condition )
2016-10-28 12:04:08 +00:00
. joins ( tables )
2019-07-04 11:16:55 +00:00
. order ( Arel . sql ( " #{ order_by } #{ direction } " ) )
2018-02-28 06:42:02 +00:00
. limit ( 2000 )
2019-07-04 11:16:55 +00:00
. 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-03-30 07:41:23 +00:00
count = Ticket . distinct . where ( overview_access_condition ) . where ( query_condition , * bind_condition ) . joins ( tables ) . count ( )
2016-03-03 01:51:24 +00:00
item = {
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
}
2015-04-27 23:19:26 +00:00
2016-03-03 01:51:24 +00:00
list . push item
2017-10-01 12:25:52 +00:00
end
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