Make total number of products in exchange work again.

Currently we are just loading the products from the server and count them.
This can be improved easily in two ways:
- we can switch this to a specific product count call to the server so that we dont load all products all the time
- or we paginate the products result and fetch the total_number from the payload of the first page.
This commit is contained in:
luisramos0
2019-11-11 13:21:33 +00:00
parent 2b3bc6d1ff
commit 3223bf930d
6 changed files with 45 additions and 34 deletions

View File

@@ -1,5 +1,23 @@
angular.module('admin.orderCycles').controller 'AdminOrderCycleIncomingCtrl', ($scope, $controller, $location, Enterprise, ocInstance) ->
$controller('AdminOrderCycleExchangesCtrl', {$scope: $scope, ocInstance: ocInstance, $location: $location})
$scope.enterpriseTotalVariants = (enterprise) ->
Enterprise.totalVariants(enterprise)
$scope.view = 'incoming'
$scope.exchangeTotalVariants = (exchange) ->
return unless this.enterprises? && this.enterprises[exchange.enterprise_id]?
enterprise = this.enterprises[exchange.enterprise_id]
return enterprise.numVariants if enterprise.numVariants?
$scope.loadExchangeProducts(this, 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

View File

@@ -44,10 +44,16 @@ angular.module('admin.orderCycles')
$scope.removeDistributionOfVariant = (variant_id) ->
OrderCycle.removeDistributionOfVariant(variant_id)
# Load exchange data
initPanel = (scope) ->
Product.index {exchange_id: scope.exchange.id, enterprise_id: scope.exchange.enterprise_id}, (products) ->
scope.enterprises[scope.exchange.enterprise_id].supplied_products = products
# Load exchange products
$scope.loadExchangeProducts = (scope, exchange) ->
return if scope.enterprises[exchange.enterprise_id].supplied_products_fetched?
scope.enterprises[exchange.enterprise_id].supplied_products_fetched = true
Product.index {exchange_id: exchange.id, enterprise_id: exchange.enterprise_id}, (products) ->
scope.enterprises[exchange.enterprise_id].supplied_products = products
initPanel = (scope, exchange) ->
scope.loadExchangeProducts(scope, scope.exchange)
# Register listener to capture first toggle open of the products panel
exchangeProdutsInitialized = []

View File

@@ -35,13 +35,4 @@ angular.module('admin.orderCycles').factory('Enterprise', ($resource) ->
variant.id for variant in product.variants
else
[product.master_id]
totalVariants: (enterprise) ->
numVariants = 0
if enterprise
counts = for product in enterprise.supplied_products
numVariants += if product.variants.length == 0 then 1 else product.variants.length
numVariants
})

View File

@@ -3,7 +3,7 @@
%td.products.panel-toggle.text-center{ name: "products" }
{{ exchangeSelectedVariants(exchange) }} /
- if type == 'supplier'
{{ enterpriseTotalVariants(enterprises[exchange.enterprise_id]) }}
{{ exchangeTotalVariants(exchange) }}
- else
{{ (incomingExchangeVariantsFor(exchange.enterprise_id)).length }}
= t('.selected')

View File

@@ -13,14 +13,23 @@ describe 'AdminOrderCycleIncomingCtrl', ->
location =
absUrl: ->
'example.com/admin/order_cycles/27/edit'
Enterprise =
totalVariants: jasmine.createSpy('totalVariants').and.returnValue('variants total')
ocInstance = {}
module('admin.orderCycles')
inject ($controller) ->
ctrl = $controller 'AdminOrderCycleIncomingCtrl', {$scope: scope, $location: location, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee, ocInstance: ocInstance}
it 'Delegates totalVariants to Enterprise', ->
expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total')
expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise')
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)

View File

@@ -56,16 +56,3 @@ describe 'Enterprise service', ->
master_id: 1
variants: [{id: 2}, {id: 3}]
expect(Enterprise.variantsOf(p)).toEqual [2, 3]
it 'counts total variants supplied by an enterprise', ->
enterprise =
supplied_products: [
{variants: []},
{variants: []},
{variants: [{}, {}, {}]}
]
expect(Enterprise.totalVariants(enterprise)).toEqual(5)
it 'returns zero when enterprise is null', ->
expect(Enterprise.totalVariants(null)).toEqual(0)