diff --git a/app/services/variant_overrides.rb b/app/services/variant_overrides.rb new file mode 100644 index 0000000000..c33f67484d --- /dev/null +++ b/app/services/variant_overrides.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +class VariantOverrides + def initialize(line_items:, distributor_ids:) + @line_items = line_items + @distributor_ids = distributor_ids + end + + def indexed + variant_overrides.each_with_object(hash_of_hashes) do |variant_override, indexed| + indexed[variant_override.hub_id][variant_override.variant] = variant_override + end + end + + private + + attr_reader :line_items, :distributor_ids + + def variant_overrides + VariantOverride + .joins(:variant) + .preload(:variant) + .where( + hub_id: distributor_ids, + variant_id: line_items.select(:variant_id) + ) + end + + def hash_of_hashes + Hash.new { |h, k| h[k] = {} } + end +end diff --git a/lib/open_food_network/orders_and_fulfillments_report.rb b/lib/open_food_network/orders_and_fulfillments_report.rb index 49414c72ef..9e724416c2 100644 --- a/lib/open_food_network/orders_and_fulfillments_report.rb +++ b/lib/open_food_network/orders_and_fulfillments_report.rb @@ -1,5 +1,4 @@ require "open_food_network/reports/line_items" -require "open_food_network/reports/variant_overrides" require "open_food_network/orders_and_fulfillments_report/supplier_totals_report" require "open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report" require "open_food_network/orders_and_fulfillments_report/distributor_totals_by_supplier_report" @@ -102,7 +101,7 @@ module OpenFoodNetwork def report_variant_overrides @report_variant_overrides ||= - Reports::VariantOverrides.new( + VariantOverrides.new( line_items: order_permissions.visible_line_items, distributor_ids: report_line_items.orders.result.select('DISTINCT distributor_id'), ).indexed diff --git a/lib/open_food_network/reports/variant_overrides.rb b/lib/open_food_network/reports/variant_overrides.rb deleted file mode 100644 index 28679e016c..0000000000 --- a/lib/open_food_network/reports/variant_overrides.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -module OpenFoodNetwork - module Reports - class VariantOverrides - def initialize(line_items:, distributor_ids:) - @line_items = line_items - @distributor_ids = distributor_ids - end - - def indexed - variant_overrides.each_with_object(hash_of_hashes) do |variant_override, indexed| - indexed[variant_override.hub_id][variant_override.variant] = variant_override - end - end - - private - - attr_reader :line_items, :distributor_ids - - def variant_overrides - VariantOverride - .joins(:variant) - .preload(:variant) - .where( - hub_id: distributor_ids, - variant_id: line_items.select(:variant_id) - ) - end - - def hash_of_hashes - Hash.new { |h, k| h[k] = {} } - end - end - end -end diff --git a/spec/lib/open_food_network/reports/variant_overrides_spec.rb b/spec/lib/open_food_network/reports/variant_overrides_spec.rb deleted file mode 100644 index b9c86bb95a..0000000000 --- a/spec/lib/open_food_network/reports/variant_overrides_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -require 'spec_helper' -require 'open_food_network/reports/variant_overrides' - -module OpenFoodNetwork::Reports - describe VariantOverrides do - 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(:line_item) { order.line_items.first } - let!(:variant_override) do - create( - :variant_override, - hub: vo_distributor, - variant: vo_variant, - ) - end - - describe '#indexed' do - let(:result) { variant_overrides.indexed } - - 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 -end diff --git a/spec/services/variant_overrides_spec.rb b/spec/services/variant_overrides_spec.rb new file mode 100644 index 0000000000..33c5b69363 --- /dev/null +++ b/spec/services/variant_overrides_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper' + +describe VariantOverrides do + 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(:line_item) { order.line_items.first } + let!(:variant_override) do + create( + :variant_override, + hub: vo_distributor, + variant: vo_variant, + ) + end + + describe '#indexed' do + let(:result) { variant_overrides.indexed } + + 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