From ca9e75dbfd8c94148178fb878d885448786cea2b Mon Sep 17 00:00:00 2001 From: Nozomi R Date: Thu, 26 Dec 2024 18:09:02 +0800 Subject: [PATCH] Fix the issue where the exception was not caught when the purchase was distributed. (#4886) --- app/controllers/purchases_controller.rb | 9 ++++++-- spec/system/purchase_system_spec.rb | 30 +++++++++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/app/controllers/purchases_controller.rb b/app/controllers/purchases_controller.rb index 760c06c1b4..a88a8d870e 100644 --- a/app/controllers/purchases_controller.rb +++ b/app/controllers/purchases_controller.rb @@ -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 diff --git a/spec/system/purchase_system_spec.rb b/spec/system/purchase_system_spec.rb index 39be2a13cb..c8a2ad73d9 100644 --- a/spec/system/purchase_system_spec.rb +++ b/spec/system/purchase_system_spec.rb @@ -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