Skip to content

Commit

Permalink
Merge pull request #4122 from rubyforgood/event-read-fixes
Browse files Browse the repository at this point in the history
Fix errors found in testing
  • Loading branch information
cielf authored Mar 4, 2024
2 parents b551e43 + f90bf2e commit 10f1c81
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 16 deletions.
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
7 changes: 7 additions & 0 deletions app/events/event_types/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ 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?
return move_item(item_id: item_id,
quantity: -quantity,
from_location: to_location,
to_location: from_location,
validate: validate)
end
if from_location
if storage_locations[from_location].nil? && validate
raise "Storage location #{from_location} not found!"
Expand Down
1 change: 1 addition & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def validate_inventory
e.message << " for #{item} in #{loc}"
if e.event != self
e.message.prepend("Error occurred when re-running events: #{e.event.type} on #{e.event.created_at.to_date}: ")
e.message += " Please contact the Human Essentials admin staff for assistance."
end
raise e
end
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
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

0 comments on commit 10f1c81

Please sign in to comment.