diff --git a/app/assets/javascripts/darkswarm/services/products.js.coffee b/app/assets/javascripts/darkswarm/services/products.js.coffee index 9b7b5e5993..59bd19efb4 100644 --- a/app/assets/javascripts/darkswarm/services/products.js.coffee +++ b/app/assets/javascripts/darkswarm/services/products.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory 'Products', (OrderCycleResource, OrderCycle, Shopfront, Dereferencer, Taxons, Properties, Cart, Variants) -> +Darkswarm.factory 'Products', (OrderCycleResource, OrderCycle, Shopfront, currentHub, Dereferencer, Taxons, Properties, Cart, Variants) -> new class Products constructor: -> @update() @@ -15,7 +15,8 @@ Darkswarm.factory 'Products', (OrderCycleResource, OrderCycle, Shopfront, Derefe @loading = false return - params['id'] = OrderCycle.order_cycle.order_cycle_id if params['id'] == undefined + params['id'] = order_cycle_id + params['distributor'] = currentHub.id OrderCycleResource.products params, (data)=> @products = [] unless load_more diff --git a/app/controllers/api/order_cycles_controller.rb b/app/controllers/api/order_cycles_controller.rb index 42eb6aa21e..685d72176b 100644 --- a/app/controllers/api/order_cycles_controller.rb +++ b/app/controllers/api/order_cycles_controller.rb @@ -8,8 +8,12 @@ module Api skip_authorization_check def products - products = OpenFoodNetwork::ProductsRenderer.new(current_distributor, current_order_cycle, params).products_json - # products = ::ProductsFilterer.new(current_distributor, current_customer, products_json).call # TBD + products = OpenFoodNetwork::ProductsRenderer.new( + distributor, + order_cycle, + customer, + params + ).products_json render json: products rescue OpenFoodNetwork::ProductsRenderer::NoProducts diff --git a/lib/open_food_network/products_renderer.rb b/lib/open_food_network/products_renderer.rb index bafbe27a59..48a35a1b11 100644 --- a/lib/open_food_network/products_renderer.rb +++ b/lib/open_food_network/products_renderer.rb @@ -12,7 +12,7 @@ module OpenFoodNetwork end def products_json - if products + if order_cycle && distributor && products enterprise_fee_calculator = EnterpriseFeeCalculator.new distributor, order_cycle ActiveModel::ArraySerializer.new(products, diff --git a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee index e9d2111d83..d1b9f38bb6 100644 --- a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee @@ -12,6 +12,7 @@ describe 'Products service', -> properties = null taxons = null Geo = {} + endpoint = "/api/order_cycles/1/products?distributor=1" beforeEach -> product = @@ -62,60 +63,60 @@ describe 'Products service', -> $httpBackend = _$httpBackend_ it "Fetches products from the backend on init", -> - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].test).toEqual "cats" it "dereferences suppliers", -> Shopfront.producers_by_id = {id: 9, name: "test"} - $httpBackend.expectGET("/api/order_cycles/1/products").respond([{supplier : {id: 9}, master: {}}]) + $httpBackend.expectGET(endpoint).respond([{supplier : {id: 9}, master: {}}]) $httpBackend.flush() expect(Products.products[0].supplier).toBe Shopfront.producers_by_id["9"] it "dereferences taxons", -> product.taxons = [2] - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].taxons[1]).toBe taxons[0] it "dereferences properties", -> product.properties_with_values = [1] - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].properties[1]).toBe properties[0] it "registers variants with Variants service", -> product.variants = [{id: 1}] - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].variants[0]).toBe Variants.variants[1] it "stores variant names", -> product.variants = [{id: 1, name_to_display: "one"}, {id: 2, name_to_display: "two"}] - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].variant_names).toEqual "one two " it "sets primaryImageOrMissing when no images are provided", -> - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].primaryImage).toBeUndefined() expect(Products.products[0].primaryImageOrMissing).toEqual "/assets/noimage/small.png" it "sets largeImage", -> - $httpBackend.expectGET("/api/order_cycles/1/products").respond([productWithImage]) + $httpBackend.expectGET(endpoint).respond([productWithImage]) $httpBackend.flush() expect(Products.products[0].largeImage).toEqual("foo.png") describe "determining the price to display for a product", -> it "displays the product price when the product does not have variants", -> - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].price).toEqual 11.00 it "displays the minimum variant price when the product has variants", -> product.variants = [{price: 22}, {price: 33}] - $httpBackend.expectGET("/api/order_cycles/1/products").respond([product]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].price).toEqual 22