Only load up variant overrides for relevant hubs

This commit is contained in:
Rob H
2020-04-11 17:33:16 +10:00
parent 14cf168e3b
commit 9a7e782102
3 changed files with 64 additions and 14 deletions

View File

@@ -51,7 +51,7 @@ module OpenFoodNetwork
@variant_scopers_by_distributor_id[distributor_id] ||=
OpenFoodNetwork::ScopeVariantToHub.new(
distributor_id,
report_variant_overrides[distributor_id]
report_variant_overrides[distributor_id] || {},
)
end
@@ -102,7 +102,10 @@ module OpenFoodNetwork
def report_variant_overrides
@report_variant_overrides ||=
Reports::VariantOverrides.new(order_permissions.visible_line_items).indexed
Reports::VariantOverrides.new(
line_items: order_permissions.visible_line_items,
distributor_ids: report_line_items.orders.result.select('DISTINCT distributor_id'),
).indexed
end
end
end

View File

@@ -3,8 +3,9 @@
module OpenFoodNetwork
module Reports
class VariantOverrides
def initialize(line_items)
def initialize(line_items:, distributor_ids:)
@line_items = line_items
@distributor_ids = distributor_ids
end
def indexed
@@ -15,11 +16,16 @@ module OpenFoodNetwork
private
attr_reader :line_items
attr_reader :line_items, :distributor_ids
def variant_overrides
VariantOverride.joins(:variant)
.where(spree_variants: { id: line_items.select(:variant_id) })
VariantOverride
.joins(:variant)
.preload(:variant)
.where(
hub_id: distributor_ids,
variant_id: line_items.select(:variant_id)
)
end
def hash_of_hashes

View File

@@ -3,29 +3,70 @@ require 'open_food_network/reports/variant_overrides'
module OpenFoodNetwork::Reports
describe VariantOverrides do
subject(:variant_overrides) { described_class.new(order.line_items) }
subject(:variant_overrides) do
described_class.new(
line_items: order.line_items,
distributor_ids: [distributor.id],
)
end
let(:distributor) { create(:distributor_enterprise) }
let(:order) do
create(:completed_order_with_totals, line_items_count: 1,
distributor: distributor)
end
let(:variant) { order.line_items.first.variant }
let(:line_item) { order.line_items.first }
let!(:variant_override) do
create(
:variant_override,
hub: distributor,
variant: variant,
hub: vo_distributor,
variant: vo_variant,
)
end
describe '#indexed' do
let(:result) { variant_overrides.indexed }
it 'indexes variant override mappings by distributor id' do
expect(variant_overrides.indexed).to eq(
distributor.id => { variant => variant_override }
)
context 'when variant overrides exist for variants of specified line items' do
let(:vo_variant) { line_item.variant }
context 'when variant overrides apply to one of the specified distributors' do
let(:vo_distributor) { distributor }
it 'they are included in the mapping' do
expect(result).to eq(
distributor.id => { line_item.variant => variant_override }
)
end
end
context 'when variant overrides don\'t apply to one of the specified distributors' do
let(:vo_distributor) { create(:distributor_enterprise) }
it 'they are not included in the mapping' do
expect(result).to eq({})
end
end
end
context 'when variant overrides exist for other variants' do
let(:vo_variant) { create(:variant) }
context 'when variant overrides apply to one of the specified distributors' do
let(:vo_distributor) { distributor }
it 'they are not included in the mapping' do
expect(result).to eq({})
end
end
context 'when variant overrides don\'t apply to one of the specified distributors' do
let(:vo_distributor) { create(:distributor_enterprise) }
it 'they are not included in the mapping' do
expect(result).to eq({})
end
end
end
end
end