Add billing and shipping address to customer

This commit is contained in:
Maikel Linke
2022-03-07 15:49:36 +11:00
parent adc7e97e62
commit 51420934f3
7 changed files with 104 additions and 1 deletions

View File

@@ -79,6 +79,11 @@ Lint/RaiseException:
Lint/StructNewOverride:
Enabled: true
Naming/VariableNumber:
AllowedIdentifiers:
- street_address_1
- street_address_2
Bundler/DuplicatedGem:
Enabled: false

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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) }

View File

@@ -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: