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.

Make the storefront name the registration it means

Łukasz Adamczyk51394ad39670
diff --git a/spree/api/app/controllers/spree/api/v3/store/carts/tax_identifiers_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts/tax_identifiers_controller.rb
index 61e33210d1..9b1046a93a 100644
--- a/spree/api/app/controllers/spree/api/v3/store/carts/tax_identifiers_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/carts/tax_identifiers_controller.rb
@@ -19,6 +19,12 @@ module Spree
before_action :find_cart!
+ # Both fields, every time. A PUT carrying only `kind` used to reuse
+ # the number already on the cart, relabelling it under a regime it
+ # was never issued for — and that claim then drives tax and is
+ # frozen onto the order at completion.
+ before_action :require_kind_and_value!, only: :update
+
# GET /api/v3/store/carts/:cart_id/tax_identifier
#
# The registration this cart will be taxed against — the override
@@ -69,6 +75,19 @@ module Spree
def permitted_params
params.permit(:kind, :value)
end
+
+ private
+
+ def require_kind_and_value!
+ missing = %i[kind value].select { |field| params[field].blank? }
+ return if missing.empty?
+
+ render_error(
+ code: ERROR_CODES[:parameter_missing],
+ message: "#{missing.join(' and ')} required — a registration is a kind and its number together",
+ status: :unprocessable_content
+ )
+ end
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/customer/tax_identifiers_controller.rb b/spree/api/app/controllers/spree/api/v3/store/customer/tax_identifiers_controller.rb
index c772e68276..b826ff32aa 100644
--- a/spree/api/app/controllers/spree/api/v3/store/customer/tax_identifiers_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/customer/tax_identifiers_controller.rb
@@ -12,6 +12,12 @@ module Spree
class TaxIdentifiersController < Store::BaseController
prepend_before_action :require_authentication!
+ # The singular endpoints address one registration, and a buyer can
+ # hold several. Without a kind, `first` picked whichever row the
+ # database happened to return — so a DELETE removed a registration
+ # the buyer never named and could not predict.
+ before_action :require_kind!, only: %i[show update destroy]
+
# GET /api/v3/store/customers/me/tax_identifiers
#
# Everything the buyer could choose between at checkout. Small and
@@ -64,12 +70,18 @@ module Spree
private
+ def require_kind!
+ return if params[:kind].present?
+
+ render_error(
+ code: ERROR_CODES[:parameter_missing],
+ message: 'kind is required — name the registration to read, replace or remove',
+ status: :unprocessable_content
+ )
+ end
+
def tax_identifier
- @tax_identifier ||= if params[:kind].present?
- current_user.tax_identifiers.for_kind(params[:kind]).first
- else
- current_user.tax_identifiers.first
- end
+ @tax_identifier ||= current_user.tax_identifiers.for_kind(params[:kind]).first
end
end
end
diff --git a/spree/api/spec/requests/spree/api/v3/store/tax_identifiers_spec.rb b/spree/api/spec/requests/spree/api/v3/store/tax_identifiers_spec.rb
index 7b15030b1c..1411f71368 100644
--- a/spree/api/spec/requests/spree/api/v3/store/tax_identifiers_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/store/tax_identifiers_spec.rb
@@ -19,15 +19,32 @@ RSpec.describe 'Store tax identifiers', type: :request do
expect(response).to have_http_status(:ok)
expect(user.tax_identifiers.count).to eq(1)
- get '/api/v3/store/customers/me/tax_identifier', headers: headers, as: :json
+ get '/api/v3/store/customers/me/tax_identifier',
+ params: { kind: 'eu_vat' }, headers: headers, as: :json
expect(JSON.parse(response.body)['value']).to eq('DE987654321')
- delete '/api/v3/store/customers/me/tax_identifier', headers: headers, as: :json
+ delete '/api/v3/store/customers/me/tax_identifier',
+ params: { kind: 'eu_vat' }, headers: headers, as: :json
expect(response).to have_http_status(:no_content)
- get '/api/v3/store/customers/me/tax_identifier', headers: headers, as: :json
+ get '/api/v3/store/customers/me/tax_identifier',
+ params: { kind: 'eu_vat' }, headers: headers, as: :json
expect(response).to have_http_status(:not_found)
end
+
+ # A buyer can hold several registrations, so the singular endpoints have to
+ # be told which one. Guessing meant a DELETE removed whichever row came back
+ # first — not the buyer's choice, and not predictable.
+ it 'refuses to guess which registration was meant' do
+ create(:tax_identifier, customer: user, kind: 'eu_vat', value: 'DE123456789')
+ create(:tax_identifier, customer: user, kind: 'gb_vat', value: 'GB123456789')
+
+ delete '/api/v3/store/customers/me/tax_identifier', headers: headers, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(JSON.parse(response.body)['error']['code']).to eq('parameter_missing')
+ expect(user.tax_identifiers.count).to eq(2)
+ end
end
describe 'a checkout override on the cart' do
@@ -75,6 +92,25 @@ RSpec.describe 'Store tax identifiers', type: :request do
expect(response).to have_http_status(:not_found)
end
+ # The claim this endpoint stores drives tax and is frozen onto the order at
+ # completion, so it must never be half-replaced: sending a new kind alone
+ # used to relabel the number already there under a regime it was not issued
+ # for.
+ it 'refuses to relabel the existing number under a new kind' do
+ create(:tax_identifier, customer: nil, cart: cart, kind: 'eu_vat', value: 'DE999999999')
+
+ put "/api/v3/store/carts/#{cart.prefixed_id}/tax_identifier",
+ params: { kind: 'gb_vat' },
+ headers: headers.merge('x-spree-token' => cart.token), as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(JSON.parse(response.body)['error']['code']).to eq('parameter_missing')
+
+ override = cart.reload.tax_identifier
+ expect(override.kind).to eq('eu_vat')
+ expect(override.value).to eq('DE999999999')
+ end
+
it 'is stored against the cart and read back' do
put "/api/v3/store/carts/#{cart.prefixed_id}/tax_identifier",
params: { kind: 'eu_vat', value: 'DE555555555' },