mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
Extract filter list fetching into a separate endpoint
This commit is contained in:
@@ -32,3 +32,4 @@ Darkswarm.controller "OrderCycleChangeCtrl", ($scope, $timeout, OrderCycle, Prod
|
||||
Products.update()
|
||||
Cart.reloadFinalisedLineItems()
|
||||
ChangeableOrdersAlert.reload()
|
||||
# Reload Filters from new endpoint after changing OC here
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Darkswarm.controller "ProductsCtrl", ($scope, $filter, $rootScope, Products, OrderCycle, FilterSelectorsService, Cart, Dereferencer, Taxons, Properties, currentHub, $timeout) ->
|
||||
Darkswarm.controller "ProductsCtrl", ($scope, $filter, $rootScope, Products, OrderCycle, OrderCycleResource, FilterSelectorsService, Cart, Dereferencer, Taxons, Properties, currentHub, $timeout) ->
|
||||
$scope.Products = Products
|
||||
$scope.Cart = Cart
|
||||
$scope.query = ""
|
||||
@@ -12,17 +12,31 @@ Darkswarm.controller "ProductsCtrl", ($scope, $filter, $rootScope, Products, Ord
|
||||
$scope.supplied_taxons = ->
|
||||
return $scope.memoized_taxons if $scope.memoized_taxons != undefined
|
||||
$scope.memoized_taxons = {}
|
||||
currentHub.supplied_taxons.map( (taxon) ->
|
||||
$scope.memoized_taxons[taxon.id] = Taxons.taxons_by_id[taxon.id]
|
||||
)
|
||||
|
||||
params = {
|
||||
id: OrderCycle.order_cycle.order_cycle_id,
|
||||
distributor: currentHub.id
|
||||
}
|
||||
OrderCycleResource.taxons params, (data)=>
|
||||
data.map( (taxon) ->
|
||||
$scope.memoized_taxons[taxon.id] = Taxons.taxons_by_id[taxon.id]
|
||||
)
|
||||
|
||||
$scope.memoized_taxons
|
||||
|
||||
$scope.supplied_properties = ->
|
||||
return $scope.memoized_properties if $scope.memoized_properties != undefined
|
||||
$scope.memoized_properties = {}
|
||||
currentHub.supplied_properties.map( (property) ->
|
||||
$scope.memoized_properties[property.id] = Properties.properties_by_id[property.id]
|
||||
)
|
||||
|
||||
params = {
|
||||
id: OrderCycle.order_cycle.order_cycle_id,
|
||||
distributor: currentHub.id
|
||||
}
|
||||
OrderCycleResource.properties params, (data)=>
|
||||
data.map( (property) ->
|
||||
$scope.memoized_properties[property.id] = Properties.properties_by_id[property.id]
|
||||
)
|
||||
|
||||
$scope.memoized_properties
|
||||
|
||||
$scope.loadMore = ->
|
||||
|
||||
@@ -6,4 +6,16 @@ Darkswarm.factory 'OrderCycleResource', ($resource) ->
|
||||
url: '/api/order_cycles/:id/products'
|
||||
params:
|
||||
id: '@id'
|
||||
'taxons':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
url: '/api/order_cycles/:id/taxons'
|
||||
params:
|
||||
id: '@id'
|
||||
'properties':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
url: '/api/order_cycles/:id/properties'
|
||||
params:
|
||||
id: '@id'
|
||||
})
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
module Api
|
||||
class OrderCyclesController < BaseController
|
||||
include EnterprisesHelper
|
||||
respond_to :json
|
||||
|
||||
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
|
||||
@@ -10,5 +13,43 @@ module Api
|
||||
rescue OpenFoodNetwork::ProductsRenderer::NoProducts
|
||||
render status: :not_found, json: ''
|
||||
end
|
||||
|
||||
def taxons
|
||||
taxons = Spree::Taxon.
|
||||
joins(:products).
|
||||
where(spree_products: { id: distributed_products(distributor, order_cycle, customer) }).
|
||||
select('DISTINCT spree_taxons.*')
|
||||
|
||||
render json: ActiveModel::ArraySerializer.new(taxons, each_serializer: Api::TaxonSerializer)
|
||||
end
|
||||
|
||||
def properties
|
||||
properties = Spree::Property.
|
||||
joins(:products).
|
||||
where(spree_products: { id: distributed_products(distributor, order_cycle, customer) }).
|
||||
select('DISTINCT spree_properties.*')
|
||||
|
||||
render json: ActiveModel::ArraySerializer.new(
|
||||
properties, each_serializer: Api::PropertySerializer
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def distributor
|
||||
Enterprise.find_by_id(params[:distributor])
|
||||
end
|
||||
|
||||
def order_cycle
|
||||
OrderCycle.find_by_id(params[:id])
|
||||
end
|
||||
|
||||
def customer
|
||||
@current_api_user.andand.customer_of(distributor) || nil
|
||||
end
|
||||
|
||||
def distributed_products(distributor, order_cycle, customer)
|
||||
OrderCycleDistributedProducts.new(distributor, order_cycle, customer).products_relation
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -46,6 +46,8 @@ Openfoodnetwork::Application.routes.draw do
|
||||
get :accessible, on: :collection
|
||||
|
||||
get :products, on: :member
|
||||
get :taxons, on: :member
|
||||
get :properties, on: :member
|
||||
end
|
||||
|
||||
resource :status do
|
||||
|
||||
Reference in New Issue
Block a user