mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Load producers of all or selected order cycles into shop
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
Darkswarm.filter 'products', (Matcher)->
|
||||
(products, text)->
|
||||
Darkswarm.filter 'products', (Matcher) ->
|
||||
(products, text) ->
|
||||
products ||= []
|
||||
text ?= ""
|
||||
products.filter (product)=>
|
||||
products.filter (product) =>
|
||||
propertiesToMatch = [product.name, product.supplier.name, product.primary_taxon.name]
|
||||
Matcher.match propertiesToMatch, text
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
Darkswarm.factory 'Dereferencer', ->
|
||||
new class Dereferencer
|
||||
dereference: (array, data) ->
|
||||
if array
|
||||
for object, i in array
|
||||
key = undefined
|
||||
key = object.id if object
|
||||
array[i] = data[key]
|
||||
@dereference_from(array, array, data)
|
||||
|
||||
dereference_from: (source, target, data) ->
|
||||
unreferenced = []
|
||||
if source && target
|
||||
for object, i in source
|
||||
key = if object then object.id else undefined
|
||||
if data.hasOwnProperty(key)
|
||||
target[i] = data[key]
|
||||
else
|
||||
delete target[i]
|
||||
unreferenced[i] = source[i]
|
||||
unreferenced
|
||||
|
||||
@@ -23,18 +23,14 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
|
||||
@dereferenceEnterprise enterprise
|
||||
|
||||
dereferenceEnterprise: (enterprise) ->
|
||||
# keep a backup of enterprise ids
|
||||
# keep unreferenced enterprise ids
|
||||
# in case we dereference again after adding more enterprises
|
||||
if enterprise.hub_references
|
||||
enterprise.hubs = enterprise.hub_references.slice()
|
||||
else
|
||||
enterprise.hub_references = enterprise.hubs.slice()
|
||||
if enterprise.producer_references
|
||||
enterprise.producers = enterprise.producer_references.slice()
|
||||
else
|
||||
enterprise.producer_references = enterprise.producers.slice()
|
||||
Dereferencer.dereference enterprise.hubs, @enterprises_by_id
|
||||
Dereferencer.dereference enterprise.producers, @enterprises_by_id
|
||||
hubs = enterprise.unreferenced_hubs || enterprise.hubs
|
||||
enterprise.unreferenced_hubs =
|
||||
Dereferencer.dereference_from hubs, enterprise.hubs, @enterprises_by_id
|
||||
producers = enterprise.unreferenced_producers || enterprise.producers
|
||||
enterprise.unreferenced_producers =
|
||||
Dereferencer.dereference_from producers, enterprise.producers, @enterprises_by_id
|
||||
|
||||
dereferenceTaxons: ->
|
||||
for enterprise in @enterprises
|
||||
|
||||
@@ -6,7 +6,12 @@ module InjectionHelper
|
||||
end
|
||||
|
||||
def inject_shop_enterprises
|
||||
inject_json_ams "enterprises", current_distributor.relatives_including_self.activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data
|
||||
ocs = if current_order_cycle
|
||||
[current_order_cycle]
|
||||
else
|
||||
OrderCycle.not_closed.with_distributor(current_distributor)
|
||||
end
|
||||
inject_json_ams "enterprises", current_distributor.relatives_and_oc_producers(ocs).activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data
|
||||
end
|
||||
|
||||
def inject_group_enterprises
|
||||
|
||||
@@ -220,6 +220,22 @@ class Enterprise < ActiveRecord::Base
|
||||
", self.id, self.id)
|
||||
end
|
||||
|
||||
def relatives_and_oc_producers(order_cycles)
|
||||
enterprise_ids = []
|
||||
order_cycles.each do |oc|
|
||||
enterprise_ids += oc.exchanges.incoming.pluck :sender_id
|
||||
end
|
||||
Enterprise.where("
|
||||
enterprises.id IN
|
||||
(SELECT child_id FROM enterprise_relationships WHERE enterprise_relationships.parent_id=?)
|
||||
OR enterprises.id IN
|
||||
(SELECT parent_id FROM enterprise_relationships WHERE enterprise_relationships.child_id=?)
|
||||
OR enterprises.id IN
|
||||
(?)
|
||||
OR enterprises.id = ?
|
||||
", id, id, enterprise_ids, id)
|
||||
end
|
||||
|
||||
def relatives_including_self
|
||||
Enterprise.where(id: relatives.pluck(:id) | [id])
|
||||
end
|
||||
|
||||
@@ -34,7 +34,7 @@ feature "As a consumer I want to shop with a distributor", js: true do
|
||||
|
||||
it "shows the producers for a distributor" do
|
||||
exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id)
|
||||
exchange.variants << variant
|
||||
add_variant_to_order_cycle(exchange, variant)
|
||||
|
||||
visit shop_path
|
||||
find("#tab_producers a").click
|
||||
@@ -68,7 +68,7 @@ feature "As a consumer I want to shop with a distributor", js: true do
|
||||
it "shows products after selecting an order cycle" do
|
||||
variant.update_attribute(:display_name, "kitten")
|
||||
variant.update_attribute(:display_as, "rabbit")
|
||||
exchange1.variants << variant ## add product to exchange
|
||||
add_variant_to_order_cycle(exchange1, variant)
|
||||
visit shop_path
|
||||
page.should_not have_content product.name
|
||||
Spree::Order.last.order_cycle.should == nil
|
||||
@@ -89,8 +89,8 @@ feature "As a consumer I want to shop with a distributor", js: true do
|
||||
it "shows the correct fees after selecting and changing an order cycle" do
|
||||
enterprise_fee = create(:enterprise_fee, amount: 1001)
|
||||
exchange2.enterprise_fees << enterprise_fee
|
||||
exchange2.variants << variant
|
||||
exchange1.variants << variant
|
||||
add_variant_to_order_cycle(exchange2, variant)
|
||||
add_variant_to_order_cycle(exchange1, variant)
|
||||
|
||||
# -- Selecting an order cycle
|
||||
visit shop_path
|
||||
@@ -116,8 +116,8 @@ feature "As a consumer I want to shop with a distributor", js: true do
|
||||
|
||||
describe "declining to clear the cart" do
|
||||
before do
|
||||
exchange2.variants << variant
|
||||
exchange1.variants << variant
|
||||
add_variant_to_order_cycle(exchange2, variant)
|
||||
add_variant_to_order_cycle(exchange1, variant)
|
||||
|
||||
visit shop_path
|
||||
select "turtles", from: "order_cycle_id"
|
||||
@@ -147,9 +147,9 @@ feature "As a consumer I want to shop with a distributor", js: true do
|
||||
|
||||
before do
|
||||
exchange.update_attribute :pickup_time, "frogs"
|
||||
exchange.variants << variant
|
||||
exchange.variants << variant1
|
||||
exchange.variants << variant2
|
||||
add_variant_to_order_cycle(exchange, variant)
|
||||
add_variant_to_order_cycle(exchange, variant1)
|
||||
add_variant_to_order_cycle(exchange, variant2)
|
||||
order.order_cycle = oc1
|
||||
end
|
||||
|
||||
|
||||
@@ -29,6 +29,15 @@ module ShopWorkflow
|
||||
end
|
||||
|
||||
def add_variant_to_order_cycle(exchange, variant)
|
||||
oc = exchange.order_cycle
|
||||
supplier = variant.product.supplier
|
||||
# An order cycle needs an incoming exchange for a supplier
|
||||
# before having its products. Otherwise the data will be inconsistent and
|
||||
# and not all needed enterprises are loaded into the shop page.
|
||||
if oc.exchanges.from_enterprise(supplier).incoming.empty?
|
||||
create(:exchange, order_cycle: oc, incoming: true,
|
||||
sender: supplier, receiver: oc.coordinator)
|
||||
end
|
||||
exchange.variants << variant
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user