Refactor updating order state

It makes the code a bit easier to read
This commit is contained in:
Gaetan Craig-Riou
2023-10-20 16:40:02 +11:00
parent 5ba21e486a
commit 25af178011
4 changed files with 28 additions and 14 deletions

View File

@@ -40,6 +40,10 @@ module CheckoutSteps
redirect_to_step_based_on_order
end
# Checkout step and allowed order state
# * step details : order state in cart, address or delivery
# * step payment : order state is payment
# * step summary : order state is confirmation
def check_step
case @order.state
when "cart", "address", "delivery"
@@ -48,14 +52,4 @@ module CheckoutSteps
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

View File

@@ -23,10 +23,12 @@ class SplitCheckoutController < ::BaseController
before_action :hide_ofn_navigation, only: [:edit, :update]
def edit
redirect_to_step_based_on_order unless params[:step]
update_order_state if params[:step]
check_step if params[:step]
if params[:step].blank?
redirect_to_step_based_on_order
else
update_order_state
check_step
end
return if available_shipping_methods.any?
@@ -128,4 +130,15 @@ class SplitCheckoutController < ::BaseController
def order_params
@order_params ||= Checkout::Params.new(@order, params, spree_current_user).call
end
# Update order state based on the step we are loading to avoid discrepancy between step and order
# state. We need to do this when moving back to a previous checkout step, the update action takes
# care of moving the order state forward.
def update_order_state
return @order.back_to_payment if @order.confirmation? && payment_step?
return unless @order.after_delivery_state? && details_step?
@order.back_to_address
end
end

View File

@@ -616,6 +616,10 @@ module Spree
state.in?(["cart", "address", "delivery"])
end
def after_delivery_state?
state.in?(["payment", "confirmation"])
end
private
def reapply_tax_on_changed_address

View File

@@ -74,6 +74,7 @@ describe SplitCheckoutController, type: :controller do
it "updates the order state to payment" do
get :edit, params: { step: "payment" }
expect(response.status).to eq 200
expect(order.reload.state).to eq("payment")
end
end
@@ -82,6 +83,7 @@ describe SplitCheckoutController, type: :controller do
it "updates the order state to address" do
get :edit, params: { step: "details" }
expect(response.status).to eq 200
expect(order.reload.state).to eq("address")
end
end
@@ -96,6 +98,7 @@ describe SplitCheckoutController, type: :controller do
it "updates the order state to address" do
get :edit, params: { step: "details" }
expect(response.status).to eq 200
expect(order.reload.state).to eq("address")
end
end