Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4176 customizing reminder message to partners #1

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
pdx_db_test
/db/data
dump.rdb
/config/database.yml

# Ignore all logfiles and tempfiles.
/log/*.log
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def schedule_reminder_email(distribution)
end

def distribution_params
params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, :reminder_email_enabled, :delivery_method, :shipping_cost, line_items_attributes: %i(item_id quantity _destroy))
params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, :reminder_email_enabled, :delivery_method, :shipping_cost, :custom_reminder, line_items_attributes: %i(item_id quantity _destroy))
end

def request_id
Expand Down
49 changes: 28 additions & 21 deletions app/mailers/distribution_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,49 @@ class DistributionMailer < ApplicationMailer
# en.distribution_mailer.partner_mailer.subject
#
def partner_mailer(current_organization, distribution, subject, distribution_changes)
return if distribution.past? || distribution.partner.deactivated?

@partner = distribution.partner
@distribution = distribution
@comment = distribution.comment
requestee_email = distribution.request ? distribution.request.user_email : @partner.email

delivery_method = @distribution.delivery? ? 'delivered' : 'picked up'
@default_email_text = current_organization.default_email_text
@default_email_text_interpolated = TextInterpolatorService.new(@default_email_text.body.to_s, {
delivery_method: delivery_method,
distribution_date: @distribution.issued_at.strftime("%m/%d/%Y"),
partner_name: @partner.name,
comment: @comment
}).call
@partner = @distribution.partner

return if @distribution.past? || @partner.deactivated?

default_email_text = current_organization.default_email_text
@default_email_text_interpolated = interpolate_custom_text(@distribution, default_email_text)

@from_email = current_organization.email.presence || current_organization.users.first.email
@distribution_changes = distribution_changes
pdf = DistributionPdf.new(current_organization, @distribution).compute_and_render
attachments[format("%s %s.pdf", @partner.name, @distribution.created_at.strftime("%Y-%m-%d"))] = pdf
cc = [@partner.email]
cc.push(@partner.profile&.pick_up_email) if distribution.pick_up?
cc.push(@partner.profile&.pick_up_email) if @distribution.pick_up?
cc.compact!
cc.uniq!

mail(to: requestee_email, cc: cc, subject: "#{subject} from #{current_organization.name}")
mail(to: requestee_email(@distribution), cc: cc, subject: "#{subject} from #{current_organization.name}")
end

def reminder_email(distribution_id)
distribution = Distribution.find(distribution_id)
@partner = distribution.partner
@distribution = distribution
requestee_email = distribution.request ? distribution.request.user_email : @partner.email
@distribution = Distribution.find(distribution_id)
@partner = @distribution.partner

return if @distribution.past? || [email protected]_reminders || @partner.deactivated?

mail(to: requestee_email, cc: @partner.email, subject: "#{@partner.name} Distribution Reminder")
@custom_reminder_interpolated = interpolate_custom_text(@distribution, @distribution.custom_reminder)

mail(to: requestee_email(@distribution), cc: @partner.email, subject: "#{@partner.name} Distribution Reminder")
end

private

def interpolate_custom_text(distribution, custom_text)
TextInterpolatorService.new(custom_text.body.to_s, {
delivery_method: distribution.delivery? ? 'delivered' : 'picked up',
distribution_date: distribution.issued_at.strftime("%m/%d/%Y"),
partner_name: distribution.partner.name,
comment: distribution.comment
}).call
end

def requestee_email(distribution)
distribution.request ? distribution.request.user_email : @partner.email
end
end
2 changes: 2 additions & 0 deletions app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Distribution < ApplicationRecord
include ItemsHelper

has_one :request, dependent: :nullify
has_rich_text :custom_reminder

accepts_nested_attributes_for :request

validates :storage_location, :partner, :organization, :delivery_method, presence: true
Expand Down
5 changes: 4 additions & 1 deletion app/views/distribution_mailer/reminder_email.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<% delivery_method = (@distribution.delivery?) ? "delivery" : "pick up" %>
<p>Hello <%= @partner.name %>,</p>
<p>This is a friendly reminder that tomorrow, <b><%= @distribution.issued_at.strftime("%A, %B #{@distribution.issued_at.day.ordinalize} %Y at %I:%M%p") %></b>, is your distribution <b><%= delivery_method %></b> date. </p>
<p>For more information: <a href="mailto:<%= @distribution.organization.email %>"><%= @distribution.organization.email %></a></p>
<% if @distribution.custom_reminder.present? %>
<%= @custom_reminder_interpolated.html_safe %>
<% end %>
<p>For more information: <a href="mailto:<%= @distribution.organization.email %>"><%= @distribution.organization.email %></a></p>
4 changes: 3 additions & 1 deletion app/views/distribution_mailer/reminder_email.text.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% delivery_method = (@distribution.delivery?) ? "delivery" : "pick up" %>
Hello <%= @partner.name %>,
This is a friendly reminder that tomorrow, <%= @distribution.issued_at.strftime("%A, %B #{@distribution.issued_at.day.ordinalize} %Y at %I:%M%p") %>, is your distribution <%= delivery_method %> date.

<% if @distribution.custom_reminder.present? %>
<%= @custom_reminder_interpolated %>
<% end %>
For more information: <%= @distribution.organization.email %>
6 changes: 6 additions & 0 deletions app/views/distributions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<%= f.input :issued_at, as: :datetime, ampm: true, minute_step: 15, label: "Distribution date and time", html5: true, :input_html => { :value => date_place_holder&.strftime("%Y-%m-%dT%0k:%M")} %>
</div>
<%= f.input :reminder_email_enabled, as: :boolean, checked_value: true, unchecked_value: false, label: "Send email reminder the day before?" %>

<% default_email_text_hint = "You can use the variables <code>%{partner_name}</code>, <code>%{delivery_method}</code>, <code>%{distribution_date}</code>, and <code>%{comment}</code> to include the partner's name, delivery method, distribution date, and comments sent in the request." %>
<%= f.input :custom_reminder, label: "Custom Reminder Email Content", hint: default_email_text_hint.html_safe do %>
<%= f.rich_text_area :custom_reminder %>
<% end %>

<%= f.input :agency_rep, label: "Agency representative" %>

<div class='d-flex flex-row' data-controller="distribution-delivery">
Expand Down
38 changes: 0 additions & 38 deletions config/database.yml

This file was deleted.

3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@
end

create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.string "item_type"
t.string "{:null=>false}"
t.bigint "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
Expand Down
34 changes: 32 additions & 2 deletions spec/mailers/distribution_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
let(:mail) { DistributionMailer.reminder_email(@distribution.id) }

context 'HTML format' do
it "renders the body with organization's email text" do
it "renders the body with organization's email text without a custom_reminder message" do
html = html_body(mail)
expect(html).to match("This is a friendly reminder")
expect(html).to match(%(For more information: <a href="mailto:[email protected]">[email protected]</a>))
Expand All @@ -109,12 +109,42 @@
expect(mail.from).to eq(["[email protected]"])
expect(mail.subject).to eq("PARTNER Distribution Reminder")
end

it "renders the body with organization's email text with a custom_reminder message" do
custom_reminder = "Custom reminder message example \n\n%{delivery_method} %{distribution_date}\n\n%{partner_name}\n\n%{comment}"
@distribution.update!({custom_reminder: custom_reminder})

mail = DistributionMailer.reminder_email(@distribution.id)

html = html_body(mail)
expect(html).to match("This is a friendly reminder")
expect(html).to match("Custom reminder message example")
expect(html).to match(%(For more information: <a href="mailto:[email protected]">[email protected]</a>))
expect(mail.to).to eq([@distribution.request.user_email])
expect(mail.cc).to eq([@distribution.partner.email])
expect(mail.from).to eq(["[email protected]"])
expect(mail.subject).to eq("PARTNER Distribution Reminder")
end
end

context 'Text format' do
it "renders the body with organization's email text" do
it "renders the body with organization's email text without a custom_reminder message" do
text = text_body(mail)
expect(text).to match("This is a friendly reminder")
expect(text).to match(%(For more information: [email protected]))
expect(mail.from).to eq(["[email protected]"])
expect(mail.subject).to eq("PARTNER Distribution Reminder")
end

it "renders the body with organization's email text with a custom_reminder message" do
custom_reminder = "Custom reminder message example \n\n%{delivery_method} %{distribution_date}\n\n%{partner_name}\n\n%{comment}"
@distribution.update!({custom_reminder: custom_reminder})

mail = DistributionMailer.reminder_email(@distribution.id)

text = text_body(mail)
expect(text).to match("This is a friendly reminder")
expect(text).to match("Custom reminder message example")
expect(text).to match(%(For more information: [email protected]))
expect(mail.from).to eq(["[email protected]"])
expect(mail.subject).to eq("PARTNER Distribution Reminder")
Expand Down