Implemented Ticket.process_pending.

This commit is contained in:
Thorsten Eckel 2015-05-21 16:40:04 +02:00
parent 36d090d7fa
commit bf86afb056
3 changed files with 97 additions and 0 deletions

View file

@ -111,6 +111,52 @@ returns
=begin =begin
processes tickets which have reached their pending time and sets next state_id
processed_tickets = Ticket.process_pending()
returns
processed_tickets = [<Ticket>, ...]
=end
def self.process_pending
ticket_states = Ticket::State.where(
state_type_id: Ticket::StateType.find_by( name: 'pending action' ),
)
.where.not(next_state_id: nil) # rubocop:disable Style/MultilineOperationIndentation
return [] if !ticket_states
next_state_map = {}
ticket_states.each { |state|
next_state_map[state.id] = state.next_state_id
}
tickets = where(
state_id: next_state_map.keys,
)
.where( 'pending_time <= ?', Time.zone.now ) # rubocop:disable Style/MultilineOperationIndentation
return [] if !tickets
result = []
tickets.each { |ticket|
ticket.state_id = next_state_map[ticket.state_id]
ticket.updated_at = Time.zone.now
ticket.updated_by_id = 1
ticket.save!
result.push ticket
}
result
end
=begin
merge tickets merge tickets
ticket = Ticket.find(123) ticket = Ticket.find(123)

View file

@ -0,0 +1,27 @@
require 'scheduler'
require 'ticket/state'
class ProcessPendingTickets < ActiveRecord::Migration
def up
# fix wrong next_state_id for state 'pending close'
pending_close_state = Ticket::State.find_by(
name: 'pending close',
)
closed_state = Ticket::State.find_by(
name: 'closed',
)
pending_close_state.next_state_id = closed_state.id
pending_close_state.save!
# add Ticket.process_pending
Scheduler.create_or_update(
name: 'Process pending tickets',
method: 'Ticket.process_pending',
period: 60 * 15,
prio: 1,
active: true,
updated_by_id: 1,
created_by_id: 1,
)
end
end

View file

@ -161,4 +161,28 @@ class TicketTest < ActiveSupport::TestCase
assert_equal( Ticket.latest_change.to_s, ticket2.updated_at.to_s ) assert_equal( Ticket.latest_change.to_s, ticket2.updated_at.to_s )
end end
test 'ticket process_pending' do
ticket = Ticket.create(
title: "pending close test",
group: Group.lookup( name: 'Users'),
customer_id: 2,
state: Ticket::State.lookup( name: 'pending close' ),
pending_time: Time.zone.now - 60,
priority: Ticket::Priority.lookup( name: '2 normal' ),
updated_by_id: 1,
created_by_id: 1,
)
lookup_ticket = Ticket.find_by( 'pending_time <= ?', Time.zone.now )
assert_equal( lookup_ticket.id, ticket.id, 'ticket.pending_time verify' )
Ticket.process_pending()
lookup_ticket = Ticket.find_by( 'pending_time <= ?', Time.zone.now )
assert_nil( lookup_ticket, 'ticket.pending_time processed verify' )
end
end end