notification.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. require 'event_buffer'
  3. require 'notification_factory'
  4. class Observer::Ticket::Notification < ActiveRecord::Observer
  5. observe :ticket, 'ticket::_article'
  6. def self.transaction
  7. # return if we run import mode
  8. return if Setting.get('import_mode')
  9. # get buffer
  10. list = EventBuffer.list
  11. # reset buffer
  12. EventBuffer.reset
  13. list.each { |event|
  14. # get current state of objects
  15. if event[:name] == 'Ticket::Article'
  16. article = Ticket::Article.lookup( :id => event[:id] )
  17. # next if article is already deleted
  18. next if !article
  19. ticket = article.ticket
  20. elsif event[:name] == 'Ticket'
  21. ticket = Ticket.lookup( :id => event[:id] )
  22. # next if ticket is already deleted
  23. next if !ticket
  24. article = ticket.articles[-1]
  25. next if !article
  26. else
  27. raise "unknown object for notification #{event[:name]}"
  28. end
  29. # send new ticket notification to agents
  30. if event[:name] == 'Ticket' && event[:type] == 'create'
  31. puts 'send new ticket notify to agent'
  32. send_notify(
  33. {
  34. :event => event,
  35. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  36. :subject => 'New Ticket (#{ticket.title})',
  37. :body => 'Hi #{recipient.firstname},
  38. a new Ticket (#{ticket.title}) via i18n(#{article.type.name}).
  39. Group: #{ticket.group.name}
  40. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  41. State: i18n(#{ticket.state.name})
  42. From: #{article.from}
  43. <snip>
  44. #{article.body}
  45. </snip>
  46. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  47. '
  48. },
  49. ticket,
  50. article,
  51. 'new ticket'
  52. )
  53. end
  54. # send new ticket notification to customers
  55. if event[:name] == 'Ticket' && event[:type] == 'create'
  56. # only for incoming emails
  57. next if article.type.name != 'email'
  58. puts 'send new ticket notify to customer'
  59. send_notify(
  60. {
  61. :event => event,
  62. :recipient => 'customer', # group|owner|to_work_on|customer
  63. :subject => 'New Ticket has been created! (#{ticket.title})',
  64. :body => 'Thanks for your email. A new ticket has been created.
  65. You wrote:
  66. <snip>
  67. #{article.body}
  68. </snip>
  69. Your email will be answered by a human ASAP
  70. Have fun with Zammad! :-)
  71. Your Zammad Team
  72. '
  73. },
  74. ticket,
  75. article,
  76. 'new ticket'
  77. )
  78. end
  79. # send follow up notification
  80. if event[:name] == 'Ticket::Article' && event[:type] == 'create'
  81. # only send article notifications after init article is created (handled by ticket create event)
  82. next if ticket.articles.count.to_i <= 1
  83. puts 'send new ticket::article notify'
  84. if article.sender.name == 'Customer'
  85. send_notify(
  86. {
  87. :event => event,
  88. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  89. :subject => 'Follow Up (#{ticket.title})',
  90. :body => 'Hi #{recipient.firstname},
  91. a follow Up (#{ticket.title}) via i18n(#{article.type.name}).
  92. Group: #{ticket.group.name}
  93. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  94. State: i18n(#{ticket.state.name})
  95. From: #{article.from}
  96. <snip>
  97. #{article.body}
  98. </snip>
  99. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  100. '
  101. },
  102. ticket,
  103. article,
  104. 'follow up'
  105. )
  106. end
  107. # send new note notification to owner
  108. # if agent == created.id
  109. if article.sender.name == 'Agent' && article.created_by_id != article.ticket.owner_id
  110. send_notify(
  111. {
  112. :event => event,
  113. :recipient => 'owner', # group|owner|to_work_on
  114. :subject => 'Updated (#{ticket.title})',
  115. :body => 'Hi #{recipient.firstname},
  116. updated (#{ticket.title}) via i18n(#{article.type.name}).
  117. Group: #{ticket.group.name}
  118. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  119. State: i18n(#{ticket.state.name})
  120. From: #{article.from}
  121. <snip>
  122. #{article.body}
  123. </snip>
  124. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  125. '
  126. },
  127. ticket,
  128. article,
  129. 'follow up',
  130. )
  131. end
  132. end
  133. }
  134. end
  135. def self.send_notify(data, ticket, article, type)
  136. # send background job
  137. params = {
  138. :ticket_id => ticket.id,
  139. :article_id => article.id,
  140. :type => type,
  141. :data => data,
  142. }
  143. Delayed::Job.enqueue( Observer::Ticket::Notification::BackgroundJob.new( params ) )
  144. end
  145. def after_create(record)
  146. # return if we run import mode
  147. return if Setting.get('import_mode')
  148. # puts 'CREATED!!!!'
  149. # puts record.inspect
  150. e = {
  151. :name => record.class.name,
  152. :type => 'create',
  153. :data => record,
  154. :id => record.id,
  155. }
  156. EventBuffer.add(e)
  157. end
  158. def before_update(record)
  159. # return if we run import mode
  160. return if Setting.get('import_mode')
  161. #puts 'before_update'
  162. current = record.class.find(record.id)
  163. # do not send anything if nothing has changed
  164. return if current.attributes == record.attributes
  165. # puts 'UPDATE!!!!!!!!'
  166. # puts 'current'
  167. # puts current.inspect
  168. # puts 'record'
  169. # puts record.inspect
  170. e = {
  171. :name => record.class.name,
  172. :type => 'update',
  173. :data => record,
  174. :id => record.id,
  175. }
  176. EventBuffer.add(e)
  177. end
  178. def after_update(record)
  179. # return if we run import mode
  180. return if Setting.get('import_mode')
  181. # puts 'after_update'
  182. # puts record.inspect
  183. # puts '-----'
  184. # puts @a.inspect
  185. # AuditTrail.new(record, "UPDATED")
  186. end
  187. end