Sha256: d7c77a76c69fa7c2bec6d7b50f2ce3571578a1b5c99613564740924eebcdb962

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module SolidusBolt
  module Users
    class SyncAddressesService < SolidusBolt::BaseService
      attr_reader :user, :access_token

      def initialize(user:, access_token:)
        @access_token = access_token
        @user = user
        super
      end

      def call
        return if user.nil? || access_token.nil?

        bolt_addresses.each do |bolt_address|
          default = bolt_address['default']
          address = convert_to_solidus(bolt_address)
          user.save_in_address_book(address, default, :shipping)
          user.save_in_address_book(address, default, :billing)
        end
      end

      private

      def user_info
        @user_info ||= Accounts::DetailService.call(access_token: access_token)
      end

      def bolt_addresses
        user_info['addresses']
      end

      def convert_to_solidus(bolt_address)
        country = Spree::Country.find_by(iso: bolt_address['country_code'])
        {
          address1: bolt_address['street_address1'],
          city: bolt_address['locality'],
          state: country.states.find_by(abbr: bolt_address['region_code']),
          zipcode: bolt_address['postal_code'],
          name: bolt_address['name'],
          country: country,
          phone: bolt_address['phone_number'] || user_info['profile']['phone']
        }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
solidus_bolt-0.7.2 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.7.1 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.7.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.6.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.5.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.4.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.3.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.2.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.1.0 app/services/solidus_bolt/users/sync_addresses_service.rb
solidus_bolt-0.0.1 app/services/solidus_bolt/users/sync_addresses_service.rb