From adc7e97e625c7f4f0fb39efe292bc3305ac26319 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 7 Mar 2022 12:38:53 +1100 Subject: [PATCH] Add tag list to customer endpoint --- .../api/v1/customers_controller.rb | 8 +++++-- app/json_schemas/customer_schema.rb | 1 + app/serializers/api/v1/customer_serializer.rb | 2 ++ spec/requests/api/v1/customers_spec.rb | 9 +++++++- swagger/v1/swagger.yaml | 22 +++++++++++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/customers_controller.rb b/app/controllers/api/v1/customers_controller.rb index bf3e035d1b..7f25a3127a 100644 --- a/app/controllers/api/v1/customers_controller.rb +++ b/app/controllers/api/v1/customers_controller.rb @@ -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 diff --git a/app/json_schemas/customer_schema.rb b/app/json_schemas/customer_schema.rb index 5c791e75a2..185cb07f37 100644 --- a/app/json_schemas/customer_schema.rb +++ b/app/json_schemas/customer_schema.rb @@ -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", diff --git a/app/serializers/api/v1/customer_serializer.rb b/app/serializers/api/v1/customer_serializer.rb index 1325d7c615..6e2edc8f04 100644 --- a/app/serializers/api/v1/customer_serializer.rb +++ b/app/serializers/api/v1/customer_serializer.rb @@ -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) diff --git a/spec/requests/api/v1/customers_spec.rb b/spec/requests/api/v1/customers_spec.rb index fa4332631c..f87a299429 100644 --- a/spec/requests/api/v1/customers_spec.rb +++ b/spec/requests/api/v1/customers_spec.rb @@ -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 diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml index d703156f03..9b7b0442c3 100644 --- a/swagger/v1/swagger.yaml +++ b/swagger/v1/swagger.yaml @@ -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