Extract InventoryReset and Products strategies

Extract this strategy classes from ResetAbsent and move #reset there.
This commit is contained in:
Pau Perez
2018-09-25 16:47:04 +02:00
parent 8d7a11b0ac
commit a9444b8909
4 changed files with 138 additions and 44 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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