feat: V2 batch update products in categories (#7125)
* wip * wip * fix: batch category update * update tet * fix type --------- Co-authored-by: Stevche Radevski <sradevski@live.com>
This commit is contained in:
co-authored by
Stevche Radevski
parent
2446151420
commit
10e120062b
@@ -0,0 +1,91 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService, ProductCategoryWorkflow } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const batchLinkProductsToCategoryStepId =
|
||||
"batch-link-products-to-category"
|
||||
export const batchLinkProductsToCategoryStep = createStep(
|
||||
batchLinkProductsToCategoryStepId,
|
||||
async (
|
||||
data: ProductCategoryWorkflow.BatchUpdateProductsOnCategoryWorkflowInput,
|
||||
{ container }
|
||||
) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
if (!data.add?.length && !data.remove?.length) {
|
||||
return new StepResponse(void 0, null)
|
||||
}
|
||||
|
||||
const toRemoveSet = new Set(data.remove?.map((id) => id))
|
||||
const dbProducts = await service.list(
|
||||
{ id: [...(data.add ?? []), ...(data.remove ?? [])] },
|
||||
{
|
||||
take: null,
|
||||
select: ["id", "categories"],
|
||||
}
|
||||
)
|
||||
|
||||
const productsWithUpdatedCategories = dbProducts.map((p) => {
|
||||
if (toRemoveSet.has(p.id)) {
|
||||
return {
|
||||
id: p.id,
|
||||
category_ids: (p.categories ?? [])
|
||||
.filter((c) => c.id !== data.id)
|
||||
.map((c) => c.id),
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: p.id,
|
||||
category_ids: [...(p.categories ?? []).map((c) => c.id), data.id],
|
||||
}
|
||||
})
|
||||
|
||||
await service.upsert(productsWithUpdatedCategories)
|
||||
|
||||
return new StepResponse(void 0, {
|
||||
id: data.id,
|
||||
remove: data.remove,
|
||||
add: data.add,
|
||||
productIds: productsWithUpdatedCategories.map((p) => p.id),
|
||||
})
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const dbProducts = await service.list(
|
||||
{ id: prevData.productIds },
|
||||
{
|
||||
take: null,
|
||||
select: ["id", "categories"],
|
||||
}
|
||||
)
|
||||
|
||||
const toRemoveSet = new Set(prevData.remove?.map((id) => id))
|
||||
const productsWithRevertedCategories = dbProducts.map((p) => {
|
||||
if (toRemoveSet.has(p.id)) {
|
||||
return {
|
||||
id: p.id,
|
||||
category_ids: [...(p.categories ?? []).map((c) => c.id), prevData.id],
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: p.id,
|
||||
category_ids: (p.categories ?? [])
|
||||
.filter((c) => c.id !== prevData.id)
|
||||
.map((c) => c.id),
|
||||
}
|
||||
})
|
||||
|
||||
await service.upsert(productsWithRevertedCategories)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductCategoryWorkflow } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { batchLinkProductsToCategoryStep } from "../steps/batch-link-products-in-category"
|
||||
|
||||
export const batchLinkProductsToCategoryWorkflowId =
|
||||
"batch-link-products-to-category"
|
||||
export const batchLinkProductsToCategoryWorkflow = createWorkflow(
|
||||
batchLinkProductsToCategoryWorkflowId,
|
||||
(
|
||||
// eslint-disable-next-line max-len
|
||||
input: WorkflowData<ProductCategoryWorkflow.BatchUpdateProductsOnCategoryWorkflowInput>
|
||||
): WorkflowData<void> => {
|
||||
return batchLinkProductsToCategoryStep(input)
|
||||
}
|
||||
)
|
||||
@@ -1,18 +1,19 @@
|
||||
export * from "./create-products"
|
||||
export * from "./delete-products"
|
||||
export * from "./update-products"
|
||||
export * from "./batch-products"
|
||||
export * from "./create-product-options"
|
||||
export * from "./delete-product-options"
|
||||
export * from "./update-product-options"
|
||||
export * from "./create-product-variants"
|
||||
export * from "./delete-product-variants"
|
||||
export * from "./update-product-variants"
|
||||
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 "./batch-product-variants"
|
||||
export * from "./batch-products"
|
||||
export * from "./batch-products-in-category"
|
||||
export * from "./create-collections"
|
||||
export * from "./create-product-options"
|
||||
export * from "./create-product-types"
|
||||
export * from "./create-product-variants"
|
||||
export * from "./create-products"
|
||||
export * from "./delete-collections"
|
||||
export * from "./delete-product-options"
|
||||
export * from "./delete-product-types"
|
||||
export * from "./delete-product-variants"
|
||||
export * from "./delete-products"
|
||||
export * from "./update-collections"
|
||||
export * from "./update-product-options"
|
||||
export * from "./update-product-types"
|
||||
export * from "./update-product-variants"
|
||||
export * from "./update-products"
|
||||
|
||||
Reference in New Issue
Block a user