From c74463404d98c1fce30b9e9a9d68101a580a6f48 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 4 Dec 2015 12:16:45 +1100 Subject: [PATCH] Only show order cycle options that include the chosen distributor --- .../controllers/orders_controller.js.coffee | 6 +++- app/helpers/admin/injection_helper.rb | 2 +- .../add_distribution_fields.html.haml.deface | 2 +- .../api/admin/basic_order_cycle_serializer.rb | 8 ++++-- spec/features/admin/orders_spec.rb | 21 +++++++------- .../orders_controller_spec.js.coffee | 28 +++++++++++++++++++ 6 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee diff --git a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee index 0b01bb48ee..e87501be42 100644 --- a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee +++ b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee @@ -4,4 +4,8 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $compile, $attr $scope.orderCycles = orderCycles $scope.distributor_id = $attrs.ofnDistributorId - $scope.order_cycle_id = $attrs.ofnOrderCycleId \ No newline at end of file + $scope.order_cycle_id = $attrs.ofnOrderCycleId + + $scope.validOrderCycle = (oc, index, array) -> + distributor_ids = (d.id for d in oc.distributors) + distributor_ids.indexOf(parseInt($scope.distributor_id)) != -1 diff --git a/app/helpers/admin/injection_helper.rb b/app/helpers/admin/injection_helper.rb index aa45f4ac2b..490e3dc00b 100644 --- a/app/helpers/admin/injection_helper.rb +++ b/app/helpers/admin/injection_helper.rb @@ -75,7 +75,7 @@ module Admin end def admin_inject_order_cycles - admin_inject_json_ams_array "admin.orders", "orderCycles", @order_cycles, Api::Admin::IdNameSerializer, current_user: spree_current_user + admin_inject_json_ams_array "admin.orders", "orderCycles", @order_cycles, Api::Admin::BasicOrderCycleSerializer, current_user: spree_current_user end def admin_inject_spree_api_key diff --git a/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface b/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface index a8e716769d..e6754e37f0 100644 --- a/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface +++ b/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface @@ -26,4 +26,4 @@ .field %label{for: "order_order_cycle_id"} Order Cycle %select{id: "order_order_cycle_id", name: "order[order_cycle_id]", 'ng-model' => 'order_cycle_id'} - %option{"ng-repeat" => "oc in orderCycles", "ng-value" => "oc.id", "ng-selected" => "order_cycle_id == oc.id", "ng-bind" => "oc.name"} + %option{"ng-repeat" => "oc in orderCycles | filter:validOrderCycle", "ng-value" => "oc.id", "ng-selected" => "order_cycle_id == oc.id", "ng-bind" => "oc.name"} diff --git a/app/serializers/api/admin/basic_order_cycle_serializer.rb b/app/serializers/api/admin/basic_order_cycle_serializer.rb index e94795821a..5984a64ecb 100644 --- a/app/serializers/api/admin/basic_order_cycle_serializer.rb +++ b/app/serializers/api/admin/basic_order_cycle_serializer.rb @@ -5,10 +5,14 @@ class Api::Admin::BasicOrderCycleSerializer < ActiveModel::Serializer has_many :distributors, serializer: Api::Admin::IdNameSerializer def first_order - object.orders_open_at.strftime("%F") + object.orders_open_at.andand.strftime("%F") end def last_order - (object.orders_close_at + 1.day).strftime("%F") + if object.orders_close_at.present? + (object.orders_close_at + 1.day).strftime("%F") + else + nil + end end end diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index fc978d1643..fd312ba0c0 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -11,7 +11,7 @@ feature %q{ @user = create(:user) @product = create(:simple_product) @distributor = create(:distributor_enterprise, charges_sales_tax: true) - @order_cycle = create(:simple_order_cycle, distributors: [@distributor], variants: [@product.variants.first]) + @order_cycle = create(:simple_order_cycle, name: 'One', distributors: [@distributor], variants: [@product.variants.first]) @order = create(:order_with_totals_and_distribution, user: @user, distributor: @distributor, order_cycle: @order_cycle, state: 'complete', payment_state: 'balance_due') @@ -22,30 +22,31 @@ feature %q{ end scenario "creating an order with distributor and order cycle", js: true, retry: 3 do - distributor = create(:distributor_enterprise) - product = create(:simple_product) - order_cycle = create(:simple_order_cycle, distributors: [distributor], variants: [product.variants.first]) + create(:simple_order_cycle, name: 'Two') login_to_admin_section visit '/admin/orders' click_link 'New Order' - select distributor.name, from: 'order_distributor_id' - select order_cycle.name, from: 'order_order_cycle_id' + # When we select a distributor, it should limit order cycle selection to those for that distributor + page.should have_select 'order_order_cycle_id', options: [''] + select @distributor.name, from: 'order_distributor_id' + page.should have_select 'order_order_cycle_id', options: ['', 'One'] + select @order_cycle.name, from: 'order_order_cycle_id' page.should have_content 'ADD PRODUCT' - targetted_select2_search product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' + targetted_select2_search @product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' click_link 'Add' page.has_selector? "table.index tbody[data-hook='admin_order_form_line_items'] tr" # Wait for JS - page.should have_selector 'td', text: product.name + page.should have_selector 'td', text: @product.name click_button 'Update' page.should have_selector 'h1', text: 'Customer Details' o = Spree::Order.last - o.distributor.should == distributor - o.order_cycle.should == order_cycle + o.distributor.should == @distributor + o.order_cycle.should == @order_cycle end scenario "can add a product to an existing order", js: true, retry: 3 do diff --git a/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee b/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee new file mode 100644 index 0000000000..6259daa6e8 --- /dev/null +++ b/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee @@ -0,0 +1,28 @@ +describe "ordersCtrl", -> + ctrl = null + scope = {} + attrs = {} + shops = [] + orderCycles = [ + {id: 10, distributors: [{id: 1, name: 'One'}]} + {id: 20, distributors: [{id: 2, name: 'Two'}]} + ] + + beforeEach -> + scope = {} + + module('admin.orders') + inject ($controller) -> + ctrl = $controller 'ordersCtrl', {$scope: scope, $attrs: attrs, shops: shops, orderCycles: orderCycles} + + + describe "finding valid order cycles for a distributor", -> + order_cycle = {id: 10, distributors: [{id: 1, name: 'One'}]} + + it "returns true when the order cycle includes the distributor", -> + scope.distributor_id = '1' + expect(scope.validOrderCycle(order_cycle, 1, [order_cycle])).toBe true + + it "returns false otherwise", -> + scope.distributor_id = '2' + expect(scope.validOrderCycle(order_cycle, 1, [order_cycle])).toBe false