From e5e8953a094a9e4a7abc853adefe14ec9269bbe4 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 3 Mar 2022 16:44:31 +1100 Subject: [PATCH] Add ToS acceptance to customer endpoint It's another read-only attribute. Please note that JSON:API specifies a format of `date-time` which we don't adhere to because it uses a `Z` in front of the timezone offset which doesn't seem to be included in the default json serialisation. So I didn't add the format and left it as simple `string`. Problem? --- app/json_schemas/customer_schema.rb | 5 +++++ app/serializers/api/v1/customer_serializer.rb | 2 +- spec/requests/api/v1/customers_spec.rb | 20 ++++++++++++++++--- swagger/v1/swagger.yaml | 12 +++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/json_schemas/customer_schema.rb b/app/json_schemas/customer_schema.rb index 3281d12d90..5c791e75a2 100644 --- a/app/json_schemas/customer_schema.rb +++ b/app/json_schemas/customer_schema.rb @@ -14,6 +14,10 @@ class CustomerSchema < JsonApiSchema code: { type: :string, nullable: true, example: "BUYER1" }, email: { type: :string, example: "alice@example.com" }, allow_charges: { type: :boolean, example: false }, + terms_and_conditions_accepted_at: { + type: :string, format: "date-time", nullable: true, + example: "2022-03-12T15:55:00.000+11:00", + }, } end @@ -25,6 +29,7 @@ class CustomerSchema < JsonApiSchema attributes.except( :id, :allow_charges, + :terms_and_conditions_accepted_at, ) end diff --git a/app/serializers/api/v1/customer_serializer.rb b/app/serializers/api/v1/customer_serializer.rb index 1cecc7ee23..1325d7c615 100644 --- a/app/serializers/api/v1/customer_serializer.rb +++ b/app/serializers/api/v1/customer_serializer.rb @@ -4,7 +4,7 @@ module Api module V1 class CustomerSerializer < BaseSerializer attributes :id, :enterprise_id, :first_name, :last_name, :code, :email, - :allow_charges + :allow_charges, :terms_and_conditions_accepted_at belongs_to :enterprise, links: { related: ->(object) { diff --git a/spec/requests/api/v1/customers_spec.rb b/spec/requests/api/v1/customers_spec.rb index a6a620b398..fa4332631c 100644 --- a/spec/requests/api/v1/customers_spec.rb +++ b/spec/requests/api/v1/customers_spec.rb @@ -5,7 +5,13 @@ require "swagger_helper" describe "Customers", type: :request do let!(:enterprise1) { create(:enterprise) } let!(:enterprise2) { create(:enterprise) } - let!(:customer1) { create(:customer, enterprise: enterprise1) } + let!(:customer1) { + create( + :customer, + enterprise: enterprise1, + terms_and_conditions_accepted_at: Time.zone.parse("2000-01-01"), + ) + } let!(:customer2) { create(:customer, enterprise: enterprise1) } let!(:customer3) { create(:customer, enterprise: enterprise2) } @@ -116,7 +122,8 @@ describe "Customers", type: :request do run_test! do expect(json_response[:data][:attributes]).to include( - allow_charges: false + allow_charges: false, + terms_and_conditions_accepted_at: nil, ) end end @@ -179,7 +186,14 @@ describe "Customers", type: :request do param(:id) { customer1.id } schema "$ref": "#/components/schemas/resources/customer" - run_test! + run_test! do + date_time_string = + json_response[:data][:attributes][:terms_and_conditions_accepted_at] + expect(date_time_string).to match /^2000-01-01T00:00:00.000[Z+-].*$/ + expect(DateTime.parse(date_time_string)).to eq( + customer1.terms_and_conditions_accepted_at + ) + end end response "404", "Not found" do diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml index 03365ac195..d703156f03 100644 --- a/swagger/v1/swagger.yaml +++ b/swagger/v1/swagger.yaml @@ -63,6 +63,11 @@ components: allow_charges: type: boolean example: false + terms_and_conditions_accepted_at: + type: string + format: date-time + nullable: true + example: '2022-03-12T15:55:00.000+11:00' required: - id - enterprise_id @@ -71,6 +76,7 @@ components: - code - email - allow_charges + - terms_and_conditions_accepted_at relationships: type: object properties: @@ -137,6 +143,11 @@ components: allow_charges: type: boolean example: false + terms_and_conditions_accepted_at: + type: string + format: date-time + nullable: true + example: '2022-03-12T15:55:00.000+11:00' required: - id - enterprise_id @@ -145,6 +156,7 @@ components: - code - email - allow_charges + - terms_and_conditions_accepted_at relationships: type: object properties: