class SolidusShipstation::Api::BatchSyncer

Attributes

client[R]
shipment_matcher[R]

Public Class Methods

from_config() click to toggle source
# File lib/solidus_shipstation/api/batch_syncer.rb, line 7
def from_config
  new(
    client: SolidusShipstation::Api::Client.from_config,
    shipment_matcher: SolidusShipstation.config.api_shipment_matcher,
  )
end
new(client:, shipment_matcher:) click to toggle source
# File lib/solidus_shipstation/api/batch_syncer.rb, line 17
def initialize(client:, shipment_matcher:)
  @client = client
  @shipment_matcher = shipment_matcher
end

Public Instance Methods

call(shipments) click to toggle source
# File lib/solidus_shipstation/api/batch_syncer.rb, line 22
def call(shipments)
  begin
    response = client.bulk_create_orders(shipments)
  rescue RateLimitedError => e
    ::Spree::Event.fire(
      'solidus_shipstation.api.rate_limited',
      shipments: shipments,
      error: e,
    )

    raise e
  rescue RequestError => e
    ::Spree::Event.fire(
      'solidus_shipstation.api.sync_errored',
      shipments: shipments,
      error: e,
    )

    raise e
  end

  return unless response

  response['results'].each do |shipstation_order|
    shipment = shipment_matcher.call(shipstation_order, shipments)

    unless shipstation_order['success']
      ::Spree::Event.fire(
        'solidus_shipstation.api.sync_failed',
        shipment: shipment,
        payload: shipstation_order,
      )

      next
    end

    shipment.update_columns(
      shipstation_synced_at: Time.zone.now,
      shipstation_order_id: shipstation_order['orderId'],
    )

    ::Spree::Event.fire(
      'solidus_shipstation.api.sync_completed',
      shipment: shipment,
      payload: shipstation_order,
    )
  end
end