reset_new_state.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Observer::Ticket::ResetNewState < ActiveRecord::Observer
  3. observe 'ticket::_article'
  4. def after_create(record)
  5. # return if we run import mode
  6. return true if Setting.get('import_mode')
  7. # only change state if not processed via postmaster
  8. return true if ApplicationHandleInfo.postmaster?
  9. # if article in internal
  10. return true if record.internal
  11. # if sender is agent
  12. return true if Ticket::Article::Sender.lookup(id: record.sender_id).name != 'Agent'
  13. # if article is a message to customer
  14. return true if !Ticket::Article::Type.lookup(id: record.type_id).communication
  15. # if current ticket state is still new
  16. ticket = Ticket.find_by(id: record.ticket_id)
  17. return true if !ticket
  18. new_state = Ticket::State.find_by(default_create: true)
  19. return true if ticket.state_id != new_state.id
  20. state = Ticket::State.find_by(default_follow_up: true)
  21. return true if !state
  22. # set ticket to open
  23. ticket.state_id = state.id
  24. # save ticket
  25. ticket.save
  26. end
  27. end