|
@@ -3,10 +3,10 @@ module Ticket::Overviews
|
|
|
|
|
|
=begin
|
|
|
|
|
|
-all overview by user
|
|
|
+all overviews by user
|
|
|
|
|
|
result = Ticket::Overviews.all(
|
|
|
- :current_user => User.find(123),
|
|
|
+ current_user: User.find(123),
|
|
|
)
|
|
|
|
|
|
returns
|
|
@@ -15,137 +15,105 @@ returns
|
|
|
|
|
|
=end
|
|
|
|
|
|
- def self.all (data)
|
|
|
+ def self.all(data)
|
|
|
|
|
|
# get customer overviews
|
|
|
if data[:current_user].role?('Customer')
|
|
|
- role = Role.find_by( name: 'Customer' )
|
|
|
+ role = Role.find_by(name: 'Customer')
|
|
|
overviews = if data[:current_user].organization_id && data[:current_user].organization.shared
|
|
|
- Overview.where( role_id: role.id, active: true )
|
|
|
+ Overview.where(role_id: role.id, active: true)
|
|
|
else
|
|
|
- Overview.where( role_id: role.id, organization_shared: false, active: true )
|
|
|
+ Overview.where(role_id: role.id, organization_shared: false, active: true)
|
|
|
end
|
|
|
return overviews
|
|
|
end
|
|
|
|
|
|
# get agent overviews
|
|
|
- return if !data[:current_user].role?( 'Agent' )
|
|
|
- role = Role.find_by( name: 'Agent' )
|
|
|
- Overview.where( role_id: role.id, active: true )
|
|
|
+ return if !data[:current_user].role?('Agent')
|
|
|
+ role = Role.find_by(name: 'Agent')
|
|
|
+ Overview.where(role_id: role.id, active: true)
|
|
|
end
|
|
|
|
|
|
=begin
|
|
|
|
|
|
-selected overview by user
|
|
|
-
|
|
|
- result = Ticket::Overviews.list(
|
|
|
- current_user: User.find(123),
|
|
|
- view: 'some_view_url',
|
|
|
- )
|
|
|
+ result = Ticket::Overviews.index(User.find(123))
|
|
|
|
|
|
returns
|
|
|
|
|
|
- result = {
|
|
|
- tickets: tickets, # [ticket1, ticket2, ticket3]
|
|
|
- tickets_count: tickets_count, # count of tickets
|
|
|
- overview: overview_selected_raw, # overview attributes
|
|
|
+ [
|
|
|
+ {
|
|
|
+ overview: {
|
|
|
+ id: 123,
|
|
|
+ updated_at: ...,
|
|
|
+ },
|
|
|
+ count: 3,
|
|
|
+ tickets: [
|
|
|
+ {
|
|
|
+ id: 1,
|
|
|
+ updated_at: ...,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ id: 2,
|
|
|
+ updated_at: ...,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ id: 3,
|
|
|
+ updated_at: ...,
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ ...
|
|
|
}
|
|
|
+ ]
|
|
|
|
|
|
=end
|
|
|
|
|
|
- def self.list (data)
|
|
|
-
|
|
|
- overviews = all(data)
|
|
|
- return if !overviews
|
|
|
-
|
|
|
- # build up attributes hash
|
|
|
- overview_selected = nil
|
|
|
- overview_selected_raw = nil
|
|
|
-
|
|
|
- overviews.each { |overview|
|
|
|
-
|
|
|
- # remember selected view
|
|
|
- if data[:view] && data[:view] == overview.link
|
|
|
- overview_selected = overview
|
|
|
- overview_selected_raw = Marshal.load( Marshal.dump(overview.attributes) )
|
|
|
- end
|
|
|
- }
|
|
|
-
|
|
|
- if data[:view] && !overview_selected
|
|
|
- raise "No such view '#{data[:view]}'"
|
|
|
- end
|
|
|
+ def self.index(user)
|
|
|
+ overviews = Ticket::Overviews.all(
|
|
|
+ current_user: user,
|
|
|
+ )
|
|
|
|
|
|
# get only tickets with permissions
|
|
|
- access_condition = Ticket.access_condition( data[:current_user] )
|
|
|
-
|
|
|
- # overview meta for navbar
|
|
|
- if !overview_selected
|
|
|
-
|
|
|
- # loop each overview
|
|
|
- result = []
|
|
|
- overviews.each { |overview|
|
|
|
+ access_condition = Ticket.access_condition(user)
|
|
|
|
|
|
- query_condition, bind_condition = Ticket.selector2sql(overview.condition, data[:current_user])
|
|
|
+ list = []
|
|
|
+ overviews.each {|overview|
|
|
|
+ query_condition, bind_condition = Ticket.selector2sql(overview.condition, user)
|
|
|
|
|
|
- # get count
|
|
|
- count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
|
|
|
-
|
|
|
- # get meta info
|
|
|
- all = {
|
|
|
- name: overview.name,
|
|
|
- prio: overview.prio,
|
|
|
- link: overview.link,
|
|
|
- }
|
|
|
-
|
|
|
- # push to result data
|
|
|
- result.push all.merge( { count: count } )
|
|
|
- }
|
|
|
- return result
|
|
|
- end
|
|
|
-
|
|
|
- # get result list
|
|
|
- if data[:array]
|
|
|
- order_by = overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s
|
|
|
- if overview_selected.group_by && !overview_selected.group_by.empty?
|
|
|
- order_by = overview_selected.group_by + '_id, ' + order_by
|
|
|
+ order_by = "#{overview.order[:by]} #{overview.order[:direction]}"
|
|
|
+ if overview.group_by && !overview.group_by.empty?
|
|
|
+ order_by = "#{overview.group_by}_id, #{order_by}"
|
|
|
end
|
|
|
|
|
|
- query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition, data[:current_user])
|
|
|
-
|
|
|
- tickets = Ticket.select('id')
|
|
|
- .where( access_condition )
|
|
|
- .where( query_condition, *bind_condition )
|
|
|
- .order( order_by )
|
|
|
- .limit( 500 )
|
|
|
-
|
|
|
- ticket_ids = []
|
|
|
- tickets.each { |ticket|
|
|
|
- ticket_ids.push ticket.id
|
|
|
+ ticket_result = Ticket.select('id, updated_at')
|
|
|
+ .where(access_condition)
|
|
|
+ .where(query_condition, *bind_condition)
|
|
|
+ .order(order_by)
|
|
|
+ .limit(500)
|
|
|
+
|
|
|
+ tickets = []
|
|
|
+ ticket_result.each { |ticket|
|
|
|
+ ticket_item = {
|
|
|
+ id: ticket.id,
|
|
|
+ updated_at: ticket.updated_at,
|
|
|
+ }
|
|
|
+ tickets.push ticket_item
|
|
|
}
|
|
|
-
|
|
|
- tickets_count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
|
|
|
-
|
|
|
- return {
|
|
|
- ticket_ids: ticket_ids,
|
|
|
- tickets_count: tickets_count,
|
|
|
- overview: overview_selected_raw,
|
|
|
+ count = Ticket.where(access_condition).where(query_condition, *bind_condition).count()
|
|
|
+ item = {
|
|
|
+ overview: {
|
|
|
+ id: overview.id,
|
|
|
+ view: overview.link,
|
|
|
+ updated_at: overview.updated_at,
|
|
|
+ },
|
|
|
+ tickets: tickets,
|
|
|
+ count: count,
|
|
|
}
|
|
|
- end
|
|
|
-
|
|
|
- # get tickets for overview
|
|
|
- data[:start_page] ||= 1
|
|
|
- query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition, data[:current_user])
|
|
|
- tickets = Ticket.where( access_condition )
|
|
|
- .where( query_condition, *bind_condition )
|
|
|
- .order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s )
|
|
|
-
|
|
|
- tickets_count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
|
|
|
|
|
|
- {
|
|
|
- tickets: tickets,
|
|
|
- tickets_count: tickets_count,
|
|
|
- overview: overview_selected_raw,
|
|
|
+ list.push item
|
|
|
}
|
|
|
+ list
|
|
|
end
|
|
|
|
|
|
end
|