mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Add count variants endpoint and use it instead of loading exchange products
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
angular.module('admin.orderCycles').controller 'AdminOrderCycleIncomingCtrl', ($scope, $controller, $location, Enterprise, OrderCycle, ocInstance) ->
|
||||
angular.module('admin.orderCycles').controller 'AdminOrderCycleIncomingCtrl', ($scope, $controller, $location, Enterprise, OrderCycle, ExchangeProduct, ocInstance) ->
|
||||
$controller('AdminOrderCycleExchangesCtrl', {$scope: $scope, ocInstance: ocInstance, $location: $location})
|
||||
|
||||
$scope.view = 'incoming'
|
||||
@@ -9,18 +9,11 @@ angular.module('admin.orderCycles').controller 'AdminOrderCycleIncomingCtrl', ($
|
||||
enterprise = $scope.enterprises[exchange.enterprise_id]
|
||||
return enterprise.numVariants if enterprise.numVariants?
|
||||
|
||||
$scope.loadExchangeProducts(exchange)
|
||||
return unless enterprise.supplied_products?
|
||||
|
||||
enterprise.numVariants = $scope.countVariants(enterprise.supplied_products)
|
||||
|
||||
$scope.countVariants = (products) ->
|
||||
return 0 unless products
|
||||
|
||||
numVariants = 0
|
||||
for product in products
|
||||
numVariants += product.variants.length
|
||||
numVariants
|
||||
enterprise.numVariants = 0
|
||||
params = { exchange_id: exchange.id, enterprise_id: exchange.enterprise_id, order_cycle_id: $scope.order_cycle.id, incoming: true}
|
||||
ExchangeProduct.countVariants params, (variants_count) ->
|
||||
enterprise.numVariants = variants_count
|
||||
return enterprise.numVariants
|
||||
|
||||
$scope.addSupplier = ($event) ->
|
||||
$event.preventDefault()
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
angular.module('admin.orderCycles').factory('ExchangeProduct', ($resource) ->
|
||||
ExchangeProductResource = $resource('/api/exchanges/:exchange_id/products.json', {}, {
|
||||
'index':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
'index': { method: 'GET' }
|
||||
'variant_count': { method: 'GET', params: { action_name: "variant_count" }}
|
||||
})
|
||||
{
|
||||
ExchangeProductResource: ExchangeProductResource
|
||||
@@ -12,4 +11,8 @@ angular.module('admin.orderCycles').factory('ExchangeProduct', ($resource) ->
|
||||
ExchangeProductResource.index params, (data) =>
|
||||
@loaded = true
|
||||
(callback || angular.noop)(data.products)
|
||||
|
||||
countVariants: (params={}, callback=null) ->
|
||||
ExchangeProductResource.variant_count params, (data) =>
|
||||
(callback || angular.noop)(data.count)
|
||||
})
|
||||
|
||||
@@ -20,15 +20,30 @@ module Api
|
||||
load_data_from_other_params
|
||||
end
|
||||
|
||||
render_paginated_products products
|
||||
render_variant_count && return if params[:action_name] == "variant_count"
|
||||
|
||||
render_paginated_products paginated_products
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_variant_count
|
||||
render text: {
|
||||
count: Spree::Variant.
|
||||
not_master.
|
||||
where(product_id: products).
|
||||
count
|
||||
}.to_json
|
||||
end
|
||||
|
||||
def products
|
||||
ExchangeProductsRenderer.
|
||||
new(@order_cycle, spree_current_user).
|
||||
exchange_products(@incoming, @enterprise).
|
||||
exchange_products(@incoming, @enterprise)
|
||||
end
|
||||
|
||||
def paginated_products
|
||||
products.
|
||||
page(params[:page] || DEFAULT_PAGE).
|
||||
per(params[:per_page] || DEFAULT_PER_PAGE)
|
||||
end
|
||||
@@ -52,23 +67,23 @@ module Api
|
||||
@incoming = params[:incoming]
|
||||
end
|
||||
|
||||
def render_paginated_products(products)
|
||||
def render_paginated_products(paginated_products)
|
||||
serializer = ActiveModel::ArraySerializer.new(
|
||||
products,
|
||||
paginated_products,
|
||||
each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer,
|
||||
order_cycle: @order_cycle
|
||||
)
|
||||
|
||||
render text: {
|
||||
products: serializer,
|
||||
pagination: pagination_data(products)
|
||||
pagination: pagination_data(paginated_products)
|
||||
}.to_json
|
||||
end
|
||||
|
||||
def pagination_data(results)
|
||||
def pagination_data(paginated_products)
|
||||
{
|
||||
results: results.total_count,
|
||||
pages: results.num_pages,
|
||||
results: paginated_products.total_count,
|
||||
pages: paginated_products.num_pages,
|
||||
page: (params[:page] || DEFAULT_PAGE).to_i,
|
||||
per_page: (params[:per_page] || DEFAULT_PER_PAGE).to_i
|
||||
}
|
||||
|
||||
@@ -23,21 +23,6 @@ describe 'AdminOrderCycleIncomingCtrl', ->
|
||||
inject ($controller) ->
|
||||
ctrl = $controller 'AdminOrderCycleIncomingCtrl', {$scope: scope, $location: location, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee, ocInstance: ocInstance}
|
||||
|
||||
it 'counts total variants in a list of products', ->
|
||||
products = [
|
||||
{variants: [{}]},
|
||||
{variants: [{}]},
|
||||
{variants: [{}, {}, {}]}
|
||||
]
|
||||
|
||||
expect(scope.countVariants(products)).toEqual(5)
|
||||
|
||||
it 'returns zero when products list is null', ->
|
||||
expect(scope.countVariants(null)).toEqual(0)
|
||||
|
||||
it 'returns zero when products list is empty', ->
|
||||
expect(scope.countVariants([])).toEqual(0)
|
||||
|
||||
it 'adds order cycle suppliers', ->
|
||||
scope.new_supplier_id = 'new supplier id'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user