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.

Added replacing a digital file from the Digital files card

Damian Legawiec622598a7379c
diff --git a/.changeset/seller-digital-assets.md b/.changeset/seller-digital-assets.md
index 9a79404c94..29fd4121c1 100644
--- a/.changeset/seller-digital-assets.md
+++ b/.changeset/seller-digital-assets.md
@@ -5,4 +5,4 @@
"@spree/dashboard": patch
---
-Sellers can manage the downloadable files on their own digital products. The Seller SDK adds `products.digitalAssets` (`list`, `get`, `create`, `update`, `delete`), and the seller panel's product page shows the same Digital files card as the dashboard: upload, replace and delete files, and set per-file download limits. The card now lives in `@spree/dashboard-core` as `DigitalAssetsCard`, and a panel enables it by registering `digitalAssets` on its API client.
+Sellers can manage the downloadable files on their own digital products. The Seller SDK adds `products.digitalAssets` (`list`, `get`, `create`, `update`, `delete`), and the seller panel's product page shows the same Digital files card as the dashboard: upload, replace and delete files, and set per-file download limits. A file the marketplace delivers through its own provider is listed but cannot be replaced by the seller. The card now lives in `@spree/dashboard-core` as `DigitalAssetsCard`, and a panel enables it by registering `digitalAssets` on its API client.
diff --git a/docs/api-reference/admin.yaml b/docs/api-reference/admin.yaml
index 975eee2448..b256c8f3a9 100644
--- a/docs/api-reference/admin.yaml
+++ b/docs/api-reference/admin.yaml
@@ -79255,7 +79255,8 @@ components:
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.
+ description: On create, defaults to the product's default variant when omitted.
+ On update, omitting it keeps the current variant.
authorized_clicks:
type: integer
description: Null means the store's download settings apply.
@@ -79281,7 +79282,8 @@ components:
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.
+ description: On create, defaults to the product's default variant when omitted.
+ On update, omitting it keeps the current variant.
authorized_clicks:
type: integer
description: Null means the store's download settings apply.
diff --git a/docs/api-reference/seller.yaml b/docs/api-reference/seller.yaml
index cd80fc8b2a..3bdff3d075 100644
--- a/docs/api-reference/seller.yaml
+++ b/docs/api-reference/seller.yaml
@@ -31838,7 +31838,8 @@ components:
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.
+ description: On create, defaults to the product's default variant when omitted.
+ On update, omitting it keeps the current variant.
authorized_clicks:
type: integer
description: Null means the store's download settings apply.
@@ -31858,7 +31859,8 @@ components:
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.
+ description: On create, defaults to the product's default variant when omitted.
+ On update, omitting it keeps the current variant.
authorized_clicks:
type: integer
description: Null means the store's download settings apply.
diff --git a/docs/developer/how-to/sell-digital-products.mdx b/docs/developer/how-to/sell-digital-products.mdx
index fe27b67f28..af63d16204 100644
--- a/docs/developer/how-to/sell-digital-products.mdx
+++ b/docs/developer/how-to/sell-digital-products.mdx
@@ -60,7 +60,7 @@ Leave `authorized_clicks` and `authorized_days` off to inherit the store's downl
A [seller](/developer/core-concepts/sellers) attaches files to their own products the same way, from the **Digital files** card on the product page in their seller panel, or through the Seller API. They can upload, replace and delete files and set per-file download limits. A product belonging to another seller answers `404`.
-Sellers upload files only. A [custom digital asset provider](/developer/how-to/custom-digital-asset-provider) is your own integration, so sellers cannot pick one. A file you attached with a provider still appears on the seller's product, without its provider settings.
+Sellers upload files only. A [custom digital asset provider](/developer/how-to/custom-digital-asset-provider) is your own integration, so sellers cannot pick one. A file you attached with a provider still appears on the seller's product, without its provider settings, and the seller cannot replace it.
<Tabs>
<Tab title="Seller SDK">
diff --git a/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts b/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts
index f2b322b92d..9ec3151f65 100644
--- a/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts
+++ b/packages/admin-sdk/src/types/params/DigitalAssetCreateParams.ts
@@ -6,7 +6,10 @@ export type DigitalAssetCreateParams = {
* file for everyone who already bought it.
*/
signed_id?: string
- /** Defaults to the product's default variant when omitted. */
+ /**
+ * On create, defaults to the product's default variant when omitted. On update, omitting it keeps
+ * the current variant.
+ */
variant_id?: string
/** Null means the store's download settings apply. */
authorized_clicks?: number | null
diff --git a/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts b/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts
index d2c8ff86e5..d10ee1770d 100644
--- a/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts
+++ b/packages/admin-sdk/src/types/params/DigitalAssetUpdateParams.ts
@@ -6,7 +6,10 @@ export type DigitalAssetUpdateParams = {
* file for everyone who already bought it.
*/
signed_id?: string
- /** Defaults to the product's default variant when omitted. */
+ /**
+ * On create, defaults to the product's default variant when omitted. On update, omitting it keeps
+ * the current variant.
+ */
variant_id?: string
/** Null means the store's download settings apply. */
authorized_clicks?: number | null
diff --git a/packages/dashboard-core/src/locales/ar.json b/packages/dashboard-core/src/locales/ar.json
index d2314b7906..443e528891 100644
--- a/packages/dashboard-core/src/locales/ar.json
+++ b/packages/dashboard-core/src/locales/ar.json
@@ -2086,7 +2086,8 @@
"upload": "إضافة ملف",
"upload_failed": "فشل رفع {{name}}: {{message}}",
"uploading": "جارٍ رفع {{name}}…",
- "uploading_short": "جارٍ الرفع…"
+ "uploading_short": "جارٍ الرفع…",
+ "replace": "استبدال الملف"
}
}
}
diff --git a/packages/dashboard-core/src/locales/de.json b/packages/dashboard-core/src/locales/de.json
index c6c95e911f..2144256f75 100644
--- a/packages/dashboard-core/src/locales/de.json
+++ b/packages/dashboard-core/src/locales/de.json
@@ -2081,7 +2081,8 @@
"upload": "Datei hinzufügen",
"upload_failed": "Hochladen von {{name}} fehlgeschlagen: {{message}}",
"uploading": "{{name}} wird hochgeladen…",
- "uploading_short": "Wird hochgeladen…"
+ "uploading_short": "Wird hochgeladen…",
+ "replace": "Datei ersetzen"
}
}
}
diff --git a/packages/dashboard-core/src/locales/en.json b/packages/dashboard-core/src/locales/en.json
index d4aa7d68ba..04cb0ba342 100644
--- a/packages/dashboard-core/src/locales/en.json
+++ b/packages/dashboard-core/src/locales/en.json
@@ -2083,7 +2083,8 @@
"upload": "Add file",
"upload_failed": "Failed to upload {{name}}: {{message}}",
"uploading": "Uploading {{name}}…",
- "uploading_short": "Uploading…"
+ "uploading_short": "Uploading…",
+ "replace": "Replace file"
}
}
}
diff --git a/packages/dashboard-core/src/locales/es.json b/packages/dashboard-core/src/locales/es.json
index 9668874af0..59680e657a 100644
--- a/packages/dashboard-core/src/locales/es.json
+++ b/packages/dashboard-core/src/locales/es.json
@@ -2083,7 +2083,8 @@
"upload": "Agregar archivo",
"upload_failed": "No se pudo subir {{name}}: {{message}}",
"uploading": "Subiendo {{name}}…",
- "uploading_short": "Subiendo…"
+ "uploading_short": "Subiendo…",
+ "replace": "Reemplazar archivo"
}
}
}
diff --git a/packages/dashboard-core/src/locales/fr.json b/packages/dashboard-core/src/locales/fr.json
index 732f974cf9..080ee1e03e 100644
--- a/packages/dashboard-core/src/locales/fr.json
+++ b/packages/dashboard-core/src/locales/fr.json
@@ -2081,7 +2081,8 @@
"upload": "Ajouter un fichier",
"upload_failed": "Échec du téléversement de {{name}} : {{message}}",
"uploading": "Téléversement de {{name}}…",
- "uploading_short": "Téléversement…"
+ "uploading_short": "Téléversement…",
+ "replace": "Remplacer le fichier"
}
}
}
diff --git a/packages/dashboard-core/src/locales/pl.json b/packages/dashboard-core/src/locales/pl.json
index fa597b1c1e..cfa1086ff4 100644
--- a/packages/dashboard-core/src/locales/pl.json
+++ b/packages/dashboard-core/src/locales/pl.json
@@ -2110,7 +2110,8 @@
"upload": "Dodaj plik",
"upload_failed": "Nie udało się przesłać {{name}}: {{message}}",
"uploading": "Przesyłanie {{name}}…",
- "uploading_short": "Przesyłanie…"
+ "uploading_short": "Przesyłanie…",
+ "replace": "Zastąp plik"
}
}
}
diff --git a/packages/dashboard-core/src/locales/zh-CN.json b/packages/dashboard-core/src/locales/zh-CN.json
index db6110627b..abd72c76b0 100644
--- a/packages/dashboard-core/src/locales/zh-CN.json
+++ b/packages/dashboard-core/src/locales/zh-CN.json
@@ -2080,7 +2080,8 @@
"upload": "添加文件",
"upload_failed": "上传 {{name}} 失败:{{message}}",
"uploading": "正在上传 {{name}}…",
- "uploading_short": "上传中…"
+ "uploading_short": "上传中…",
+ "replace": "替换文件"
}
}
}
diff --git a/packages/dashboard-core/src/products/digital-assets-card.tsx b/packages/dashboard-core/src/products/digital-assets-card.tsx
index f7bebf59a9..e7e9c5ea37 100644
--- a/packages/dashboard-core/src/products/digital-assets-card.tsx
+++ b/packages/dashboard-core/src/products/digital-assets-card.tsx
@@ -50,6 +50,7 @@ import {
useDeleteDigitalAsset,
useDigitalAssetProviders,
useDigitalAssets,
+ useUpdateDigitalAsset,
} from './use-digital-assets'
export function DigitalAssetsCard({
@@ -64,6 +65,9 @@ export function DigitalAssetsCard({
const { permissions } = usePermissions()
const canWrite = permissions.can('update', Subject.Product)
const fileInputRef = useRef<HTMLInputElement>(null)
+ const replaceInputRef = useRef<HTMLInputElement>(null)
+ // The file whose replacement the picker was opened for.
+ const replacingRef = useRef<PanelDigitalAsset | null>(null)
const [uploading, setUploading] = useState(false)
const [page, setPage] = useState(1)
const [editing, setEditing] = useState<PanelDigitalAsset | null>(null)
@@ -77,6 +81,7 @@ export function DigitalAssetsCard({
const { data: providersData } = useDigitalAssetProviders(productId ?? '', Boolean(productId))
const createAsset = useCreateDigitalAsset(productId ?? '')
const deleteAsset = useDeleteDigitalAsset(productId ?? '')
+ const updateAsset = useUpdateDigitalAsset(productId ?? '')
const assets: PanelDigitalAsset[] = data?.data ?? []
const meta = data?.meta
@@ -125,6 +130,35 @@ export function DigitalAssetsCard({
if (files.length > 0) void handleFiles(files)
}
+ function openReplace(asset: PanelDigitalAsset) {
+ replacingRef.current = asset
+ replaceInputRef.current?.click()
+ }
+
+ // Replaces the file in place, so links buyers already received serve the
+ // new one.
+ async function handleReplaceInput(event: React.ChangeEvent<HTMLInputElement>) {
+ const file = event.target.files?.[0]
+ event.target.value = ''
+ const asset = replacingRef.current
+ replacingRef.current = null
+ if (!file || !asset) return
+
+ setUploading(true)
+ try {
+ const { signedId } = await fileUpload.mutateAsync(file)
+ await updateAsset.mutateAsync({ id: asset.id, signed_id: signedId })
+ } catch (err) {
+ const message = err instanceof Error ? err.message : t('admin.errors.unexpected')
+ toastManager.add({
+ type: 'error',
+ title: t('admin.digital_assets.upload_failed', { name: file.name, message }),
+ })
+ } finally {
+ setUploading(false)
+ }
+ }
+
function handleDrop(event: React.DragEvent) {
event.preventDefault()
if (event.dataTransfer.files.length > 0) void handleFiles(event.dataTransfer.files)
@@ -241,6 +275,7 @@ export function DigitalAssetsCard({
className="hidden"
onChange={handleFileInput}
/>
+ <input ref={replaceInputRef} type="file" className="hidden" onChange={handleReplaceInput} />
{isLoading ? (
<Skeleton className="h-24 w-full" />
@@ -325,6 +360,14 @@ export function DigitalAssetsCard({
visible: Boolean(asset.download_url),
onSelect: () => window.open(asset.download_url ?? '', '_blank'),
},
+ {
+ key: 'replace',
+ label: t('admin.digital_assets.replace'),
+ icon: <UploadIcon className="size-4" />,
+ // A provider delivers its own deliverable; there is no file to swap.
+ visible: canWrite && !asset.provider_type,
+ onSelect: () => openReplace(asset),
+ },
{ key: 'edit', visible: canWrite, onSelect: () => setEditing(asset) },
{
key: 'delete',
diff --git a/packages/dashboard/e2e/digital-assets.spec.ts b/packages/dashboard/e2e/digital-assets.spec.ts
index 7de94f7402..94654c7051 100644
--- a/packages/dashboard/e2e/digital-assets.spec.ts
+++ b/packages/dashboard/e2e/digital-assets.spec.ts
@@ -23,10 +23,11 @@ test.describe('product digital files', () => {
.filter({ has: page.locator('[data-slot="card-title"]', { hasText: /^Digital files$/ }) })
await expect(digitalCard).toHaveCount(1)
- // The empty card is a dropzone with a hidden file input behind it — set the
- // file directly rather than simulating the click-through.
+ // The empty card is a dropzone; clicking it opens the file picker.
const fileName = path.basename(FIXTURE_FILE)
- await digitalCard.locator('input[type="file"]').setInputFiles(FIXTURE_FILE)
+ const chooser = page.waitForEvent('filechooser')
+ await digitalCard.getByRole('button', { name: /drag & drop files here/i }).click()
+ await (await chooser).setFiles(FIXTURE_FILE)
// The uploaded file appears as a row in this card once the asset is created.
const fileRow = digitalCard.locator('tr').filter({ hasText: fileName })
diff --git a/packages/dashboard/e2e/seller-panel-products.spec.ts b/packages/dashboard/e2e/seller-panel-products.spec.ts
index 12321fd2c8..6523d2efcb 100644
--- a/packages/dashboard/e2e/seller-panel-products.spec.ts
+++ b/packages/dashboard/e2e/seller-panel-products.spec.ts
@@ -83,7 +83,9 @@ test.describe('seller panel — products', () => {
})
// A seller selling downloads manages the files on their own listing.
- test('uploads a digital file, sets its download limits, then removes it', async ({ page }) => {
+ test('uploads a digital file, sets its download limits, replaces it, then removes it', async ({
+ page,
+ }) => {
const home = await sellerLogin(page, FIXTURE_SELLER_WRITER_EMAIL)
await listProduct(page, home, `E2E Amp Model ${Date.now()}`)
@@ -93,7 +95,9 @@ test.describe('seller panel — products', () => {
await expect(digitalCard).toHaveCount(1)
const fileName = path.basename(DIGITAL_FILE)
- await digitalCard.locator('input[type="file"]').setInputFiles(DIGITAL_FILE)
+ const chooser = page.waitForEvent('filechooser')
+ await digitalCard.getByRole('button', { name: /drag & drop files here/i }).click()
+ await (await chooser).setFiles(DIGITAL_FILE)
const fileRow = digitalCard.locator('tr').filter({ hasText: fileName })
await expect(fileRow).toBeVisible({ timeout: 30_000 })
@@ -111,14 +115,28 @@ test.describe('seller panel — products', () => {
})
await expect(fileRow.getByRole('cell', { name: '30', exact: true })).toBeVisible()
+ // Replacing keeps the row and its limits; only the file changes.
+ const replacementName = 'clean-tube-amp.nam'
+ const replaceChooser = page.waitForEvent('filechooser')
await openRowMenu(page, fileName)
+ await page.getByRole('menuitem', { name: /replace file/i }).click()
+ await (await replaceChooser).setFiles({
+ name: replacementName,
+ mimeType: 'application/octet-stream',
+ buffer: Buffer.from('replacement capture'),
+ })
+ const replacedRow = digitalCard.locator('tr').filter({ hasText: replacementName })
+ await expect(replacedRow).toBeVisible({ timeout: 30_000 })
+ await expect(replacedRow.getByRole('cell', { name: '3', exact: true })).toBeVisible()
+
+ await openRowMenu(page, replacementName)
await page.getByRole('menuitem', { name: /^delete$/i }).click()
await page
.getByRole('dialog')
.getByRole('button', { name: /^confirm$/i })
.click()
- await expect(fileRow).toBeHidden({ timeout: 15_000 })
+ await expect(replacedRow).toBeHidden({ timeout: 15_000 })
await expect(digitalCard.getByText(/drag & drop files here/i)).toBeVisible()
})
})
diff --git a/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts b/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts
index 16ced544b4..e0661401cb 100644
--- a/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts
+++ b/packages/seller-sdk/src/types/params/DigitalAssetCreateParams.ts
@@ -6,7 +6,10 @@ export type DigitalAssetCreateParams = {
* file for everyone who already bought it.
*/
signed_id: string
- /** Defaults to the product's default variant when omitted. */
+ /**
+ * On create, defaults to the product's default variant when omitted. On update, omitting it keeps
+ * the current variant.
+ */
variant_id?: string
/** Null means the store's download settings apply. */
authorized_clicks?: number | null
diff --git a/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts b/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts
index b99fba3d2d..a108435422 100644
--- a/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts
+++ b/packages/seller-sdk/src/types/params/DigitalAssetUpdateParams.ts
@@ -6,7 +6,10 @@ export type DigitalAssetUpdateParams = {
* file for everyone who already bought it.
*/
signed_id?: string
- /** Defaults to the product's default variant when omitted. */
+ /**
+ * On create, defaults to the product's default variant when omitted. On update, omitting it keeps
+ * the current variant.
+ */
variant_id?: string
/** Null means the store's download settings apply. */
authorized_clicks?: number | null
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 00f79c3fe9..0f9561f6e7 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
@@ -12,7 +12,7 @@ module Spree
# be pointed at a variant of another product or store.
attribute :variant_id, :id,
permit: false,
- description: "Defaults to the product's default variant when omitted."
+ description: "On create, defaults to the product's default variant when omitted. On update, omitting it keeps the current variant."
attribute :authorized_clicks, :integer,
nullable: true,
description: "Null means the store's download settings apply."