Fixed ticket list in user and org profile.

This commit is contained in:
Martin Edenhofer 2015-09-23 16:25:06 +02:00
parent e260261bda
commit 2311d1c393
5 changed files with 45 additions and 19 deletions

View file

@ -389,11 +389,11 @@ class TicketsController < ApplicationController
if params[:user_id] if params[:user_id]
user = User.find( params[:user_id] ) user = User.find( params[:user_id] )
condition = { condition = {
'tickets.state_id' => { 'ticket.state_id' => {
operator: 'is', operator: 'is',
value: Ticket::State.by_category('open').map(&:id), value: Ticket::State.by_category('open').map(&:id),
}, },
'tickets.customer_id' => { 'ticket.customer_id' => {
operator: 'is', operator: 'is',
value: user.id, value: user.id,
}, },
@ -407,11 +407,11 @@ class TicketsController < ApplicationController
# lookup closed user tickets # lookup closed user tickets
condition = { condition = {
'tickets.state_id' => { 'ticket.state_id' => {
operator: 'is', operator: 'is',
value: Ticket::State.by_category('closed').map(&:id), value: Ticket::State.by_category('closed').map(&:id),
}, },
'tickets.customer_id' => { 'ticket.customer_id' => {
operator: 'is', operator: 'is',
value: user.id, value: user.id,
}, },
@ -463,11 +463,11 @@ class TicketsController < ApplicationController
if params[:organization_id] && !params[:organization_id].empty? if params[:organization_id] && !params[:organization_id].empty?
condition = { condition = {
'tickets.state_id' => { 'ticket.state_id' => {
operator: 'is', operator: 'is',
value: Ticket::State.by_category('open').map(&:id), value: Ticket::State.by_category('open').map(&:id),
}, },
'tickets.organization_id' => { 'ticket.organization_id' => {
operator: 'is', operator: 'is',
value: params[:organization_id], value: params[:organization_id],
}, },
@ -481,11 +481,11 @@ class TicketsController < ApplicationController
# lookup closed org tickets # lookup closed org tickets
condition = { condition = {
'tickets.state_id' => { 'ticket.state_id' => {
operator: 'is', operator: 'is',
value: Ticket::State.by_category('closed').map(&:id), value: Ticket::State.by_category('closed').map(&:id),
}, },
'tickets.organization_id' => { 'ticket.organization_id' => {
operator: 'is', operator: 'is',
value: params[:organization_id], value: params[:organization_id],
}, },

View file

@ -285,16 +285,41 @@ returns
false false
end end
=begin
get count of tickets and tickets which match on selector
ticket_count, tickets = Ticket.selectors(params[:condition], 6)
=end
def self.selectors(selectors, limit = 10) def self.selectors(selectors, limit = 10)
return if !selectors return if !selectors
query, bind_params, tables = _selectors(selectors) query, bind_params, tables = selector2sql(selectors)
return [] if !query return [] if !query
ticket_count = Ticket.where(query, *bind_params).joins(tables).count ticket_count = Ticket.where(query, *bind_params).joins(tables).count
tickets = Ticket.where(query, *bind_params).joins(tables).limit(limit) tickets = Ticket.where(query, *bind_params).joins(tables).limit(limit)
[ticket_count, tickets] [ticket_count, tickets]
end end
def self._selectors(selectors) =begin
generate condition query to search for tickets based on condition
query_condition, bind_condition = selector2sql(params[:condition])
condition example
{
'ticket.state_id' => {
operator: 'is',
value: [1,2,5]
}
}
=end
def self.selector2sql(selectors)
return if !selectors return if !selectors
query = '' query = ''
bind_params = [] bind_params = []
@ -308,13 +333,14 @@ returns
tables.push selector[0].to_sym tables.push selector[0].to_sym
} }
selectors.each {|attribute, selector| selectors.each {|attribute, selector_raw|
if query != '' if query != ''
query += ' AND ' query += ' AND '
end end
next if !selector selector = selector_raw.stringify_keys
next if !selector.respond_to?(:key?) fail "Invalid selector #{selector.inspect}" if !selector
next if !selector['operator'] fail "Invalid selector #{selector.inspect}" if !selector.respond_to?(:key?)
fail "Invalid selector, operator missing #{selector.inspect}" if !selector['operator']
return nil if !selector['value'] return nil if !selector['value']
return nil if selector['value'].respond_to?(:empty?) && selector['value'].empty? return nil if selector['value'].respond_to?(:empty?) && selector['value'].empty?
attributes = attribute.split(/\./) attributes = attribute.split(/\./)

View file

@ -206,7 +206,7 @@ returns
if !sla.condition || sla.condition.empty? if !sla.condition || sla.condition.empty?
sla_selected = sla sla_selected = sla
elsif sla.condition elsif sla.condition
query_condition, bind_condition = Ticket._selectors(sla.condition) query_condition, bind_condition = Ticket.selector2sql(sla.condition)
ticket = Ticket.where( query_condition, *bind_condition ).find_by(id: id) ticket = Ticket.where( query_condition, *bind_condition ).find_by(id: id)
next if !ticket next if !ticket
sla_selected = sla sla_selected = sla

View file

@ -111,7 +111,7 @@ returns
result = [] result = []
overviews.each { |overview| overviews.each { |overview|
query_condition, bind_condition = Ticket._selectors(overview.condition) query_condition, bind_condition = Ticket.selector2sql(overview.condition)
# get count # get count
count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count() count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
@ -136,7 +136,7 @@ returns
order_by = overview_selected.group_by + '_id, ' + order_by order_by = overview_selected.group_by + '_id, ' + order_by
end end
query_condition, bind_condition = Ticket._selectors(overview_selected.condition) query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition)
tickets = Ticket.select('id') tickets = Ticket.select('id')
.where( access_condition ) .where( access_condition )
@ -160,7 +160,7 @@ returns
# get tickets for overview # get tickets for overview
data[:start_page] ||= 1 data[:start_page] ||= 1
query_condition, bind_condition = Ticket._selectors(overview_selected.condition) query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition)
tickets = Ticket.where( access_condition ) tickets = Ticket.where( access_condition )
.where( query_condition, *bind_condition ) .where( query_condition, *bind_condition )
.order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s ) .order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s )

View file

@ -160,7 +160,7 @@ returns
.order('`tickets`.`created_at` DESC') .order('`tickets`.`created_at` DESC')
.limit(limit) .limit(limit)
else else
query_condition, bind_condition = _selectors(params[:condition]) query_condition, bind_condition = selector2sql(params[:condition])
tickets_all = Ticket.select('DISTINCT(tickets.id)') tickets_all = Ticket.select('DISTINCT(tickets.id)')
.where(access_condition) .where(access_condition)
.where(query_condition, *bind_condition) .where(query_condition, *bind_condition)