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

Fix errors found in testing #4122

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions app/events/event_types/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def set_item_quantity(item_id:, quantity:, location:)
# @param to_location [Integer]
# @param validate [Boolean]
def move_item(item_id:, quantity:, from_location: nil, to_location: nil, validate: true)
if quantity.negative?
move_item(item_id: item_id,
quantity: -quantity,
from_location: to_location,
to_location: from_location,
validate: validate)
return
dorner marked this conversation as resolved.
Show resolved Hide resolved
end
if from_location
if storage_locations[from_location].nil? && validate
raise "Storage location #{from_location} not found!"
Expand Down
3 changes: 2 additions & 1 deletion app/models/view/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def quantity_for(storage_location: nil, item_id: nil)

if storage_location
if item_id
@inventory.storage_locations[storage_location.to_i].items[item_id.to_i]&.quantity || 0
@inventory.storage_locations[storage_location.to_i]&.items&.dig(item_id.to_i)&.quantity || 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unrelatedly, I learned a while back that javascript's ? operator is short-cutting, so that the first one in a long chain immediately halts the whole sequence. I like the explicitness of Ruby's a bit better, though it ends up more verbose.

else
@inventory.storage_locations[storage_location.to_i]&.items&.values&.map(&:quantity)&.sum || 0
end
Expand All @@ -100,6 +100,7 @@ def storage_locations_for_item(item_id)
# @param storage_location [Integer]
# @return [Float]
def total_value_in_dollars(storage_location: nil)
return 0.0 if @inventory.storage_locations[storage_location].nil?
total = @inventory.storage_locations[storage_location].items.values
.map { |i| i.value_in_cents ? i.quantity * i.value_in_cents : 0 }.sum
total.to_f / 100
Expand Down
13 changes: 13 additions & 0 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,19 @@
end

context "subsequent event is incorrect" do
it "should handle negative quantities" do
next unless Event.read_events?(organization) # only relevant if flag is on

donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 100, item: item1)
DonationEvent.publish(donation)
distribution = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location1)
distribution.line_items << build(:line_item, quantity: 90, item: item1)
DistributionEvent.publish(distribution)
donation.line_items.first.quantity = 20
expect { DonationEvent.publish(donation) }.to raise_error(InventoryError)
end

it "should add the event to the message" do
next unless Event.read_events?(organization) # only relevant if flag is on

Expand Down
Loading