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 5 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
2 changes: 2 additions & 0 deletions app/controllers/audits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def finalize
end
@audit.finalized!
redirect_to audit_path(@audit), notice: "Audit is Finalized."
rescue => e
redirect_back(fallback_location: audits_path, alert: "Could not finalize audit: #{e.message}")
end

def update
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ def index
def create
@donation = current_organization.donations.new(donation_params)

if DonationCreateService.call(@donation)
begin
DonationCreateService.call(@donation)
flash[:notice] = "Donation created and logged!"
redirect_to donations_path
else
rescue => e
load_form_collections
@donation.line_items.build if @donation.line_items.count.zero?
flash[:error] = "There was an error starting this donation, try again?"
Rails.logger.error "[!] DonationsController#create Error: #{@donation.errors}"
flash[:error] = "There was an error starting this donation: #{e.message}"
Rails.logger.error "[!] DonationsController#create Error: #{e.message}"
render action: :new
end
end
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ def index

def create
@purchase = current_organization.purchases.new(purchase_params)
if PurchaseCreateService.call(@purchase)
begin
PurchaseCreateService.call(@purchase)
flash[:notice] = "New Purchase logged!"
redirect_to purchases_path
else
rescue => e
load_form_collections
@purchase.line_items.build if @purchase.line_items.count.zero?
flash[:error] = "Failed to create purchase due to: #{@purchase.errors.full_messages}"
Rails.logger.error "[!] PurchasesController#create ERROR: #{@purchase.errors.full_messages}"
flash[:error] = "Failed to create purchase due to: #{e.message}"
Rails.logger.error "[!] PurchasesController#create ERROR: #{e.message}"
render action: :new
end
end
Expand Down
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
7 changes: 4 additions & 3 deletions app/services/donation_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module DonationCreateService
class << self
def call(donation)
Donation.transaction do
if donation.save
donation.storage_location.increase_inventory(donation.line_item_values)
DonationEvent.publish(donation)
unless donation.save
raise donation.errors.full_messages.join("\n")
end
donation.storage_location.increase_inventory(donation.line_item_values)
DonationEvent.publish(donation)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions app/services/purchase_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ class PurchaseCreateService
class << self
def call(purchase)
Purchase.transaction do
if purchase.save
purchase.storage_location.increase_inventory(purchase.line_item_values)
PurchaseEvent.publish(purchase)
unless purchase.save
raise purchase.errors.full_messages.join("\n")
end
purchase.storage_location.increase_inventory(purchase.line_item_values)
PurchaseEvent.publish(purchase)
end
end
end
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
2 changes: 1 addition & 1 deletion spec/system/purchase_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@

it "should display failure with error messages" do
click_button "Save"
expect(page).to have_content('Failed to create purchase due to: ["Vendor must exist", "Amount spent is not a number", "Amount spent in cents must be greater than 0"]')
expect(page).to have_content('Failed to create purchase due to: Vendor must exist Amount spent is not a number Amount spent in cents must be greater than 0')
end
end
end
Expand Down
Loading