mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #3328 from luisramos0/2-0-pi-prod-reset
[Spree Upgrade] Phase 2 - Product Import - Adapt product_reset_strategy
This commit is contained in:
@@ -7,18 +7,16 @@ module ProductImport
|
||||
def reset(enterprise_ids)
|
||||
@enterprise_ids = enterprise_ids
|
||||
|
||||
if enterprise_ids.present?
|
||||
relation.update_all(count_on_hand: 0)
|
||||
else
|
||||
0
|
||||
end
|
||||
return 0 if enterprise_ids.blank?
|
||||
|
||||
reset_variants_count_on_hand(enterprise_variants_relation)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :excluded_items_ids, :enterprise_ids
|
||||
|
||||
def relation
|
||||
def enterprise_variants_relation
|
||||
relation = Spree::Variant
|
||||
.joins(:product)
|
||||
.where(
|
||||
@@ -30,5 +28,18 @@ module ProductImport
|
||||
|
||||
relation.where('spree_variants.id NOT IN (?)', excluded_items_ids)
|
||||
end
|
||||
|
||||
def reset_variants_count_on_hand(variants)
|
||||
updated_records_count = 0
|
||||
variants.each do |variant|
|
||||
updated_records_count += 1 if reset_variant_count_on_hand(variant)
|
||||
end
|
||||
updated_records_count
|
||||
end
|
||||
|
||||
def reset_variant_count_on_hand(variant)
|
||||
variant.count_on_hand = 0
|
||||
variant.count_on_hand.zero?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,41 +1,34 @@
|
||||
require 'spec_helper'
|
||||
|
||||
xdescribe ProductImport::ProductsResetStrategy do
|
||||
describe ProductImport::ProductsResetStrategy do
|
||||
let(:products_reset) { described_class.new(excluded_items_ids) }
|
||||
|
||||
describe '#reset' do
|
||||
let(:supplier_ids) { enterprise.id }
|
||||
let(:enterprise) { variant.product.supplier }
|
||||
let(:variant) { create(:variant, count_on_hand: 2) }
|
||||
let(:product) { create(:product) }
|
||||
let(:enterprise) { product.supplier }
|
||||
let(:variant) { product.variants.first }
|
||||
|
||||
before { variant.count_on_hand = 2 }
|
||||
|
||||
context 'when there are excluded_items_ids' do
|
||||
let(:excluded_items_ids) { [variant.id] }
|
||||
|
||||
context 'and supplier_ids is []' do
|
||||
let(:supplier_ids) { [] }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
it 'does not reset the variant.count_on_hand' do
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
expect(variant.reload.count_on_hand).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
let(:supplier_ids) { nil }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
it 'does not reset the variant.count_on_hand' do
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
expect(variant.reload.count_on_hand).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,9 +41,9 @@ xdescribe ProductImport::ProductsResetStrategy do
|
||||
it 'updates the count_on_hand of the non-excluded items' do
|
||||
non_excluded_variant = create(
|
||||
:variant,
|
||||
count_on_hand: 3,
|
||||
product: variant.product
|
||||
)
|
||||
non_excluded_variant.count_on_hand = 3
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(non_excluded_variant.reload.count_on_hand).to eq(0)
|
||||
end
|
||||
@@ -62,36 +55,37 @@ xdescribe ProductImport::ProductsResetStrategy do
|
||||
|
||||
context 'and supplier_ids is []' do
|
||||
let(:supplier_ids) { [] }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
it 'does not reset the variant.count_on_hand' do
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
expect(variant.reload.count_on_hand).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
let(:supplier_ids) { nil }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
it 'does not reset the variant.count_on_hand' do
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
expect(variant.reload.count_on_hand).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
context 'and supplier_ids is not nil' do
|
||||
it 'sets all count_on_hand to 0' do
|
||||
products_reset.reset(supplier_ids)
|
||||
updated_records_count = products_reset.reset(supplier_ids)
|
||||
expect(variant.reload.count_on_hand).to eq(0)
|
||||
expect(updated_records_count).to eq(1)
|
||||
end
|
||||
|
||||
context 'and there is an unresetable variant' do
|
||||
before do
|
||||
variant.stock_items = [] # this makes variant.count_on_hand raise an error
|
||||
end
|
||||
|
||||
it 'returns correct number of resetted variants' do
|
||||
expect { products_reset.reset(supplier_ids) }.to raise_error RuntimeError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -101,29 +95,18 @@ xdescribe ProductImport::ProductsResetStrategy do
|
||||
|
||||
context 'and supplier_ids is []' do
|
||||
let(:supplier_ids) { [] }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
it 'does not reset the variant.count_on_hand' do
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
expect(variant.reload.count_on_hand).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
let(:supplier_ids) { nil }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
it 'does not reset the variant.count_on_hand' do
|
||||
products_reset.reset(supplier_ids)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
expect(variant.reload.count_on_hand).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user