Sha256: 195b6e622e268f7ac3327d0cafa0c90dd16b2d1ed9b71a2c3349036457f3ee11

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module Workarea
  class SetShippingEstimationAddress
    delegate :shipping, :shippings, :order, to: :@checkout

    def initialize(checkout, location, user = nil)
      @checkout = checkout
      @location = location
      @user = user
    end

    def perform
      return if shipping.nil?
      set_address unless shipping.address.present?
      recalculate_price
    end

    private

    def set_address
      if @user.present? && @user.default_shipping_address.present?
        shipping.set_address(
          @user.default_shipping_address
              .attributes
              .except(*excluded_saved_adddress_fields)
              .merge(used_for_shipping_estimation: true)
        )
      elsif @location.postal_code.present?
        shipping.set_address(
          postal_code: @location.postal_code,
          region: @location.region,
          country: @location.country,
          used_for_shipping_estimation: true
        )
      end
    end

    def recalculate_price
      Pricing.perform(order, shippings)
      Workarea::Checkout::Steps::Shipping.new(@checkout).update(
        shipping_service: shipping.shipping_service&.name
      )
    end

    def excluded_saved_adddress_fields
      %w(_id _type created_at updated_at last_shipped_at last_billed_at)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-shipping_estimation-1.1.3 app/services/workarea/set_shipping_estimation_address.rb
workarea-shipping_estimation-1.1.2 app/services/workarea/set_shipping_estimation_address.rb