docs: added workflow hooks docs + changed workflow response (#8364)
* docs: added workflow hooks docs + changed workflow response * Update page.mdx
This commit is contained in:
@@ -142,7 +142,7 @@ export const highlights = [
|
||||
```ts title="src/api/middlewares.ts" highlights={highlights}
|
||||
import {
|
||||
defineMiddlewares,
|
||||
authenticate
|
||||
authenticate,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export default defineMiddlewares({
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
export const metadata = {
|
||||
title: `${pageNumber} Workflow Hooks`,
|
||||
}
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this chapter, you'll learn what a workflow hook is, how to expose it in your workflow, and how to consume the hook with a handler.
|
||||
|
||||
## What is a Workflow Hook?
|
||||
|
||||
A workflow hook is a point in a workflow where you can inject custom functionality as a step function, called a hook handler.
|
||||
|
||||
Hook handlers receive input from the workflow to perform custom actions during the workflow's execution.
|
||||
|
||||
<Note title="Use workflow hooks when" type="success">
|
||||
|
||||
- Your workflow is reusable in other applications, and you allow performing an external action at some point in your workflow.
|
||||
|
||||
</Note>
|
||||
|
||||
<Note title="Don't use workflow hooks if" type="error">
|
||||
|
||||
- Your workflow isn't reusable by other applications. Use a step that performs what a hook handler would instead.
|
||||
|
||||
</Note>
|
||||
|
||||
---
|
||||
|
||||
## How to Expose a Hook in a Workflow?
|
||||
|
||||
To expose a hook in your workflow, use the `createHook` function imported from `@medusajs/workflows-sdk`.
|
||||
|
||||
For example:
|
||||
|
||||
export const hookHighlights = [
|
||||
["13", "createHook", "Add a hook to the workflow."],
|
||||
["14", `"productCreated"`, "The hook's name."],
|
||||
["15", "productId", "The data to pass to the hook handler."],
|
||||
["19", "hooks", "Return the list of hooks in the workflow."]
|
||||
]
|
||||
|
||||
```ts title="src/workflows/my-workflow/index.ts" highlights={hookHighlights}
|
||||
import {
|
||||
createStep,
|
||||
createHook,
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createProductStep } from "./steps/create-product"
|
||||
|
||||
export const myWorkflow = createWorkflow(
|
||||
"my-workflow",
|
||||
function (input) {
|
||||
const product = createProductStep(input)
|
||||
const productCreatedHook = createHook(
|
||||
"productCreated",
|
||||
{ productId: product.id }
|
||||
)
|
||||
|
||||
return new WorkflowResponse(product, {
|
||||
hooks: [productCreatedHook],
|
||||
})
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
The `createHook` function accepts two parameters:
|
||||
|
||||
1. The first is a string indicating the hook's name. This is used to add a hook handler later.
|
||||
2. The second is the input to pass to the hook handler.
|
||||
|
||||
The workflow must also pass an object having a `hooks` property as a second parameter to the `WorkflowResponse` constructor. Its value is an array of the workflow's hooks.
|
||||
|
||||
---
|
||||
|
||||
## How to Consume a Hook?
|
||||
|
||||
You consume a hook by registering a hook handler on the workflow. A hook handler is registered in a TypeScript or JavaScript file created in the `src/workflows/hooks` directory.
|
||||
|
||||
You'll find a workflow's exposed hooks in its `hooks` property.
|
||||
|
||||
For example, to consume the hook of the workflow in the previous example, create the file `src/workflows/hooks/my-workflow.ts` with the following content:
|
||||
|
||||
export const handlerHighlights = [
|
||||
["3", "productCreated", "Invoke the hook, passing it a step function as a parameter."],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/hooks/my-workflow.ts" highlights={handlerHighlights}
|
||||
import { myWorkflow } from "../my-workflow"
|
||||
|
||||
myWorkflow.hooks.productCreated(
|
||||
async ({ productId }, { container }) => {
|
||||
// TODO perform an action
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
<Note>
|
||||
|
||||
A hook can have only one handler.
|
||||
|
||||
</Note>
|
||||
|
||||
The hook is available on the workflow's `hooks` property using its name `productCreated`. You invoke the hook, passing the handler as a parameter, which is a step function.
|
||||
|
||||
Similar to a step, the handler receives the hook's input as a first parameter, and the container in the object as a second parameter.
|
||||
|
||||
### Hook Handler Compensation
|
||||
|
||||
You can also pass a compensation function as a second parameter:
|
||||
|
||||
```ts
|
||||
import { myWorkflow } from "../my-workflow"
|
||||
|
||||
myWorkflow.hooks.productCreated(
|
||||
async ({ productId }, { container }) => {
|
||||
// TODO perform an action
|
||||
},
|
||||
async () => {
|
||||
// undo the performed action
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
The compensation function is executed if an error occurs in the workflow to undo the actions performed by the hook handler.
|
||||
@@ -205,8 +205,11 @@ With the steps ready, you'll create the workflow that runs these steps to update
|
||||
|
||||
Change the content of `src/workflows/update-product-erp/index.ts` to the following:
|
||||
|
||||
```ts title="src/workflows/update-product-erp/index.ts" collapsibleLines="1-5" expandButtonLabel="Show Imports"
|
||||
import { createWorkflow } from "@medusajs/workflows-sdk"
|
||||
```ts title="src/workflows/update-product-erp/index.ts" collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { UpsertProductDTO } from "@medusajs/types"
|
||||
import updateProduct from "./steps/update-product"
|
||||
import updateErp from "./steps/update-erp"
|
||||
@@ -219,10 +222,10 @@ const updateProductAndErpWorkflow = createWorkflow(
|
||||
const product = updateProduct(input)
|
||||
const erpData = updateErp(input)
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
product,
|
||||
erpData,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export default updateProductAndErpWorkflow
|
||||
|
||||
@@ -54,10 +54,11 @@ const step2 = createStep(
|
||||
|
||||
2. Use the steps in a workflow. For example:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts" collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
```ts title="src/workflows/hello-world.ts" collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
// other imports...
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
// ...
|
||||
@@ -68,9 +69,9 @@ const myWorkflow = createWorkflow(
|
||||
const str1 = step1()
|
||||
step2()
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
message: str1,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
@@ -19,14 +19,15 @@ Since if-conditions aren't allowed in the workflow constructor function, use the
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["15", "input", "The data to pass as a parameter to the function in the second parameter"],
|
||||
["17", "return", "The function must return a boolean value indicating whether\nthe callback function passed to `then` should be executed."],
|
||||
["19", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."]
|
||||
["16", "input", "The data to pass as a parameter to the function in the second parameter"],
|
||||
["18", "return", "The function must return a boolean value indicating whether\nthe callback function passed to `then` should be executed."],
|
||||
["20", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."]
|
||||
]
|
||||
|
||||
```ts highlights={highlights}
|
||||
import {
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
when,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
// step imports...
|
||||
@@ -49,7 +50,11 @@ const workflow = createWorkflow(
|
||||
})
|
||||
|
||||
// executed without condition
|
||||
return anotherStep(result)
|
||||
const anotherStepResult = anotherStep(result)
|
||||
|
||||
return new WorkflowResponse(
|
||||
anotherStepResult
|
||||
)
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
@@ -22,10 +22,11 @@ A workflow is considered long-running if at least one step has its `async` confi
|
||||
|
||||
For example, consider the following workflow and steps:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["14"]]} collapsibleLines="1-10" expandButtonLabel="Show More"
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["15"]]} collapsibleLines="1-11" expandButtonLabel="Show More"
|
||||
import {
|
||||
createStep,
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
@@ -54,9 +55,9 @@ const myWorkflow = createWorkflow(
|
||||
step2()
|
||||
const message = step3()
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
message,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
@@ -15,13 +15,14 @@ The workflow waits until all steps passed to the `parallelize` function finish e
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["21", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."],
|
||||
["21", "parallelize", "Run the steps passed as parameters in parallel."],
|
||||
["22", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."],
|
||||
["22", "parallelize", "Run the steps passed as parameters in parallel."],
|
||||
]
|
||||
|
||||
```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
parallelize,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
@@ -46,7 +47,9 @@ const myWorkflow = createWorkflow(
|
||||
)
|
||||
|
||||
const id = product.id
|
||||
return getProductStep(product.id)
|
||||
const refetchedProduct = getProductStep(product.id)
|
||||
|
||||
return new WorkflowResponse(refetchedProduct)
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
@@ -14,10 +14,11 @@ You can configure the step to retry on failure. The `createStep` function can ac
|
||||
|
||||
For example:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["9"]]} collapsibleLines="1-5" expandButtonLabel="Show Imports"
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["10"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
const step1 = createStep(
|
||||
@@ -37,9 +38,9 @@ const myWorkflow = createWorkflow(
|
||||
function () {
|
||||
const str1 = step1()
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
message: str1,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
@@ -20,10 +20,11 @@ Timeout doesn't stop the execution of a running step. The timeout only affects t
|
||||
|
||||
For example:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["15"]]} collapsibleLines="1-12" expandButtonLabel="Show More"
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["16"]]} collapsibleLines="1-13" expandButtonLabel="Show More"
|
||||
import {
|
||||
createStep,
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
const step1 = createStep(
|
||||
@@ -39,9 +40,9 @@ const myWorkflow = createWorkflow({
|
||||
}, function () {
|
||||
const str1 = step1()
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
message: str1,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
@@ -56,6 +56,7 @@ Next, add the following to the same file to create the workflow using the `creat
|
||||
import {
|
||||
// other imports...
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
// ...
|
||||
@@ -67,9 +68,9 @@ const myWorkflow = createWorkflow(
|
||||
// to pass input
|
||||
const str2 = step2(input)
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
message: str1,
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -78,6 +79,8 @@ export default myWorkflow
|
||||
|
||||
This creates a `hello-world` workflow. When you create a workflow, it’s constructed but not executed yet.
|
||||
|
||||
The workflow must return an instance of `WorkflowResponse`, whose first parameter is returned to workflow executors.
|
||||
|
||||
### 3. Execute the Workflow
|
||||
|
||||
You can execute a workflow from different resources within Medusa.
|
||||
@@ -223,19 +226,20 @@ Each step in the workflow receives as a second parameter a `context` object. The
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["11", "resolve", "Resolve the Product Module's main service."],
|
||||
["12", "resolve", "Resolve the Product Module's main service."],
|
||||
[
|
||||
"11",
|
||||
"12",
|
||||
"ModuleRegistrationName.PRODUCT",
|
||||
"The resource registration name imported from `@medusajs/modules-sdk`.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/product-count.ts" highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
|
||||
```ts title="src/workflows/product-count.ts" highlights={highlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
@@ -254,9 +258,9 @@ const myWorkflow = createWorkflow(
|
||||
function () {
|
||||
const count = step1()
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
count,
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -207,6 +207,10 @@ export const sidebar = sidebarAttachHrefCommonOptions(
|
||||
path: "/advanced-development/workflows/compensation-function",
|
||||
title: "Compensation Function",
|
||||
},
|
||||
{
|
||||
path: "/advanced-development/workflows/add-workflow-hook",
|
||||
title: "Workflow Hooks",
|
||||
},
|
||||
{
|
||||
path: "/advanced-development/workflows/access-workflow-errors",
|
||||
title: "Access Workflow Errors",
|
||||
|
||||
@@ -33,7 +33,7 @@ export const highlights = [
|
||||
```ts title="src/api/middlewares.ts" highlights={highlights}
|
||||
import {
|
||||
defineMiddlewares,
|
||||
authenticate
|
||||
authenticate,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export default defineMiddlewares({
|
||||
|
||||
@@ -37,10 +37,10 @@ Start by creating a workflow that does two things:
|
||||
For example, create the file `src/workflows/create-manager.ts`. with the following content:
|
||||
|
||||
export const workflowHighlights = [
|
||||
["20", "createManagerStep", "Thi step creates a manager."],
|
||||
["37", "createManagerWorkflow", "The workflow used to create a manager and set its auth identity's actor type."],
|
||||
["44", "setAuthAppMetadataStep", "Set the actor type of the manager's associated auth identity."],
|
||||
["46", '"manager"', "The actor type of the manager."]
|
||||
["21", "createManagerStep", "Thi step creates a manager."],
|
||||
["38", "createManagerWorkflow", "The workflow used to create a manager and set its auth identity's actor type."],
|
||||
["45", "setAuthAppMetadataStep", "Set the actor type of the manager's associated auth identity."],
|
||||
["47", '"manager"', "The actor type of the manager."]
|
||||
]
|
||||
|
||||
```ts title="src/workflows/create-manager.ts" highlights={workflowHighlights}
|
||||
@@ -48,6 +48,7 @@ import {
|
||||
createWorkflow,
|
||||
createStep,
|
||||
StepResponse,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
setAuthAppMetadataStep,
|
||||
@@ -93,7 +94,7 @@ const createManagerWorkflow = createWorkflow(
|
||||
value: manager.id,
|
||||
})
|
||||
|
||||
return manager
|
||||
return new WorkflowResponse(manager)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -184,7 +185,7 @@ export const middlewareHighlights = [
|
||||
```ts title="src/api/middlewares.ts" highlights={middlewareHighlights}
|
||||
import {
|
||||
defineMiddlewares,
|
||||
authenticate
|
||||
authenticate,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export default defineMiddlewares({
|
||||
|
||||
@@ -30,15 +30,15 @@ const query = remoteQueryObjectFromString({
|
||||
fields: [
|
||||
"*",
|
||||
"variants.*",
|
||||
"variants.prices.*"
|
||||
"variants.prices.*",
|
||||
],
|
||||
variables: {
|
||||
filters: {
|
||||
id: [
|
||||
"prod_123"
|
||||
]
|
||||
"prod_123",
|
||||
],
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
// `result` is array of products
|
||||
@@ -72,19 +72,19 @@ const query = remoteQueryObjectFromString({
|
||||
fields: [
|
||||
"*",
|
||||
"variants.*",
|
||||
"variants.calculated_price.*"
|
||||
"variants.calculated_price.*",
|
||||
],
|
||||
variables: {
|
||||
filters: {
|
||||
id
|
||||
id,
|
||||
},
|
||||
"variants.calculated_price": {
|
||||
context: {
|
||||
region_id: "reg_01J3MRPDNXXXDSCC76Y6YCZARS",
|
||||
currency_code: "eur"
|
||||
}
|
||||
}
|
||||
}
|
||||
currency_code: "eur",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// `result` is array of products
|
||||
|
||||
@@ -35,7 +35,7 @@ module.exports = defineConfig({
|
||||
admin: {
|
||||
disable: process.env.DISABLE_MEDUSA_ADMIN === "true",
|
||||
backendUrl: process.env.MEDUSA_BACKEND_URL,
|
||||
path: process.env.MEDUSA_ADMIN_PATH
|
||||
path: process.env.MEDUSA_ADMIN_PATH,
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -50,7 +50,7 @@ So, add the following configuration in `medusa-config.js`:
|
||||
module.exports = defineConfig({
|
||||
projectConfig: {
|
||||
// ...
|
||||
workerMode: process.env.MEDUSA_WORKER_MODE
|
||||
workerMode: process.env.MEDUSA_WORKER_MODE,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -77,8 +77,8 @@ Add the following configuration in `medusa-config.js`:
|
||||
module.exports = defineConfig({
|
||||
// ...
|
||||
admin: {
|
||||
disable: process.env.DISABLE_MEDUSA_ADMIN === "true"
|
||||
}
|
||||
disable: process.env.DISABLE_MEDUSA_ADMIN === "true",
|
||||
},
|
||||
})
|
||||
|
||||
```
|
||||
@@ -93,7 +93,7 @@ Add the following configuration in `medusa-config.js` :
|
||||
module.exports = defineConfig({
|
||||
projectConfig: {
|
||||
// ...
|
||||
redisUrl: process.env.REDIS_URL
|
||||
redisUrl: process.env.REDIS_URL,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -142,7 +142,7 @@ For example, add the following dependencies in `package.json` for the Cache, Eve
|
||||
Then, add these modules in `medusa-config.js`:
|
||||
|
||||
```js title="medusa-config.js"
|
||||
import { Modules } from '@medusajs/utils'
|
||||
import { Modules } from "@medusajs/utils"
|
||||
|
||||
module.exports = defineConfig({
|
||||
// ...
|
||||
@@ -167,8 +167,8 @@ module.exports = defineConfig({
|
||||
url: process.env.REDIS_URL,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -293,7 +293,7 @@ module.exports = defineConfig({
|
||||
// ...
|
||||
admin: {
|
||||
// ...
|
||||
backendUrl: process.env.MEDUSA_BACKEND_URL
|
||||
backendUrl: process.env.MEDUSA_BACKEND_URL,
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -485,7 +485,7 @@ export const syncProductsWorkflowHighlight = [
|
||||
import {
|
||||
StepResponse,
|
||||
createStep,
|
||||
createWorkflow
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
type RetrieveStoreStepInput = {
|
||||
@@ -597,9 +597,7 @@ export const syncProductsWorkflowHighlight = [
|
||||
store_id: string
|
||||
}
|
||||
|
||||
export const syncProductsWorkflow = createWorkflow<
|
||||
SyncProductsWorkflowInput, {}
|
||||
>(
|
||||
export const syncProductsWorkflow = createWorkflow(
|
||||
"sync-products-workflow",
|
||||
function ({ store_id }: SyncProductsWorkflowInput) {
|
||||
const { store } = retrieveStoreStep({
|
||||
|
||||
@@ -163,24 +163,25 @@ Workflows guarantee data consistency through their compensation feature. You can
|
||||
For example, create the following workflow in `src/workflows/create-product.ts`:
|
||||
|
||||
export const workflowHighlights = [
|
||||
["18", "createInErpStep", "A step that creates a product in the ERP system."],
|
||||
["21", "erpModuleService", "Resolve the ERP Module's main service."],
|
||||
["24", "productModuleService", "Resolve the Product Module's main service."],
|
||||
["28", "retrieve", "Retrieve the created product's data."],
|
||||
["30", "createProduct", "Create the product in the ERP system."],
|
||||
["34", "update", "Update the product in Medusa with the ID of the ERP product."],
|
||||
["43", "erpId", "Pass the ERP product's ID to the compensation function."],
|
||||
["44", "productId", "Pass the product's ID to the compensation function."],
|
||||
["46", "", "Define a compensation function that rolls back changes when an error occurs."],
|
||||
["53", "deleteProduct", "Undo creating the product in the ERP system by deleting it."],
|
||||
["54", "update", "Update the product in Medusa to remove the ERP product's ID."]
|
||||
["19", "createInErpStep", "A step that creates a product in the ERP system."],
|
||||
["22", "erpModuleService", "Resolve the ERP Module's main service."],
|
||||
["25", "productModuleService", "Resolve the Product Module's main service."],
|
||||
["29", "retrieve", "Retrieve the created product's data."],
|
||||
["31", "createProduct", "Create the product in the ERP system."],
|
||||
["35", "update", "Update the product in Medusa with the ID of the ERP product."],
|
||||
["44", "erpId", "Pass the ERP product's ID to the compensation function."],
|
||||
["45", "productId", "Pass the product's ID to the compensation function."],
|
||||
["47", "", "Define a compensation function that rolls back changes when an error occurs."],
|
||||
["54", "deleteProduct", "Undo creating the product in the ERP system by deleting it."],
|
||||
["55", "update", "Update the product in Medusa to remove the ERP product's ID."]
|
||||
]
|
||||
|
||||
```ts title="src/workflows/create-product.ts" highlights={workflowHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
```ts title="src/workflows/create-product.ts" highlights={workflowHighlights} collapsibleLines="1-10" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
createWorkflow
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
@@ -240,7 +241,7 @@ export const workflowHighlights = [
|
||||
>("create-product-in-systems", function (input) {
|
||||
const erpData = createInErpStep(input)
|
||||
|
||||
return erpData
|
||||
return new WorkflowResponse(erpData)
|
||||
})
|
||||
|
||||
export default createProductWorkflow
|
||||
|
||||
@@ -292,12 +292,15 @@ In the compensation function, which runs if an error occurs in the workflow, it
|
||||
Then, create the workflow at `src/workflows/marketplace/create-vendor-admin/index.ts` with the following content:
|
||||
|
||||
export const vendorAdminWorkflowHighlights = [
|
||||
["20", "createVendorAdminStep", "Create the vendor admin."],
|
||||
["24", "setAuthAppMetadataStep", "Step is imported from `@medusajs/core-flows`."]
|
||||
["23", "createVendorAdminStep", "Create the vendor admin."],
|
||||
["27", "setAuthAppMetadataStep", "Step is imported from `@medusajs/core-flows`."]
|
||||
]
|
||||
|
||||
```ts title="src/workflows/marketplace/create-vendor-admin/index.ts" highlights={vendorAdminWorkflowHighlights}
|
||||
import { createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
setAuthAppMetadataStep,
|
||||
} from "@medusajs/core-flows"
|
||||
@@ -326,7 +329,7 @@ const createVendorAdminWorkflow = createWorkflow(
|
||||
value: vendorAdmin.id,
|
||||
})
|
||||
|
||||
return vendorAdmin
|
||||
return new WorkflowResponse(vendorAdmin)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -457,7 +460,7 @@ Next, create the file `src/api/middlewares.ts` with the following content:
|
||||
```ts title="src/api/middlewares.ts"
|
||||
import {
|
||||
defineMiddlewares,
|
||||
authenticate
|
||||
authenticate,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export default defineMiddlewares({
|
||||
@@ -709,13 +712,13 @@ Finally, in `src/api/middlewares.ts`, apply a middleware on the create products
|
||||
```ts title="src/api/middlewares.ts"
|
||||
import {
|
||||
defineMiddlewares,
|
||||
authenticate
|
||||
authenticate,
|
||||
} from "@medusajs/medusa"
|
||||
import {
|
||||
validateAndTransformBody
|
||||
validateAndTransformBody,
|
||||
} from "@medusajs/medusa/dist/api/utils/validate-body"
|
||||
import {
|
||||
AdminCreateProduct
|
||||
AdminCreateProduct,
|
||||
} from "@medusajs/medusa/dist/api/admin/products/validators"
|
||||
|
||||
export default defineMiddlewares({
|
||||
@@ -1002,7 +1005,7 @@ try {
|
||||
const items = vendorsItems[vendorId]
|
||||
const vendor = vendors.find((v) => v.id === vendorId)!
|
||||
|
||||
const {result: childOrder} = await createOrdersWorkflow(
|
||||
const { result: childOrder } = await createOrdersWorkflow(
|
||||
container
|
||||
)
|
||||
.run({
|
||||
@@ -1123,19 +1126,22 @@ The compensation function cancels all child orders received from the step. It us
|
||||
Finally, create the workflow at the file `src/workflows/marketplace/create-vendor-orders/index.ts`:
|
||||
|
||||
export const createVendorOrdersWorkflowHighlights = [
|
||||
["18", "useRemoteQueryStep", "Retrieve the cart's details."],
|
||||
["26", "completeCartWorkflow", "Create the parent order from the cart."],
|
||||
["32", "groupVendorItemsStep", "Group the items by their vendor."],
|
||||
["39", "createVendorOrdersStep", "Create child orders for each vendor"],
|
||||
["44", "createRemoteLinkStep", "Create the links returned by the previous step."]
|
||||
["21", "useRemoteQueryStep", "Retrieve the cart's details."],
|
||||
["29", "completeCartWorkflow", "Create the parent order from the cart."],
|
||||
["35", "groupVendorItemsStep", "Group the items by their vendor."],
|
||||
["42", "createVendorOrdersStep", "Create child orders for each vendor"],
|
||||
["47", "createRemoteLinkStep", "Create the links returned by the previous step."]
|
||||
]
|
||||
|
||||
```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-10" expandMoreLabel="Show Imports"
|
||||
import { createWorkflow } from "@medusajs/workflows-sdk"
|
||||
```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-13" expandMoreLabel="Show Imports"
|
||||
import {
|
||||
createWorkflow,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
useRemoteQueryStep,
|
||||
createRemoteLinkStep,
|
||||
completeCartWorkflow
|
||||
completeCartWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { CartDTO } from "@medusajs/types"
|
||||
import groupVendorItemsStep from "./steps/group-vendor-items"
|
||||
@@ -1150,7 +1156,7 @@ const createVendorOrdersWorkflow = createWorkflow(
|
||||
(input: WorkflowInput) => {
|
||||
const cart = useRemoteQueryStep({
|
||||
entry_point: "cart",
|
||||
fields: ['items.*'],
|
||||
fields: ["items.*"],
|
||||
variables: { id: input.cart_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
@@ -1158,28 +1164,28 @@ const createVendorOrdersWorkflow = createWorkflow(
|
||||
|
||||
const order = completeCartWorkflow.runAsStep({
|
||||
input: {
|
||||
id: cart.id
|
||||
}
|
||||
id: cart.id,
|
||||
},
|
||||
})
|
||||
|
||||
const { vendorsItems } = groupVendorItemsStep({
|
||||
cart
|
||||
cart,
|
||||
})
|
||||
|
||||
const {
|
||||
orders: vendorOrders,
|
||||
linkDefs
|
||||
linkDefs,
|
||||
} = createVendorOrdersStep({
|
||||
parentOrder: order,
|
||||
vendorsItems
|
||||
vendorsItems,
|
||||
})
|
||||
|
||||
createRemoteLinkStep(linkDefs)
|
||||
|
||||
return {
|
||||
return new WorkflowResponse({
|
||||
parent_order: order,
|
||||
vendor_orders: vendorOrders
|
||||
}
|
||||
vendor_orders: vendorOrders,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ const post = await postModuleService.createPosts({
|
||||
name: "My Post",
|
||||
published_at: new Date(),
|
||||
metadata: {
|
||||
external_id: "1234"
|
||||
}
|
||||
external_id: "1234",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -32,12 +32,12 @@ If an object is passed of the method, an object of the created record is also re
|
||||
const posts = await postModuleService.createPosts([
|
||||
{
|
||||
name: "My Post",
|
||||
published_at: new Date()
|
||||
published_at: new Date(),
|
||||
},
|
||||
{
|
||||
name: "My Other Post",
|
||||
published_at: new Date()
|
||||
}
|
||||
published_at: new Date(),
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ To delete one record, pass its ID as a parameter of the method.
|
||||
```ts
|
||||
await postModuleService.deletePosts([
|
||||
"123",
|
||||
"321"
|
||||
"321",
|
||||
])
|
||||
```
|
||||
|
||||
@@ -37,7 +37,7 @@ To delete multiple records, pass an array of IDs as a parameter of the method.
|
||||
|
||||
```ts
|
||||
await postModuleService.deletePosts({
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ If no parameters are passed, the method returns an array of the first `15` recor
|
||||
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({
|
||||
id: ["123", "321"]
|
||||
id: ["123", "321"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -54,7 +54,7 @@ This applies to relations between data models of the same module. To retrieve li
|
||||
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({}, {
|
||||
relations: ["author"]
|
||||
relations: ["author"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -72,7 +72,7 @@ The method returns an array of the first `15` records matching the filters.
|
||||
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({}, {
|
||||
select: ["id", "name"]
|
||||
select: ["id", "name"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -93,7 +93,7 @@ The method returns an array of the first `15` records matching the filters.
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({}, {
|
||||
take: 20,
|
||||
skip: 10
|
||||
skip: 10,
|
||||
})
|
||||
```
|
||||
|
||||
@@ -115,8 +115,8 @@ The method returns an array of records. The number of records is less than or eq
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({}, {
|
||||
order: {
|
||||
name: "ASC"
|
||||
}
|
||||
name: "ASC",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ If no parameters are passed, the method returns an array with two items:
|
||||
|
||||
```ts
|
||||
const [posts, count] = await postModuleService.listAndCountPosts({
|
||||
id: ["123", "321"]
|
||||
id: ["123", "321"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -60,7 +60,7 @@ This applies to relations between data models of the same module. To retrieve li
|
||||
|
||||
```ts
|
||||
const [posts, count] = await postModuleService.listAndCountPosts({}, {
|
||||
relations: ["author"]
|
||||
relations: ["author"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -81,7 +81,7 @@ The method returns an array with two items:
|
||||
|
||||
```ts
|
||||
const [posts, count] = await postModuleService.listAndCountPosts({}, {
|
||||
select: ["id", "name"]
|
||||
select: ["id", "name"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -105,7 +105,7 @@ The method returns an array with two items:
|
||||
```ts
|
||||
const [posts, count] = await postModuleService.listAndCountPosts({}, {
|
||||
take: 20,
|
||||
skip: 10
|
||||
skip: 10,
|
||||
})
|
||||
```
|
||||
|
||||
@@ -130,8 +130,8 @@ The method returns an array with two items:
|
||||
```ts
|
||||
const [posts, count] = await postModuleService.listAndCountPosts({}, {
|
||||
order: {
|
||||
name: "ASC"
|
||||
}
|
||||
name: "ASC",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ For example, the returned object of the above example is:
|
||||
|
||||
```ts
|
||||
restoredPosts = {
|
||||
post_id: ["123"]
|
||||
post_id: ["123"],
|
||||
}
|
||||
```
|
||||
|
||||
@@ -39,7 +39,7 @@ restoredPosts = {
|
||||
```ts
|
||||
const restoredPosts = await postModuleService.restorePosts([
|
||||
"123",
|
||||
"321"
|
||||
"321",
|
||||
])
|
||||
```
|
||||
|
||||
@@ -57,8 +57,8 @@ For example, the returned object of the above example is:
|
||||
restoredPosts = {
|
||||
post_id: [
|
||||
"123",
|
||||
"321"
|
||||
]
|
||||
"321",
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
@@ -68,7 +68,7 @@ restoredPosts = {
|
||||
|
||||
```ts
|
||||
const restoredPosts = await postModuleService.restorePosts({
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
})
|
||||
```
|
||||
|
||||
@@ -91,7 +91,7 @@ For example, the returned object of the above example is:
|
||||
```ts
|
||||
restoredPosts = {
|
||||
post_id: [
|
||||
"123"
|
||||
]
|
||||
"123",
|
||||
],
|
||||
}
|
||||
```
|
||||
@@ -36,7 +36,7 @@ This applies to relations between data models of the same module. To retrieve li
|
||||
|
||||
```ts
|
||||
const post = await postModuleService.retrievePost("123", {
|
||||
relations: ["author"]
|
||||
relations: ["author"],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -54,7 +54,7 @@ The method returns the record as an object.
|
||||
|
||||
```ts
|
||||
const post = await postModuleService.retrievePost("123", {
|
||||
select: ["id", "name"]
|
||||
select: ["id", "name"],
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ For example, the returned object of the above example is:
|
||||
|
||||
```ts
|
||||
deletedPosts = {
|
||||
post_id: ["123"]
|
||||
post_id: ["123"],
|
||||
}
|
||||
```
|
||||
|
||||
@@ -41,7 +41,7 @@ deletedPosts = {
|
||||
```ts
|
||||
const deletedPosts = await postModuleService.softDeletePosts([
|
||||
"123",
|
||||
"321"
|
||||
"321",
|
||||
])
|
||||
```
|
||||
|
||||
@@ -59,8 +59,8 @@ For example, the returned object of the above example is:
|
||||
deletedPosts = {
|
||||
post_id: [
|
||||
"123",
|
||||
"321"
|
||||
]
|
||||
"321",
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
@@ -70,7 +70,7 @@ deletedPosts = {
|
||||
|
||||
```ts
|
||||
const deletedPosts = await postModuleService.softDeletePosts({
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
})
|
||||
```
|
||||
|
||||
@@ -92,6 +92,6 @@ For example, the returned object of the above example is:
|
||||
|
||||
```ts
|
||||
deletedPosts = {
|
||||
post_id: ["123"]
|
||||
post_id: ["123"],
|
||||
}
|
||||
```
|
||||
@@ -15,7 +15,7 @@ This method updates one or more records of the data model.
|
||||
```ts
|
||||
const post = await postModuleService.updatePosts({
|
||||
id: "123",
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
})
|
||||
```
|
||||
|
||||
@@ -37,12 +37,12 @@ The method returns the updated record as an object.
|
||||
const posts = await postModuleService.updatePosts([
|
||||
{
|
||||
id: "123",
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
},
|
||||
{
|
||||
id: "321",
|
||||
published_at: new Date()
|
||||
}
|
||||
published_at: new Date(),
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
@@ -63,11 +63,11 @@ The method returns an array of objects of updated records.
|
||||
```ts
|
||||
const posts = await postModuleService.updatePosts({
|
||||
selector: {
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
},
|
||||
data: {
|
||||
published_at: new Date()
|
||||
}
|
||||
published_at: new Date(),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -98,22 +98,22 @@ The method returns an array of objects of updated records.
|
||||
const posts = await postModuleService.updatePosts([
|
||||
{
|
||||
selector: {
|
||||
name: "My Post"
|
||||
name: "My Post",
|
||||
},
|
||||
data: {
|
||||
published_at: new Date()
|
||||
}
|
||||
published_at: new Date(),
|
||||
},
|
||||
},
|
||||
{
|
||||
selector: {
|
||||
name: "Another Post"
|
||||
name: "Another Post",
|
||||
},
|
||||
data: {
|
||||
metadata: {
|
||||
external_id: "123"
|
||||
}
|
||||
}
|
||||
}
|
||||
external_id: "123",
|
||||
},
|
||||
},
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ In the example above, only posts having the name `My Post 2` are retrieved.
|
||||
const posts = await postModuleService.listPosts({
|
||||
views: [
|
||||
50,
|
||||
100
|
||||
]
|
||||
100,
|
||||
],
|
||||
})
|
||||
```
|
||||
|
||||
@@ -57,9 +57,9 @@ In the example above, only posts having either `50` or `100` views are retrieved
|
||||
const posts = await postModuleService.listPosts({
|
||||
name: {
|
||||
$nin: [
|
||||
"My Post"
|
||||
]
|
||||
}
|
||||
"My Post",
|
||||
],
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -80,8 +80,8 @@ This filter only applies to text-like properties, including `id` and `enum` prop
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({
|
||||
name: {
|
||||
$like: "My%"
|
||||
}
|
||||
$like: "My%",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -102,8 +102,8 @@ This filter only applies to the `number` and `dateTime` properties.
|
||||
```ts
|
||||
const posts = await postModuleService.listPosts({
|
||||
published_at: {
|
||||
$lt: new Date()
|
||||
}
|
||||
$lt: new Date(),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -128,8 +128,8 @@ endToday.setHours(23, 59, 59, 59)
|
||||
const posts = await postModuleService.listPosts({
|
||||
published_at: {
|
||||
$gte: startToday,
|
||||
$lte: endToday
|
||||
}
|
||||
$lte: endToday,
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -149,10 +149,10 @@ const posts = await postModuleService.listPosts({
|
||||
},
|
||||
{
|
||||
published_at: {
|
||||
$lt: new Date()
|
||||
}
|
||||
}
|
||||
]
|
||||
$lt: new Date(),
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ export const fetchHighlights = [
|
||||
const queryParams = new URLSearchParams({
|
||||
fields: `*variants.calculated_price`,
|
||||
region_id: region.id,
|
||||
country_code: region.countries[0].iso_2
|
||||
country_code: region.countries[0].iso_2,
|
||||
})
|
||||
|
||||
fetch(`http://localhost:9000/store/products/${id}?${queryParams.toString()}`, {
|
||||
@@ -135,7 +135,7 @@ export default function Product({ params: { id } }: Params) {
|
||||
const queryParams = new URLSearchParams({
|
||||
fields: `*variants.calculated_price`,
|
||||
region_id: region.id,
|
||||
country_code: region.countries[0].iso_2
|
||||
country_code: region.countries[0].iso_2,
|
||||
})
|
||||
|
||||
fetch(`http://localhost:9000/store/products/${id}?${queryParams.toString()}`, {
|
||||
|
||||
Reference in New Issue
Block a user