Use customer-nested params for Customer v1 API writing operations

This commit is contained in:
François Turbelin
2025-10-31 14:07:47 +01:00
parent 275326eaa6
commit cf4cd311b3
3 changed files with 147 additions and 115 deletions

View File

@@ -88,17 +88,20 @@ module Api
attributes = params.require(:customer).permit(
:email, :enterprise_id,
:code, :first_name, :last_name,
:billing_address, shipping_address: [
:billing_address,
shipping_address: [
:phone, :latitude, :longitude,
:first_name, :last_name,
:street_address_1, :street_address_2,
:postal_code, :locality,
{ region: [:name, :code], country: [:name, :code] },
]
],
tags: [],
).to_h
attributes.merge!(created_manually: true)
attributes.merge!(tag_list: params[:tags]) if params.key?(:tags)
tags = attributes.delete(:tags)
attributes.merge!(tag_list: tags) if tags.present?
transform_address!(attributes, :billing_address, :bill_address)
transform_address!(attributes, :shipping_address, :ship_address)

View File

@@ -173,15 +173,23 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do
parameter name: :customer, in: :body, schema: {
type: :object,
properties: CustomerSchema.writable_attributes,
required: CustomerSchema.required_attributes
properties: {
customer: {
type: :object,
properties: CustomerSchema.writable_attributes,
required: CustomerSchema.required_attributes
}
},
required: ['customer']
}
response "201", "Minimal customer created" do
let(:customer) do
{
email: "test@example.com",
enterprise_id: enterprise1.id.to_s
customer: {
email: "test@example.com",
enterprise_id: enterprise1.id.to_s
}
}
end
schema '$ref': "#/components/schemas/customer"
@@ -199,12 +207,15 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do
response "201", "Example customer created" do
let(:customer) do
CustomerSchema.writable_attributes.transform_values do |attribute|
attribute[:example]
end.merge(
enterprise_id: enterprise1.id,
)
{
customer: CustomerSchema.writable_attributes.transform_values do |attribute|
attribute[:example]
end.merge(
enterprise_id: enterprise1.id,
)
}
end
schema '$ref': "#/components/schemas/customer"
run_test! do
@@ -223,9 +234,11 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do
response "422", "Unpermitted parameter" do
let(:customer) do
{
email: "test@example.com",
enterprise_id: enterprise1.id.to_s,
allow_charges: true,
customer: {
email: "test@example.com",
enterprise_id: enterprise1.id.to_s,
allow_charges: true,
}
}
end
schema '$ref': "#/components/schemas/error_response"
@@ -347,8 +360,14 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do
parameter name: :customer, in: :body, schema: {
type: :object,
properties: CustomerSchema.writable_attributes,
required: CustomerSchema.required_attributes
properties: {
customer: {
type: :object,
properties: CustomerSchema.writable_attributes,
required: CustomerSchema.required_attributes
}
},
required: ['customer']
}
response "200", "Customer updated" do

View File

@@ -347,57 +347,62 @@ paths:
schema:
type: object
properties:
enterprise_id:
type: integer
example: 2
first_name:
type: string
nullable: true
example: Alice
last_name:
type: string
nullable: true
example: Springs
code:
type: string
nullable: true
example: BUYER1
email:
type: string
example: alice@example.com
tags:
type: array
items:
type: string
example:
- staff
- discount
billing_address:
customer:
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:
code: Vic
name: Victoria
country:
code: AU
name: Australia
properties:
enterprise_id:
type: integer
example: 2
first_name:
type: string
nullable: true
example: Alice
last_name:
type: string
nullable: true
example: Springs
code:
type: string
nullable: true
example: BUYER1
email:
type: string
example: alice@example.com
tags:
type: array
items:
type: string
example:
- staff
- discount
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:
code: Vic
name: Victoria
country:
code: AU
name: Australia
required:
- enterprise_id
- email
required:
- enterprise_id
- email
- customer
"/api/v1/customers/{id}":
get:
summary: Show customer
@@ -570,57 +575,62 @@ paths:
schema:
type: object
properties:
enterprise_id:
type: integer
example: 2
first_name:
type: string
nullable: true
example: Alice
last_name:
type: string
nullable: true
example: Springs
code:
type: string
nullable: true
example: BUYER1
email:
type: string
example: alice@example.com
tags:
type: array
items:
type: string
example:
- staff
- discount
billing_address:
customer:
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:
code: Vic
name: Victoria
country:
code: AU
name: Australia
properties:
enterprise_id:
type: integer
example: 2
first_name:
type: string
nullable: true
example: Alice
last_name:
type: string
nullable: true
example: Springs
code:
type: string
nullable: true
example: BUYER1
email:
type: string
example: alice@example.com
tags:
type: array
items:
type: string
example:
- staff
- discount
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:
code: Vic
name: Victoria
country:
code: AU
name: Australia
required:
- enterprise_id
- email
required:
- enterprise_id
- email
- customer
delete:
summary: Delete customer
tags: