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
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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<BatchProductWorkflowInput>
|
||||
): WorkflowResponse<BatchWorkflowOutput<ProductTypes.ProductDTO>> => {
|
||||
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 ?? [],
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user