resets_ticket_state.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Reopens the ticket in case certain new articles are created.
  3. module Ticket::Article::ResetsTicketState
  4. extend ActiveSupport::Concern
  5. included do
  6. after_create :ticket_article_reset_ticket_state
  7. end
  8. private
  9. def ticket_article_reset_ticket_state
  10. # return if we run import mode
  11. return true if Setting.get('import_mode')
  12. # only change state if not processed via postmaster
  13. return true if ApplicationHandleInfo.postmaster?
  14. # if article in internal
  15. return true if internal
  16. # if sender is agent
  17. return true if Ticket::Article::Sender.lookup(id: sender_id).name != 'Agent'
  18. # if article is a message to customer
  19. return true if !Ticket::Article::Type.lookup(id: type_id).communication
  20. # if current ticket state is still new
  21. ticket = Ticket.find_by(id: ticket_id)
  22. return true if !ticket
  23. new_state = Ticket::State.find_by(default_create: true)
  24. return true if ticket.state_id != new_state.id
  25. state = Ticket::State.find_by(default_follow_up: true)
  26. return true if !state
  27. # set ticket to open
  28. ticket.state_id = state.id
  29. # save ticket
  30. ticket.save
  31. end
  32. end