Move products_reset_count to ResetAbsent

This commit is contained in:
Pau Perez
2018-09-24 13:49:27 +02:00
parent 3150741849
commit 663db47433
4 changed files with 99 additions and 21 deletions

View File

@@ -4,7 +4,9 @@
module ProductImport
class EntryProcessor
attr_reader :inventory_created, :inventory_updated, :products_created, :variants_created, :variants_updated, :products_reset_count, :supplier_products, :total_supplier_products, :import_settings
delegate :products_reset_count, to: :reset_absent
attr_reader :inventory_created, :inventory_updated, :products_created, :variants_created, :variants_updated, :supplier_products, :total_supplier_products, :import_settings
def initialize(importer, validator, import_settings, spreadsheet_data, editable_enterprises, import_time, updated_ids)
@importer = importer
@@ -20,7 +22,6 @@ module ProductImport
@products_created = 0
@variants_created = 0
@variants_updated = 0
@products_reset_count = 0
@supplier_products = {}
@total_supplier_products = 0
end
@@ -60,7 +61,11 @@ module ProductImport
end
def reset_absent_items
ResetAbsent.new(self).call
reset_absent.call
end
def reset_absent
@reset_absent ||= ResetAbsent.new(self)
end
def total_saved_count

View File

@@ -2,6 +2,13 @@ require 'delegate'
module ProductImport
class ResetAbsent < SimpleDelegator
attr_reader :products_reset_count
def initialize(decorated)
super
@products_reset_count = 0
end
def call
# For selected enterprises; set stock to zero for all products/inventory
# that were not listed in the newly uploaded spreadsheet
@@ -26,20 +33,29 @@ module ProductImport
end
unless suppliers_to_reset_inventories.empty?
@products_reset_count += VariantOverride.
where('variant_overrides.hub_id IN (?)
AND variant_overrides.id NOT IN (?)', suppliers_to_reset_inventories, import_settings[:updated_ids]).
update_all(count_on_hand: 0)
relation = VariantOverride
.where(
'variant_overrides.hub_id IN (?) ' \
'AND variant_overrides.id NOT IN (?)',
suppliers_to_reset_inventories,
import_settings[:updated_ids]
)
@products_reset_count += relation.update_all(count_on_hand: 0)
end
return if suppliers_to_reset_products.empty?
@products_reset_count += 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, import_settings[:updated_ids]).
update_all(count_on_hand: 0)
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,
import_settings[:updated_ids]
)
@products_reset_count += relation.update_all(count_on_hand: 0)
end
private

View File

@@ -22,7 +22,7 @@ describe ProductImport::EntryProcessor do
end
describe '#reset_absent_items' do
let(:reset_absent) { double(ProductImport::ResetAbsent, call: true) }
let(:reset_absent) { instance_double(ProductImport::ResetAbsent, call: true) }
before do
allow(ProductImport::ResetAbsent)
@@ -37,4 +37,21 @@ describe ProductImport::EntryProcessor do
.to have_received(:new).with(entry_processor)
end
end
describe '#products_reset_count' do
let(:reset_absent) { instance_double(ProductImport::ResetAbsent) }
before do
allow(ProductImport::ResetAbsent)
.to receive(:new)
.and_return(reset_absent)
allow(reset_absent).to receive(:products_reset_count)
end
it 'delegates to ResetAbsent' do
entry_processor.products_reset_count
expect(reset_absent).to have_received(:products_reset_count)
end
end
end

View File

@@ -51,36 +51,76 @@ describe ProductImport::ResetAbsent do
let(:import_settings) do
{
settings: { 'reset_all_absent' => true },
updated_ids: [1],
enterprises_to_reset: [2]
updated_ids: [0],
enterprises_to_reset: [enterprise.id]
}
end
before do
allow(entry_processor).to receive(:permission_by_id?).with(2) { true }
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise.id) { true }
end
context 'and not importing into inventory' do
let(:variant) { create(:variant) }
let(:enterprise) { variant.product.supplier }
before do
allow(entry_processor)
.to receive(:importing_into_inventory?) { false }
end
it 'returns true' do
expect(reset_absent.call).to eq(true)
it 'returns the number of products reset' do
expect(reset_absent.call).to eq(2)
end
end
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
variant_override
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise.id) { true }
end
before do
allow(entry_processor)
.to receive(:importing_into_inventory?) { true }
end
it 'returns true' do
expect(reset_absent.call).to eq(true)
it 'returns nil' do
expect(reset_absent.call).to be_nil
end
end
end
end
describe '#products_reset_count' do
let(:variant) { create(:variant) }
let(:enterprise_id) { variant.product.supplier_id }
before do
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise_id) { true }
end
let(:import_settings) do
{
settings: { 'reset_all_absent' => true },
updated_ids: [0],
enterprises_to_reset: [enterprise_id]
}
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