diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index bc04130375..17eb6e823a 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -171,9 +171,7 @@ angular.module('order_cycle', ['ngResource']) this.order_cycle create: -> - this.removeInactiveExchanges() - - oc = new OrderCycle({order_cycle: this.order_cycle}) + oc = new OrderCycle({order_cycle: this.dataForSubmit()}) oc.$create (data) -> if data['success'] $window.location = '/admin/order_cycles' @@ -181,20 +179,30 @@ angular.module('order_cycle', ['ngResource']) console.log('fail') update: -> - this.removeInactiveExchanges() - - oc = new OrderCycle({order_cycle: this.order_cycle}) + oc = new OrderCycle({order_cycle: this.dataForSubmit()}) oc.$update {order_cycle_id: this.order_cycle.id}, (data) -> if data['success'] $window.location = '/admin/order_cycles' else console.log('fail') - removeInactiveExchanges: -> - this.order_cycle.incoming_exchanges = - (exchange for exchange in this.order_cycle.incoming_exchanges when exchange.active) - this.order_cycle.outgoing_exchanges = - (exchange for exchange in this.order_cycle.outgoing_exchanges when exchange.active) + dataForSubmit: -> + data = angular.extend({}, this.order_cycle) + data = this.removeInactiveExchanges(data) + data = this.translateCoordinatorFees(data) + data + + removeInactiveExchanges: (order_cycle)-> + order_cycle.incoming_exchanges = + (exchange for exchange in order_cycle.incoming_exchanges when exchange.active) + order_cycle.outgoing_exchanges = + (exchange for exchange in order_cycle.outgoing_exchanges when exchange.active) + order_cycle + + translateCoordinatorFees: (order_cycle)-> + order_cycle.coordinator_fee_ids = (fee.id for fee in order_cycle.coordinator_fees) + delete order_cycle.coordinator_fees + order_cycle }]) .factory('Enterprise', ['$resource', ($resource) -> diff --git a/spec/features/admin/order_cycles_spec.rb b/spec/features/admin/order_cycles_spec.rb index 3dbd500f78..56884a373f 100644 --- a/spec/features/admin/order_cycles_spec.rb +++ b/spec/features/admin/order_cycles_spec.rb @@ -42,13 +42,16 @@ feature %q{ scenario "creating an order cycle" do # Given coordinating, supplying and distributing enterprises with some products with variants - create(:distributor_enterprise, name: 'My coordinator') + coordinator = create(:distributor_enterprise, name: 'My coordinator') supplier = create(:supplier_enterprise, name: 'My supplier') product = create(:product, supplier: supplier) create(:variant, product: product) create(:variant, product: product) distributor = create(:distributor_enterprise, name: 'My distributor') + # And some enterprise fees + coordinator_fee = create(:enterprise_fee, enterprise: coordinator, name: 'Coord fee') + # When I go to the new order cycle page login_to_admin_section click_link 'Order Cycles' @@ -60,6 +63,10 @@ feature %q{ fill_in 'order_cycle_orders_close_at', with: '2012-11-13 17:00:00' select 'My coordinator', from: 'order_cycle_coordinator_id' + # And I add a coordinator fee + click_button 'Add coordinator fee' + select 'Coord fee', from: 'order_cycle_coordinator_fee_0_id' + # And I add a supplier and some products select 'My supplier', from: 'new_supplier_id' click_button 'Add supplier' @@ -93,6 +100,9 @@ feature %q{ page.should have_selector 'td.suppliers', text: 'My supplier' page.should have_selector 'td.distributors', text: 'My distributor' + # And it should have a coordinator fee + OrderCycle.last.coordinator_fees.should == [coordinator_fee] + # And it should have some variants selected OrderCycle.last.exchanges.first.variants.count.should == 2 OrderCycle.last.exchanges.last.variants.count.should == 2 @@ -160,13 +170,17 @@ feature %q{ oc = create(:order_cycle) # And a coordinating, supplying and distributing enterprise with some products with variants - create(:distributor_enterprise, name: 'My coordinator') + coordinator = create(:distributor_enterprise, name: 'My coordinator') supplier = create(:supplier_enterprise, name: 'My supplier') distributor = create(:distributor_enterprise, name: 'My distributor') product = create(:product, supplier: supplier) v1 = create(:variant, product: product) v2 = create(:variant, product: product) + # And some enterprise fees + coordinator_fee1 = create(:enterprise_fee, enterprise: coordinator, name: 'Coord fee 1') + coordinator_fee2 = create(:enterprise_fee, enterprise: coordinator, name: 'Coord fee 2') + # When I go to its edit page login_to_admin_section click_link 'Order Cycles' @@ -179,6 +193,11 @@ feature %q{ fill_in 'order_cycle_orders_close_at', with: '2012-11-13 17:00:00' select 'My coordinator', from: 'order_cycle_coordinator_id' + # And I configure some coordinator fees + select 'Coord fee 1', from: 'order_cycle_coordinator_fee_0_id' + click_button 'Add coordinator fee' + select 'Coord fee 2', from: 'order_cycle_coordinator_fee_1_id' + # And I add a supplier and some products select 'My supplier', from: 'new_supplier_id' click_button 'Add supplier' @@ -217,6 +236,9 @@ feature %q{ page.should have_selector 'td.suppliers', text: 'My supplier' page.should have_selector 'td.distributors', text: 'My distributor' + # And my coordinator fees should have been configured + OrderCycle.last.coordinator_fee_ids.sort.should == [coordinator_fee1.id, coordinator_fee2.id].sort + # And it should have some variants selected OrderCycle.last.variants.map { |v| v.id }.sort.should == [1, v1.id, v2.id].sort diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 326e1c7bfb..643c55d4e7 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -424,30 +424,60 @@ describe 'OrderCycle services', -> describe 'creating an order cycle', -> it 'redirects to the order cycles page on success', -> OrderCycle.order_cycle = 'this is the order cycle' - spyOn(OrderCycle, 'removeInactiveExchanges') + spyOn(OrderCycle, 'dataForSubmit').andReturn('this is the submit data') $httpBackend.expectPOST('/admin/order_cycles.json', { - order_cycle: 'this is the order cycle' + order_cycle: 'this is the submit data' }).respond {success: true} OrderCycle.create() $httpBackend.flush() - expect(OrderCycle.removeInactiveExchanges).toHaveBeenCalled() expect($window.location).toEqual('/admin/order_cycles') it 'does not redirect on error', -> OrderCycle.order_cycle = 'this is the order cycle' - spyOn(OrderCycle, 'removeInactiveExchanges') + spyOn(OrderCycle, 'dataForSubmit').andReturn('this is the submit data') $httpBackend.expectPOST('/admin/order_cycles.json', { - order_cycle: 'this is the order cycle' + order_cycle: 'this is the submit data' }).respond {success: false} OrderCycle.create() $httpBackend.flush() - expect(OrderCycle.removeInactiveExchanges).toHaveBeenCalled() expect($window.location).toEqual(undefined) + describe 'updating an order cycle', -> + it 'redirects to the order cycles page on success', -> + OrderCycle.order_cycle = 'this is the order cycle' + spyOn(OrderCycle, 'dataForSubmit').andReturn('this is the submit data') + $httpBackend.expectPUT('/admin/order_cycles.json', { + order_cycle: 'this is the submit data' + }).respond {success: true} + + OrderCycle.update() + $httpBackend.flush() + expect($window.location).toEqual('/admin/order_cycles') + + it 'does not redirect on error', -> + OrderCycle.order_cycle = 'this is the order cycle' + spyOn(OrderCycle, 'dataForSubmit').andReturn('this is the submit data') + $httpBackend.expectPUT('/admin/order_cycles.json', { + order_cycle: 'this is the submit data' + }).respond {success: false} + + OrderCycle.update() + $httpBackend.flush() + expect($window.location).toEqual(undefined) + + describe 'preparing data for form submission', -> + it 'calls all the methods', -> + OrderCycle.order_cycle = {foo: 'bar'} + spyOn(OrderCycle, 'removeInactiveExchanges') + spyOn(OrderCycle, 'translateCoordinatorFees') + OrderCycle.dataForSubmit() + expect(OrderCycle.removeInactiveExchanges).toHaveBeenCalled() + expect(OrderCycle.translateCoordinatorFees).toHaveBeenCalled() + it 'removes inactive exchanges', -> - OrderCycle.order_cycle = + data = incoming_exchanges: [ {enterprise_id: "1", active: false} {enterprise_id: "2", active: true} @@ -458,11 +488,26 @@ describe 'OrderCycle services', -> {enterprise_id: "5", active: false} {enterprise_id: "6", active: true} ] - OrderCycle.removeInactiveExchanges() - expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [ + + data = OrderCycle.removeInactiveExchanges(data) + + expect(data.incoming_exchanges).toEqual [ {enterprise_id: "2", active: true} ] - expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [ + expect(data.outgoing_exchanges).toEqual [ {enterprise_id: "4", active: true} {enterprise_id: "6", active: true} ] + + it 'converts coordinator fees into a list of ids', -> + data = + coordinator_fees: [ + {id: 1} + {id: 2} + ] + + data = OrderCycle.translateCoordinatorFees(data) + + expect(data.coordinator_fees).toBeUndefined() + expect(data.coordinator_fee_ids).toEqual([1, 2]) + \ No newline at end of file