From c1ae373967393184695a8c2002603696cc416df2 Mon Sep 17 00:00:00 2001 From: Roman Shuper Date: Fri, 24 Jan 2025 11:48:37 +0200 Subject: [PATCH] product categories hooks (#11093) * hooks * workflow result fixed --- .../workflows/create-product-categories.ts | 36 ++++++++++++------- .../workflows/delete-product-categories.ts | 9 ++++- .../workflows/update-product-categories.ts | 11 ++++-- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/core/core-flows/src/product-category/workflows/create-product-categories.ts b/packages/core/core-flows/src/product-category/workflows/create-product-categories.ts index 42fe507116..fabd95eb8b 100644 --- a/packages/core/core-flows/src/product-category/workflows/create-product-categories.ts +++ b/packages/core/core-flows/src/product-category/workflows/create-product-categories.ts @@ -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 - ): WorkflowResponse => { - 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], + }) } ) diff --git a/packages/core/core-flows/src/product-category/workflows/delete-product-categories.ts b/packages/core/core-flows/src/product-category/workflows/delete-product-categories.ts index d29c0e3dc4..c86d5a180b 100644 --- a/packages/core/core-flows/src/product-category/workflows/delete-product-categories.ts +++ b/packages/core/core-flows/src/product-category/workflows/delete-product-categories.ts @@ -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] + }) } ) diff --git a/packages/core/core-flows/src/product-category/workflows/update-product-categories.ts b/packages/core/core-flows/src/product-category/workflows/update-product-categories.ts index f99ed0019e..ac52077416 100644 --- a/packages/core/core-flows/src/product-category/workflows/update-product-categories.ts +++ b/packages/core/core-flows/src/product-category/workflows/update-product-categories.ts @@ -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 - ): WorkflowResponse => { + ) => { 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] + }) } )