From b09054a76a84f35e3031fdf71dd01d8f29b8f51e Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 18 Oct 2023 09:45:05 +1100 Subject: [PATCH] Update order state when moving back through checkout step Some important logic happens after the order transition from one state to another. In particular, voucher are recalculated. To fix any inconsistency, we make sure the order is the state matching the checkout step we are on. --- app/controllers/concerns/checkout_steps.rb | 18 ++++++++-- app/controllers/split_checkout_controller.rb | 2 ++ .../split_checkout_controller_spec.rb | 36 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/app/controllers/concerns/checkout_steps.rb b/app/controllers/concerns/checkout_steps.rb index 83eca5ed83..556c934166 100644 --- a/app/controllers/concerns/checkout_steps.rb +++ b/app/controllers/concerns/checkout_steps.rb @@ -13,6 +13,10 @@ module CheckoutSteps params[:step] == "payment" end + def details_step? + params[:step] == "details" + end + def redirect_to_step_based_on_order case @order.state when "cart", "address", "delivery" @@ -39,9 +43,19 @@ module CheckoutSteps def check_step case @order.state when "cart", "address", "delivery" - redirect_to checkout_step_path(:details) unless params[:step] == "details" + redirect_to checkout_step_path(:details) unless details_step? when "payment" - redirect_to checkout_step_path(:payment) if params[:step] == "summary" + redirect_to checkout_step_path(:payment) if summary_step? end end + + def update_order_state + if @order.state == "confirmation" && payment_step? + @order.back_to_payment + end + + return unless @order.state.in?(["payment", "confirmation"]) && details_step? + + @order.back_to_address + end end diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index 6b1266eaf8..a855774874 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -24,6 +24,8 @@ class SplitCheckoutController < ::BaseController def edit redirect_to_step_based_on_order unless params[:step] + + update_order_state if params[:step] check_step if params[:step] return if available_shipping_methods.any? diff --git a/spec/controllers/split_checkout_controller_spec.rb b/spec/controllers/split_checkout_controller_spec.rb index df46630878..c4ead98e3f 100644 --- a/spec/controllers/split_checkout_controller_spec.rb +++ b/spec/controllers/split_checkout_controller_spec.rb @@ -67,6 +67,42 @@ describe SplitCheckoutController, type: :controller do expect(response).to redirect_to checkout_step_path(:payment) end end + + context "when order state is 'confirmation'" do + before do + order.update!(state: "confirmation") + end + + context "when loading payment step" do + it "updates the order state to payment" do + get :edit, params: { step: "payment" } + + expect(order.reload.state).to eq("payment") + end + end + + context "when loading address step" do + it "updates the order state to address" do + get :edit, params: { step: "details" } + + expect(order.reload.state).to eq("address") + end + end + end + + context "when order state is 'payment'" do + context "when loading address step" do + before do + order.update!(state: "payment") + end + + it "updates the order state to address" do + get :edit, params: { step: "details" } + + expect(order.reload.state).to eq("address") + end + end + end end end