trabajo-afectivo/app/models/observer/ticket/article/communicate_email/background_job.rb

92 lines
2.4 KiB
Ruby
Raw Normal View History

2014-02-05 16:14:57 +00:00
class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
def initialize(id)
@article_id = id
end
2014-02-05 16:14:57 +00:00
def perform
record = Ticket::Article.find(@article_id)
2014-02-05 16:14:57 +00:00
# build subject
ticket = Ticket.lookup(id: record.ticket_id)
subject = ticket.subject_build(record.subject)
2014-02-05 16:14:57 +00:00
# send email
2015-08-28 00:53:14 +00:00
if !ticket.group.email_address_id
fail "Can't send email, no email address definde for group id '#{ticket.group.id}'"
elsif !ticket.group.email_address.channel_id
fail "Can't send email, no channel definde for email_address id '#{ticket.group.email_address_id}'"
end
# get references headers
references = []
if record.in_reply_to
references.push record.in_reply_to
end
# add all other article message_ids
Ticket::Article.where(ticket_id: record.ticket_id).each {|article|
if article.in_reply_to && !article.in_reply_to.empty?
references.push article.in_reply_to
end
next if !article.message_id
next if !article.message_id.empty?
next if article.id == @article_id
references.push article.message_id
}
2015-08-28 00:53:14 +00:00
channel = ticket.group.email_address.channel
# get linked channel and send
message = channel.deliver(
2014-02-05 16:14:57 +00:00
{
message_id: record.message_id,
in_reply_to: record.in_reply_to,
references: references,
from: record.from,
to: record.to,
cc: record.cc,
subject: subject,
content_type: record.content_type,
body: record.body,
attachments: record.attachments
2014-02-05 16:14:57 +00:00
}
)
# store mail plain
Store.add(
object: 'Ticket::Article::Mail',
o_id: record.id,
data: message.to_s,
filename: "ticket-#{ticket.number}-#{record.id}.eml",
preferences: {},
created_by_id: record.created_by_id,
2014-02-05 16:14:57 +00:00
)
# add history record
recipient_list = ''
[:to, :cc].each { |key|
next if !record[key]
next if record[key] == ''
if recipient_list != ''
recipient_list += ','
2014-02-05 16:14:57 +00:00
end
recipient_list += record[key]
2014-02-05 16:14:57 +00:00
}
return if recipient_list == ''
History.add(
o_id: record.id,
history_type: 'email',
history_object: 'Ticket::Article',
related_o_id: ticket.id,
related_history_object: 'Ticket',
value_from: record.subject,
value_to: recipient_list,
created_by_id: record.created_by_id,
)
2014-02-05 16:14:57 +00:00
end
end