From d789fb32e9fd7e6189bdc57dcec22f112347fe3c Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 24 Mar 2022 17:04:36 +1100 Subject: [PATCH] Make customer's address writable on API --- .../api/v1/customers_controller.rb | 42 +++++++++++++++++++ app/json_schemas/customer_schema.rb | 1 - spec/requests/api/v1/customers_spec.rb | 1 + swagger/v1/swagger.yaml | 38 +++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/customers_controller.rb b/app/controllers/api/v1/customers_controller.rb index 9a2ee3e325..36b1f65968 100644 --- a/app/controllers/api/v1/customers_controller.rb +++ b/app/controllers/api/v1/customers_controller.rb @@ -73,13 +73,55 @@ module Api attributes = params.require(:customer).permit( :email, :enterprise_id, :code, :first_name, :last_name, + :billing_address, shipping_address: [ + :phone, :latitude, :longitude, + :first_name, :last_name, + :street_address_1, :street_address_2, + :postal_code, :locality, :region, :country, + ] ).to_h attributes.merge!(tag_list: params[:tags]) if params.key?(:tags) + transform_address!(attributes, :billing_address, :bill_address) + transform_address!(attributes, :shipping_address, :ship_address) + attributes end + def transform_address!(attributes, from, to) + return unless attributes.key?(from) + + address = attributes.delete(from) + + if address.nil? + attributes[to] = nil + return + end + + address.transform_keys! do |key| + { + phone: :phone, latitude: :latitude, longitude: :longitude, + first_name: :firstname, last_name: :lastname, + street_address_1: :address1, street_address_2: :address2, + postal_code: :zipcode, + locality: :city, + region: :state_name, + country: :country, + }.with_indifferent_access[key] + end + + if address[:state_name].present? + address[:state] = Spree::State.find_by(name: address[:state_name]) + end + + if address[:country].present? + address[:country] = Spree::Country.find_by(name: address[:country]) + end + + attributes["#{to}_attributes"] = address + end + def editable_enterprises OpenFoodNetwork::Permissions.new(current_api_user).editable_enterprises.select(:id) end diff --git a/app/json_schemas/customer_schema.rb b/app/json_schemas/customer_schema.rb index 9f8b2a79f4..45529f19e3 100644 --- a/app/json_schemas/customer_schema.rb +++ b/app/json_schemas/customer_schema.rb @@ -55,7 +55,6 @@ class CustomerSchema < JsonApiSchema :id, :allow_charges, :terms_and_conditions_accepted_at, - :billing_address, :shipping_address, ) end diff --git a/spec/requests/api/v1/customers_spec.rb b/spec/requests/api/v1/customers_spec.rb index 3a47110e11..090c38545b 100644 --- a/spec/requests/api/v1/customers_spec.rb +++ b/spec/requests/api/v1/customers_spec.rb @@ -148,6 +148,7 @@ describe "Customers", type: :request do email: "alice@example.com", enterprise_id: enterprise1.id, tags: ["staff", "discount"], + shipping_address: CustomerSchema.address_example ) end end diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml index 84f42e1eed..c67e71b8a9 100644 --- a/swagger/v1/swagger.yaml +++ b/swagger/v1/swagger.yaml @@ -350,6 +350,25 @@ paths: 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: Victoria + country: Australia required: - enterprise_id - email @@ -435,6 +454,25 @@ paths: 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: Victoria + country: Australia required: - enterprise_id - email