From a9444b8909b7665545c689876989bf60825ced8d Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 25 Sep 2018 16:47:04 +0200 Subject: [PATCH] Extract InventoryReset and Products strategies Extract this strategy classes from ResetAbsent and move #reset there. --- app/models/product_import/inventory_reset.rb | 25 +++++++ app/models/product_import/products_reset.rb | 29 +++++++ app/models/product_import/reset_absent.rb | 53 +++++++------ .../product_import/reset_absent_spec.rb | 75 +++++++++++++------ 4 files changed, 138 insertions(+), 44 deletions(-) create mode 100644 app/models/product_import/inventory_reset.rb create mode 100644 app/models/product_import/products_reset.rb diff --git a/app/models/product_import/inventory_reset.rb b/app/models/product_import/inventory_reset.rb new file mode 100644 index 0000000000..136a5856ee --- /dev/null +++ b/app/models/product_import/inventory_reset.rb @@ -0,0 +1,25 @@ +module ProductImport + class InventoryReset + def initialize(updated_ids, supplier_ids) + @updated_ids = updated_ids + @supplier_ids = supplier_ids + end + + def reset + relation.update_all(count_on_hand: 0) + end + + private + + attr_reader :updated_ids, :supplier_ids + + def relation + VariantOverride.where( + 'variant_overrides.hub_id IN (?) ' \ + 'AND variant_overrides.id NOT IN (?)', + supplier_ids, + updated_ids + ) + end + end +end diff --git a/app/models/product_import/products_reset.rb b/app/models/product_import/products_reset.rb new file mode 100644 index 0000000000..fd93c3b684 --- /dev/null +++ b/app/models/product_import/products_reset.rb @@ -0,0 +1,29 @@ +module ProductImport + class ProductsReset + def initialize(updated_ids, supplier_ids) + @updated_ids = updated_ids + @supplier_ids = supplier_ids + end + + def reset + relation.update_all(count_on_hand: 0) + end + + private + + attr_reader :updated_ids, :supplier_ids + + def relation + Spree::Variant + .joins(:product) + .where( + 'spree_products.supplier_id IN (?) ' \ + 'AND spree_variants.id NOT IN (?) ' \ + 'AND spree_variants.is_master = false ' \ + 'AND spree_variants.deleted_at IS NULL', + supplier_ids, + updated_ids + ) + end + end +end diff --git a/app/models/product_import/reset_absent.rb b/app/models/product_import/reset_absent.rb index f3d08456d4..8925c2386c 100644 --- a/app/models/product_import/reset_absent.rb +++ b/app/models/product_import/reset_absent.rb @@ -27,33 +27,40 @@ module ProductImport end end - if @suppliers_to_reset_inventories.present? - relation = VariantOverride - .where( - 'variant_overrides.hub_id IN (?) ' \ - 'AND variant_overrides.id NOT IN (?)', - @suppliers_to_reset_inventories, - settings.updated_ids - ) - @products_reset_count += relation.update_all(count_on_hand: 0) - nil - elsif @suppliers_to_reset_products.present? - relation = Spree::Variant - .joins(:product) - .where( - 'spree_products.supplier_id IN (?) ' \ - 'AND spree_variants.id NOT IN (?) ' \ - 'AND spree_variants.is_master = false ' \ - 'AND spree_variants.deleted_at IS NULL', - @suppliers_to_reset_products, - settings.updated_ids - ) - @products_reset_count += relation.update_all(count_on_hand: 0) - end + reset_stock if suppliers_to_reset? end private attr_reader :settings + + def reset_stock + @products_reset_count += strategy.reset + end + + def strategy + strategy_factory.new(settings.updated_ids, supplier_ids) + end + + def strategy_factory + if @suppliers_to_reset_inventories.present? + ProductImport::InventoryReset + elsif @suppliers_to_reset_products.present? + ProductImport::ProductsReset + end + end + + def supplier_ids + if @suppliers_to_reset_inventories.present? + @suppliers_to_reset_inventories + elsif @suppliers_to_reset_products.present? + @suppliers_to_reset_products + end + end + + def suppliers_to_reset? + @suppliers_to_reset_inventories.present? || + @suppliers_to_reset_products.present? + end end end diff --git a/spec/models/product_import/reset_absent_spec.rb b/spec/models/product_import/reset_absent_spec.rb index 1d50e65a7d..6ebe7db206 100644 --- a/spec/models/product_import/reset_absent_spec.rb +++ b/spec/models/product_import/reset_absent_spec.rb @@ -94,8 +94,8 @@ describe ProductImport::ResetAbsent do .to receive(:importing_into_inventory?) { true } end - it 'returns nil' do - expect(reset_absent.call).to be_nil + it 'returns the number of products reset' do + expect(reset_absent.call).to eq(1) end it 'resets the inventories of the specified suppliers' do @@ -139,30 +139,63 @@ describe ProductImport::ResetAbsent do end describe '#products_reset_count' do - let(:variant) { create(:variant) } - let(:enterprise_id) { variant.product.supplier_id } + context 'and importing into inventory' do + let(:variant) { create(:variant) } + let(:enterprise) { variant.product.supplier } + let(:variant_override) do + create(:variant_override, variant: variant, hub: enterprise) + end - before do - allow(entry_processor) - .to receive(:permission_by_id?).with(enterprise_id.to_s) { true } + let(:settings) do + instance_double( + ProductImport::Settings, + updated_ids: [0], + enterprises_to_reset: [enterprise.id.to_s] + ) + end - allow(entry_processor) - .to receive(:importing_into_inventory?) { false } + before do + variant_override + + allow(entry_processor) + .to receive(:permission_by_id?).with(enterprise.id.to_s) { true } + end + + before do + allow(entry_processor) + .to receive(:importing_into_inventory?) { true } + end + + it 'returns the number of reset variant overrides' do + reset_absent.call + expect(reset_absent.products_reset_count).to eq(1) + end end - let(:settings) do - instance_double( - ProductImport::Settings, - reset_all_absent?: true, - data_for_stock_reset?: true, - updated_ids: [0], - enterprises_to_reset: [enterprise_id.to_s] - ) - end + context 'and not importing into inventory' do + let(:variant) { create(:variant) } + let(:enterprise_id) { variant.product.supplier_id } - it 'returns the number of reset products or variants' do - reset_absent.call - expect(reset_absent.products_reset_count).to eq(2) + before do + allow(entry_processor) + .to receive(:permission_by_id?).with(enterprise_id.to_s) { true } + + allow(entry_processor) + .to receive(:importing_into_inventory?) { false } + end + + let(:settings) do + instance_double( + ProductImport::Settings, + updated_ids: [0], + enterprises_to_reset: [enterprise_id.to_s] + ) + end + + it 'returns the number of reset products or variants' do + reset_absent.call + expect(reset_absent.products_reset_count).to eq(2) + end end end end