diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b8b45d19f..d62aa2b9d9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -139,17 +139,6 @@ class ApplicationController < ActionController::Base !current_distributor.ready_for_checkout? end - def check_order_cycle_expiry - if current_order_cycle&.closed? - Bugsnag.notify("Notice: order cycle closed during checkout completion", order: current_order) - current_order.empty! - current_order.set_order_cycle! nil - flash[:info] = I18n.t('order_cycle_closed') - - redirect_to main_app.shop_path - end - end - # All render calls within the block will be performed with the specified format # Useful for rendering html within a JSON response, particularly if the specified # template or partial then goes on to render further partials without specifying diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index beaf68ef28..7816066b8b 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -12,7 +12,6 @@ class BaseController < ApplicationController include OrderCyclesHelper before_action :set_locale - before_action :check_order_cycle_expiry private diff --git a/app/controllers/concerns/order_stock_check.rb b/app/controllers/concerns/order_stock_check.rb index a3c09d1838..3a46ee9ea0 100644 --- a/app/controllers/concerns/order_stock_check.rb +++ b/app/controllers/concerns/order_stock_check.rb @@ -18,6 +18,17 @@ module OrderStockCheck redirect_to main_app.cart_path end + def check_order_cycle_expiry + if current_order_cycle&.closed? + Bugsnag.notify("Notice: order cycle closed during checkout completion", order: current_order) + current_order.empty! + current_order.set_order_cycle! nil + flash[:info] = I18n.t('order_cycle_closed') + + redirect_to main_app.shop_path + end + end + private def sufficient_stock? diff --git a/spec/controllers/base_controller_spec.rb b/spec/controllers/base_controller_spec.rb index 98d6183ea4..edad99c70b 100644 --- a/spec/controllers/base_controller_spec.rb +++ b/spec/controllers/base_controller_spec.rb @@ -99,17 +99,4 @@ describe BaseController, type: :controller do controller.current_order(true) end end - - it "redirects to shopfront with message if order cycle is expired" do - expect(controller).to receive(:current_order_cycle).and_return(oc) - expect(controller).to receive(:current_order).and_return(order).at_least(:twice) - expect(oc).to receive(:closed?).and_return(true) - expect(order).to receive(:empty!) - expect(order).to receive(:set_order_cycle!).with(nil) - - get :index - - expect(response).to redirect_to shop_url - expect(flash[:info]).to eq I18n.t('order_cycle_closed') - end end diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index 844ffac75e..342c6444ee 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -25,6 +25,20 @@ describe CheckoutController, type: :controller do expect(response).to redirect_to shop_path end + it "redirects to shopfront with message if order cycle is expired" do + allow(controller).to receive(:current_distributor).and_return(distributor) + expect(controller).to receive(:current_order_cycle).and_return(order_cycle).at_least(:once) + expect(controller).to receive(:current_order).and_return(order).at_least(:once) + expect(order_cycle).to receive(:closed?).and_return(true) + expect(order).to receive(:empty!) + expect(order).to receive(:set_order_cycle!).with(nil) + + get :edit + + expect(response).to redirect_to shop_url + expect(flash[:info]).to eq I18n.t('order_cycle_closed') + end + it "redirects home with message if hub is not ready for checkout" do allow(distributor).to receive(:ready_for_checkout?) { false } allow(order).to receive_messages(distributor: distributor, order_cycle: order_cycle)