diff --git a/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.test.tsx b/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.test.tsx index 122a76b1..52a7b4db 100644 --- a/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.test.tsx +++ b/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.test.tsx @@ -1239,13 +1239,19 @@ describe("NonIntegratedDetailClient", () => { expect(mockedUploadDetachedNonIntegratedFile).toHaveBeenCalledWith( undefined, expect.objectContaining({ name: "remote-preview.png" }), - undefined, + { + category: undefined, + onProgress: undefined, + }, ); - const [, uploadedFile, uploadProgressHandler] = + const [, uploadedFile, uploadOptions] = mockedUploadDetachedNonIntegratedFile.mock.calls[0] ?? []; expect(uploadedFile).toBeInstanceOf(File); expect(uploadedFile.name).toBe("remote-preview.png"); - expect(uploadProgressHandler).toBeUndefined(); + expect(uploadOptions).toEqual({ + category: undefined, + onProgress: undefined, + }); expect(mockedSaveSharedNonIntegratedProduct.mock.calls[0]?.[0]).toEqual( expect.objectContaining({ previewImage: "http://test.local/remote-preview.png", @@ -1256,6 +1262,47 @@ describe("NonIntegratedDetailClient", () => { ); }); + it("passes the active workspace category when uploading SKU images on an existing product", async () => { + (globalThis as NonIntegratedRemoteAssetStorageTestGlobal) + .__NON_INTEGRATED_REMOTE_ASSET_STORAGE_TEST_OVERRIDE__ = true; + const actor = userEvent.setup(); + const productId = "DNZ0001-sku-image-upload"; + + const { container } = render( + , + ); + + const skuImageInput = container.querySelector( + "#sku-image-input-sku-image-row-1", + ); + expect(skuImageInput).not.toBeNull(); + await actor.upload( + skuImageInput!, + new File(["sku-image"], "DNZ000101.png", { type: "image/png" }), + ); + + await waitFor(() => + expect(mockedUploadDetachedNonIntegratedFile).toHaveBeenCalledTimes(1), + ); + expect(mockedUploadDetachedNonIntegratedFile).toHaveBeenCalledWith( + productId, + expect.objectContaining({ name: "DNZ000101.png" }), + expect.objectContaining({ + category: "基础信息", + onProgress: expect.any(Function), + }), + ); + }); + it("uses the opening reference image as the shared product main image", async () => { const actor = userEvent.setup(); saveNonIntegratedProduct( diff --git a/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.tsx b/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.tsx index d5f871dd..de9e637a 100644 --- a/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.tsx +++ b/furniture-3d-system/src/app/intake/non-integrated/NonIntegratedDetailClient.tsx @@ -9500,6 +9500,7 @@ async function uploadFileToServer( file: File, options?: { productId?: string; + category?: string | null; }, onProgress?: NonIntegratedUploadProgressHandler, ) { @@ -9514,7 +9515,10 @@ async function uploadFileToServer( const payload = await uploadDetachedNonIntegratedFile( options?.productId, uploadFile, - onProgress, + { + category: options?.category, + onProgress, + }, ); return { @@ -13649,6 +13653,7 @@ export default function NonIntegratedDetailClient({ const detachedUploadOptions = sharedUploadProductId ? { productId: sharedUploadProductId, + category: activePermissionWorkspaceCategory, } : undefined; const uploadProgressPercent = uploadProgress diff --git a/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.test.ts b/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.test.ts index 5f243db5..53147ed5 100644 --- a/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.test.ts +++ b/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.test.ts @@ -1,8 +1,12 @@ import { uploadDetachedNonIntegratedFile } from './nonIntegratedApi' -type MutableGlobal = typeof globalThis & { fetch?: typeof fetch } +type MutableGlobal = typeof globalThis & { + fetch?: typeof fetch + XMLHttpRequest?: typeof XMLHttpRequest +} const originalFetch = (globalThis as MutableGlobal).fetch +const originalXMLHttpRequest = (globalThis as MutableGlobal).XMLHttpRequest function setFetch(fetchMock: typeof fetch) { Object.defineProperty(globalThis, 'fetch', { @@ -19,6 +23,24 @@ function restoreFetch() { } } +function useFetchUploadBranch() { + Object.defineProperty(globalThis, 'XMLHttpRequest', { + configurable: true, + value: undefined, + }) +} + +function restoreXMLHttpRequest() { + if (originalXMLHttpRequest) { + Object.defineProperty(globalThis, 'XMLHttpRequest', { + configurable: true, + value: originalXMLHttpRequest, + }) + } else { + delete (globalThis as MutableGlobal).XMLHttpRequest + } +} + function jsonResponse(data: unknown, status = 200) { return { ok: status >= 200 && status < 300, @@ -27,6 +49,7 @@ function jsonResponse(data: unknown, status = 200) { get: () => null, }, json: async () => data, + text: async () => JSON.stringify(data), } as Response } @@ -41,6 +64,7 @@ function emptyResponse( get: (name: string) => headers[name] ?? headers[name.toLowerCase()] ?? null, }, json: async () => null, + text: async () => '', } as Response } @@ -48,6 +72,7 @@ describe('nonIntegratedApi uploads', () => { afterEach(() => { jest.restoreAllMocks() restoreFetch() + restoreXMLHttpRequest() }) it('uploads detached files without a product id through the temporary upload endpoint', async () => { @@ -63,6 +88,7 @@ describe('nonIntegratedApi uploads', () => { }), ) setFetch(fetchMock as typeof fetch) + useFetchUploadBranch() const payload = await uploadDetachedNonIntegratedFile( undefined, @@ -101,14 +127,21 @@ describe('nonIntegratedApi uploads', () => { }), ) setFetch(fetchMock as typeof fetch) + useFetchUploadBranch() const file = new File(['tiny'], 'tiny.png', { type: 'image/png' }) - await uploadDetachedNonIntegratedFile('product-1', file) + await uploadDetachedNonIntegratedFile('product-1', file, { + category: '基础信息', + }) expect(fetchMock).toHaveBeenCalledTimes(1) expect(fetchMock.mock.calls[0][0]).toBe( '/api/intake/non-integrated/product-1/upload', ) + expect(fetchMock.mock.calls[0][1]?.body).toBeInstanceOf(FormData) + expect((fetchMock.mock.calls[0][1]?.body as FormData).get('category')).toBe( + '基础信息', + ) }) it('uses chunk upload before production request body limits can reject images', async () => { @@ -135,18 +168,27 @@ describe('nonIntegratedApi uploads', () => { }, 201, ), - ) + ) setFetch(fetchMock as typeof fetch) + useFetchUploadBranch() const file = new File([new Uint8Array(600 * 1024)], 'large.png', { type: 'image/png', }) - await uploadDetachedNonIntegratedFile('product-1', file) + await uploadDetachedNonIntegratedFile('product-1', file, { + category: '产品实拍图', + }) expect(fetchMock).toHaveBeenCalledTimes(2) expect(fetchMock.mock.calls.map((call) => call[0])).toEqual([ '/api/intake/non-integrated/product-1/upload/chunk', '/api/intake/non-integrated/product-1/upload/chunk', ]) + expect((fetchMock.mock.calls[0][1]?.body as FormData).get('category')).toBe( + '产品实拍图', + ) + expect((fetchMock.mock.calls[1][1]?.body as FormData).get('category')).toBe( + '产品实拍图', + ) }) }) diff --git a/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.ts b/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.ts index 893db72e..ef6b9fb8 100644 --- a/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.ts +++ b/furniture-3d-system/src/app/intake/non-integrated/nonIntegratedApi.ts @@ -24,6 +24,11 @@ export type NonIntegratedUploadProgressHandler = ( progress: NonIntegratedUploadProgress, ) => void +export type NonIntegratedDetachedUploadOptions = { + category?: string | null + onProgress?: NonIntegratedUploadProgressHandler +} + export type NonIntegratedProductPageInfo = { page: number pageSize: number @@ -385,15 +390,28 @@ export async function uploadSharedNonIntegratedFile( export async function uploadDetachedNonIntegratedFile( productId: string | undefined, file: File, - onProgress?: NonIntegratedUploadProgressHandler, + optionsOrProgress?: NonIntegratedDetachedUploadOptions | NonIntegratedUploadProgressHandler, ) { + const options = + typeof optionsOrProgress === 'function' + ? { onProgress: optionsOrProgress } + : (optionsOrProgress ?? {}) + const category = + typeof options.category === 'string' && options.category.trim() + ? options.category.trim() + : undefined + const onProgress = options.onProgress + if (productId && file.size > NON_INTEGRATED_UPLOAD_CHUNK_BYTES) { - return uploadDetachedNonIntegratedFileInChunks(productId, file, onProgress) + return uploadDetachedNonIntegratedFileInChunks(productId, file, category, onProgress) } const formData = new FormData() formData.append('file', file) formData.append('attach', 'false') + if (category) { + formData.append('category', category) + } reportFileUploadProgress(onProgress, file.size, 0) @@ -432,12 +450,14 @@ async function uploadSharedNonIntegratedFileInChunks( async function uploadDetachedNonIntegratedFileInChunks( productId: string, file: File, + category?: string, onProgress?: NonIntegratedUploadProgressHandler, ) { return uploadNonIntegratedFileInChunks({ productId, file, attach: false, + category, onProgress, }) }