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.

Fixed second review findings on seller digital assets

Damian Legawiec5d0c1457fd06
diff --git a/docs/api-reference/admin.yaml b/docs/api-reference/admin.yaml
index 266e381673..975eee2448 100644
--- a/docs/api-reference/admin.yaml
+++ b/docs/api-reference/admin.yaml
@@ -79252,8 +79252,7 @@ components:
signed_id:
type: string
description: 'A `signed_id` from `files.upload()` with `visibility: ''private''`.
- Required for an uploaded file, omitted for a provider-backed asset. On
- update it replaces the file for everyone who already bought it.'
+ On update it replaces the file for everyone who already bought it.'
variant_id:
type: string
description: Defaults to the product's default variant when omitted.
@@ -79268,7 +79267,7 @@ components:
provider_type:
type: string
description: A provider shorthand from the providers endpoint. Omit for
- an uploaded file.
+ an uploaded file; a provider-backed asset sends no `signed_id`.
provider_settings:
type: object
additionalProperties: true
@@ -79279,8 +79278,7 @@ components:
signed_id:
type: string
description: 'A `signed_id` from `files.upload()` with `visibility: ''private''`.
- Required for an uploaded file, omitted for a provider-backed asset. On
- update it replaces the file for everyone who already bought it.'
+ On update it replaces the file for everyone who already bought it.'
variant_id:
type: string
description: Defaults to the product's default variant when omitted.
@@ -79295,7 +79293,7 @@ components:
provider_type:
type: string
description: A provider shorthand from the providers endpoint. Omit for
- an uploaded file.
+ an uploaded file; a provider-backed asset sends no `signed_id`.
provider_settings:
type: object
additionalProperties: true
diff --git a/docs/api-reference/seller.yaml b/docs/api-reference/seller.yaml
index 0e5dbed390..cd80fc8b2a 100644
--- a/docs/api-reference/seller.yaml
+++ b/docs/api-reference/seller.yaml
@@ -31835,8 +31835,7 @@ components:
signed_id:
type: string
description: 'A `signed_id` from `files.upload()` with `visibility: ''private''`.
- Required for an uploaded file, omitted for a provider-backed asset. On
- update it replaces the file for everyone who already bought it.'
+ On update it replaces the file for everyone who already bought it.'
variant_id:
type: string
description: Defaults to the product's default variant when omitted.
@@ -31848,14 +31847,15 @@ components:
type: integer
description: Null means the store's download settings apply.
nullable: true
+ required:
+ - signed_id
DigitalAssetUpdateParams:
type: object
properties:
signed_id:
type: string
description: 'A `signed_id` from `files.upload()` with `visibility: ''private''`.
- Required for an uploaded file, omitted for a provider-backed asset. On
- update it replaces the file for everyone who already bought it.'
+ On update it replaces the file for everyone who already bought it.'
variant_id:
type: string
description: Defaults to the product's default variant when omitted.
diff --git a/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts b/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts
index 567fdf1f6e..f2b322b92d 100644
--- a/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts
+++ b/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts
@@ -2,9 +2,8 @@
export type DigitalAssetCreateParams = {
/**
- * A `signed_id` from `files.upload()` with `visibility: 'private'`. Required for an uploaded
- * file, omitted for a provider-backed asset. On update it replaces the file for everyone who
- * already bought it.
+ * A `signed_id` from `files.upload()` with `visibility: 'private'`. On update it replaces the
+ * file for everyone who already bought it.
*/
signed_id?: string
/** Defaults to the product's default variant when omitted. */
@@ -13,7 +12,10 @@ export type DigitalAssetCreateParams = {
authorized_clicks?: number | null
/** Null means the store's download settings apply. */
authorized_days?: number | null
- /** A provider shorthand from the providers endpoint. Omit for an uploaded file. */
+ /**
+ * A provider shorthand from the providers endpoint. Omit for an uploaded file; a provider-backed
+ * asset sends no `signed_id`.
+ */
provider_type?: string
/** Values for the provider's settings schema, keyed by field name. */
provider_settings?: Record<string, unknown>
diff --git a/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts b/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts
index fbe81f7d36..d2c8ff86e5 100644
--- a/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts
+++ b/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts
@@ -2,9 +2,8 @@
export type DigitalAssetUpdateParams = {
/**
- * A `signed_id` from `files.upload()` with `visibility: 'private'`. Required for an uploaded
- * file, omitted for a provider-backed asset. On update it replaces the file for everyone who
- * already bought it.
+ * A `signed_id` from `files.upload()` with `visibility: 'private'`. On update it replaces the
+ * file for everyone who already bought it.
*/
signed_id?: string
/** Defaults to the product's default variant when omitted. */
@@ -13,7 +12,10 @@ export type DigitalAssetUpdateParams = {
authorized_clicks?: number | null
/** Null means the store's download settings apply. */
authorized_days?: number | null
- /** A provider shorthand from the providers endpoint. Omit for an uploaded file. */
+ /**
+ * A provider shorthand from the providers endpoint. Omit for an uploaded file; a provider-backed
+ * asset sends no `signed_id`.
+ */
provider_type?: string
/** Values for the provider's settings schema, keyed by field name. */
provider_settings?: Record<string, unknown>
diff --git a/packages/dashboard-core/src/products/assignable-variants.ts b/packages/dashboard-core/src/products/assignable-variants.ts
index f11c66b96a..5697949eb7 100644
--- a/packages/dashboard-core/src/products/assignable-variants.ts
+++ b/packages/dashboard-core/src/products/assignable-variants.ts
@@ -5,15 +5,15 @@ interface LabelledVariant {
id: string
options_text?: string | null
sku?: string | null
+ name?: string | null
}
/** The name a variant goes by in a picker. */
export function variantLabel(variant: LabelledVariant): string {
- const named = variant as LabelledVariant & { name?: string }
return (
- named.options_text ||
- named.name ||
- named.sku ||
+ variant.options_text ||
+ variant.name ||
+ variant.sku ||
variant.id ||
i18n.t('admin.products.variants.variant_label')
)
diff --git a/packages/dashboard-core/src/products/digital-assets-card.tsx b/packages/dashboard-core/src/products/digital-assets-card.tsx
index 6f0dacef0b..f7bebf59a9 100644
--- a/packages/dashboard-core/src/products/digital-assets-card.tsx
+++ b/packages/dashboard-core/src/products/digital-assets-card.tsx
@@ -1,5 +1,11 @@
import type { DigitalAssetProvider } from '@spree/admin-sdk'
-import { formatFileSize, Subject, useFileUpload, usePermissions } from '@spree/dashboard-core'
+import {
+ formatFileSize,
+ getApiClient,
+ Subject,
+ useFileUpload,
+ usePermissions,
+} from '@spree/dashboard-core'
import {
Button,
Card,
@@ -74,6 +80,8 @@ export function DigitalAssetsCard({
const assets: PanelDigitalAsset[] = data?.data ?? []
const meta = data?.meta
+ // Across every page, so emptying a later page does not read as no files.
+ const hasAssets = (meta?.count ?? assets.length) > 0
const hasVariants = (variants?.length ?? 0) > 1
const providers = providersData?.data ?? []
// Only providers that resolve their own deliverable go in the source menu;
@@ -150,7 +158,9 @@ export function DigitalAssetsCard({
}),
variant: 'destructive',
})
- if (confirmed) await deleteAsset.mutateAsync(asset.id)
+ if (!confirmed) return
+ await deleteAsset.mutateAsync(asset.id)
+ if (assets.length === 1 && page > 1) setPage(page - 1)
}
// A blank limit means the store's setting applies — show the number actually
@@ -164,7 +174,7 @@ export function DigitalAssetsCard({
return variant ? variantLabel(variant) : '—'
}
- if (!productId) return null
+ if (!productId || !getApiClient().digitalAssets) return null
return (
<Card>
@@ -175,7 +185,7 @@ export function DigitalAssetsCard({
</CardTitle>
{/* The header action only appears once files exist — an empty card
invites the first upload through its dropzone, not a button. */}
- {canWrite && assets.length > 0 && (
+ {canWrite && hasAssets && (
<CardAction>
{sourceProviders.length === 0 ? (
<Button
@@ -234,9 +244,9 @@ export function DigitalAssetsCard({
{isLoading ? (
<Skeleton className="h-24 w-full" />
- ) : assets.length === 0 && !canWrite ? (
+ ) : !hasAssets && !canWrite ? (
<p className="text-muted-foreground text-sm">{t('admin.digital_assets.empty')}</p>
- ) : assets.length === 0 ? (
+ ) : !hasAssets ? (
<button
type="button"
onDrop={handleDrop}
diff --git a/packages/dashboard-core/src/products/use-digital-assets.ts b/packages/dashboard-core/src/products/use-digital-assets.ts
index 41f6361210..84c64348c4 100644
--- a/packages/dashboard-core/src/products/use-digital-assets.ts
+++ b/packages/dashboard-core/src/products/use-digital-assets.ts
@@ -15,7 +15,7 @@ export function useDigitalAssets(productId: string, page = 1, enabled = true) {
return useQuery({
queryKey: useResourceKey('products', productId, 'digital-assets', page),
queryFn: () => digitalAssetsClient().list(productId, { page, limit: 25 }),
- enabled: enabled && Boolean(productId),
+ enabled: enabled && Boolean(productId) && Boolean(getApiClient().digitalAssets),
})
}
@@ -25,7 +25,8 @@ export function useDigitalAssets(productId: string, page = 1, enabled = true) {
export function useDigitalAssetProviders(productId: string, enabled = true) {
const providers = getApiClient().digitalAssets?.providers
return useQuery({
- queryKey: useResourceKey('products', productId, 'digital-asset-providers'),
+ // Not keyed by product: the registry is the same for all of them.
+ queryKey: useResourceKey('digital-asset-providers'),
queryFn: () => providers!(productId),
enabled: enabled && Boolean(productId) && Boolean(providers),
staleTime: Number.POSITIVE_INFINITY, // the registry doesn't change at runtime
diff --git a/packages/seller-dashboard/src/api-client.ts b/packages/seller-dashboard/src/api-client.ts
index b90d1df13b..3307f6cf7d 100644
--- a/packages/seller-dashboard/src/api-client.ts
+++ b/packages/seller-dashboard/src/api-client.ts
@@ -148,8 +148,11 @@ export function createSellerApiClient({
},
digitalAssets: {
list: (productId, params) => sellerClient().products.digitalAssets.list(productId, params),
- create: (productId, params) =>
- sellerClient().products.digitalAssets.create(productId, params),
+ // Sellers upload files only, so a file is the one thing a new asset must carry.
+ create: (productId, { signed_id, ...params }) => {
+ if (!signed_id) throw new Error('Upload a file first.')
+ return sellerClient().products.digitalAssets.create(productId, { ...params, signed_id })
+ },
update: (productId, id, params) =>
sellerClient().products.digitalAssets.update(productId, id, params),
delete: (productId, id) => sellerClient().products.digitalAssets.delete(productId, id),
diff --git a/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts b/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts
index bf6c6f148b..16ced544b4 100644
--- a/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts
+++ b/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts
@@ -2,11 +2,10 @@
export type DigitalAssetCreateParams = {
/**
- * A `signed_id` from `files.upload()` with `visibility: 'private'`. Required for an uploaded
- * file, omitted for a provider-backed asset. On update it replaces the file for everyone who
- * already bought it.
+ * A `signed_id` from `files.upload()` with `visibility: 'private'`. On update it replaces the
+ * file for everyone who already bought it.
*/
- signed_id?: string
+ signed_id: string
/** Defaults to the product's default variant when omitted. */
variant_id?: string
/** Null means the store's download settings apply. */
diff --git a/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts b/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts
index 46f2441a10..b99fba3d2d 100644
--- a/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts
+++ b/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts
@@ -2,9 +2,8 @@
export type DigitalAssetUpdateParams = {
/**
- * A `signed_id` from `files.upload()` with `visibility: 'private'`. Required for an uploaded
- * file, omitted for a provider-backed asset. On update it replaces the file for everyone who
- * already bought it.
+ * A `signed_id` from `files.upload()` with `visibility: 'private'`. On update it replaces the
+ * file for everyone who already bought it.
*/
signed_id?: string
/** Defaults to the product's default variant when omitted. */
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 4df06f595d..e25c7403a7 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
@@ -12,7 +12,7 @@ module Spree
body.include_attributes Spree::Api::V3::DigitalAssetRequestSchema
# The model validates it names a registered provider.
body.attribute :provider_type, :string,
- description: 'A provider shorthand from the providers endpoint. Omit for an uploaded file.'
+ description: 'A provider shorthand from the providers endpoint. Omit for an uploaded file; a provider-backed asset sends no `signed_id`.'
# Its keys are declared by the provider, so it lands verbatim
# under one key in the asset's metadata.
body.attribute :provider_settings, :hash,
diff --git a/spree/api/app/controllers/spree/api/v3/seller/products/digital_assets_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/products/digital_assets_controller.rb
index 2491d918d3..283e48aec7 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/products/digital_assets_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/products/digital_assets_controller.rb
@@ -17,6 +17,19 @@ module Spree
request_params :digital_asset do |body|
body.include_attributes Spree::Api::V3::DigitalAssetRequestSchema
+ body.require_attributes :signed_id, on: :create
+ end
+
+ # PATCH /api/v3/seller/products/:product_id/digital_assets/:id
+ #
+ # A provider-backed asset delivers through the operator's provider,
+ # so a file attached to it would show here but never reach a buyer.
+ def update
+ if params[:signed_id].present? && [email protected]_class.requires_attachment?
+ return render_validation_error(I18n.t('spree.digital_assets_provider_takes_no_file'))
+ end
+
+ super
end
protected
diff --git a/spree/api/app/request_schemas/spree/api/v3/digital_asset_request_schema.rb b/spree/api/app/request_schemas/spree/api/v3/digital_asset_request_schema.rb
index a97e68b770..00f79c3fe9 100644
--- a/spree/api/app/request_schemas/spree/api/v3/digital_asset_request_schema.rb
+++ b/spree/api/app/request_schemas/spree/api/v3/digital_asset_request_schema.rb
@@ -7,7 +7,7 @@ module Spree
# Names a blob rather than a column, so it is attached separately.
attribute :signed_id, :string,
permit: false,
- description: "A `signed_id` from `files.upload()` with `visibility: 'private'`. Required for an uploaded file, omitted for a provider-backed asset. On update it replaces the file for everyone who already bought it."
+ description: "A `signed_id` from `files.upload()` with `visibility: 'private'`. On update it replaces the file for everyone who already bought it."
# Resolved through the parent rather than assigned, so an asset can't
# be pointed at a variant of another product or store.
attribute :variant_id, :id,
diff --git a/spree/api/app/serializers/concerns/spree/api/v3/digital_asset_details.rb b/spree/api/app/serializers/concerns/spree/api/v3/digital_asset_details.rb
index a0c9776d0f..5b8bd556e2 100644
--- a/spree/api/app/serializers/concerns/spree/api/v3/digital_asset_details.rb
+++ b/spree/api/app/serializers/concerns/spree/api/v3/digital_asset_details.rb
@@ -8,8 +8,12 @@ module Spree
module DigitalAssetDetails
extend ActiveSupport::Concern
- # The request's store; the asset's own store covers serialization
- # outside a request.
+ # The store whose download settings apply: the request's, or the
+ # asset's own outside a request.
+ #
+ # @param digital_asset [Spree::DigitalAsset]
+ # @param params [Hash, nil] the serializer params
+ # @return [Spree::Store]
def self.store_for(digital_asset, params)
params&.dig(:store) || Spree::Current.store || digital_asset.store
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 d5648fe181..f7f16c7abb 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
@@ -179,6 +179,26 @@ RSpec.describe Spree::Api::V3::Admin::Products::DigitalAssetsController, type: :
end
end
+ # Changing a product's files is a change to the product.
+ context 'with a role that can only view products' do
+ include_context 'API v3 Admin with custom permissions'
+
+ let(:custom_permissions) { %w[read_products] }
+ let!(:digital_asset) { create(:digital_asset, variant: variant) }
+
+ it 'lists the files' do
+ get :index, params: { product_id: product.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'cannot add one' do
+ post :create, params: { product_id: product.prefixed_id, signed_id: blob.signed_id }, as: :json
+
+ expect(response).to have_http_status(:forbidden)
+ end
+ end
+
context 'with a product from another store' do
let(:other_product) { create(:product, store: create(:store)) }
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/products/digital_assets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/products/digital_assets_controller_spec.rb
index cae7da1523..076805390b 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/products/digital_assets_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/products/digital_assets_controller_spec.rb
@@ -102,6 +102,18 @@ RSpec.describe Spree::Api::V3::Seller::Products::DigitalAssetsController, type:
after { Spree.digital_asset_providers.delete(stub_provider) }
+ it "refuses a file on an asset the operator's provider delivers" do
+ digital_asset = create(:digital_asset, variant: variant)
+ digital_asset.attachment.purge
+ digital_asset.update!(provider_type: 'Spree::DigitalAssetProvider::Stub')
+
+ patch :update, params: { product_id: product.prefixed_id, id: digital_asset.prefixed_id,
+ signed_id: blob.signed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(digital_asset.reload.attachment).not_to be_attached
+ end
+
it 'cannot route a sale through it' do
post :create, params: { product_id: product.prefixed_id, provider_type: 'stub' }, as: :json
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 8e3993f9a3..4d82e6f605 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -822,6 +822,7 @@ en:
digital_delivery: Digital Delivery
digital_assets: Digital assets
digital_assets_attachment_must_be_private: Upload the file to private storage before attaching it
+ digital_assets_provider_takes_no_file: This file is delivered by the marketplace's own system, so a replacement file cannot be uploaded
discontinued: Discontinued
discontinued_variants_present: Some line items in this order have products that are no longer available.
display_delivery_range: Delivery in %{delivery_range} business days