Skip to content

Commit

Permalink
use Customer#with_completed_orders on the customers listing endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
abdellani committed Apr 18, 2023
1 parent ee37c5d commit bab45fa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/customers_controller.rb
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v0/customers_controller.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/customers_controller.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/customer.rb
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions spec/models/customer_spec.rb
Expand Up @@ -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) }

Check failure on line 123 in spec/models/customer_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [128/100] Raw Output: spec/models/customer_spec.rb:123:101: C: Layout/LineLength: Line is too long. [128/100]
let!(:completed_order) {create(:completed_order_with_totals, customer: customer4)}

Check failure on line 124 in spec/models/customer_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Space missing inside {. Raw Output: spec/models/customer_spec.rb:124:31: C: Layout/SpaceInsideBlockBraces: Space missing inside {.

Check failure on line 124 in spec/models/customer_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Space missing inside }. Raw Output: spec/models/customer_spec.rb:124:88: C: Layout/SpaceInsideBlockBraces: Space missing inside }.

it 'returns customers with completed orders' do
expect(Customer.with_completed_orders).to match_array [customer4]
end
end
end
end

0 comments on commit bab45fa

Please sign in to comment.