diff --git a/app/jobs/open_order_cycle_job.rb b/app/jobs/open_order_cycle_job.rb index b11660ad94..c2f7dc9469 100644 --- a/app/jobs/open_order_cycle_job.rb +++ b/app/jobs/open_order_cycle_job.rb @@ -57,14 +57,14 @@ class OpenOrderCycleJob < ApplicationJob catalog = DfcCatalog.load(dfc_user, catalog_url) catalog.apply_wholesale_values! - variants = Spree::Variant.where(id: catalog_links.pluck(:subject_id)) - importer = DfcCatalogImporter.new(variants, catalog) - importer.reset_absent_variants - catalog_links.each do |link| catalog_item = catalog.item(link.semantic_id) - SuppliedProductImporter.update_product(catalog_item, link.subject) if catalog_item + if catalog_item + SuppliedProductImporter.update_product(catalog_item, link.subject) + else + DfcCatalogImporter.reset_variant(link.subject) + end end end end diff --git a/app/services/dfc_catalog_importer.rb b/app/services/dfc_catalog_importer.rb index ab5837caee..16d81ac6d3 100644 --- a/app/services/dfc_catalog_importer.rb +++ b/app/services/dfc_catalog_importer.rb @@ -1,6 +1,14 @@ # frozen_string_literal: true class DfcCatalogImporter + def self.reset_variant(variant) + if variant.on_demand + variant.on_demand = false + else + variant.on_hand = 0 + end + end + attr_reader :catalog, :existing_variants def initialize(existing_variants, catalog) @@ -22,11 +30,7 @@ class DfcCatalogImporter # we don't want to lose the connection to previous orders. def reset_absent_variants absent_variants.map do |variant| - if variant.on_demand - variant.on_demand = false - else - variant.on_hand = 0 - end + self.class.reset_variant(variant) end end