Skip to content
Merged
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
10 changes: 8 additions & 2 deletions app/services/distribution_content_change_service.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class DistributionContentChangeService
attr_reader :updates, :removed
attr_reader :added, :updates, :removed

def initialize(old_line_items, new_line_items)
@old_line_items = to_hash(old_line_items)
@new_line_items = to_hash(new_line_items)
@added = []
@updates = []
@removed = []
end
Expand All @@ -14,13 +15,14 @@ def call
end

def any_change?
updates.any? || removed.any?
added.any? || updates.any? || removed.any?
end

def changes
return {} unless any_change?

{
added: added,
updates: updates,
removed: removed
}
Expand All @@ -37,6 +39,10 @@ def to_hash(line_items)
end

def identify_changes
new_line_items.each do |key, item|
added << item.slice(:name, :quantity) unless old_line_items.key?(key)
end

old_line_items.each do |k, v|
if new_line_items[k]
if new_line_items[k][:quantity] != v[:quantity]
Expand Down
11 changes: 11 additions & 0 deletions app/views/distribution_mailer/_distribution_changes.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<p>We've had to change some items on your distribution</p>
<ul style="list-style-type:none">
<% if @distribution_changes[:added].present? %>
<li>Items Added</li>
<ol style="list-style-type:none">
<% @distribution_changes[:added].each do |item| %>
<li>
<p><%= "#{item[:name]}: #{item[:quantity]}" %></p>
</li>
<% end %>
</ol>
</li>
<% end %>
<% if @distribution_changes[:updates].any? %>
<li>Items Updated</li>
<ol style="list-style-type:none">
Expand Down
42 changes: 42 additions & 0 deletions spec/controllers/distributions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@

it "redirects with a flash notice and send send_notification" do
expected_distribution_changes = {
added: [{name: "Item 2", quantity: 4}],
removed: [],
updates: [
{
Expand All @@ -406,6 +407,47 @@
end
end

context "when only an item is added to a distribution" do
let(:existing_item) { create(:item, name: "Adult Diapers", organization: organization) }
let(:added_item) { create(:item, name: "Wipes (Adult)", organization: organization) }
let(:storage_location) { create(:storage_location, organization: organization) }
let(:distribution) do
create(:distribution, :with_items, organization: organization, partner: partner,
storage_location: storage_location, item: existing_item, item_quantity: 10,
issued_at: 2.days.from_now)
end

before do
TestInventory.create_inventory(organization, {
storage_location.id => {existing_item.id => 100, added_item.id => 100}
})
end

it "sends a change notification listing the added item" do
expected_changes = {
added: [{name: "Wipes (Adult)", quantity: 5}],
updates: [],
removed: []
}

expect(PartnerMailerJob).to receive(:perform_now)
.with(organization.id, distribution.id, "Your Distribution Has Changed", expected_changes)

put :update, params: {
id: distribution.id,
distribution: {
storage_location_id: storage_location.id,
line_items_attributes: {
"0": {item_id: existing_item.id, quantity: 10},
"1": {item_id: added_item.id, quantity: 5}
}
}
}

expect(response).to have_http_status(:redirect)
end
end

context "when distribution reminder email is enabled" do
let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 0) }
let(:storage_location) { create(:storage_location, organization: organization) }
Expand Down
10 changes: 10 additions & 0 deletions spec/mailers/distribution_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
end
end

context "with an added item" do
let(:distribution_changes) do
{added: [{name: "Wipes (Adult)", quantity: 88}], updates: [], removed: []}
end

it "shows the added item and its quantity" do
expect(html_body(mail)).to include("Items Added", "Wipes (Adult): 88")
end
end

context "when organization does not have custom default_email_text" do
before do
# When default_email_text is cleared through UI, it still contains whitespace/empty divs/HTML comments
Expand Down
23 changes: 23 additions & 0 deletions spec/services/distribution_content_change_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
end
end

context "when a line item is added" do
let(:old_line_items) { [line_item1] }
let(:new_line_items) { [line_item1, line_item2] }

it "returns true" do
expect(subject.any_change?).to be true
end
end

context "when there are line items removed and updated" do
let(:old_line_items) { [line_item1, line_item2, line_item3, line_item4] }
let(:new_line_items) { [line_item1.clone, line_item2, line_item3] }
Expand Down Expand Up @@ -66,6 +75,7 @@

it "returns true" do
expected_changes = {
added: [],
removed: [line_item4],
updates: [
{
Expand All @@ -78,5 +88,18 @@
expect(subject.changes).to eq expected_changes
end
end

context "when line items are added and updated" do
let(:old_line_items) { [line_item1, line_item2] }
let(:new_line_items) { [line_item1.merge(quantity: 30), line_item2, line_item3] }

it "includes the added item and quantity" do
expect(subject.changes).to eq(
added: [{name: "Wipes (Adult)", quantity: 88}],
updates: [{name: "Adult Incontinence Pads", new_quantity: 30, old_quantity: 382}],
removed: []
)
end
end
end
end
Loading