Skip to content

Commit

Permalink
Merge pull request #4894 from nozomirin/4886-catch-validation-error-o…
Browse files Browse the repository at this point in the history
…f-purchase-deletion

Fix the issue where the exception was not caught when the purchase was distributed.
  • Loading branch information
cielf authored Jan 5, 2025
2 parents 2584427 + ca9e75d commit aadeccf
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 aadeccf

Please sign in to comment.