product categories hooks (#11093)

* hooks

* workflow result fixed
This commit is contained in:
Roman Shuper
2025-01-24 10:48:37 +01:00
committed by GitHub
parent 232281d5a4
commit c1ae373967
3 changed files with 40 additions and 16 deletions
@@ -1,10 +1,14 @@
import { ProductCategoryDTO, ProductCategoryWorkflow } from "@medusajs/framework/types"
import {
ProductCategoryDTO,
ProductCategoryWorkflow,
} from "@medusajs/framework/types"
import { ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
transform,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common"
import { createProductCategoriesStep } from "../steps"
@@ -18,10 +22,10 @@ export const createProductCategoriesWorkflowId = "create-product-categories"
/**
* This workflow creates one or more product categories. It's used by the
* [Create Product Category Admin API Route](https://docs.medusajs.com/api/admin#product-categories_postproductcategories).
*
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to
* create product categories within your custom flows.
*
*
* @example
* const { result } = await createProductCategoriesWorkflow(container)
* .run({
@@ -33,22 +37,22 @@ export const createProductCategoriesWorkflowId = "create-product-categories"
* ]
* }
* })
*
*
* @summary
*
*
* Create product categories.
*/
export const createProductCategoriesWorkflow = createWorkflow(
createProductCategoriesWorkflowId,
(
input: WorkflowData<ProductCategoryWorkflow.CreateProductCategoriesWorkflowInput>
): WorkflowResponse<CreateProductCategoriesWorkflowOutput> => {
const createdProducts = createProductCategoriesStep(input)
) => {
const createdCategories = createProductCategoriesStep(input)
const productCategoryIdEvents = transform(
{ createdProducts },
({ createdProducts }) => {
return createdProducts.map((v) => {
{ createdCategories },
({ createdCategories }) => {
return createdCategories.map((v) => {
return { id: v.id }
})
}
@@ -59,6 +63,12 @@ export const createProductCategoriesWorkflow = createWorkflow(
data: productCategoryIdEvents,
})
return new WorkflowResponse(createdProducts)
const categoriesCreated = createHook("categoriesCreated", {
categories: createdCategories,
})
return new WorkflowResponse(createdCategories, {
hooks: [categoriesCreated],
})
}
)
@@ -4,6 +4,7 @@ import {
WorkflowResponse,
createWorkflow,
transform,
createHook
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common"
import { deleteProductCategoriesStep } from "../steps"
@@ -47,6 +48,12 @@ export const deleteProductCategoriesWorkflow = createWorkflow(
data: productCategoryIdEvents,
})
return new WorkflowResponse(deleted)
const categoriesDeleted = createHook("categoriesDeleted", {
ids: input,
})
return new WorkflowResponse(deleted, {
hooks: [categoriesDeleted]
})
}
)
@@ -5,6 +5,7 @@ import {
WorkflowResponse,
createWorkflow,
transform,
createHook,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common"
import { updateProductCategoriesStep } from "../steps"
@@ -43,7 +44,7 @@ export const updateProductCategoriesWorkflow = createWorkflow(
updateProductCategoriesWorkflowId,
(
input: WorkflowData<ProductCategoryWorkflow.UpdateProductCategoriesWorkflowInput>
): WorkflowResponse<UpdateProductCategoriesWorkflowOutput> => {
) => {
const updatedCategories = updateProductCategoriesStep(input)
const productCategoryIdEvents = transform(
@@ -64,6 +65,12 @@ export const updateProductCategoriesWorkflow = createWorkflow(
data: productCategoryIdEvents,
})
return new WorkflowResponse(updatedCategories)
const categoriesUpdated = createHook("categoriesUpdated", {
categories: updatedCategories
})
return new WorkflowResponse(updatedCategories, {
hooks: [categoriesUpdated]
})
}
)