diff --git a/lib/open_food_network/permissions.rb b/lib/open_food_network/permissions.rb index 6284099845..1de4089f8e 100644 --- a/lib/open_food_network/permissions.rb +++ b/lib/open_food_network/permissions.rb @@ -186,12 +186,10 @@ module OpenFoodNetwork producers = granting(:add_to_order_cycle, to: [hub], scope: managed_producers_in(options[:order_cycle])) permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers) - # PLUS any of my producers variants that are already in an outgoing exchange of this hub, so things don't break + # PLUS any of my incoming producers' variants that are already in an outgoing exchange of this hub, so things don't break # TODO: Remove this when all P-OC are sorted out - active_variants = [] - options[:order_cycle].exchanges.outgoing.where(receiver_id: hub).limit(1).each do |exchange| - active_variants = exchange.variants.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer) - end + active_variants = Spree::Variant.joins(:exchanges, :product). + where("exchanges.receiver_id = (?) AND spree_products.supplier_id IN (?) AND incoming = 'f'", hub, managed_enterprises.is_primary_producer) Spree::Variant.where(id: permitted_variants | active_variants) end @@ -227,12 +225,7 @@ module OpenFoodNetwork granting_producers = granting(:add_to_order_cycle, to: [hub], scope: granted_producers) permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', granting_producers) - # PLUS any of my incoming producers' variants that are already in an outgoing exchange of this hub, so things don't break - # TODO: Remove this when all P-OC are sorted out - active_variants = Spree::Variant.joins(:exchanges, :product). - where('exchanges.receiver_id = (?) AND spree_products.supplier_id IN (?)', hub, managed_enterprises.is_primary_producer) - - Spree::Variant.where(id: permitted_variants | active_variants) + Spree::Variant.where(id: permitted_variants ) end end diff --git a/spec/lib/open_food_network/permissions_spec.rb b/spec/lib/open_food_network/permissions_spec.rb index bfadbd29b6..82c4260933 100644 --- a/spec/lib/open_food_network/permissions_spec.rb +++ b/spec/lib/open_food_network/permissions_spec.rb @@ -746,10 +746,9 @@ module OpenFoodNetwork before { ex.variants << v2 } - it "returns those variants that are in the exchange" do + it "does not return my variants" do visible = permissions.editable_variants_for_outgoing_exchanges_between(e1, e2, order_cycle: oc) - expect(visible).to_not include v1, v3 - expect(visible).to include v2 + expect(visible).to_not include v1, v2, v3 end end end