Perform refresh of products cache when supplier fee is changed

This commit is contained in:
Rohan Mitchell
2016-01-29 15:59:17 +11:00
parent fbedff4eca
commit 3bcd3257a1
2 changed files with 69 additions and 0 deletions

View File

@@ -50,6 +50,7 @@ module OpenFoodNetwork
def self.enterprise_fee_changed(enterprise_fee)
refresh_supplier_fee enterprise_fee
refresh_coordinator_fee enterprise_fee
refresh_distributor_fee enterprise_fee
end
@@ -76,6 +77,19 @@ module OpenFoodNetwork
end
def self.refresh_supplier_fee(enterprise_fee)
outgoing_exchanges = Set.new
incoming_exchanges_for_enterprise_fee(enterprise_fee).each do |exchange|
outgoing_exchanges.merge outgoing_exchanges_with_variants(exchange.order_cycle, exchange.variant_ids)
end
outgoing_exchanges.each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
def self.refresh_coordinator_fee(enterprise_fee)
enterprise_fee.order_cycles.each do |order_cycle|
order_cycle_changed order_cycle
@@ -96,6 +110,21 @@ module OpenFoodNetwork
end
def self.incoming_exchanges_for_enterprise_fee(enterprise_fee)
enterprise_fee.exchanges.incoming.
joins(:order_cycle).
merge(OrderCycle.dated).
merge(OrderCycle.not_closed)
end
def self.outgoing_exchanges_with_variants(order_cycle, variant_ids)
order_cycle.exchanges.outgoing.
joins(:exchange_variants).
where('exchange_variants.variant_id IN (?)', variant_ids)
end
def self.refresh_cache(distributor, order_cycle)
Delayed::Job.enqueue RefreshProductsCacheJob.new distributor.id, order_cycle.id
end

View File

@@ -174,6 +174,46 @@ module OpenFoodNetwork
let(:oc) { create(:open_order_cycle, coordinator: c) }
describe "updating exchanges when it's a supplier fee" do
let(:v) { create(:variant) }
let!(:ex1) { create(:exchange, order_cycle: oc, sender: s, receiver: c, incoming: true, variants: [v], enterprise_fees: [ef]) }
let!(:ex2) { create(:exchange, order_cycle: oc, sender: c, receiver: d1, incoming: false, variants: [v]) }
let!(:ex3) { create(:exchange, order_cycle: oc, sender: c, receiver: d2, incoming: false, variants: []) }
before { ef.reload }
describe "updating distributions that include one of the supplier's variants" do
it "does not update undated order cycles" do
oc.update_attributes! orders_open_at: nil, orders_close_at: nil
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
expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once
ProductsCache.enterprise_fee_changed ef
end
it "updates open order cycles" do
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
expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).never
ProductsCache.enterprise_fee_changed ef
end
end
it "doesn't update distributions that don't include any of the supplier's variants" do
expect(ProductsCache).to receive(:refresh_cache).with(d2, oc).never
ProductsCache.enterprise_fee_changed ef
end
end
it "updates order cycles when it's a coordinator fee" do
ef_coord
expect(ProductsCache).to receive(:order_cycle_changed).with(oc).once