Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Address Book API endpoints

Konrad Gorazd3147a8a9cd21
diff --git a/api/app/controllers/spree/api/v2/storefront/account/addresses_controller.rb b/api/app/controllers/spree/api/v2/storefront/account/addresses_controller.rb
new file mode 100644
index 0000000000..628042605a
--- /dev/null
+++ b/api/app/controllers/spree/api/v2/storefront/account/addresses_controller.rb
@@ -0,0 +1,70 @@
+module Spree
+ module Api
+ module V2
+ module Storefront
+ module Account
+ class AddressesController < ::Spree::Api::V2::BaseController
+ before_action :require_spree_current_user
+
+ def index
+ render_serialized_payload { serialize_collection(collection) }
+ end
+
+ def create
+ resource = collection.new(address_params)
+ if resource.save
+ render_serialized_payload { serialize_resource(resource) }
+ else
+ render_error_payload(Spree::ServiceModule::ResultError.new(resource.errors))
+ end
+ end
+
+ def update
+ if resource.update(address_params)
+ render_serialized_payload { serialize_resource(resource) }
+ else
+ render_error_payload(Spree::ServiceModule::ResultError.new(resource.errors))
+ end
+ end
+
+ private
+
+ def collection
+ spree_current_user.addresses
+ end
+
+ def resource
+ @resource ||= collection.find(params[:id])
+ end
+
+ def collection_serializer
+ Spree::Api::Dependencies.storefront_address_serializer.constantize
+ end
+
+ def resource_serializer
+ Spree::Api::Dependencies.storefront_address_serializer.constantize
+ end
+
+ def serialize_collection(collection)
+ collection_serializer.new(collection).serializable_hash
+ end
+
+ def address_params
+ replace_country_iso_with_id(params).require(:address).permit(permitted_address_attributes)
+ end
+
+ def replace_country_iso_with_id(params)
+ iso = params.dig(:address, :country_iso)
+ return params unless iso.present?
+
+ replaced_params = params
+ replaced_params[:address]['country_id'] = Spree::Country.find_by(iso: iso)&.id
+ replaced_params[:address].delete(:country_iso)
+ replaced_params
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/models/spree/api_dependencies.rb b/api/app/models/spree/api_dependencies.rb
index 340bae67ce..36a2b09952 100644
--- a/api/app/models/spree/api_dependencies.rb
+++ b/api/app/models/spree/api_dependencies.rb
@@ -14,7 +14,7 @@ module Spree
:storefront_collection_paginator, :storefront_user_serializer, :storefront_products_sorter, :storefront_products_finder,
:storefront_product_serializer, :storefront_taxon_serializer, :storefront_taxon_finder, :storefront_find_by_variant_finder,
:storefront_cart_update_service, :storefront_cart_estimate_shipping_rates_service, :storefront_estimated_shipment_serializer,
- :storefront_store_serializer
+ :storefront_store_serializer, :storefront_address_serializer
].freeze
attr_accessor *INJECTION_POINTS
@@ -50,6 +50,7 @@ module Spree
@storefront_checkout_get_shipping_rates_service = Spree::Dependencies.checkout_get_shipping_rates_service
# serializers
+ @storefront_address_serializer = 'Spree::V2::Storefront::AddressSerializer'
@storefront_cart_serializer = 'Spree::V2::Storefront::CartSerializer'
@storefront_credit_card_serializer = 'Spree::V2::Storefront::CreditCardSerializer'
@storefront_country_serializer = 'Spree::V2::Storefront::CountrySerializer'
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 95caf4397e..f14e3bf85b 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -152,6 +152,7 @@ Spree::Core::Engine.add_routes do
resource :account, controller: :account, only: %i[show]
namespace :account do
+ resources :addresses, controller: :addresses, only: %i[index create update]
resources :credit_cards, controller: :credit_cards, only: %i[index show]
resources :orders, controller: :orders, only: %i[index show]
end
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index 190012d09d..d07a26e798 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -131,6 +131,85 @@ paths:
$ref: '#/components/schemas/Error'
security:
- bearerAuth: []
+ '/account/addresses':
+ get:
+ description: >-
+ Returns a list of Addresses for the signed in User
+ tags:
+ - Account
+ operationId: 'Addresses list'
+ responses:
+ '200':
+ description: Listing user addresses.
+ content:
+ application/vnd.api+json:
+ schema:
+ $ref: '#/components/schemas/AddressList'
+ '403':
+ $ref: '#/components/responses/403Forbidden'
+ security:
+ - bearerAuth: []
+ post:
+ description: >-
+ Create a new Address for the signed in User
+ tags:
+ - Account
+ operationId: 'Create Address'
+ requestBody:
+ required: true
+ content:
+ application/vnd.api+json:
+ schema:
+ type: object
+ properties:
+ address:
+ $ref: '#/components/schemas/AddressPayload'
+ responses:
+ '200':
+ description: Created Address.
+ content:
+ application/vnd.api+json:
+ schema:
+ $ref: '#/components/schemas/AddressList'
+ '403':
+ $ref: '#/components/responses/403Forbidden'
+ security:
+ - bearerAuth: []
+ '/account/addresses/{id}':
+ patch:
+ description: >-
+ Update selected Address for the signed in User
+ tags:
+ - Account
+ operationId: 'Update Address'
+ parameters:
+ - name: id
+ in: path
+ required: true
+ description: Selected Address for update
+ schema:
+ type: string
+ example: '1'
+ requestBody:
+ required: true
+ content:
+ application/vnd.api+json:
+ schema:
+ type: object
+ properties:
+ address:
+ $ref: '#/components/schemas/AddressPayload'
+ responses:
+ '200':
+ description: Updated Address.
+ content:
+ application/vnd.api+json:
+ schema:
+ $ref: '#/components/schemas/AddressList'
+ '403':
+ $ref: '#/components/responses/403Forbidden'
+ security:
+ - bearerAuth: []
'/account/credit_cards':
get:
description: >-
@@ -1206,6 +1285,14 @@ components:
company:
type: string
example: 'Google Inc.'
+ AddressList:
+ required:
+ - data
+ properties:
+ data:
+ type: array
+ items:
+ $ref: '#/components/schemas/Address'
Cart:
required:
- data
diff --git a/api/spec/requests/spree/api/v2/storefront/account/addresses_spec.rb b/api/spec/requests/spree/api/v2/storefront/account/addresses_spec.rb
new file mode 100644
index 0000000000..178ada255c
--- /dev/null
+++ b/api/spec/requests/spree/api/v2/storefront/account/addresses_spec.rb
@@ -0,0 +1,177 @@
+require 'spec_helper'
+
+describe 'Storefront API v2 Addresses spec', type: :request do
+ let!(:user) { create(:user) }
+ let!(:params) { { user_id: user.id } }
+ let!(:addresses) { create_list(:address, 3, user_id: user.id) }
+ let!(:country) { create(:country, iso: 'GBR') }
+
+ shared_examples 'returns valid user addresses resource JSON' do
+ it 'returns a valid user addresses resource JSON response' do
+ expect(response.status).to eq(200)
+
+ expect(json_response['data'][0]).to have_type('address')
+ expect(json_response['data'][0]).to have_attribute(:firstname)
+ expect(json_response['data'][0]).to have_attribute(:lastname)
+ expect(json_response['data'][0]).to have_attribute(:address1)
+ expect(json_response['data'][0]).to have_attribute(:address2)
+ expect(json_response['data'][0]).to have_attribute(:city)
+ expect(json_response['data'][0]).to have_attribute(:zipcode)
+ expect(json_response['data'][0]).to have_attribute(:phone)
+ expect(json_response['data'][0]).to have_attribute(:state_name)
+ expect(json_response['data'][0]).to have_attribute(:company)
+ expect(json_response['data'][0]).to have_attribute(:country_name)
+ expect(json_response['data'][0]).to have_attribute(:country_iso3)
+ expect(json_response['data'][0]).to have_attribute(:country_iso)
+ expect(json_response['data'][0]).to have_attribute(:state_code)
+ end
+ end
+
+ include_context 'API v2 tokens'
+
+ describe 'addresses#index' do
+ context 'without options' do
+ before { get '/api/v2/storefront/account/addresses', headers: headers_bearer }
+
+ it_behaves_like 'returns valid user addresses resource JSON'
+
+ it 'returns all user addresses' do
+ expect(json_response['data'][0]).to have_type('address')
+ expect(json_response['data'].size).to eq(addresses.count)
+ end
+ end
+
+ context 'with missing authorization token' do
+ before { get '/api/v2/storefront/account/addresses' }
+
+ it_behaves_like 'returns 403 HTTP status'
+ end
+ end
+
+ describe 'addresses#create' do
+ context 'valid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: 'EAW',
+ country_iso: 'GBR'
+ }
+ end
+ let(:params) { { address: new_attributes } }
+
+ before { post '/api/v2/storefront/account/addresses', params: params, headers: headers_bearer }
+
+ it 'creates and returns address' do
+ expect(json_response['data']).to have_attribute(:firstname).with_value(new_attributes[:firstname])
+ expect(json_response['data']).to have_attribute(:lastname).with_value(new_attributes[:lastname])
+ expect(json_response['data']).to have_attribute(:address1).with_value(new_attributes[:address1])
+ expect(json_response['data']).to have_attribute(:address2).with_value(new_attributes[:address2])
+ expect(json_response['data']).to have_attribute(:city).with_value(new_attributes[:city])
+ expect(json_response['data']).to have_attribute(:phone).with_value(new_attributes[:phone])
+ expect(json_response['data']).to have_attribute(:zipcode).with_value(new_attributes[:zipcode])
+ expect(json_response['data']).to have_attribute(:state_name).with_value(new_attributes[:state_name])
+ expect(json_response['data']).to have_attribute(:country_iso).with_value(new_attributes[:country_iso])
+ expect(json_response.size).to eq(1)
+ end
+ end
+
+ context 'invalid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor'
+ }
+ end
+ let(:params) { { address: new_attributes } }
+
+ before { post '/api/v2/storefront/account/addresses', params: params, headers: headers_bearer }
+
+ it 'returns errors' do
+ expect(json_response['error']).to eq("City can't be blank, Country can't be blank, Zip Code can't be blank, Phone can't be blank")
+ expect(json_response['errors']).to eq(
+ 'city' => ["can't be blank"],
+ 'zipcode' => ["can't be blank"],
+ 'country' => ["can't be blank"],
+ 'phone' => ["can't be blank"]
+ )
+ end
+ end
+
+ context 'with missing authorization token' do
+ before { post '/api/v2/storefront/account/addresses' }
+
+ it_behaves_like 'returns 403 HTTP status'
+ end
+ end
+
+ describe 'addresses#update' do
+ let(:address) { addresses.last }
+
+ context 'valid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: 'EAW',
+ country_iso: 'GBR'
+ }
+ end
+ let(:params) { { address: new_attributes } }
+
+ before { patch "/api/v2/storefront/account/addresses/#{address.id}", params: params, headers: headers_bearer }
+
+ it 'updates and returns address' do
+ expect(json_response['data']).to have_id(address.id.to_s)
+ expect(json_response['data']).to have_attribute(:firstname).with_value(new_attributes[:firstname])
+ expect(json_response['data']).to have_attribute(:lastname).with_value(new_attributes[:lastname])
+ expect(json_response['data']).to have_attribute(:address1).with_value(new_attributes[:address1])
+ expect(json_response['data']).to have_attribute(:address2).with_value(new_attributes[:address2])
+ expect(json_response['data']).to have_attribute(:city).with_value(new_attributes[:city])
+ expect(json_response['data']).to have_attribute(:phone).with_value(new_attributes[:phone])
+ expect(json_response['data']).to have_attribute(:zipcode).with_value(new_attributes[:zipcode])
+ expect(json_response['data']).to have_attribute(:state_name).with_value(new_attributes[:state_name])
+ expect(json_response['data']).to have_attribute(:country_iso).with_value(new_attributes[:country_iso])
+ expect(json_response.size).to eq(1)
+ end
+ end
+
+ context 'invalid request' do
+ let(:new_attributes) do
+ {
+ city: '',
+ zipcode: ''
+ }
+ end
+ let(:params) { { address: new_attributes } }
+
+ before { patch "/api/v2/storefront/account/addresses/#{address.id}", params: params, headers: headers_bearer }
+
+ it 'returns errors' do
+ expect(json_response['error']).to eq("City can't be blank, Zip Code can't be blank")
+ expect(json_response['errors']).to eq(
+ 'city' => ["can't be blank"],
+ 'zipcode' => ["can't be blank"]
+ )
+ end
+ end
+
+ context 'with missing authorization token' do
+ before { patch "/api/v2/storefront/account/addresses/#{address.id}" }
+
+ it_behaves_like 'returns 403 HTTP status'
+ end
+ end
+end