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::ScreenOptions
2013-08-17 21:10:11 +00:00
= begin
list attributes
result = Ticket :: ScreenOptions . attributes_to_change (
2015-06-30 22:26:24 +00:00
ticket_id : 123 ,
2013-08-17 21:10:11 +00:00
2015-06-30 22:26:24 +00:00
ticket : ticket_model ,
2017-06-16 20:43:09 +00:00
current_user : User . find ( 123 ) ,
2013-08-17 21:10:11 +00:00
)
2018-01-12 12:44:02 +00:00
or only with user
result = Ticket :: ScreenOptions . attributes_to_change (
current_user : User . find ( 123 ) ,
)
2013-08-17 21:10:11 +00:00
returns
result = {
2018-01-12 12:44:02 +00:00
:form_meta = > {
:filter = > {
:state_id = > [ 1 , 2 , 4 , 7 , 3 ] ,
:priority_id = > [ 2 , 1 , 3 ] ,
:type_id = > [ 10 , 5 ] ,
:group_id = > [ 12 ]
} ,
} ,
:dependencies = > {
:group_id = > {
" " = > {
: owner_id = > [ ]
} ,
12 = > {
: owner_id = > [ 4 , 5 , 6 , 7 ]
}
}
}
2013-08-17 21:10:11 +00:00
= end
2015-04-27 23:19:26 +00:00
def self . attributes_to_change ( params )
2017-06-16 20:43:09 +00:00
raise 'current_user param needed' if ! params [ :current_user ]
2018-01-12 12:44:02 +00:00
if params [ :ticket ] . blank? && params [ :ticket_id ] . present?
2015-11-17 14:04:36 +00:00
params [ :ticket ] = Ticket . find ( params [ :ticket_id ] )
2015-04-27 23:19:26 +00:00
end
2013-08-17 21:10:11 +00:00
2015-04-27 23:19:26 +00:00
filter = { }
assets = { }
2014-09-09 23:42:20 +00:00
2015-04-27 23:19:26 +00:00
# get ticket states
state_ids = [ ]
2018-01-12 12:44:02 +00:00
if params [ :ticket ] . present?
2015-04-27 23:19:26 +00:00
state_type = params [ :ticket ] . state . state_type
end
state_types = [ 'open' , 'closed' , 'pending action' , 'pending reminder' ]
2020-09-30 09:07:01 +00:00
if state_type && state_types . exclude? ( state_type . name )
2018-01-12 12:44:02 +00:00
state_ids . push params [ :ticket ] . state_id
2015-04-27 23:19:26 +00:00
end
2017-06-16 20:43:09 +00:00
state_types . each do | type |
2015-11-17 14:04:36 +00:00
state_type = Ticket :: StateType . find_by ( name : type )
2015-05-07 09:04:40 +00:00
next if ! state_type
2018-10-09 06:17:41 +00:00
2017-06-16 20:43:09 +00:00
state_type . states . each do | state |
2015-05-07 09:04:40 +00:00
assets = state . assets ( assets )
state_ids . push state . id
2017-06-16 20:43:09 +00:00
end
end
2015-04-27 23:19:26 +00:00
filter [ :state_id ] = state_ids
# get priorities
priority_ids = [ ]
2017-06-16 20:43:09 +00:00
Ticket :: Priority . where ( active : true ) . each do | priority |
2015-04-27 23:19:26 +00:00
assets = priority . assets ( assets )
priority_ids . push priority . id
2017-06-16 20:43:09 +00:00
end
2015-04-27 23:19:26 +00:00
filter [ :priority_id ] = priority_ids
type_ids = [ ]
if params [ :ticket ]
2017-11-23 08:09:44 +00:00
types = %w[ note phone ]
2015-04-27 23:19:26 +00:00
if params [ :ticket ] . group . email_address_id
types . push 'email'
2013-08-17 21:10:11 +00:00
end
2017-06-16 20:43:09 +00:00
types . each do | type_name |
2018-01-28 18:10:22 +00:00
type = Ticket :: Article :: Type . lookup ( name : type_name )
2017-06-16 20:43:09 +00:00
next if type . blank?
2018-10-09 06:17:41 +00:00
2017-06-16 20:43:09 +00:00
type_ids . push type . id
end
2015-04-27 23:19:26 +00:00
end
filter [ :type_id ] = type_ids
# get group / user relations
dependencies = { group_id : { '' = > { owner_id : [ ] } } }
2017-06-16 20:43:09 +00:00
filter [ :group_id ] = [ ]
groups = if params [ :current_user ] . permissions? ( 'ticket.agent' )
2020-11-13 14:10:24 +00:00
if params [ :view ] == 'ticket_create'
params [ :current_user ] . groups_access ( %w[ create ] )
else
params [ :current_user ] . groups_access ( %w[ create change ] )
end
2017-06-16 20:43:09 +00:00
else
Group . where ( active : true )
end
2018-01-12 12:44:02 +00:00
agents = { }
agent_role_ids = Role . with_permissions ( 'ticket.agent' ) . pluck ( :id )
2020-12-10 09:13:57 +00:00
agent_user_ids = User . joins ( :roles ) . where ( users : { active : true } ) . where ( 'roles_users.role_id' = > agent_role_ids ) . pluck ( :id )
2018-01-12 12:44:02 +00:00
groups . each do | group |
filter [ :group_id ] . push group . id
assets = group . assets ( assets )
dependencies [ :group_id ] [ group . id ] = { owner_id : [ ] }
group_agent_user_ids = User . joins ( ', groups_users' ) . where ( " users.id = groups_users.user_id AND groups_users.access = 'full' AND groups_users.group_id = ? AND users.id IN (?) " , group . id , agent_user_ids ) . pluck ( :id )
group_agent_roles_ids = Role . joins ( ', roles_groups' ) . where ( " roles.id = roles_groups.role_id AND roles_groups.access = 'full' AND roles_groups.group_id = ? AND roles.id IN (?) " , group . id , agent_role_ids ) . pluck ( :id )
group_agent_role_user_ids = User . joins ( :roles ) . where ( roles : { id : group_agent_roles_ids } ) . pluck ( :id )
2018-08-06 09:43:40 +00:00
User . where ( id : group_agent_user_ids . concat ( group_agent_role_user_ids ) . uniq , active : true ) . pluck ( :id ) . each do | user_id |
dependencies [ :group_id ] [ group . id ] [ :owner_id ] . push user_id
next if agents [ user_id ]
2018-10-09 06:17:41 +00:00
2018-08-06 09:43:40 +00:00
agents [ user_id ] = true
next if assets [ :User ] && assets [ :User ] [ user_id ]
2018-10-09 06:17:41 +00:00
2018-08-06 09:43:40 +00:00
user = User . lookup ( id : user_id )
next if ! user
2018-10-09 06:17:41 +00:00
2018-01-12 12:44:02 +00:00
assets = user . assets ( assets )
end
end
2017-06-16 20:43:09 +00:00
2020-08-20 07:10:08 +00:00
configure_attributes = nil
if params [ :ticket ] . present?
configure_attributes = ObjectManager :: Object . new ( 'Ticket' ) . attributes ( params [ :current_user ] , params [ :ticket ] )
2017-06-16 20:43:09 +00:00
end
2020-08-20 07:10:08 +00:00
2015-04-27 23:19:26 +00:00
{
2017-06-16 20:43:09 +00:00
assets : assets ,
2015-11-17 14:04:36 +00:00
form_meta : {
2020-08-20 07:10:08 +00:00
filter : filter ,
dependencies : dependencies ,
configure_attributes : configure_attributes ,
2015-11-17 14:04:36 +00:00
}
2015-04-27 23:19:26 +00:00
}
end
2013-08-17 21:10:11 +00:00
= begin
2019-07-31 08:23:48 +00:00
list tickets by customer group in state categories open and closed
2013-08-17 21:10:11 +00:00
result = Ticket :: ScreenOptions . list_by_customer (
2015-06-30 22:26:24 +00:00
customer_id : 123 ,
limit : 15 , # optional, default 15
2013-08-17 21:10:11 +00:00
)
returns
result = {
2015-06-30 22:26:24 +00:00
ticket_ids_open : tickets_open ,
ticket_ids_closed : tickets_closed ,
assets : { ... list of assets ... } ,
2013-08-17 21:10:11 +00:00
}
= end
2015-04-27 23:19:26 +00:00
def self . list_by_customer ( data )
# get closed/open states
2019-04-07 15:23:03 +00:00
state_id_list_open = Ticket :: State . by_category ( :open ) . pluck ( :id )
state_id_list_closed = Ticket :: State . by_category ( :closed ) . pluck ( :id )
2015-04-27 23:19:26 +00:00
2020-02-13 11:53:09 +00:00
# open tickets by customer
access_condition = Ticket . access_condition ( data [ :current_user ] , 'read' )
2015-04-27 23:19:26 +00:00
# get tickets
tickets_open = Ticket . where (
customer_id : data [ :customer_id ] ,
2019-04-07 15:23:03 +00:00
state_id : state_id_list_open
2020-02-13 11:53:09 +00:00
)
. where ( access_condition )
. limit ( data [ :limit ] || 15 ) . order ( created_at : :desc )
2015-04-27 23:19:26 +00:00
assets = { }
ticket_ids_open = [ ]
2017-10-01 12:25:52 +00:00
tickets_open . each do | ticket |
2015-04-27 23:19:26 +00:00
ticket_ids_open . push ticket . id
assets = ticket . assets ( assets )
2017-10-01 12:25:52 +00:00
end
2015-04-27 23:19:26 +00:00
tickets_closed = Ticket . where (
customer_id : data [ :customer_id ] ,
2019-04-07 15:23:03 +00:00
state_id : state_id_list_closed
2020-02-13 11:53:09 +00:00
)
. where ( access_condition )
. limit ( data [ :limit ] || 15 ) . order ( created_at : :desc )
2015-04-27 23:19:26 +00:00
ticket_ids_closed = [ ]
2017-10-01 12:25:52 +00:00
tickets_closed . each do | ticket |
2015-04-27 23:19:26 +00:00
ticket_ids_closed . push ticket . id
assets = ticket . assets ( assets )
2017-10-01 12:25:52 +00:00
end
2015-04-27 23:19:26 +00:00
{
2018-12-19 17:31:51 +00:00
ticket_ids_open : ticket_ids_open ,
2015-04-27 23:19:26 +00:00
ticket_ids_closed : ticket_ids_closed ,
2018-12-19 17:31:51 +00:00
assets : assets ,
2015-04-27 23:19:26 +00:00
}
2015-04-27 21:27:51 +00:00
end
2015-04-27 14:15:29 +00:00
end