Adding permissions for outgoing hubs to see producers whose variants they are distributing, despite P-OC not existing

This commit is contained in:
Rob Harrington
2015-04-08 10:15:50 +10:00
parent 4a87798bb4
commit 24062e5585
2 changed files with 20 additions and 1 deletions

View File

@@ -62,6 +62,7 @@ module OpenFoodNetwork
managed_active = []
hubs_active = []
producers_active = []
if order_cycle
# TODO: remove this when permissions are all sorted out
# Any enterprises that I manage that are already in the order_cycle
@@ -72,9 +73,15 @@ module OpenFoodNetwork
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer)
active_exchanges = order_cycle.exchanges.outgoing.with_any_variant(variants)
hubs_active = active_exchanges.map(&:receiver_id)
# TODO: Remove this when all P-OC are sorted out
# Any producers of variants that hubs I manage are currently distributing in this OC
variants = Spree::Variant.joins(:exchanges).where("exchanges.receiver_id IN (?) AND exchanges.order_cycle_id = (?) AND exchanges.incoming = 'f'", managed_hubs_in(order_cycle), order_cycle).pluck(:id).uniq
products = Spree::Product.joins(:variants_including_master).where("spree_variants.id IN (?)", variants).pluck(:id).uniq
producers_active = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", products).pluck(:id).uniq
end
Enterprise.where(id: coordinator_permitted | managed_permitted | managed_active | hubs_permitted | producers_permitted | hubs_active)
Enterprise.where(id: coordinator_permitted | managed_permitted | hubs_permitted | producers_permitted | managed_active | hubs_active | producers_active )
end
end

View File

@@ -105,6 +105,18 @@ module OpenFoodNetwork
expect(enterprises).to_not include producer, coordinator
end
end
context "and distributes variants distributed by an unmanaged and unpermitted producer" do
let!(:ex_outgoing) { create(:exchange, order_cycle: oc, sender: coordinator, receiver: hub, incoming: false) }
before { ex_outgoing.variants << create(:variant, product: create(:product, supplier: producer)) }
# TODO: update this when we are confident about P-OCs
it "returns that producer as well" do
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include producer, hub
expect(enterprises).to_not include coordinator
end
end
end
end