Sha256: 317e35fa08666244d90c8d2bff7bad1f01925641973a9baab85949a6423df0b0

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module SolidusBactracs
  module Api
    class ScheduleShipmentSyncsJob < ApplicationJob
      queue_as :default

      def perform
        shipments = query_shipments
        Rails.logger.info("#{self.class.name} - #{shipments.count} shipments to sync to Bactracs")

        shipments.find_in_batches(batch_size: SolidusBactracs.config.api_batch_size) do |batch|
          SyncShipmentsJob.perform_later(batch.to_a)
        end
      rescue StandardError => e
        SolidusBactracs.config.error_handler.call(e, {})
      end

      def shippable_skus
        SolidusBactracs.config.shippable_skus.present? ? SolidusBactracs.config.shippable_skus : Spree::Variant.pluck(:sku)
      end

      def query_shipments
        shipments = SolidusBactracs::Shipment::PendingApiSyncQuery.apply(all_elegible_shipments)
      end

      def all_elegible_shipments(skus: SolidusBactracs.config.shippable_skus, state: :ready)
        ::Spree::Shipment
            .joins(inventory_units: [:variant])
            .where("spree_variants.sku" => skus)
            .where("spree_shipments.state" => state)
            .where(bactracs_synced_at: nil)
            .distinct
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_bactracs-3.3.1 app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb
solidus_bactracs-3.3.0 app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb
solidus_bactracs-3.2.2 app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb
solidus_bactracs-3.2.1 app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb
solidus_bactracs-3.2.0 app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb