diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 7bef119a1..3c29ae9a1 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -110,32 +110,13 @@ class TicketsController < ApplicationController # GET /api/tickets_customer def ticket_customer - # get closed/open states - ticket_state_list_open = Ticket::State.where( - :state_type_id => Ticket::StateType.where( :name => ['new','open', 'pending reminder', 'pending action'] ) - ) - ticket_state_list_closed = Ticket::State.where( - :state_type_id => Ticket::StateType.where( :name => ['closed'] ) - ) - - # get tickets - tickets_open = Ticket.where( - :customer_id => params[:customer_id], - :ticket_state_id => ticket_state_list_open - ).limit(15).order('created_at DESC') - - tickets_closed = Ticket.where( - :customer_id => params[:customer_id], - :ticket_state_id => ticket_state_list_closed - ).limit(15).order('created_at DESC') - # return result + result = Ticket.list_by_customer( + :customer_id => params[:customer_id], + :limit => 15, + ) render :json => { - :tickets => { - :open => tickets_open, - :closed => tickets_closed - } - # :users => users, + :tickets => result } end @@ -212,12 +193,11 @@ class TicketsController < ApplicationController # GET /api/ticket_merge_list/1 def ticket_merge_list - # get closed/open states - ticket_states = Ticket::State.where( - :state_type_id => Ticket::StateType.where( :name => ['new','open', 'pending reminder', 'pending action', 'closed'] ) + ticket = Ticket.find( params[:ticket_id] ) + ticket_list = Ticket.where( + :customer_id => ticket.customer_id, + :ticket_state_id => Ticket::State.by_category( 'open' ) ) - ticket = Ticket.find( params[:ticket_id] ) - ticket_list = Ticket.where( :customer_id => ticket.customer_id, :ticket_state_id => ticket_states ) .where( 'id != ?', [ ticket.id ] ) .order('created_at DESC') .limit(6) diff --git a/app/models/observer/ticket/user_ticket_counter.rb b/app/models/observer/ticket/user_ticket_counter.rb index 69495aca1..e3accb90d 100644 --- a/app/models/observer/ticket/user_ticket_counter.rb +++ b/app/models/observer/ticket/user_ticket_counter.rb @@ -14,25 +14,17 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer return if !record.customer_id # open ticket count - ticket_state_open_ids = Cache.get( 'ticket::state_ids::open' ) - if !ticket_state_open_ids - ticket_state_open_ids = self.state_ids( ['new','open', 'pending reminder', 'pending action'] ) - Cache.write( 'ticket::state_ids::open', ticket_state_open_ids, { :expires_in => 1.hour } ) - end - tickets_open = Ticket.where( + ticket_state_open = Ticket::State.by_category( 'open' ) + tickets_open = Ticket.where( :customer_id => record.customer_id, - :ticket_state_id => ticket_state_open_ids, + :ticket_state_id => ticket_state_open, ).count() # closed ticket count - ticket_state_closed_ids = Cache.get( 'ticket::state_ids::closed' ) - if !ticket_state_closed_ids - ticket_state_closed_ids = self.state_ids( ['closed'] ) - Cache.write( 'ticket::state_ids::closed', ticket_state_closed_ids, { :expires_in => 1.hour } ) - end - tickets_closed = Ticket.where( + ticket_state_closed = Ticket::State.by_category( 'closed' ) + tickets_closed = Ticket.where( :customer_id => record.customer_id, - :ticket_state_id => ticket_state_closed_ids, + :ticket_state_id => ticket_state_closed, ).count() # check if update is needed @@ -51,17 +43,5 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer end end - def state_ids(ticket_state_types) - ticket_state_types = Ticket::StateType.where( - :name => ticket_state_types, - ) - ticket_states = Ticket::State.where( :state_type_id => ticket_state_types ) - ticket_state_ids = [] - ticket_states.each {|ticket_state| - ticket_state_ids.push ticket_state.id - } - ticket_state_ids - end - end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 0ea4101be..a90522c27 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -520,11 +520,8 @@ class Ticket < ApplicationModel end def self.escalation_calculation_rebuild - ticket_state_list_open = Ticket::State.where( - :state_type_id => Ticket::StateType.where( - :name => ['new','open', 'pending reminder', 'pending action'] - ) - ) + ticket_state_list_open = Ticket::State.by_category( 'open' ) + tickets = Ticket.where( :ticket_state_id => ticket_state_list_open ) tickets.each {|ticket| ticket.escalation_calculation @@ -693,6 +690,47 @@ class Ticket < ApplicationModel self.save end +=begin + +list tickets by customer groupd in state categroie open and closed + + result = Ticket.list_by_customer( + :customer_id => 123, + :limit => 15, # optional, default 15 + ) + +returns + + result = { + :open => tickets_open, + :closed => tickets_closed, + } + +=end + + def self.list_by_customer(data) + + # get closed/open states + ticket_state_list_open = Ticket::State.by_category( 'open' ) + ticket_state_list_closed = Ticket::State.by_category( 'closed' ) + + # get tickets + tickets_open = Ticket.where( + :customer_id => data[:customer_id], + :ticket_state_id => ticket_state_list_open + ).limit( data[:limit] || 15 ).order('created_at DESC') + + tickets_closed = Ticket.where( + :customer_id => data[:customer_id], + :ticket_state_id => ticket_state_list_closed + ).limit( data[:limit] || 15 ).order('created_at DESC') + + return { + :open => tickets_open, + :closed => tickets_closed, + } + end + private def number_generate diff --git a/app/models/ticket/state.rb b/app/models/ticket/state.rb index 6ab1aef3d..734af04d3 100644 --- a/app/models/ticket/state.rb +++ b/app/models/ticket/state.rb @@ -3,4 +3,25 @@ class Ticket::State < ApplicationModel belongs_to :state_type, :class_name => 'Ticket::StateType' validates :name, :presence => true + +=begin + +list tickets by customer + + states = Ticket::State.by_category('open') # open|closed + +=end + + def self.by_category(category) + if category == 'open' + return Ticket::State.where( + :state_type_id => Ticket::StateType.where( :name => ['new', 'open', 'pending reminder', 'pending action'] ) + ) + elsif category == 'closed' + return Ticket::State.where( + :state_type_id => Ticket::StateType.where( :name => ['closed'] ) + ) + end + raise "Unknown category '#{category}'" + end end