diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index 3ab665d9b5..630ffd361b 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -17,9 +17,10 @@ module Admin respond_to do |format| format.html format.json do - render_as_json @collection, - tag_rule_mapping: tag_rule_mapping, - customer_tags: customer_tags_by_id + render json: @collection, + each_serializer: ::Api::Admin::CustomerWithBalanceSerializer, + tag_rule_mapping: tag_rule_mapping, + customer_tags: customer_tags_by_id end end end @@ -63,9 +64,11 @@ module Admin private def collection - return Customer.where("1=0") unless json_request? && params[:enterprise_id].present? - - CustomersWithBalance.new(managed_enterprise_id).query + if json_request? && params[:enterprise_id].present? + CustomersWithBalance.new(managed_enterprise_id).query + else + Customer.where('1=0') + end end def managed_enterprise_id diff --git a/app/serializers/api/admin/customer_serializer.rb b/app/serializers/api/admin/customer_serializer.rb index 1d8b2ffc3f..99d8bad28c 100644 --- a/app/serializers/api/admin/customer_serializer.rb +++ b/app/serializers/api/admin/customer_serializer.rb @@ -4,13 +4,11 @@ module Api module Admin class CustomerSerializer < ActiveModel::Serializer attributes :id, :email, :enterprise_id, :user_id, :code, :tags, :tag_list, :name, - :allow_charges, :default_card_present?, :balance, :balance_status + :allow_charges, :default_card_present? has_one :ship_address, serializer: Api::AddressSerializer has_one :bill_address, serializer: Api::AddressSerializer - delegate :balance_value, to: :object - def name object.name.presence || object.bill_address.andand.full_name end @@ -19,20 +17,6 @@ module Api customer_tag_list.join(",") end - def balance - Spree::Money.new(balance_value, currency: Spree::Config[:currency]).to_s - end - - def balance_status - if balance_value.positive? - "credit_owed" - elsif balance_value.negative? - "balance_due" - else - "" - end - end - def tags customer_tag_list.map do |tag| tag_rule_map = options[:tag_rule_mapping].andand[tag] diff --git a/app/serializers/api/admin/customer_with_balance_serializer.rb b/app/serializers/api/admin/customer_with_balance_serializer.rb new file mode 100644 index 0000000000..dfbb088991 --- /dev/null +++ b/app/serializers/api/admin/customer_with_balance_serializer.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Api + module Admin + class CustomerWithBalanceSerializer < CustomerSerializer + attributes :balance, :balance_status + + delegate :balance_value, to: :object + + def balance + Spree::Money.new(balance_value, currency: Spree::Config[:currency]).to_s + end + + def balance_status + if balance_value.positive? + "credit_owed" + elsif balance_value.negative? + "balance_due" + else + "" + end + end + end + end +end diff --git a/spec/controllers/admin/customers_controller_spec.rb b/spec/controllers/admin/customers_controller_spec.rb index 1a4595ee19..9331da4e2c 100644 --- a/spec/controllers/admin/customers_controller_spec.rb +++ b/spec/controllers/admin/customers_controller_spec.rb @@ -41,6 +41,11 @@ module Admin expect(ActiveModel::ArraySerializer).to receive(:new) spree_get :index, params end + + it 'includes the customer balance in the response' do + spree_get :index, params + expect(json_response.first["balance"]).to eq("$0.00") + end end context "and enterprise_id is not given in params" do diff --git a/spec/features/admin/customers_spec.rb b/spec/features/admin/customers_spec.rb index 4ff41be6c7..37b0595a2d 100644 --- a/spec/features/admin/customers_spec.rb +++ b/spec/features/admin/customers_spec.rb @@ -97,9 +97,33 @@ feature 'Customers' do describe "for a shop with multiple customers" do before do - build_balance(customer1, managed_distributor1, 88) - build_balance(customer2, managed_distributor1, -99) - build_balance(customer4, managed_distributor1, 0) + create( + :order, + total: 0, + payment_total: 88, + distributor: managed_distributor1, + user: nil, + completed_at: Time.zone.now, + customer: customer1 + ) + create( + :order, + total: 99, + payment_total: 0, + distributor: managed_distributor1, + user: nil, + completed_at: Time.zone.now, + customer: customer2 + ) + create( + :order, + total: 0, + payment_total: 0, + distributor: managed_distributor1, + user: nil, + completed_at: Time.zone.now, + customer: customer4 + ) customer4.update enterprise: managed_distributor1 end @@ -121,14 +145,6 @@ feature 'Customers' do expect(page).to have_content "$0.00" end end - - def build_balance(customer, enterprise, balance) - order = build(:order, total: balance, payment_total: 0, distributor: enterprise) - order.email = customer.email - order.customer_id = customer.id - order.completed_at = Time.zone.now - order.save! - end end it "allows updating of attributes" do diff --git a/spec/serializers/api/admin/customer_with_balance_serializer_spec.rb b/spec/serializers/api/admin/customer_with_balance_serializer_spec.rb new file mode 100644 index 0000000000..5c0c3f1df5 --- /dev/null +++ b/spec/serializers/api/admin/customer_with_balance_serializer_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Api::Admin::CustomerWithBalanceSerializer do + let(:serialized_customer) { described_class.new(customer) } + + describe '#balance' do + let(:customer) { double(Customer, balance_value: 1.2) } + let(:money) { instance_double(Spree::Money, to_s: "$1.20") } + + before do + allow(Spree::Money).to receive(:new).with(1.2, currency: "AUD") { money } + end + + it 'returns the balance_value as a money amount' do + expect(serialized_customer.balance).to eq("$1.20") + end + end + + describe '#balance_status' do + context 'when the balance_value is positive' do + let(:customer) { double(Customer, balance_value: 1) } + + it 'returns credit_owed' do + expect(serialized_customer.balance_status).to eq("credit_owed") + end + end + + context 'when the balance_value is negative' do + let(:customer) { double(Customer, balance_value: -1) } + + it 'returns credit_owed' do + expect(serialized_customer.balance_status).to eq("balance_due") + end + end + + context 'when the balance_value is zero' do + let(:customer) { double(Customer, balance_value: 0) } + + it 'returns credit_owed' do + expect(serialized_customer.balance_status).to eq("") + end + end + end +end