Skip to content

Commit 1085e87

Browse files
authored
Merge pull request #5727 from augustocbx/5666-include-added-items-in-distribution-emails
Include added items in distribution change emails (5666)
2 parents 2493ab0 + 27d0efc commit 1085e87

5 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎app/services/distribution_content_change_service.rb‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class DistributionContentChangeService
2-
attr_reader :updates, :removed
2+
attr_reader :added, :updates, :removed
33

44
def initialize(old_line_items, new_line_items)
55
@old_line_items = to_hash(old_line_items)
66
@new_line_items = to_hash(new_line_items)
7+
@added = []
78
@updates = []
89
@removed = []
910
end
@@ -14,13 +15,14 @@ def call
1415
end
1516

1617
def any_change?
17-
updates.any? || removed.any?
18+
added.any? || updates.any? || removed.any?
1819
end
1920

2021
def changes
2122
return {} unless any_change?
2223

2324
{
25+
added: added,
2426
updates: updates,
2527
removed: removed
2628
}
@@ -37,6 +39,10 @@ def to_hash(line_items)
3739
end
3840

3941
def identify_changes
42+
new_line_items.each do |key, item|
43+
added << item.slice(:name, :quantity) unless old_line_items.key?(key)
44+
end
45+
4046
old_line_items.each do |k, v|
4147
if new_line_items[k]
4248
if new_line_items[k][:quantity] != v[:quantity]

‎app/views/distribution_mailer/_distribution_changes.html.erb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<p>We've had to change some items on your distribution</p>
22
<ul style="list-style-type:none">
3+
<% if @distribution_changes[:added].present? %>
4+
<li>Items Added</li>
5+
<ol style="list-style-type:none">
6+
<% @distribution_changes[:added].each do |item| %>
7+
<li>
8+
<p><%= "#{item[:name]}: #{item[:quantity]}" %></p>
9+
</li>
10+
<% end %>
11+
</ol>
12+
</li>
13+
<% end %>
314
<% if @distribution_changes[:updates].any? %>
415
<li>Items Updated</li>
516
<ol style="list-style-type:none">

‎spec/controllers/distributions_controller_spec.rb‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@
385385

386386
it "redirects with a flash notice and send send_notification" do
387387
expected_distribution_changes = {
388+
added: [{name: "Item 2", quantity: 4}],
388389
removed: [],
389390
updates: [
390391
{
@@ -406,6 +407,47 @@
406407
end
407408
end
408409

410+
context "when only an item is added to a distribution" do
411+
let(:existing_item) { create(:item, name: "Adult Diapers", organization: organization) }
412+
let(:added_item) { create(:item, name: "Wipes (Adult)", organization: organization) }
413+
let(:storage_location) { create(:storage_location, organization: organization) }
414+
let(:distribution) do
415+
create(:distribution, :with_items, organization: organization, partner: partner,
416+
storage_location: storage_location, item: existing_item, item_quantity: 10,
417+
issued_at: 2.days.from_now)
418+
end
419+
420+
before do
421+
TestInventory.create_inventory(organization, {
422+
storage_location.id => {existing_item.id => 100, added_item.id => 100}
423+
})
424+
end
425+
426+
it "sends a change notification listing the added item" do
427+
expected_changes = {
428+
added: [{name: "Wipes (Adult)", quantity: 5}],
429+
updates: [],
430+
removed: []
431+
}
432+
433+
expect(PartnerMailerJob).to receive(:perform_now)
434+
.with(organization.id, distribution.id, "Your Distribution Has Changed", expected_changes)
435+
436+
put :update, params: {
437+
id: distribution.id,
438+
distribution: {
439+
storage_location_id: storage_location.id,
440+
line_items_attributes: {
441+
"0": {item_id: existing_item.id, quantity: 10},
442+
"1": {item_id: added_item.id, quantity: 5}
443+
}
444+
}
445+
}
446+
447+
expect(response).to have_http_status(:redirect)
448+
end
449+
end
450+
409451
context "when distribution reminder email is enabled" do
410452
let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 0) }
411453
let(:storage_location) { create(:storage_location, organization: organization) }

‎spec/mailers/distribution_mailer_spec.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@
104104
end
105105
end
106106

107+
context "with an added item" do
108+
let(:distribution_changes) do
109+
{added: [{name: "Wipes (Adult)", quantity: 88}], updates: [], removed: []}
110+
end
111+
112+
it "shows the added item and its quantity" do
113+
expect(html_body(mail)).to include("Items Added", "Wipes (Adult): 88")
114+
end
115+
end
116+
107117
context "when organization does not have custom default_email_text" do
108118
before do
109119
# When default_email_text is cleared through UI, it still contains whitespace/empty divs/HTML comments

‎spec/services/distribution_content_change_service_spec.rb‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
end
3131
end
3232

33+
context "when a line item is added" do
34+
let(:old_line_items) { [line_item1] }
35+
let(:new_line_items) { [line_item1, line_item2] }
36+
37+
it "returns true" do
38+
expect(subject.any_change?).to be true
39+
end
40+
end
41+
3342
context "when there are line items removed and updated" do
3443
let(:old_line_items) { [line_item1, line_item2, line_item3, line_item4] }
3544
let(:new_line_items) { [line_item1.clone, line_item2, line_item3] }
@@ -66,6 +75,7 @@
6675

6776
it "returns true" do
6877
expected_changes = {
78+
added: [],
6979
removed: [line_item4],
7080
updates: [
7181
{
@@ -78,5 +88,18 @@
7888
expect(subject.changes).to eq expected_changes
7989
end
8090
end
91+
92+
context "when line items are added and updated" do
93+
let(:old_line_items) { [line_item1, line_item2] }
94+
let(:new_line_items) { [line_item1.merge(quantity: 30), line_item2, line_item3] }
95+
96+
it "includes the added item and quantity" do
97+
expect(subject.changes).to eq(
98+
added: [{name: "Wipes (Adult)", quantity: 88}],
99+
updates: [{name: "Adult Incontinence Pads", new_quantity: 30, old_quantity: 382}],
100+
removed: []
101+
)
102+
end
103+
end
81104
end
82105
end

0 commit comments

Comments
 (0)