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

Email request units #4419

Closed
Closed
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
9 changes: 4 additions & 5 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 @@ -19,12 +18,12 @@ def fetch_items(request)
end

def combined_items(request)
return [] if request.request_items.size == 0
return [] if request.item_requests.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.item_requests.group_by { |item_request| [item_request.item_id, item_request.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, item_requests|
{ 'item_id' => id_unit.first, 'quantity' => item_requests.map { |i| i.quantity.to_i }.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 %>
Comment on lines +7 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight indention change :)

Suggested change
<% if Flipper.enabled?(:enable_packs) && item['unit'] %>
<%= pluralize(item['quantity'] || item['person_count'], item['unit']) %>
<% else %>
<%= item['quantity'] || item['person_count'] %>
<% end %>
<% 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
12 changes: 12 additions & 0 deletions spec/factories/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def random_request_items
request_items { random_request_items }
comments { "Urgent" }
partner_user { ::User.partner_users.first || create(:partners_user) }
after(:build) do |request|
request.request_items.each do |request_item|
item = Item.find(request_item['item_id'])
item_request = Partners::ItemRequest.new(
item_id: request_item['item_id'],
quantity: request_item['quantity'],
name: item.name,
partner_key: item.partner_key
)
request.item_requests << item_request
end
end
end

trait :started do
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 = create(:request, :pending, request_items: [])
create(:item_request, request: request, quantity: 1, request_unit: "Pack", item: item1)
create(:item_request, request: request, quantity: 7, request_unit: "Pack", item: item2)
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: [])
create(:item_request, request: request, quantity: 7, item: item)
email = RequestsConfirmationMailer.confirmation_email(request)

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