feat: Add batch method to collection and clean up some batch implementations (#7102)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { BatchLinkProductsToCollectionDTO } from "@medusajs/types/src"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const batchLinkProductsToCollectionStepId =
|
||||
"batch-link-products-to-collection"
|
||||
export const batchLinkProductsToCollectionStep = createStep(
|
||||
batchLinkProductsToCollectionStepId,
|
||||
async (data: BatchLinkProductsToCollectionDTO, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
if (!data.add?.length && !data.remove?.length) {
|
||||
return new StepResponse(void 0, null)
|
||||
}
|
||||
|
||||
const dbCollection = await service.retrieveCollection(data.id, {
|
||||
take: null,
|
||||
select: ["id", "products.id"],
|
||||
relations: ["products"],
|
||||
})
|
||||
const existingProductIds = dbCollection.products?.map((p) => p.id) ?? []
|
||||
const toRemoveMap = new Map(data.remove?.map((id) => [id, true]) ?? [])
|
||||
|
||||
const newProductIds = [
|
||||
...existingProductIds.filter((id) => !toRemoveMap.has(id)),
|
||||
...(data.add ?? []),
|
||||
]
|
||||
|
||||
await service.updateCollections(data.id, {
|
||||
product_ids: newProductIds,
|
||||
})
|
||||
|
||||
return new StepResponse(void 0, {
|
||||
id: data.id,
|
||||
productIds: existingProductIds,
|
||||
})
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.updateCollections(prevData.id, {
|
||||
product_ids: prevData.productIds,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -14,6 +14,7 @@ export * from "./batch-product-variants"
|
||||
export * from "./create-collections"
|
||||
export * from "./update-collections"
|
||||
export * from "./delete-collections"
|
||||
export * from "./batch-link-products-collection"
|
||||
export * from "./create-product-types"
|
||||
export * from "./update-product-types"
|
||||
export * from "./delete-product-types"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { BatchLinkProductsToCollectionDTO } from "@medusajs/types/src"
|
||||
import { batchLinkProductsToCollectionStep } from "../steps/batch-link-products-collection"
|
||||
|
||||
export const batchLinkProductsToCollectionWorkflowId =
|
||||
"batch-link-products-to-collection"
|
||||
export const batchLinkProductsToCollectionWorkflow = createWorkflow(
|
||||
batchLinkProductsToCollectionWorkflowId,
|
||||
(
|
||||
input: WorkflowData<BatchLinkProductsToCollectionDTO>
|
||||
): WorkflowData<void> => {
|
||||
return batchLinkProductsToCollectionStep(input)
|
||||
}
|
||||
)
|
||||
@@ -12,6 +12,7 @@ export * from "./batch-product-variants"
|
||||
export * from "./create-collections"
|
||||
export * from "./delete-collections"
|
||||
export * from "./update-collections"
|
||||
export * from "./batch-link-products-collection"
|
||||
export * from "./create-product-types"
|
||||
export * from "./delete-product-types"
|
||||
export * from "./update-product-types"
|
||||
|
||||
Reference in New Issue
Block a user