From f00bb8efcffecab4c21ef39947cd455ff750defd Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 6 Mar 2025 14:41:20 +0100 Subject: [PATCH] fix(core-flows): conditionally create, update or delete products when input is present (#11758) what: - runs create / update /delete workflows for bulk workflow conditionally FIXES https://github.com/medusajs/medusa/issues/11749 depends on https://github.com/medusajs/medusa/pull/11756 --- .../__tests__/product/admin/product.spec.ts | 19 +++++++++++ .../src/product/workflows/batch-products.ts | 33 ++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/integration-tests/http/__tests__/product/admin/product.spec.ts b/integration-tests/http/__tests__/product/admin/product.spec.ts index 5152a853e3..e472fbe77f 100644 --- a/integration-tests/http/__tests__/product/admin/product.spec.ts +++ b/integration-tests/http/__tests__/product/admin/product.spec.ts @@ -3100,6 +3100,25 @@ medusaIntegrationTestRunner({ ) }) + it("should successfully delete products", async () => { + const response = await api.post( + "/admin/products/batch", + { delete: [baseProduct.id] }, + adminHeaders + ) + + expect(response.status).toEqual(200) + expect(response.data.created).toHaveLength(0) + expect(response.data.updated).toHaveLength(0) + expect(response.data.deleted.ids).toHaveLength(1) + + expect(response.data.created).toEqual([]) + expect(response.data.updated).toEqual([]) + expect(response.data.deleted).toEqual( + expect.objectContaining({ ids: [baseProduct.id] }) + ) + }) + it("successfully creates, updates, and deletes product variants", async () => { const productWithMultipleVariants = getProductFixture({ title: "Test batch variants", diff --git a/packages/core/core-flows/src/product/workflows/batch-products.ts b/packages/core/core-flows/src/product/workflows/batch-products.ts index baf1d1bf4b..2954cde1c4 100644 --- a/packages/core/core-flows/src/product/workflows/batch-products.ts +++ b/packages/core/core-flows/src/product/workflows/batch-products.ts @@ -11,6 +11,7 @@ import { createWorkflow, parallelize, transform, + when, } from "@medusajs/framework/workflows-sdk" import { createProductsWorkflow } from "./create-products" import { deleteProductsWorkflow } from "./delete-products" @@ -25,6 +26,22 @@ export interface BatchProductWorkflowInput UpdateProductWorkflowInputDTO > {} +const conditionallyCreateProducts = (input: BatchProductWorkflowInput) => + when({ input }, ({ input }) => !!input.create?.length).then(() => + createProductsWorkflow.runAsStep({ input: { products: input.create! } }) + ) + +const conditionallyUpdateProducts = (input: BatchProductWorkflowInput) => + when({ input }, ({ input }) => !!input.update?.length).then(() => + updateProductsWorkflow.runAsStep({ input: { products: input.update! } }) + ) + +const conditionallyDeleteProducts = (input: BatchProductWorkflowInput) => + when({ input }, ({ input }) => !!input.delete?.length).then(() => + deleteProductsWorkflow.runAsStep({ input: { ids: input.delete! } }) + ) + + export const batchProductsWorkflowId = "batch-products" /** * This workflow creates, updates, or deletes products. It's used by the @@ -82,22 +99,16 @@ export const batchProductsWorkflow = createWorkflow( input: WorkflowData ): WorkflowResponse> => { const res = parallelize( - createProductsWorkflow.runAsStep({ - input: { products: input.create ?? [] }, - }), - updateProductsWorkflow.runAsStep({ - input: { products: input.update ?? [] }, - }), - deleteProductsWorkflow.runAsStep({ - input: { ids: input.delete ?? [] }, - }) + conditionallyCreateProducts(input), + conditionallyUpdateProducts(input), + conditionallyDeleteProducts(input) ) return new WorkflowResponse( transform({ res, input }, (data) => { return { - created: data.res[0], - updated: data.res[1], + created: data.res[0] ?? [], + updated: data.res[1] ?? [], deleted: data.input.delete ?? [], } })