diff --git a/lib/open_food_network/products_cache.rb b/lib/open_food_network/products_cache.rb index 639076fb82..5013f5b37a 100644 --- a/lib/open_food_network/products_cache.rb +++ b/lib/open_food_network/products_cache.rb @@ -51,6 +51,7 @@ module OpenFoodNetwork def self.enterprise_fee_changed(enterprise_fee) refresh_coordinator_fee enterprise_fee + refresh_distributor_fee enterprise_fee end @@ -82,6 +83,19 @@ module OpenFoodNetwork end + def self.refresh_distributor_fee(enterprise_fee) + enterprise_fee.exchange_fees. + joins(:exchange => :order_cycle). + merge(Exchange.outgoing). + merge(OrderCycle.dated). + merge(OrderCycle.not_closed). + each do |exf| + + refresh_cache exf.exchange.receiver, exf.exchange.order_cycle + end + end + + def self.refresh_cache(distributor, order_cycle) Delayed::Job.enqueue RefreshProductsCacheJob.new distributor.id, order_cycle.id end diff --git a/spec/lib/open_food_network/products_cache_spec.rb b/spec/lib/open_food_network/products_cache_spec.rb index 26f3a9b844..c11565212e 100644 --- a/spec/lib/open_food_network/products_cache_spec.rb +++ b/spec/lib/open_food_network/products_cache_spec.rb @@ -179,6 +179,54 @@ module OpenFoodNetwork expect(ProductsCache).to receive(:order_cycle_changed).with(oc).once ProductsCache.enterprise_fee_changed ef_coord end + + + describe "updating exchanges when it's a distributor fee" do + let(:ex0) { create(:exchange, order_cycle: oc, sender: s, receiver: c, incoming: true, enterprise_fees: [ef]) } + let(:ex1) { create(:exchange, order_cycle: oc, sender: c, receiver: d1, incoming: false, enterprise_fees: [ef]) } + let(:ex2) { create(:exchange, order_cycle: oc, sender: c, receiver: d2, incoming: false, enterprise_fees: []) } + + describe "updating distributions that include the fee" do + it "does not update undated order cycles" do + oc.update_attributes! orders_open_at: nil, orders_close_at: nil + ex1 + expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).never + ProductsCache.enterprise_fee_changed ef + end + + it "updates upcoming order cycles" do + oc.update_attributes! orders_open_at: 1.week.from_now, orders_close_at: 2.weeks.from_now + ex1 + expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once + ProductsCache.enterprise_fee_changed ef + end + + it "updates open order cycles" do + ex1 + expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once + ProductsCache.enterprise_fee_changed ef + end + + it "does not update closed order cycles" do + oc.update_attributes! orders_open_at: 2.weeks.ago, orders_close_at: 1.week.ago + ex1 + expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).never + ProductsCache.enterprise_fee_changed ef + end + end + + it "doesn't update exchanges that don't include the fee" do + ex1; ex2 + expect(ProductsCache).to receive(:refresh_cache).with(d2, oc).never + ProductsCache.enterprise_fee_changed ef + end + + it "doesn't update incoming exchanges" do + ex0 + expect(ProductsCache).to receive(:refresh_cache).with(c, oc).never + ProductsCache.enterprise_fee_changed ef + end + end end describe "refreshing the cache" do