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. (#4886)
  • Loading branch information
nozomirin committed Dec 28, 2024
1 parent 6e767db commit ca9e75d
Show file tree
Hide file tree
Showing 2 changed files with 30 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 => 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
30 changes: 23 additions & 7 deletions spec/system/purchase_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,30 @@
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
it "delete a purchase should get an error" do
allow(PurchaseDestroyService).to receive(:call).with(purchase).and_raise(InventoryError)

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")
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 ca9e75d

Please sign in to comment.