background_job.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. class Observer::Ticket::Notification::BackgroundJob
  2. def initialize(params)
  3. @ticket_id = params[:ticket_id]
  4. @article_id = params[:article_id]
  5. @data = params[:data]
  6. end
  7. def perform
  8. ticket = Ticket.find(@ticket_id)
  9. article = Ticket::Article.find(@article_id)
  10. data = @data
  11. # find recipients
  12. recipients = []
  13. # group of agents to work on
  14. if data[:recipient] == 'group'
  15. recipients = ticket.agent_of_group()
  16. # owner
  17. elsif data[:recipient] == 'owner'
  18. if ticket.owner_id != 1
  19. recipients.push ticket.owner
  20. end
  21. # customer
  22. elsif data[:recipient] == 'customer'
  23. if ticket.customer_id != 1
  24. # temporarily disabled
  25. # recipients.push ticket.customer
  26. end
  27. # owner or group of agents to work on
  28. elsif data[:recipient] == 'to_work_on'
  29. if ticket.owner_id != 1
  30. recipients.push ticket.owner
  31. else
  32. recipients = ticket.agent_of_group()
  33. end
  34. end
  35. # send notifications
  36. recipient_list = ''
  37. notification_subject = ''
  38. recipients.each do |user|
  39. next if !user.email || user.email == ''
  40. # add recipient_list
  41. if recipient_list != ''
  42. recipient_list += ','
  43. end
  44. recipient_list += user.email.to_s
  45. # prepare subject & body
  46. notification = {}
  47. [:subject, :body].each { |key|
  48. notification[key.to_sym] = NotificationFactory.build(
  49. :locale => user.locale,
  50. :string => data[key.to_sym],
  51. :objects => {
  52. :ticket => ticket,
  53. :article => article,
  54. :recipient => user,
  55. }
  56. )
  57. }
  58. notification_subject = notification[:subject]
  59. # rebuild subject
  60. notification[:subject] = ticket.subject_build( notification[:subject] )
  61. # send notification
  62. NotificationFactory.send(
  63. :recipient => user,
  64. :subject => notification[:subject],
  65. :body => notification[:body]
  66. )
  67. end
  68. # add history record
  69. if recipient_list != ''
  70. History.add(
  71. :o_id => ticket.id,
  72. :history_type => 'notification',
  73. :history_object => 'Ticket',
  74. :value_from => notification_subject,
  75. :value_to => recipient_list,
  76. :created_by_id => article.created_by_id || 1
  77. )
  78. end
  79. end
  80. end