Skip to content

Commit

Permalink
Fix the issue where the exception was not caught when the purchase wa…
Browse files Browse the repository at this point in the history
…s distributed. (rubyforgood#4886)
  • Loading branch information
nozomirin committed Dec 26, 2024
1 parent 6e767db commit 7a88ad3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
9 changes: 7 additions & 2 deletions app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ def update

def destroy
purchase = current_organization.purchases.find(params[:id])
PurchaseDestroyService.call(purchase)
begin
PurchaseDestroyService.call(purchase)
rescue InventoryError => e
flash[:error] = e.message
else
flash[:notice] = "Purchase #{params[:id]} has been removed!"
end

flash[:notice] = "Purchase #{params[:id]} has been removed!"
redirect_to purchases_path
end

Expand Down
50 changes: 43 additions & 7 deletions spec/system/purchase_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,50 @@
sign_in organization_admin
end

it "allows deletion of a purchase" do
visit "#{subject}/#{purchase.id}"
expect(page).to have_link("Delete")
accept_confirm do
click_on "Delete"
context "When the purchase remains in storage location" do
it "allows deletion of a purchase" do
visit "#{subject}/#{purchase.id}"
expect(page).to have_link("Delete")
accept_confirm do
click_on "Delete"
end
expect(page).to have_content "Purchase #{purchase.id} has been removed!"
expect(page).to have_content "0 (Total)"
end
end

context "When the purchase has been distributed" do
let(:item_quantity) { 10 }
let!(:partner) { create(:partner, organization: organization, name: "Test Partner") }
let!(:item) { create(:item, name: "RandomItem", organization: organization) }
let!(:storage_location) { create(:storage_location, :with_items, name: "storage1", organization: organization, item: item, item_quantity: 0) }
let!(:purchase) { create(:purchase, :with_items, storage_location: storage_location, item_quantity: item_quantity, organization: organization, item: item) }

it "delete a purchase should get an error" do
# distribute the purchase
visit new_distribution_path
select item.name, from: "distribution_line_items_attributes_0_item_id"
fill_in "distribution_line_items_attributes_0_quantity", with: 5

select partner.name, from: "Partner"
select purchase.storage_location.name, from: "From storage location"
choose "Pick up"
fill_in "Distribution date", with: '01/01/2001 10:15:00 AM'
click_button "Save"
within "#distributionConfirmationModal" do
click_button "Yes, it's correct"
end
click_link "Distribution Complete"

inventory = View::Inventory.new(organization.id).items_for_location(storage_location.id).first

visit "#{subject}/#{purchase.id}"
expect(page).to have_link("Delete")
accept_confirm do
click_on "Delete"
end
expect(page).to have_css(".alert.error.alert-danger", text: "Could not reduce quantity by #{item_quantity} - current quantity is #{inventory.quantity} for #{item.name} in #{purchase.storage_location.name}")
end
expect(page).to have_content "Purchase #{purchase.id} has been removed!"
expect(page).to have_content "0 (Total)"
end
end
end

0 comments on commit 7a88ad3

Please sign in to comment.