mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Rely on Settings and don't access internal struct.
This commit is contained in:
@@ -7,25 +7,29 @@ module ProductImport
|
||||
def initialize(decorated)
|
||||
super
|
||||
@products_reset_count = 0
|
||||
|
||||
settings = ProductImport::Settings.new(import_settings)
|
||||
@settings = settings.settings
|
||||
@updated_ids = settings.updated_ids
|
||||
@enterprises_to_reset = settings.enterprises_to_reset
|
||||
end
|
||||
|
||||
def call
|
||||
# For selected enterprises; set stock to zero for all products/inventory
|
||||
# that were not listed in the newly uploaded spreadsheet
|
||||
return unless data_for_stock_reset?
|
||||
|
||||
suppliers_to_reset_products = []
|
||||
suppliers_to_reset_inventories = []
|
||||
|
||||
settings = import_settings[:settings]
|
||||
|
||||
import_settings[:enterprises_to_reset].each do |enterprise_id|
|
||||
if settings['reset_all_absent'] &&
|
||||
enterprises_to_reset.each do |enterprise_id|
|
||||
if reset_all_absent? &&
|
||||
permission_by_id?(enterprise_id) &&
|
||||
!importing_into_inventory?
|
||||
suppliers_to_reset_products.push(Integer(enterprise_id))
|
||||
end
|
||||
|
||||
if settings['reset_all_absent'] &&
|
||||
if reset_all_absent? &&
|
||||
permission_by_id?(enterprise_id) &&
|
||||
importing_into_inventory?
|
||||
suppliers_to_reset_inventories.push(Integer(enterprise_id))
|
||||
@@ -38,7 +42,7 @@ module ProductImport
|
||||
'variant_overrides.hub_id IN (?) ' \
|
||||
'AND variant_overrides.id NOT IN (?)',
|
||||
suppliers_to_reset_inventories,
|
||||
import_settings[:updated_ids]
|
||||
updated_ids
|
||||
)
|
||||
@products_reset_count += relation.update_all(count_on_hand: 0)
|
||||
end
|
||||
@@ -53,17 +57,21 @@ module ProductImport
|
||||
'AND spree_variants.is_master = false ' \
|
||||
'AND spree_variants.deleted_at IS NULL',
|
||||
suppliers_to_reset_products,
|
||||
import_settings[:updated_ids]
|
||||
updated_ids
|
||||
)
|
||||
@products_reset_count += relation.update_all(count_on_hand: 0)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :settings, :updated_ids, :enterprises_to_reset
|
||||
|
||||
def data_for_stock_reset?
|
||||
import_settings[:settings] &&
|
||||
import_settings[:updated_ids] &&
|
||||
import_settings[:enterprises_to_reset]
|
||||
settings && updated_ids && enterprises_to_reset
|
||||
end
|
||||
|
||||
def reset_all_absent?
|
||||
settings['reset_all_absent']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,8 +6,20 @@ module ProductImport
|
||||
|
||||
def defaults(entry)
|
||||
@import_settings.key?(:settings) &&
|
||||
@import_settings[:settings][entry.supplier_id.to_s] &&
|
||||
@import_settings[:settings][entry.supplier_id.to_s]['defaults']
|
||||
settings[entry.supplier_id.to_s] &&
|
||||
settings[entry.supplier_id.to_s]['defaults']
|
||||
end
|
||||
|
||||
def settings
|
||||
@import_settings[:settings]
|
||||
end
|
||||
|
||||
def updated_ids
|
||||
@import_settings[:updated_ids]
|
||||
end
|
||||
|
||||
def enterprises_to_reset
|
||||
@import_settings[:enterprises_to_reset]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,4 +48,58 @@ describe ProductImport::Settings do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#settings' do
|
||||
context 'when settings are specified' do
|
||||
let(:import_settings) { { settings: { foo: 'bar' } } }
|
||||
|
||||
it 'returns them' do
|
||||
expect(settings.settings).to eq(foo: 'bar')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when settings are not specified' do
|
||||
let(:import_settings) { {} }
|
||||
|
||||
it 'returns nil' do
|
||||
expect(settings.settings).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#updated_ids' do
|
||||
context 'when updated_ids are specified' do
|
||||
let(:import_settings) { { updated_ids: [2] } }
|
||||
|
||||
it 'returns them' do
|
||||
expect(settings.updated_ids).to eq([2])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when updated_ids are not specified' do
|
||||
let(:import_settings) { {} }
|
||||
|
||||
it 'returns nil' do
|
||||
expect(settings.updated_ids).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#enterprises_to_reset' do
|
||||
context 'when enterprises_to_reset are specified' do
|
||||
let(:import_settings) { { enterprises_to_reset: [2] } }
|
||||
|
||||
it 'returns them' do
|
||||
expect(settings.enterprises_to_reset).to eq([2])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when enterprises_to_reset are not specified' do
|
||||
let(:import_settings) { {} }
|
||||
|
||||
it 'returns nil' do
|
||||
expect(settings.enterprises_to_reset).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user