diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 07bf0a60ce..29d4e78156 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -105,6 +105,10 @@ module Spree before_save :update_payment_fees!, if: :complete? # -- Scopes + scope :not_empty, -> { + left_outer_joins(:line_items).where.not(spree_line_items: { id: nil }) + } + scope :managed_by, lambda { |user| if user.has_spree_role?('admin') where(nil) diff --git a/app/services/search_orders.rb b/app/services/search_orders.rb index 0da52721f3..4b2f87e396 100644 --- a/app/services/search_orders.rb +++ b/app/services/search_orders.rb @@ -23,7 +23,8 @@ class SearchOrders end def search_query - base_query = ::Permissions::Order.new(current_user).editable_orders + base_query = ::Permissions::Order.new(current_user).editable_orders.not_empty + return base_query unless params[:shipping_method_id] base_query diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index fa4757a424..6b724b02f3 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -119,7 +119,7 @@ feature ' context "with incomplete order" do scenario "can edit order" do - incomplete_order = create(:order, distributor: distributor, order_cycle: order_cycle) + incomplete_order = create(:order_with_line_items, distributor: distributor, order_cycle: order_cycle, line_items_count: 1) login_as_admin_and_visit spree.admin_orders_path uncheck 'Only show complete orders' @@ -133,25 +133,29 @@ feature ' context "test the 'Only show the complete orders' checkbox" do scenario "display or not incomplete order" do - incomplete_order = create(:order, distributor: distributor, order_cycle: order_cycle) + incomplete_order = create(:order_with_line_items, distributor: distributor, order_cycle: order_cycle, line_items_count: 1) complete_order = create( - :order, + :order_with_line_items, distributor: distributor, order_cycle: order_cycle, user: user, state: 'complete', payment_state: 'balance_due', - completed_at: 1.day.ago + completed_at: 1.day.ago, + line_items_count: 1 ) + empty_order = create(:order, distributor: distributor, order_cycle: order_cycle) login_as_admin_and_visit spree.admin_orders_path expect(page).to have_content complete_order.number expect(page).to have_no_content incomplete_order.number + expect(page).to have_no_content empty_order.number uncheck 'Only show complete orders' page.find('a.icon-search').click expect(page).to have_content complete_order.number expect(page).to have_content incomplete_order.number + expect(page).to have_no_content empty_order.number end end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index b0fc32a39a..ecc4d5dae8 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -849,6 +849,16 @@ describe Spree::Order do expect(Spree::Order.not_state(:canceled)).not_to include o end end + + describe "not_empty" do + let!(:order_with_line_items) { create(:order_with_line_items, line_items_count: 1) } + let!(:order_without_line_items) { create(:order) } + + it "returns only orders which have line items" do + expect(Spree::Order.not_empty).to include order_with_line_items + expect(Spree::Order.not_empty).to_not include order_without_line_items + end + end end describe "sending confirmation emails" do diff --git a/spec/requests/api/orders_spec.rb b/spec/requests/api/orders_spec.rb index ef25152bbc..36c38b1b0e 100644 --- a/spec/requests/api/orders_spec.rb +++ b/spec/requests/api/orders_spec.rb @@ -38,15 +38,22 @@ describe 'api/v0/orders', type: :request do let!(:order_dist_1) { create(:order_with_distributor, email: "specific_name@example.com") } + let!(:li1) { create(:line_item, order: order_dist_1) } let!(:order_dist_2) { create(:order_with_totals_and_distribution) } + let!(:li2) { create(:line_item, order: order_dist_2) } let!(:order_dist_1_complete) { - create(:order, distributor: order_dist_1.distributor, state: 'complete', - completed_at: Time.zone.today - 7.days) + create(:completed_order_with_totals, distributor: order_dist_1.distributor, state: 'complete', + completed_at: Time.zone.today - 7.days, line_items_count: 1) } let!(:order_dist_1_credit_owed) { create(:order, distributor: order_dist_1.distributor, payment_state: 'credit_owed', completed_at: Time.zone.today) } + let!(:li4) { create(:line_item_with_shipment, order: order_dist_1_credit_owed) } + + let!(:order_empty) { + create(:order_with_line_items, line_items_count: 0) + } let(:user) { order_dist_1.distributor.owner } let(:'X-Spree-Token') do diff --git a/spec/services/search_orders_spec.rb b/spec/services/search_orders_spec.rb index 145e09e33f..e4b543aacd 100644 --- a/spec/services/search_orders_spec.rb +++ b/spec/services/search_orders_spec.rb @@ -4,9 +4,10 @@ require 'spec_helper' describe SearchOrders do let!(:distributor) { create(:distributor_enterprise) } - let!(:order1) { create(:order, distributor: distributor) } - let!(:order2) { create(:order, distributor: distributor) } - let!(:order3) { create(:order, distributor: distributor) } + let!(:order1) { create(:order_with_line_items, distributor: distributor, line_items_count: 3) } + let!(:order2) { create(:order_with_line_items, distributor: distributor, line_items_count: 2) } + let!(:order3) { create(:order_with_line_items, distributor: distributor, line_items_count: 1) } + let!(:order_empty) { create(:order, distributor: distributor) } let(:enterprise_user) { distributor.owner }