diff --git a/.rubocop_styleguide.yml b/.rubocop_styleguide.yml index 500b642c70..1cf9623b42 100644 --- a/.rubocop_styleguide.yml +++ b/.rubocop_styleguide.yml @@ -79,6 +79,11 @@ Lint/RaiseException: Lint/StructNewOverride: Enabled: true +Naming/VariableNumber: + AllowedIdentifiers: + - street_address_1 + - street_address_2 + Bundler/DuplicatedGem: Enabled: false diff --git a/app/controllers/api/v1/customers_controller.rb b/app/controllers/api/v1/customers_controller.rb index 7f25a3127a..9a2ee3e325 100644 --- a/app/controllers/api/v1/customers_controller.rb +++ b/app/controllers/api/v1/customers_controller.rb @@ -58,7 +58,7 @@ module Api end def search_customers - customers = visible_customers + customers = visible_customers.includes(:bill_address, :ship_address) customers = customers.where(enterprise_id: params[:enterprise_id]) if params[:enterprise_id] customers.ransack(params[:q]).result end diff --git a/app/json_schemas/customer_schema.rb b/app/json_schemas/customer_schema.rb index 185cb07f37..9f8b2a79f4 100644 --- a/app/json_schemas/customer_schema.rb +++ b/app/json_schemas/customer_schema.rb @@ -19,6 +19,30 @@ class CustomerSchema < JsonApiSchema type: :string, format: "date-time", nullable: true, example: "2022-03-12T15:55:00.000+11:00", }, + billing_address: { + type: :object, nullable: true, + example: nil, + }, + shipping_address: { + type: :object, nullable: true, + example: address_example, + }, + } + end + + def self.address_example + { + phone: "0404 333 222 111", + latitude: -37.817375100000, + longitude: 144.964803195704, + first_name: "Alice", + last_name: "Springs", + street_address_1: "1 Flinders Street", + street_address_2: "", + postal_code: "1234", + locality: "Melbourne", + region: "Victoria", + country: "Australia", } end @@ -31,6 +55,7 @@ class CustomerSchema < JsonApiSchema :id, :allow_charges, :terms_and_conditions_accepted_at, + :billing_address, :shipping_address, ) end diff --git a/app/serializers/api/v1/address_serializer.rb b/app/serializers/api/v1/address_serializer.rb new file mode 100644 index 0000000000..da53af79f2 --- /dev/null +++ b/app/serializers/api/v1/address_serializer.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Api + module V1 + class AddressSerializer < BaseSerializer + attributes :phone, :latitude, :longitude + + attribute :first_name, &:firstname + attribute :last_name, &:lastname + attribute :street_address_1, &:address1 + attribute :street_address_2, &:address2 + attribute :postal_code, &:zipcode + attribute :locality, &:city + attribute :region, &:state_name + attribute :country, ->(object, _) { object.country.name } + end + end +end diff --git a/app/serializers/api/v1/customer_serializer.rb b/app/serializers/api/v1/customer_serializer.rb index 6e2edc8f04..b5e59648b5 100644 --- a/app/serializers/api/v1/customer_serializer.rb +++ b/app/serializers/api/v1/customer_serializer.rb @@ -8,11 +8,23 @@ module Api attribute :tags, &:tag_list + attribute :billing_address do |object| + address(object.billing_address) + end + + attribute :shipping_address do |object| + address(object.shipping_address) + end + belongs_to :enterprise, links: { related: ->(object) { url_helpers.api_v1_enterprise_url(id: object.enterprise_id) } } + + def self.address(record) + AddressSerializer.new(record).serializable_hash.dig(:data, :attributes) + end end end end diff --git a/spec/requests/api/v1/customers_spec.rb b/spec/requests/api/v1/customers_spec.rb index f87a299429..3a47110e11 100644 --- a/spec/requests/api/v1/customers_spec.rb +++ b/spec/requests/api/v1/customers_spec.rb @@ -11,6 +11,7 @@ describe "Customers", type: :request do enterprise: enterprise1, terms_and_conditions_accepted_at: Time.zone.parse("2000-01-01"), tag_list: ["long-term"], + ship_address: create(:address), ) } let!(:customer2) { create(:customer, enterprise: enterprise1) } diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml index 9b7b0442c3..84f42e1eed 100644 --- a/swagger/v1/swagger.yaml +++ b/swagger/v1/swagger.yaml @@ -73,6 +73,25 @@ components: format: date-time nullable: true example: '2022-03-12T15:55:00.000+11:00' + billing_address: + type: object + nullable: true + example: + shipping_address: + type: object + nullable: true + example: + phone: 0404 333 222 111 + latitude: -37.8173751 + longitude: 144.964803195704 + first_name: Alice + last_name: Springs + street_address_1: 1 Flinders Street + street_address_2: '' + postal_code: '1234' + locality: Melbourne + region: Victoria + country: Australia required: - id - enterprise_id @@ -83,6 +102,8 @@ components: - allow_charges - tags - terms_and_conditions_accepted_at + - billing_address + - shipping_address relationships: type: object properties: @@ -159,6 +180,25 @@ components: format: date-time nullable: true example: '2022-03-12T15:55:00.000+11:00' + billing_address: + type: object + nullable: true + example: + shipping_address: + type: object + nullable: true + example: + phone: 0404 333 222 111 + latitude: -37.8173751 + longitude: 144.964803195704 + first_name: Alice + last_name: Springs + street_address_1: 1 Flinders Street + street_address_2: '' + postal_code: '1234' + locality: Melbourne + region: Victoria + country: Australia required: - id - enterprise_id @@ -169,6 +209,8 @@ components: - allow_charges - tags - terms_and_conditions_accepted_at + - billing_address + - shipping_address relationships: type: object properties: