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.

Reconcile inline digital files by id and fix their error message

Damian Legawiec6c42c4e3fa73
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
index a113494cd8..068d7ad4f5 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
@@ -129,7 +129,7 @@ module Spree
end
def render_private_storage_error
- render_validation_error(Spree.t('digital_assets.attachment_must_be_private'))
+ render_validation_error(Spree.t(:digital_assets_attachment_must_be_private))
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
index 661f1ad172..7af7b2edfb 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
@@ -104,6 +104,9 @@ RSpec.describe Spree::Api::V3::Admin::Products::DigitalAssetsController, type: :
expect(response).to have_http_status(:unprocessable_content)
expect(Spree::DigitalAsset.count).to eq(0)
+ # The refusal carries a real, translated message rather than a missing
+ # translation placeholder.
+ expect(response.parsed_body.dig('error', 'message')).to match(/private storage/)
end
it 'rejects a non-positive limit' do
diff --git a/spree/core/app/workflows/spree/products/nested_attributes.rb b/spree/core/app/workflows/spree/products/nested_attributes.rb
index 97258361c5..2a121868cd 100644
--- a/spree/core/app/workflows/spree/products/nested_attributes.rb
+++ b/spree/core/app/workflows/spree/products/nested_attributes.rb
@@ -186,11 +186,14 @@ module Spree
end
# Attaches downloadable files a product write ships inline, so a new
- # product can carry its digital files in the same request. Create-only,
- # additive: each entry carries a `signed_id` (an uploaded file) or a
+ # product can carry its digital files in the same request. Additive, like
+ # {#apply_media}: each entry carries a `signed_id` (an uploaded file) or a
# `provider_type` (a provider-backed asset), and attaches to the default
- # variant unless `variant_id` names another. Edits and deletes go through
- # the nested digital_assets endpoints.
+ # variant unless `variant_id` names another. An entry carrying an `id`
+ # patches that asset rather than building a duplicate — so a client
+ # replaying a product's existing digital_assets list on update does not
+ # accumulate copies. Omitting an entry leaves it alone; removal is the
+ # nested digital_assets DELETE endpoint's job.
#
# @param product [Spree::Product]
# @param digital_assets_params [Array<Hash>, nil]
@@ -201,18 +204,31 @@ module Spree
digital_assets_params.each do |raw|
attrs = (raw.respond_to?(:to_h) ? raw.to_h : raw).with_indifferent_access
+ asset_id = attrs.delete(:id)
signed_id = attrs.delete(:signed_id)
variant_id = attrs.delete(:variant_id)
- target = variant_id.present? ? product.variants.find_by_param!(variant_id) : product.default_variant
- next unless target
- asset = target.digital_assets.build(attrs.except(:id))
+ # An id patches an existing asset; without one this is a new asset on
+ # the named variant (or the default). A patch for an asset that is
+ # gone is skipped rather than recreated.
+ if asset_id.present?
+ asset = product.digital_assets.find_by_param(asset_id) || next
+ else
+ target = variant_id.present? ? product.variants.find_by_param!(variant_id) : product.default_variant
+ next unless target
+
+ asset = target.digital_assets.build
+ end
+
+ asset.assign_attributes(attrs)
+
if signed_id.present?
# Attaching never moves a blob between storage services, so a file
# uploaded to the public bucket would stay publicly readable. Refuse
# it rather than attaching it wrongly.
blob = ActiveStorage::Blob.find_signed(signed_id)
unless blob && blob.service_name.to_s == Spree.private_storage_service_name.to_s
+ asset.errors.add(:attachment, Spree.t(:digital_assets_attachment_must_be_private))
raise ActiveRecord::RecordInvalid.new(asset)
end
diff --git a/spree/core/spec/workflows/spree/products/lifecycle_spec.rb b/spree/core/spec/workflows/spree/products/lifecycle_spec.rb
index ca962051e3..bd9b99809c 100644
--- a/spree/core/spec/workflows/spree/products/lifecycle_spec.rb
+++ b/spree/core/spec/workflows/spree/products/lifecycle_spec.rb
@@ -137,7 +137,7 @@ module Spree
store: store,
attributes: { name: 'Wrong bucket', digital_assets: [{ signed_id: public_blob.signed_id }] }
)
- end.to raise_error(ActiveRecord::RecordInvalid)
+ end.to raise_error(ActiveRecord::RecordInvalid, /private storage/)
end.to change(Spree::Product, :count).by(0).and change(Spree::DigitalAsset, :count).by(0)
end
end
@@ -183,6 +183,24 @@ module Spree
expect(result).to be_failure
expect(product.reload.name).to eq('Original')
end
+
+ # A client replaying the product's own digital_assets list (importer, host
+ # app, direct PATCH) must not accumulate copies: an entry carrying an id
+ # patches that asset in place rather than building a duplicate.
+ context 'with an inline digital_assets entry carrying an id' do
+ let!(:asset) { create(:digital_asset, variant: product.default_variant, authorized_clicks: 2) }
+
+ it 'patches the existing asset instead of duplicating it' do
+ expect do
+ described_class.call(
+ product: product,
+ attributes: { digital_assets: [{ id: asset.prefixed_id, authorized_clicks: 9 }] }
+ )
+ end.not_to change(Spree::DigitalAsset, :count)
+
+ expect(asset.reload.authorized_clicks).to eq(9)
+ end
+ end
end
describe Products::Destroy do