From 3223bf930d48a5eb288b00cbe5bc24307f545b6c Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 11 Nov 2019 13:21:33 +0000 Subject: [PATCH] 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. --- .../controllers/incoming_controller.js.coffee | 22 +++++++++++++++++-- ...order_cycle_exchanges_controller.js.coffee | 14 ++++++++---- .../services/enterprise.js.coffee | 9 -------- .../order_cycles/_exchange_form.html.haml | 2 +- .../incoming_controller_spec.js.coffee | 19 +++++++++++----- .../services/enterprise_spec.js.coffee | 13 ----------- 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee index d3a1797643..266e03f569 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee @@ -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 diff --git a/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee index bf42a58a89..d439fd6089 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee @@ -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 = [] diff --git a/app/assets/javascripts/admin/order_cycles/services/enterprise.js.coffee b/app/assets/javascripts/admin/order_cycles/services/enterprise.js.coffee index 596b4ffa2a..96a303c4b3 100644 --- a/app/assets/javascripts/admin/order_cycles/services/enterprise.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/services/enterprise.js.coffee @@ -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 }) diff --git a/app/views/admin/order_cycles/_exchange_form.html.haml b/app/views/admin/order_cycles/_exchange_form.html.haml index da9cf926f9..e844ffc6aa 100644 --- a/app/views/admin/order_cycles/_exchange_form.html.haml +++ b/app/views/admin/order_cycles/_exchange_form.html.haml @@ -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') diff --git a/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee b/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee index 77f17eb419..45f56fe75d 100644 --- a/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee @@ -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) diff --git a/spec/javascripts/unit/admin/order_cycles/services/enterprise_spec.js.coffee b/spec/javascripts/unit/admin/order_cycles/services/enterprise_spec.js.coffee index 7455d7652c..959a9483bb 100644 --- a/spec/javascripts/unit/admin/order_cycles/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/admin/order_cycles/services/enterprise_spec.js.coffee @@ -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)