# frozen_string_literal: true

# Executes actions after checkout
module Checkout
  class PostCheckoutActions
    def initialize(order)
      @order = order
    end

    def success(controller, params, current_user)
      set_customer_terms_and_conditions_accepted_at(params)
      save_order_addresses_as_user_default(params, current_user)
      OrderCompletionReset.new(controller, @order).call
    end

    def failure
      @order.updater.shipping_address_from_distributor
      OrderCheckoutRestart.new(@order).call
    end

    private

    def save_order_addresses_as_user_default(params, current_user)
      return unless params[:order]

      user_default_address_setter = UserDefaultAddressSetter.new(@order, current_user)
      user_default_address_setter.set_default_bill_address if params[:order][:default_bill_address]
      user_default_address_setter.set_default_ship_address if params[:order][:default_ship_address]
    end

    def set_customer_terms_and_conditions_accepted_at(params)
      @order.customer.update(terms_and_conditions_accepted_at: Time.zone.now) if params[:order][:terms_and_conditions_accepted]
    end
  end
end

# frozen_string_literal: true

# Executes actions after checkout
module Checkout
  class PostCheckoutActions
    def initialize(order)
      @order = order
    end

    def success(controller, params, current_user)
      set_customer_terms_and_conditions_accepted_at(params)
      save_order_addresses_as_user_default(params, current_user)
      OrderCompletionReset.new(controller, @order).call
    end

    def failure
      @order.updater.shipping_address_from_distributor
      OrderCheckoutRestart.new(@order).call
    end

    private

    def save_order_addresses_as_user_default(params, current_user)
      return unless params[:order]

      user_default_address_setter = UserDefaultAddressSetter.new(@order, current_user)
      user_default_address_setter.set_default_bill_address if params[:order][:default_bill_address]
      user_default_address_setter.set_default_ship_address if params[:order][:default_ship_address]
    end

    def set_customer_terms_and_conditions_accepted_at(params)
      return unless params[:order]

      @order.customer.update(terms_and_conditions_accepted_at: Time.zone.now) if params[:order][:terms_and_conditions_accepted]
    end
  end
end

Fix edge case and some specs in post checkout actions
Fix edge case and some specs in post checkout actions
