2014-08-26 00:18:31 +00:00
|
|
|
|
class Observer::Ticket::Notification::BackgroundJob
|
2014-08-25 23:58:04 +00:00
|
|
|
|
def initialize(params)
|
|
|
|
|
@ticket_id = params[:ticket_id]
|
|
|
|
|
@article_id = params[:article_id]
|
2014-08-26 08:18:59 +00:00
|
|
|
|
@type = params[:type]
|
2014-08-25 23:58:04 +00:00
|
|
|
|
@data = params[:data]
|
|
|
|
|
end
|
|
|
|
|
def perform
|
|
|
|
|
ticket = Ticket.find(@ticket_id)
|
|
|
|
|
article = Ticket::Article.find(@article_id)
|
|
|
|
|
data = @data
|
|
|
|
|
|
|
|
|
|
# find recipients
|
|
|
|
|
recipients = []
|
|
|
|
|
|
|
|
|
|
# group of agents to work on
|
|
|
|
|
if data[:recipient] == 'group'
|
|
|
|
|
recipients = ticket.agent_of_group()
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# owner
|
2014-08-25 23:58:04 +00:00
|
|
|
|
elsif data[:recipient] == 'owner'
|
|
|
|
|
if ticket.owner_id != 1
|
|
|
|
|
recipients.push ticket.owner
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# customer
|
2014-08-25 23:58:04 +00:00
|
|
|
|
elsif data[:recipient] == 'customer'
|
|
|
|
|
if ticket.customer_id != 1
|
|
|
|
|
# temporarily disabled
|
|
|
|
|
# recipients.push ticket.customer
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# owner or group of agents to work on
|
2014-08-25 23:58:04 +00:00
|
|
|
|
elsif data[:recipient] == 'to_work_on'
|
|
|
|
|
if ticket.owner_id != 1
|
|
|
|
|
recipients.push ticket.owner
|
|
|
|
|
else
|
|
|
|
|
recipients = ticket.agent_of_group()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# send notifications
|
2015-01-02 15:53:59 +00:00
|
|
|
|
recipient_list = ''
|
2014-08-25 23:58:04 +00:00
|
|
|
|
notification_subject = ''
|
|
|
|
|
recipients.each do |user|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
|
2015-01-02 15:53:59 +00:00
|
|
|
|
next if ticket.updated_by_id == user.id
|
|
|
|
|
next if !user.active
|
2015-01-02 15:50:31 +00:00
|
|
|
|
|
|
|
|
|
# create desktop notification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# create online notification
|
2014-08-26 07:59:59 +00:00
|
|
|
|
OnlineNotification.add(
|
2014-08-26 08:18:59 +00:00
|
|
|
|
:type => @type,
|
2014-08-26 07:59:59 +00:00
|
|
|
|
:object => 'Ticket',
|
|
|
|
|
:o_id => ticket.id,
|
|
|
|
|
:seen => false,
|
2014-08-26 08:18:59 +00:00
|
|
|
|
:created_by_id => article.created_by_id || 1,
|
2014-08-26 07:59:59 +00:00
|
|
|
|
:user_id => user.id,
|
|
|
|
|
)
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# create email notification
|
2014-08-25 23:58:04 +00:00
|
|
|
|
next if !user.email || user.email == ''
|
|
|
|
|
|
|
|
|
|
# add recipient_list
|
|
|
|
|
if recipient_list != ''
|
|
|
|
|
recipient_list += ','
|
|
|
|
|
end
|
|
|
|
|
recipient_list += user.email.to_s
|
|
|
|
|
|
|
|
|
|
# prepare subject & body
|
|
|
|
|
notification = {}
|
|
|
|
|
[:subject, :body].each { |key|
|
|
|
|
|
notification[key.to_sym] = NotificationFactory.build(
|
|
|
|
|
:locale => user.locale,
|
|
|
|
|
:string => data[key.to_sym],
|
|
|
|
|
:objects => {
|
|
|
|
|
:ticket => ticket,
|
|
|
|
|
:article => article,
|
|
|
|
|
:recipient => user,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
notification_subject = notification[:subject]
|
|
|
|
|
|
|
|
|
|
# rebuild subject
|
|
|
|
|
notification[:subject] = ticket.subject_build( notification[:subject] )
|
|
|
|
|
|
|
|
|
|
# send notification
|
|
|
|
|
NotificationFactory.send(
|
|
|
|
|
:recipient => user,
|
|
|
|
|
:subject => notification[:subject],
|
|
|
|
|
:body => notification[:body]
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# add history record
|
|
|
|
|
if recipient_list != ''
|
2015-01-02 15:50:31 +00:00
|
|
|
|
puts "send... #{recipient_list} #{ticket.id}"
|
2014-08-25 23:58:04 +00:00
|
|
|
|
History.add(
|
|
|
|
|
:o_id => ticket.id,
|
|
|
|
|
:history_type => 'notification',
|
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
|
:value_from => notification_subject,
|
|
|
|
|
:value_to => recipient_list,
|
|
|
|
|
:created_by_id => article.created_by_id || 1
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|