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.

Close the gaps a connector finds on the paths it actually uses

Damian Legawiece414d57a831d
diff --git a/spree/api/app/controllers/spree/api/v3/admin/concerns/external_references.rb b/spree/api/app/controllers/spree/api/v3/admin/concerns/external_references.rb
index 90edd06d3f..134d4fde7d 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/concerns/external_references.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/concerns/external_references.rb
@@ -30,8 +30,20 @@ module Spree
module ExternalReferences
extend ActiveSupport::Concern
+ # Raised when a payload's external id already names a different
+ # record in this store.
+ class ConflictingReference < StandardError; end
+
included do
before_action :update_instead_when_external_id_known, only: :create
+
+ rescue_from ConflictingReference do |error|
+ render_error(
+ code: 'conflicting_external_reference',
+ message: error.message,
+ status: :unprocessable_content
+ )
+ end
end
protected
@@ -152,6 +164,12 @@ module Spree
resource.assign_external_references(external_reference_params)
resource.external_references.reload
+ rescue ActiveRecord::RecordInvalid => e
+ # The key already names a different record in this store. The
+ # resource itself is written by now, so this reports what could
+ # not be recorded rather than pretending the write failed.
+ raise Spree::Api::V3::Admin::Concerns::ExternalReferences::ConflictingReference,
+ e.record.errors.full_messages.to_sentence
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
index 0201cd2266..dad32457b0 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
@@ -23,6 +23,14 @@ module Spree
protected
+ # `url` and `signed_id` say how to *fetch* the bytes; neither is an
+ # attribute on the row. A re-synced asset already has its file, so
+ # the replay only carries them as leftovers from the create shape.
+ def normalize_upsert_params!
+ params.delete(:url)
+ params.delete(:signed_id)
+ end
+
def model_class
Spree::Media
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb
index b08e654c4b..9b8500ee14 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb
@@ -161,6 +161,11 @@ module Spree
external = reference.is_a?(Hash) ? reference[:external_id] : nil
return if external.blank?
+ # Only the `{ system => id }` map addresses a record; a bare string
+ # names no system, so it resolves to nothing and the row is
+ # reported as invalid rather than raising.
+ return unless external.respond_to?(:to_h) && !external.is_a?(String)
+
system, external_id = external.to_h.first
scope.find_by_external_id(system, external_id)&.id
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/external_references_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/external_references_spec.rb
index b67c19b4ac..2116c4f09a 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/external_references_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/external_references_spec.rb
@@ -170,6 +170,57 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, 'upsert by external id',
end
end
+RSpec.describe Spree::Api::V3::Admin::MediaController, 'upsert by external id', type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:product) { create(:product) }
+
+ before { request.headers.merge!(headers) }
+
+ # `url` and `signed_id` say how to fetch the bytes; neither is an attribute
+ # on the row. A DAM re-posting an asset it already synced must not have them
+ # reinterpreted as attributes by the update action.
+ it 'accepts a replay carrying the create payload\'s fetch keys' do
+ media = create(:image, viewable: product)
+ media.set_external_id('dam', 'ASSET-1')
+
+ expect do
+ post :create, params: {
+ product_id: product.prefixed_id,
+ url: 'https://dam.example.com/asset-1.jpg',
+ alt: 'A widget',
+ external_references: { dam: 'ASSET-1' }
+ }, as: :json
+ end.not_to change(Spree::Media, :count)
+
+ expect(response).to have_http_status(:ok)
+ expect(media.reload.alt).to eq('A widget')
+ end
+end
+
+RSpec.describe Spree::Api::V3::Admin::CategoriesController, 'conflicting external id', type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ before { request.headers.merge!(headers) }
+
+ it 'reports a key that already names another record rather than raising' do
+ taken = create(:category)
+ taken.set_external_id('pim', 'CAT-1')
+
+ patch :update, params: {
+ id: create(:category).prefixed_id,
+ external_references: { pim: 'CAT-1' }
+ }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(json_response['error']['code']).to eq('conflicting_external_reference')
+ end
+end
+
RSpec.describe Spree::Api::V3::Admin::CompanyLocationsController, type: :controller do
render_views
diff --git a/spree/core/app/models/spree/media.rb b/spree/core/app/models/spree/media.rb
index ff4f490fa6..aa942d7cf3 100644
--- a/spree/core/app/models/spree/media.rb
+++ b/spree/core/app/models/spree/media.rb
@@ -383,6 +383,7 @@ module Spree
errors.add(:external_video_url, :unsupported_video_provider)
end
+
def touch_product_variants
product = viewable.is_a?(Spree::Product) ? viewable : viewable.product
product.variants.touch_all
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 14babbf2ee..4af296ac25 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -540,6 +540,7 @@ en:
messages:
already_confirmed: was already confirmed
invalid_document_number_affix: 'can only contain uppercase letters, digits, dashes and #'
+ invalid_system_key: can only contain lowercase letters, digits and underscores
locked_after_first_number: cannot change once numbering has started
not_a_number: is not a number
not_found: not found