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