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

4419 - Add Units to confirmation email #4421

Merged
7 changes: 3 additions & 4 deletions app/mailers/requests_confirmation_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def confirmation_email(request)
@partner = request.partner
@request_items = fetch_items(request)
requestee_email = request.user_email

mail(to: requestee_email, cc: @partner.email, subject: "#{@organization.name} - Requests Confirmation")
end

Expand All @@ -21,10 +20,10 @@ def fetch_items(request)
def combined_items(request)
return [] if request.request_items.size == 0
# convert items into a hash of (id => list of items with that ID)
grouped = request.request_items.group_by { |i| i['item_id'] }
grouped = request.request_items.group_by { |i| [i['item_id'], i['request_unit']] }
# convert hash into an array of items with combined quantities
grouped.map do |id, items|
{ 'item_id' => id, 'quantity' => items.map { |i| i['quantity'] }.sum }
grouped.map do |id_unit, items|
{ 'item_id' => id_unit.first, 'quantity' => items.map { |i| i['quantity'] }.sum, "unit" => id_unit.last }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
<p>This is an email confirmation that the <%= @organization.name %> has received your request of:</p>
<ul>
<% @request_items.each do |item| %>
<li><%= item['name'] %> - <%= item['quantity'] || item['person_count'] %></li>
<li><%= item['name'] %> -
<% if Flipper.enabled?(:enable_packs) && item['unit'] %>
<%= pluralize(item['quantity'] || item['person_count'], item['unit']) %>
<% else %>
<%= item['quantity'] || item['person_count'] %>
<% end %>
</li>

<% end %>
</ul>

Expand Down
25 changes: 25 additions & 0 deletions spec/mailers/requests_confirmation_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,29 @@
expect(mail_w_varied_quantities.body.encoded).to include(expected_string)
}
end
it "shows units" do
Flipper.enable(:enable_packs)
item1 = create(:item, organization:)
item2 = create(:item, organization:)
create(:item_unit, item: item1, name: "Pack")
create(:item_unit, item: item2, name: "Pack")
request_items = [
{item_id: item1.id, quantity: 1, request_unit: "Pack"},
{item_id: item2.id, quantity: 7, request_unit: "Pack"}
]
request = create(:request, :pending, request_items:)
email = RequestsConfirmationMailer.confirmation_email(request)
expect(email.body.encoded).to match("1 Pack")
expect(email.body.encoded).to match("7 Packs")
end

it "skips units when are not provided" do
Flipper.enable(:enable_packs)
item = create(:item, organization:)
create(:item_unit, item: item, name: "Pack")
request = create(:request, :pending, request_items: [{item_id: item.id, quantity: 7}])
email = RequestsConfirmationMailer.confirmation_email(request)

expect(email.body.encoded).not_to match("7 Packs")
end
end