Add tag list to customer endpoint

This commit is contained in:
Maikel Linke
2022-03-07 12:38:53 +11:00
parent e5e8953a09
commit adc7e97e62
5 changed files with 39 additions and 3 deletions

View File

@@ -70,10 +70,14 @@ module Api
end
def customer_params
params.require(:customer).permit(
attributes = params.require(:customer).permit(
:email, :enterprise_id,
:code, :first_name, :last_name,
)
).to_h
attributes.merge!(tag_list: params[:tags]) if params.key?(:tags)
attributes
end
def editable_enterprises

View File

@@ -14,6 +14,7 @@ class CustomerSchema < JsonApiSchema
code: { type: :string, nullable: true, example: "BUYER1" },
email: { type: :string, example: "alice@example.com" },
allow_charges: { type: :boolean, example: false },
tags: { type: :array, example: ["staff", "discount"] },
terms_and_conditions_accepted_at: {
type: :string, format: "date-time", nullable: true,
example: "2022-03-12T15:55:00.000+11:00",

View File

@@ -6,6 +6,8 @@ module Api
attributes :id, :enterprise_id, :first_name, :last_name, :code, :email,
:allow_charges, :terms_and_conditions_accepted_at
attribute :tags, &:tag_list
belongs_to :enterprise, links: {
related: ->(object) {
url_helpers.api_v1_enterprise_url(id: object.enterprise_id)

View File

@@ -10,6 +10,7 @@ describe "Customers", type: :request do
:customer,
enterprise: enterprise1,
terms_and_conditions_accepted_at: Time.zone.parse("2000-01-01"),
tag_list: ["long-term"],
)
}
let!(:customer2) { create(:customer, enterprise: enterprise1) }
@@ -145,6 +146,7 @@ describe "Customers", type: :request do
code: "BUYER1",
email: "alice@example.com",
enterprise_id: enterprise1.id,
tags: ["staff", "discount"],
)
end
end
@@ -258,7 +260,12 @@ describe "Customers", type: :request do
end
schema "$ref": "#/components/schemas/resources/customer"
run_test!
run_test! do
# Tags should not be overridden when the param is missing:
expect(json_response[:data][:attributes]).to include(
tags: ["long-term"],
)
end
end
response "422", "Unprocessable entity" do

View File

@@ -63,6 +63,11 @@ components:
allow_charges:
type: boolean
example: false
tags:
type: array
example:
- staff
- discount
terms_and_conditions_accepted_at:
type: string
format: date-time
@@ -76,6 +81,7 @@ components:
- code
- email
- allow_charges
- tags
- terms_and_conditions_accepted_at
relationships:
type: object
@@ -143,6 +149,11 @@ components:
allow_charges:
type: boolean
example: false
tags:
type: array
example:
- staff
- discount
terms_and_conditions_accepted_at:
type: string
format: date-time
@@ -156,6 +167,7 @@ components:
- code
- email
- allow_charges
- tags
- terms_and_conditions_accepted_at
relationships:
type: object
@@ -291,6 +303,11 @@ paths:
email:
type: string
example: alice@example.com
tags:
type: array
example:
- staff
- discount
required:
- enterprise_id
- email
@@ -371,6 +388,11 @@ paths:
email:
type: string
example: alice@example.com
tags:
type: array
example:
- staff
- discount
required:
- enterprise_id
- email