mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Perform refresh of products cache for variant override change
This commit is contained in:
@@ -29,6 +29,9 @@ module OpenFoodNetwork
|
||||
|
||||
|
||||
def self.variant_override_changed(variant_override)
|
||||
exchanges_featuring_variants(variant_override.variant, distributor: variant_override.hub).each do |exchange|
|
||||
refresh_cache exchange.receiver, exchange.order_cycle
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -38,13 +41,17 @@ module OpenFoodNetwork
|
||||
|
||||
private
|
||||
|
||||
def self.exchanges_featuring_variants(variants)
|
||||
Exchange.
|
||||
def self.exchanges_featuring_variants(variants, distributor: nil)
|
||||
exchanges = Exchange.
|
||||
outgoing.
|
||||
with_any_variant(variants).
|
||||
joins(:order_cycle).
|
||||
merge(OrderCycle.dated).
|
||||
merge(OrderCycle.not_closed)
|
||||
|
||||
exchanges = exchanges.to_enterprise(distributor) if distributor
|
||||
|
||||
exchanges
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -89,6 +89,25 @@ module OpenFoodNetwork
|
||||
end
|
||||
end
|
||||
|
||||
describe "when a variant override changes" do
|
||||
let(:variant) { create(:variant) }
|
||||
let(:d1) { create(:distributor_enterprise) }
|
||||
let(:d2) { create(:distributor_enterprise) }
|
||||
let!(:vo) { create(:variant_override, variant: variant, hub: d1) }
|
||||
let!(:oc) { create(:open_order_cycle, distributors: [d1, d2], variants: [variant]) }
|
||||
|
||||
it "refreshes the distributions that the variant override affects" do
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once
|
||||
ProductsCache.variant_override_changed vo
|
||||
end
|
||||
|
||||
it "does not refresh other distributors of the variant" do
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(d2, oc).never
|
||||
ProductsCache.variant_override_changed vo
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "refreshing the cache" do
|
||||
let(:distributor) { double(:distributor, id: 123) }
|
||||
let(:order_cycle) { double(:order_cycle, id: 456) }
|
||||
|
||||
Reference in New Issue
Block a user