From bab45fac4c4dae16450407b1455fc82634e20107 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Sat, 15 Apr 2023 05:54:37 +0100 Subject: [PATCH] use Customer#with_completed_orders on the customers listing endpoints --- app/controllers/admin/customers_controller.rb | 2 +- app/controllers/api/v0/customers_controller.rb | 2 +- app/controllers/api/v1/customers_controller.rb | 2 +- app/models/customer.rb | 1 + spec/models/customer_spec.rb | 16 ++++++++++++++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index fcf32350e5d8..773ec2efc638 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -83,7 +83,7 @@ def collection def customers return @customers if @customers.present? - @customers = Customer.managed_by(spree_current_user) + @customers = Customer.with_completed_orders.managed_by(spree_current_user) return @customers if params[:enterprise_id].blank? @customers = @customers.where(enterprise_id: params[:enterprise_id]) diff --git a/app/controllers/api/v0/customers_controller.rb b/app/controllers/api/v0/customers_controller.rb index a10b65bed764..6f5af1d56b21 100644 --- a/app/controllers/api/v0/customers_controller.rb +++ b/app/controllers/api/v0/customers_controller.rb @@ -6,7 +6,7 @@ class CustomersController < Api::V0::BaseController skip_authorization_check only: :index def index - @customers = current_api_user.customers + @customers = current_api_user.customers.with_completed_orders render json: @customers, each_serializer: CustomerSerializer end diff --git a/app/controllers/api/v1/customers_controller.rb b/app/controllers/api/v1/customers_controller.rb index 37172ccbfb8c..2dd388df24a2 100644 --- a/app/controllers/api/v1/customers_controller.rb +++ b/app/controllers/api/v1/customers_controller.rb @@ -80,7 +80,7 @@ def search_customers end def visible_customers - Customer.managed_by(current_api_user) + Customer.with_completed_orders.managed_by(current_api_user) end def customer_params diff --git a/app/models/customer.rb b/app/models/customer.rb index 6e5468fed112..cfc1debb71b2 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -36,6 +36,7 @@ class Customer < ApplicationRecord scope :of, ->(enterprise) { where(enterprise_id: enterprise) } scope :managed_by, ->(user) { user&.persisted? ? where(user: user).or(of(Enterprise.managed_by(user))) : none } + scope :with_completed_orders, -> { joins(:orders).merge(Spree::Order.complete).distinct } before_create :associate_user diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb index 1b4f93cf02a4..f14cbb16f111 100644 --- a/spec/models/customer_spec.rb +++ b/spec/models/customer_spec.rb @@ -111,5 +111,21 @@ expect(Customer.managed_by(guest)).to match_array [] end end + + context 'with_completed_orders' do + let!(:customer) { create(:customer) } + let!(:customer2) { create(:customer) } + let!(:customer3) { create(:customer) } + let!(:customer4) { create(:customer) } + + let!(:order_ready_for_details) { create(:order_ready_for_details, customer: customer) } + let!(:order_ready_for_payment){ create(:order_ready_for_payment, customer: customer2) } + # let!(:order_ready_for_confirmation) { create(:order_ready_for_confirmation, state: :confirmation, customer: customer3) } + let!(:completed_order) {create(:completed_order_with_totals, customer: customer4)} + + it 'returns customers with completed orders' do + expect(Customer.with_completed_orders).to match_array [customer4] + end + end end end