From 2fc393037a734db515e1e19f5e94d486cf45b8ab Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 19 Feb 2025 15:05:57 +1100 Subject: [PATCH] Add order_cycle_per_item_enterprise_fee_applicators_for It retrieves all the per item fees associated with an order cycle and create the appropriate Fee Applicator. --- .../enterprise_fee_calculator.rb | 19 +++++++++ .../enterprise_fee_calculator_spec.rb | 40 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/open_food_network/enterprise_fee_calculator.rb b/lib/open_food_network/enterprise_fee_calculator.rb index 739725ce79..a4c0fb631a 100644 --- a/lib/open_food_network/enterprise_fee_calculator.rb +++ b/lib/open_food_network/enterprise_fee_calculator.rb @@ -97,6 +97,25 @@ module OpenFoodNetwork fees end + def order_cycle_per_item_enterprise_fee_applicators_for(variant) + fees = [] + + return fees unless @order_cycle && @distributor + + @order_cycle.exchanges.supplying_to(@distributor).each do |exchange| + exchange.enterprise_fees.per_item.each do |enterprise_fee| + fees << OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, variant, + exchange.role) + end + end + + @order_cycle.coordinator_fees.per_item.each do |enterprise_fee| + fees << OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, variant, 'coordinator') + end + + fees + end + private def load_enterprise_fees diff --git a/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb b/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb index 61ff704128..2a011f1a66 100644 --- a/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb +++ b/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb @@ -373,4 +373,44 @@ RSpec.describe OpenFoodNetwork::EnterpriseFeeCalculator do end end end + + describe "#order_cycle_per_item_enterprise_fee_applicators_for" do + subject { calculator.order_cycle_per_item_enterprise_fee_applicators_for(variant) } + + let(:calculator) { described_class.new(distributor, order_cycle) } + let(:distributor) { create(:distributor_enterprise) } + let(:order_cycle) { create(:order_cycle, distributors: [distributor]) } + let(:variant) { instance_double(Spree::Variant) } + + context "with no distributor" do + let(:order_cycle) { create(:order_cycle) } + let(:distributor) { nil } + + it "return an empty array" do + expect(subject).to eq([]) + end + end + + context "with no order cycle" do + let(:order_cycle) { nil } + + it "return an empty array" do + expect(subject).to eq([]) + end + end + + it "returns enterprise fee applicators including per item exchange fees" do + applicators = subject + exchanges = order_cycle.exchanges.supplying_to(distributor) + fees = exchanges.map { |ex| ex.enterprise_fees.per_item }.flatten + + expect(applicators.map(&:enterprise_fee)).to include(*fees) + end + + it "returns enterprise fee applicators including per item coordinator fees" do + applicators = subject + + expect(applicators.map(&:enterprise_fee)).to include(*order_cycle.coordinator_fees.per_item) + end + end end