Class: SolidusBactracs::Api::ScheduleShipmentSyncsJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb

Instance Method Summary collapse

Instance Method Details

#all_eligible_shipments(skus: SolidusBactracs.config.shippable_skus, state: :ready) ⇒ Object



27
28
29
30
31
32
33
34
# File 'app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb', line 27

def all_eligible_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

#performObject



8
9
10
11
12
13
14
15
16
17
# File 'app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb', line 8

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

#query_shipmentsObject



23
24
25
# File 'app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb', line 23

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

#shippable_skusObject



19
20
21
# File 'app/jobs/solidus_bactracs/api/schedule_shipment_syncs_job.rb', line 19

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