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 () => {
|
it("successfully creates, updates, and deletes product variants", async () => {
|
||||||
const productWithMultipleVariants = getProductFixture({
|
const productWithMultipleVariants = getProductFixture({
|
||||||
title: "Test batch variants",
|
title: "Test batch variants",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
|
when,
|
||||||
} from "@medusajs/framework/workflows-sdk"
|
} from "@medusajs/framework/workflows-sdk"
|
||||||
import { createProductsWorkflow } from "./create-products"
|
import { createProductsWorkflow } from "./create-products"
|
||||||
import { deleteProductsWorkflow } from "./delete-products"
|
import { deleteProductsWorkflow } from "./delete-products"
|
||||||
@@ -25,6 +26,22 @@ export interface BatchProductWorkflowInput
|
|||||||
UpdateProductWorkflowInputDTO
|
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"
|
export const batchProductsWorkflowId = "batch-products"
|
||||||
/**
|
/**
|
||||||
* This workflow creates, updates, or deletes products. It's used by the
|
* This workflow creates, updates, or deletes products. It's used by the
|
||||||
@@ -82,22 +99,16 @@ export const batchProductsWorkflow = createWorkflow(
|
|||||||
input: WorkflowData<BatchProductWorkflowInput>
|
input: WorkflowData<BatchProductWorkflowInput>
|
||||||
): WorkflowResponse<BatchWorkflowOutput<ProductTypes.ProductDTO>> => {
|
): WorkflowResponse<BatchWorkflowOutput<ProductTypes.ProductDTO>> => {
|
||||||
const res = parallelize(
|
const res = parallelize(
|
||||||
createProductsWorkflow.runAsStep({
|
conditionallyCreateProducts(input),
|
||||||
input: { products: input.create ?? [] },
|
conditionallyUpdateProducts(input),
|
||||||
}),
|
conditionallyDeleteProducts(input)
|
||||||
updateProductsWorkflow.runAsStep({
|
|
||||||
input: { products: input.update ?? [] },
|
|
||||||
}),
|
|
||||||
deleteProductsWorkflow.runAsStep({
|
|
||||||
input: { ids: input.delete ?? [] },
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(
|
return new WorkflowResponse(
|
||||||
transform({ res, input }, (data) => {
|
transform({ res, input }, (data) => {
|
||||||
return {
|
return {
|
||||||
created: data.res[0],
|
created: data.res[0] ?? [],
|
||||||
updated: data.res[1],
|
updated: data.res[1] ?? [],
|
||||||
deleted: data.input.delete ?? [],
|
deleted: data.input.delete ?? [],
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user