background_job.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. OnlineNotification.add(
  40. :type => type,
  41. :object => 'Ticket',
  42. :o_id => ticket.id,
  43. :seen => false,
  44. :created_by_id => UserInfo.current_user_id || 1,
  45. :user_id => user.id,
  46. )
  47. next if !user.email || user.email == ''
  48. # add recipient_list
  49. if recipient_list != ''
  50. recipient_list += ','
  51. end
  52. recipient_list += user.email.to_s
  53. # prepare subject & body
  54. notification = {}
  55. [:subject, :body].each { |key|
  56. notification[key.to_sym] = NotificationFactory.build(
  57. :locale => user.locale,
  58. :string => data[key.to_sym],
  59. :objects => {
  60. :ticket => ticket,
  61. :article => article,
  62. :recipient => user,
  63. }
  64. )
  65. }
  66. notification_subject = notification[:subject]
  67. # rebuild subject
  68. notification[:subject] = ticket.subject_build( notification[:subject] )
  69. # send notification
  70. NotificationFactory.send(
  71. :recipient => user,
  72. :subject => notification[:subject],
  73. :body => notification[:body]
  74. )
  75. end
  76. # add history record
  77. if recipient_list != ''
  78. History.add(
  79. :o_id => ticket.id,
  80. :history_type => 'notification',
  81. :history_object => 'Ticket',
  82. :value_from => notification_subject,
  83. :value_to => recipient_list,
  84. :created_by_id => article.created_by_id || 1
  85. )
  86. end
  87. end
  88. end