20170830000001_last_owner_update.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class LastOwnerUpdate < ActiveRecord::Migration
  2. def up
  3. # return if it's a new setup
  4. return if !Setting.find_by(name: 'system_init_done')
  5. # reset assignment_timeout to prevent unwanted things happen
  6. Group.all.each { |group|
  7. group.assignment_timeout = nil
  8. group.save!
  9. }
  10. add_column :tickets, :last_owner_update_at, :timestamp, limit: 3, null: true
  11. add_index :tickets, [:last_owner_update_at]
  12. Ticket.reset_column_information
  13. Scheduler.create_if_not_exists(
  14. name: 'Process auto unassign tickets',
  15. method: 'Ticket.process_auto_unassign',
  16. period: 10.minutes,
  17. prio: 1,
  18. active: true,
  19. )
  20. state_ids = Ticket::State.by_category(:work_on).pluck(:id)
  21. if state_ids.present?
  22. ticket_ids = Ticket.where('tickets.state_id IN (?) AND tickets.owner_id != 1', state_ids).order(created_at: :desc).limit(1000).pluck(:id)
  23. ticket_ids.each { |ticket_id|
  24. ticket = Ticket.find_by(id: ticket_id)
  25. next if !ticket
  26. ticket.last_owner_update_at = last_owner_update_at(ticket)
  27. ticket.save!
  28. }
  29. end
  30. end
  31. def last_owner_update_at(ticket)
  32. type = History::Type.lookup(name: 'updated')
  33. if type
  34. object = History::Object.lookup(name: 'Ticket')
  35. if object
  36. attribute = History::Attribute.lookup(name: 'owner')
  37. if attribute
  38. history = History.where(o_id: ticket.id, history_type_id: type.id, history_object_id: object.id, history_attribute_id: attribute.id).where.not(id_to: 1).order(created_at: :desc).limit(1)
  39. if history.present?
  40. return history.first.created_at
  41. end
  42. end
  43. end
  44. end
  45. return nil if ticket.owner_id == 1
  46. ticket.created_at
  47. end
  48. end