Skip to content

Commit

Permalink
Refactor the mailbot to make sending more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
cycomachead committed Mar 12, 2024
1 parent 965489d commit 3381bed
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
28 changes: 12 additions & 16 deletions app/mailers/mailbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,19 @@ def ticket_confirmation_mail(ticket_purchase)
attachments["ticket_for_#{@conference.short_title}_#{physical_ticket.id}.pdf"] = pdf.render
end

# if email subject is empty, use custom template
if @ticket_purchase.ticket.email_subject.empty? && !@ticket_purchase.ticket.email_body.empty?
@ticket_purchase.ticket.email_body = @ticket_purchase.generate_confirmation_mail(@ticket_purchase.ticket.email_body)
mail(subject: "#{@conference.title} | Ticket Confirmation and PDF!", template_name: 'custom_ticket_confirmation_template')
# if email body is empty, use default template with subject
elsif !@ticket_purchase.ticket.email_subject.empty? && @ticket_purchase.ticket.email_body.empty?
@ticket_purchase.ticket.email_subject = @ticket_purchase.generate_confirmation_mail(@ticket_purchase.ticket.email_subject)
mail(subject: @ticket_purchase.ticket.email_subject, template_name: 'ticket_confirmation_template')
# if both exist, use custom
elsif !@ticket_purchase.ticket.email_subject.empty? && !@ticket_purchase.ticket.email_body.empty?
@ticket_purchase.ticket.email_body = @ticket_purchase.generate_confirmation_mail(@ticket_purchase.ticket.email_body)
@ticket_purchase.ticket.email_subject = @ticket_purchase.generate_confirmation_mail(@ticket_purchase.ticket.email_subject)
mail(subject: @ticket_purchase.ticket.email_subject, template_name: 'custom_ticket_confirmation_template')
# if both empty, use default
else
mail(subject: "#{@conference.title} | Ticket Confirmation and PDF!", template_name: 'ticket_confirmation_template')
email_subject = "#{@conference.title} | Ticket Confirmation and PDF!"
email_template = 'ticket_confirmation_template'

if @ticket_purchase.ticket.email_subject.present?
email_subject = @ticket_purchase.render_email_data(@ticket_purchase.ticket.email_subject)
end

if @ticket_purchase.ticket.email_body.present?
@rendered_email_body = @ticket_purchase.render_email_data(@ticket_purchase.ticket.email_body)
email_template = 'custom_ticket_confirmation_template'
end

mail(subject: email_subject, template_name: email_template)
end

def acceptance_mail(event)
Expand Down
2 changes: 1 addition & 1 deletion app/models/ticket_purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def registration_ticket_already_purchased
end
end

def generate_confirmation_mail(event_template)
def render_email_data(event_template)
parser = EmailTemplateParser.new(conference, user)
values = parser.retrieve_values(nil, nil, quantity, ticket)
EmailTemplateParser.parse_template(event_template, values)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= render partial: "layouts/mailbot_header" %>
<div id="content">
<span style="white-space: pre-line">
<%= @ticket_purchase.ticket.email_body %>
<%= @rendered_email_body %>
</span>
</div>
<%= render partial: "layouts/mailbot_footer" %>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= @ticket_purchase.ticket.email_body %>
<%= @rendered_email_body %>
1 change: 0 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@

# Mailbot settings
config.mailbot = {
ytlf_ticket_id: ENV.fetch('YTLF_TICKET_ID', 50),
bcc_address: ENV.fetch('OSEM_MESSAGE_BCC_ADDRESS', nil)
}

Expand Down

0 comments on commit 3381bed

Please sign in to comment.