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
2017-08-22 12:55:36 +00:00
result = Ticket :: Overviews . all ( 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' )
2017-09-05 09:49:32 +00:00
overview_filter = { active : true , organization_shared : false }
if current_user . organization_id && current_user . organization . shared
overview_filter . delete ( :organization_shared )
end
overviews = Overview . joins ( :roles ) . where ( overviews_roles : { role_id : role_ids } , overviews : overview_filter ) . distinct ( 'overview.id' ) . order ( :prio )
2016-03-14 15:05:52 +00:00
overviews_list = [ ]
2017-10-01 12:25:52 +00:00
overviews . each do | overview |
2016-03-14 15:05:52 +00:00
user_ids = overview . user_ids
2017-11-23 08:09:44 +00:00
next if user_ids . present? && ! user_ids . include? ( current_user . id )
2016-03-14 15:05:52 +00:00
overviews_list . push overview
2017-10-01 12:25:52 +00:00
end
2016-03-14 15:05:52 +00:00
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-09-05 09:49:32 +00:00
overview_filter = { active : true }
overview_filter_not = { out_of_office : true }
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
overviews = Overview . joins ( :roles ) . where ( overviews_roles : { role_id : role_ids } , overviews : overview_filter ) . where . not ( overview_filter_not ) . distinct ( 'overview.id' ) . order ( :prio )
2016-03-14 15:05:52 +00:00
overviews_list = [ ]
2017-10-01 12:25:52 +00:00
overviews . each do | overview |
2016-03-14 15:05:52 +00:00
user_ids = overview . user_ids
2017-10-16 13:45:04 +00:00
next if user_ids . present? && ! user_ids . include? ( current_user . id )
2016-03-14 15:05:52 +00:00
overviews_list . push overview
2017-10-01 12:25:52 +00:00
end
2016-03-14 15:05:52 +00:00
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
2017-06-16 20:43:09 +00:00
access_condition = Ticket . access_condition ( user , 'overview' )
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 |
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
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
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 = [ ]
2017-10-01 12:25:52 +00:00
ticket_result . each do | ticket |
2016-03-03 01:51:24 +00:00
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
2017-10-01 12:25:52 +00:00
end
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
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