diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index 4b416c99d8..fe9014406d 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -21,6 +21,9 @@ angular.module('order_cycle', ['ngResource']) $scope.incomingExchangesVariants = -> OrderCycle.incomingExchangesVariants() + $scope.exchangeDirection = (exchange) -> + OrderCycle.exchangeDirection(exchange) + $scope.participatingEnterprises = -> $scope.enterprises[id] for id in OrderCycle.participatingEnterpriseIds() @@ -82,6 +85,9 @@ angular.module('order_cycle', ['ngResource']) $scope.incomingExchangesVariants = -> OrderCycle.incomingExchangesVariants() + $scope.exchangeDirection = (exchange) -> + OrderCycle.exchangeDirection(exchange) + $scope.participatingEnterprises = -> $scope.enterprises[id] for id in OrderCycle.participatingEnterpriseIds() @@ -141,6 +147,9 @@ angular.module('order_cycle', ['ngResource']) numActiveVariants++ for id, active of exchange.variants when active numActiveVariants + exchangeDirection: (exchange) -> + if this.order_cycle.incoming_exchanges.indexOf(exchange) == -1 then 'outgoing' else 'incoming' + toggleProducts: (exchange) -> exchange.showProducts = !exchange.showProducts diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 7e2f040a97..80bfe52ba7 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -17,6 +17,7 @@ describe 'OrderCycle controllers', -> exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').andReturn('variants selected') productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').andReturn('product supplied') variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').andReturn('variant supplied') + exchangeDirection: jasmine.createSpy('exchangeDirection').andReturn('exchange direction') toggleProducts: jasmine.createSpy('toggleProducts') addSupplier: jasmine.createSpy('addSupplier') addDistributor: jasmine.createSpy('addDistributor') @@ -66,6 +67,10 @@ describe 'OrderCycle controllers', -> expect(scope.variantSuppliedToOrderCycle('variant')).toEqual('variant supplied') expect(OrderCycle.variantSuppliedToOrderCycle).toHaveBeenCalledWith('variant') + it 'Delegates exchangeDirection to OrderCycle', -> + expect(scope.exchangeDirection('exchange')).toEqual('exchange direction') + expect(OrderCycle.exchangeDirection).toHaveBeenCalledWith('exchange') + it 'Finds enterprises participating in the order cycle', -> scope.enterprises = 1: {id: 1, name: 'Eaterprises'} @@ -141,6 +146,7 @@ describe 'OrderCycle controllers', -> exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').andReturn('variants selected') productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').andReturn('product supplied') variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').andReturn('variant supplied') + exchangeDirection: jasmine.createSpy('exchangeDirection').andReturn('exchange direction') toggleProducts: jasmine.createSpy('toggleProducts') addSupplier: jasmine.createSpy('addSupplier') addDistributor: jasmine.createSpy('addDistributor') @@ -189,6 +195,10 @@ describe 'OrderCycle controllers', -> expect(scope.variantSuppliedToOrderCycle('variant')).toEqual('variant supplied') expect(OrderCycle.variantSuppliedToOrderCycle).toHaveBeenCalledWith('variant') + it 'Delegates exchangeDirection to OrderCycle', -> + expect(scope.exchangeDirection('exchange')).toEqual('exchange direction') + expect(OrderCycle.exchangeDirection).toHaveBeenCalledWith('exchange') + it 'Finds enterprises participating in the order cycle', -> scope.enterprises = 1: {id: 1, name: 'Eaterprises'} @@ -350,6 +360,19 @@ describe 'OrderCycle services', -> result = OrderCycle.exchangeSelectedVariants({variants: {1: true, 2: false, 3: true}}) expect(result).toEqual(2) + describe 'fetching the direction for an exchange', -> + it 'returns "incoming" for incoming exchanges', -> + exchange = {id: 1} + OrderCycle.order_cycle.incoming_exchanges = [exchange] + OrderCycle.order_cycle.outgoing_exchanges = [] + expect(OrderCycle.exchangeDirection(exchange)).toEqual 'incoming' + + it 'returns "outgoing" for outgoing exchanges', -> + exchange = {id: 1} + OrderCycle.order_cycle.incoming_exchanges = [] + OrderCycle.order_cycle.outgoing_exchanges = [exchange] + expect(OrderCycle.exchangeDirection(exchange)).toEqual 'outgoing' + describe 'toggling products', -> exchange = null