Skip to content

Commit aadeccf

Browse files
authored
Merge pull request #4894 from nozomirin/4886-catch-validation-error-of-purchase-deletion
Fix the issue where the exception was not caught when the purchase was distributed.
2 parents 2584427 + ca9e75d commit aadeccf

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

app/controllers/purchases_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ def update
7878

7979
def destroy
8080
purchase = current_organization.purchases.find(params[:id])
81-
PurchaseDestroyService.call(purchase)
81+
begin
82+
PurchaseDestroyService.call(purchase)
83+
rescue => e
84+
flash[:error] = e.message
85+
else
86+
flash[:notice] = "Purchase #{params[:id]} has been removed!"
87+
end
8288

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

spec/system/purchase_system_spec.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,30 @@
301301
sign_in organization_admin
302302
end
303303

304-
it "allows deletion of a purchase" do
305-
visit "#{subject}/#{purchase.id}"
306-
expect(page).to have_link("Delete")
307-
accept_confirm do
308-
click_on "Delete"
304+
context "When the purchase remains in storage location" do
305+
it "allows deletion of a purchase" do
306+
visit "#{subject}/#{purchase.id}"
307+
expect(page).to have_link("Delete")
308+
accept_confirm do
309+
click_on "Delete"
310+
end
311+
expect(page).to have_content "Purchase #{purchase.id} has been removed!"
312+
expect(page).to have_content "0 (Total)"
313+
end
314+
end
315+
316+
context "When the purchase has been distributed" do
317+
it "delete a purchase should get an error" do
318+
allow(PurchaseDestroyService).to receive(:call).with(purchase).and_raise(InventoryError)
319+
320+
visit "#{subject}/#{purchase.id}"
321+
expect(page).to have_link("Delete")
322+
accept_confirm do
323+
click_on "Delete"
324+
end
325+
326+
expect(page).to have_css(".alert.error.alert-danger")
309327
end
310-
expect(page).to have_content "Purchase #{purchase.id} has been removed!"
311-
expect(page).to have_content "0 (Total)"
312328
end
313329
end
314330
end

0 commit comments

Comments
 (0)