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:
Shahed Nasser authored and GitHub committed 2024-07-31 17:01:33 +03:00
1 parent 27837b8833
commit 6f973d9f2b
29 files changed
+321 -167

No files matched your search

@@ -142,7 +142,7 @@ export const highlights = [
```ts title="src/api/middlewares.ts" highlights={highlights} ```ts title="src/api/middlewares.ts" highlights={highlights}
import { import {
defineMiddlewares, defineMiddlewares,
authenticate authenticate,
} from "@medusajs/medusa" } from "@medusajs/medusa"
export default defineMiddlewares({ 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: 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" ```ts title="src/workflows/update-product-erp/index.ts" collapsibleLines="1-8" expandButtonLabel="Show Imports"
import { createWorkflow } from "@medusajs/workflows-sdk" import {
createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk"
import { UpsertProductDTO } from "@medusajs/types" import { UpsertProductDTO } from "@medusajs/types"
import updateProduct from "./steps/update-product" import updateProduct from "./steps/update-product"
import updateErp from "./steps/update-erp" import updateErp from "./steps/update-erp"
@@ -219,10 +222,10 @@ const updateProductAndErpWorkflow = createWorkflow(
const product = updateProduct(input) const product = updateProduct(input)
const erpData = updateErp(input) const erpData = updateErp(input)
return { return new WorkflowResponse({
product, product,
erpData, erpData,
} })
}) })
export default updateProductAndErpWorkflow export default updateProductAndErpWorkflow
@@ -54,10 +54,11 @@ const step2 = createStep(
2. Use the steps in a workflow. For example: 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 { import {
// other imports... // other imports...
createWorkflow, createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
// ... // ...
@@ -68,9 +69,9 @@ const myWorkflow = createWorkflow(
const str1 = step1() const str1 = step1()
step2() step2()
return { return new WorkflowResponse({
message: str1, message: str1,
} })
}) })
export default myWorkflow export default myWorkflow
@@ -19,14 +19,15 @@ Since if-conditions aren't allowed in the workflow constructor function, use the
For example: For example:
export const highlights = [ export const highlights = [
["15", "input", "The data to pass as a parameter to the function in the second parameter"], ["16", "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."], ["18", "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."] ["20", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."]
] ]
```ts highlights={highlights} ```ts highlights={highlights}
import { import {
createWorkflow, createWorkflow,
WorkflowResponse,
when, when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
// step imports... // step imports...
@@ -49,7 +50,11 @@ const workflow = createWorkflow(
}) })
// executed without condition // 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: 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 { import {
createStep, createStep,
createWorkflow, createWorkflow,
WorkflowResponse,
StepResponse, StepResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
@@ -54,9 +55,9 @@ const myWorkflow = createWorkflow(
step2() step2()
const message = step3() const message = step3()
return { return new WorkflowResponse({
message, message,
} })
}) })
export default myWorkflow export default myWorkflow
@@ -15,13 +15,14 @@ The workflow waits until all steps passed to the `parallelize` function finish e
For example: For example:
export const highlights = [ export const highlights = [
["21", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."], ["22", "[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", "parallelize", "Run the steps passed as parameters in parallel."],
] ]
```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports" ```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
import { import {
createWorkflow, createWorkflow,
WorkflowResponse,
parallelize, parallelize,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { import {
@@ -46,7 +47,9 @@ const myWorkflow = createWorkflow(
) )
const id = product.id 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: 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 { import {
createStep, createStep,
createWorkflow, createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
const step1 = createStep( const step1 = createStep(
@@ -37,9 +38,9 @@ const myWorkflow = createWorkflow(
function () { function () {
const str1 = step1() const str1 = step1()
return { return new WorkflowResponse({
message: str1, message: str1,
} })
}) })
export default myWorkflow export default myWorkflow
@@ -20,10 +20,11 @@ Timeout doesn't stop the execution of a running step. The timeout only affects t
For example: 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 { import {
createStep, createStep,
createWorkflow, createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
const step1 = createStep( const step1 = createStep(
@@ -39,9 +40,9 @@ const myWorkflow = createWorkflow({
}, function () { }, function () {
const str1 = step1() const str1 = step1()
return { return new WorkflowResponse({
message: str1, message: str1,
} })
}) })
export default myWorkflow export default myWorkflow
+11 -7
View File
@@ -56,6 +56,7 @@ Next, add the following to the same file to create the workflow using the `creat
import { import {
// other imports... // other imports...
createWorkflow, createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
// ... // ...
@@ -67,9 +68,9 @@ const myWorkflow = createWorkflow(
// to pass input // to pass input
const str2 = step2(input) const str2 = step2(input)
return { return new WorkflowResponse({
message: str1, 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. 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 ### 3. Execute the Workflow
You can execute a workflow from different resources within Medusa. 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: For example:
export const highlights = [ 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", "ModuleRegistrationName.PRODUCT",
"The resource registration name imported from `@medusajs/modules-sdk`.", "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 { import {
createStep, createStep,
StepResponse, StepResponse,
createWorkflow, createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { IProductModuleService } from "@medusajs/types" import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils" import { ModuleRegistrationName } from "@medusajs/utils"
@@ -254,9 +258,9 @@ const myWorkflow = createWorkflow(
function () { function () {
const count = step1() const count = step1()
return { return new WorkflowResponse({
count, count,
} })
} }
) )
+4
View File
@@ -207,6 +207,10 @@ export const sidebar = sidebarAttachHrefCommonOptions(
path: "/advanced-development/workflows/compensation-function", path: "/advanced-development/workflows/compensation-function",
title: "Compensation Function", title: "Compensation Function",
}, },
{
path: "/advanced-development/workflows/add-workflow-hook",
title: "Workflow Hooks",
},
{ {
path: "/advanced-development/workflows/access-workflow-errors", path: "/advanced-development/workflows/access-workflow-errors",
title: "Access Workflow Errors", title: "Access Workflow Errors",
@@ -33,7 +33,7 @@ export const highlights = [
```ts title="src/api/middlewares.ts" highlights={highlights} ```ts title="src/api/middlewares.ts" highlights={highlights}
import { import {
defineMiddlewares, defineMiddlewares,
authenticate authenticate,
} from "@medusajs/medusa" } from "@medusajs/medusa"
export default defineMiddlewares({ 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: For example, create the file `src/workflows/create-manager.ts`. with the following content:
export const workflowHighlights = [ export const workflowHighlights = [
["20", "createManagerStep", "Thi step creates a manager."], ["21", "createManagerStep", "Thi step creates a manager."],
["37", "createManagerWorkflow", "The workflow used to create a manager and set its auth identity's actor type."], ["38", "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."], ["45", "setAuthAppMetadataStep", "Set the actor type of the manager's associated auth identity."],
["46", '"manager"', "The actor type of the manager."] ["47", '"manager"', "The actor type of the manager."]
] ]
```ts title="src/workflows/create-manager.ts" highlights={workflowHighlights} ```ts title="src/workflows/create-manager.ts" highlights={workflowHighlights}
@@ -48,6 +48,7 @@ import {
createWorkflow, createWorkflow,
createStep, createStep,
StepResponse, StepResponse,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { import {
setAuthAppMetadataStep, setAuthAppMetadataStep,
@@ -93,7 +94,7 @@ const createManagerWorkflow = createWorkflow(
value: manager.id, value: manager.id,
}) })
return manager return new WorkflowResponse(manager)
} }
) )
@@ -184,7 +185,7 @@ export const middlewareHighlights = [
```ts title="src/api/middlewares.ts" highlights={middlewareHighlights} ```ts title="src/api/middlewares.ts" highlights={middlewareHighlights}
import { import {
defineMiddlewares, defineMiddlewares,
authenticate authenticate,
} from "@medusajs/medusa" } from "@medusajs/medusa"
export default defineMiddlewares({ export default defineMiddlewares({
@@ -30,15 +30,15 @@ const query = remoteQueryObjectFromString({
fields: [ fields: [
"*", "*",
"variants.*", "variants.*",
"variants.prices.*" "variants.prices.*",
], ],
variables: { variables: {
filters: { filters: {
id: [ id: [
"prod_123" "prod_123",
] ],
}, },
} },
}) })
// `result` is array of products // `result` is array of products
@@ -72,19 +72,19 @@ const query = remoteQueryObjectFromString({
fields: [ fields: [
"*", "*",
"variants.*", "variants.*",
"variants.calculated_price.*" "variants.calculated_price.*",
], ],
variables: { variables: {
filters: { filters: {
id id,
}, },
"variants.calculated_price": { "variants.calculated_price": {
context: { context: {
region_id: "reg_01J3MRPDNXXXDSCC76Y6YCZARS", region_id: "reg_01J3MRPDNXXXDSCC76Y6YCZARS",
currency_code: "eur" currency_code: "eur",
} },
} },
} },
}) })
// `result` is array of products // `result` is array of products
@@ -35,7 +35,7 @@ module.exports = defineConfig({
admin: { admin: {
disable: process.env.DISABLE_MEDUSA_ADMIN === "true", disable: process.env.DISABLE_MEDUSA_ADMIN === "true",
backendUrl: process.env.MEDUSA_BACKEND_URL, 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({ module.exports = defineConfig({
projectConfig: { 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({ module.exports = defineConfig({
// ... // ...
admin: { 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({ module.exports = defineConfig({
projectConfig: { 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`: Then, add these modules in `medusa-config.js`:
```js title="medusa-config.js" ```js title="medusa-config.js"
import { Modules } from '@medusajs/utils' import { Modules } from "@medusajs/utils"
module.exports = defineConfig({ module.exports = defineConfig({
// ... // ...
@@ -167,8 +167,8 @@ module.exports = defineConfig({
url: process.env.REDIS_URL, url: process.env.REDIS_URL,
}, },
}, },
} },
} },
}) })
``` ```
@@ -293,7 +293,7 @@ module.exports = defineConfig({
// ... // ...
admin: { admin: {
// ... // ...
backendUrl: process.env.MEDUSA_BACKEND_URL backendUrl: process.env.MEDUSA_BACKEND_URL,
}, },
}) })
``` ```
@@ -485,7 +485,7 @@ export const syncProductsWorkflowHighlight = [
import { import {
StepResponse, StepResponse,
createStep, createStep,
createWorkflow createWorkflow,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
type RetrieveStoreStepInput = { type RetrieveStoreStepInput = {
@@ -597,9 +597,7 @@ export const syncProductsWorkflowHighlight = [
store_id: string store_id: string
} }
export const syncProductsWorkflow = createWorkflow< export const syncProductsWorkflow = createWorkflow(
SyncProductsWorkflowInput, {}
>(
"sync-products-workflow", "sync-products-workflow",
function ({ store_id }: SyncProductsWorkflowInput) { function ({ store_id }: SyncProductsWorkflowInput) {
const { store } = retrieveStoreStep({ 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`: For example, create the following workflow in `src/workflows/create-product.ts`:
export const workflowHighlights = [ export const workflowHighlights = [
["18", "createInErpStep", "A step that creates a product in the ERP system."], ["19", "createInErpStep", "A step that creates a product in the ERP system."],
["21", "erpModuleService", "Resolve the ERP Module's main service."], ["22", "erpModuleService", "Resolve the ERP Module's main service."],
["24", "productModuleService", "Resolve the Product Module's main service."], ["25", "productModuleService", "Resolve the Product Module's main service."],
["28", "retrieve", "Retrieve the created product's data."], ["29", "retrieve", "Retrieve the created product's data."],
["30", "createProduct", "Create the product in the ERP system."], ["31", "createProduct", "Create the product in the ERP system."],
["34", "update", "Update the product in Medusa with the ID of the ERP product."], ["35", "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", "erpId", "Pass the ERP product's ID to the compensation function."],
["44", "productId", "Pass the product's ID to the compensation function."], ["45", "productId", "Pass the product's ID to the compensation function."],
["46", "", "Define a compensation function that rolls back changes when an error occurs."], ["47", "", "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", "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."] ["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 { import {
createStep, createStep,
StepResponse, StepResponse,
createWorkflow createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { IProductModuleService } from "@medusajs/types" import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils" import { ModuleRegistrationName } from "@medusajs/utils"
@@ -240,7 +241,7 @@ export const workflowHighlights = [
>("create-product-in-systems", function (input) { >("create-product-in-systems", function (input) {
const erpData = createInErpStep(input) const erpData = createInErpStep(input)
return erpData return new WorkflowResponse(erpData)
}) })
export default createProductWorkflow 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: Then, create the workflow at `src/workflows/marketplace/create-vendor-admin/index.ts` with the following content:
export const vendorAdminWorkflowHighlights = [ export const vendorAdminWorkflowHighlights = [
["20", "createVendorAdminStep", "Create the vendor admin."], ["23", "createVendorAdminStep", "Create the vendor admin."],
["24", "setAuthAppMetadataStep", "Step is imported from `@medusajs/core-flows`."] ["27", "setAuthAppMetadataStep", "Step is imported from `@medusajs/core-flows`."]
] ]
```ts title="src/workflows/marketplace/create-vendor-admin/index.ts" highlights={vendorAdminWorkflowHighlights} ```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 { import {
setAuthAppMetadataStep, setAuthAppMetadataStep,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
@@ -326,7 +329,7 @@ const createVendorAdminWorkflow = createWorkflow(
value: vendorAdmin.id, 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" ```ts title="src/api/middlewares.ts"
import { import {
defineMiddlewares, defineMiddlewares,
authenticate authenticate,
} from "@medusajs/medusa" } from "@medusajs/medusa"
export default defineMiddlewares({ 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" ```ts title="src/api/middlewares.ts"
import { import {
defineMiddlewares, defineMiddlewares,
authenticate authenticate,
} from "@medusajs/medusa" } from "@medusajs/medusa"
import { import {
validateAndTransformBody validateAndTransformBody,
} from "@medusajs/medusa/dist/api/utils/validate-body" } from "@medusajs/medusa/dist/api/utils/validate-body"
import { import {
AdminCreateProduct AdminCreateProduct,
} from "@medusajs/medusa/dist/api/admin/products/validators" } from "@medusajs/medusa/dist/api/admin/products/validators"
export default defineMiddlewares({ export default defineMiddlewares({
@@ -1002,7 +1005,7 @@ try {
const items = vendorsItems[vendorId] const items = vendorsItems[vendorId]
const vendor = vendors.find((v) => v.id === vendorId)! const vendor = vendors.find((v) => v.id === vendorId)!
const {result: childOrder} = await createOrdersWorkflow( const { result: childOrder } = await createOrdersWorkflow(
container container
) )
.run({ .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`: Finally, create the workflow at the file `src/workflows/marketplace/create-vendor-orders/index.ts`:
export const createVendorOrdersWorkflowHighlights = [ export const createVendorOrdersWorkflowHighlights = [
["18", "useRemoteQueryStep", "Retrieve the cart's details."], ["21", "useRemoteQueryStep", "Retrieve the cart's details."],
["26", "completeCartWorkflow", "Create the parent order from the cart."], ["29", "completeCartWorkflow", "Create the parent order from the cart."],
["32", "groupVendorItemsStep", "Group the items by their vendor."], ["35", "groupVendorItemsStep", "Group the items by their vendor."],
["39", "createVendorOrdersStep", "Create child orders for each vendor"], ["42", "createVendorOrdersStep", "Create child orders for each vendor"],
["44", "createRemoteLinkStep", "Create the links returned by the previous step."] ["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" ```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-13" expandMoreLabel="Show Imports"
import { createWorkflow } from "@medusajs/workflows-sdk" import {
createWorkflow,
WorkflowResponse,
} from "@medusajs/workflows-sdk"
import { import {
useRemoteQueryStep, useRemoteQueryStep,
createRemoteLinkStep, createRemoteLinkStep,
completeCartWorkflow completeCartWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { CartDTO } from "@medusajs/types" import { CartDTO } from "@medusajs/types"
import groupVendorItemsStep from "./steps/group-vendor-items" import groupVendorItemsStep from "./steps/group-vendor-items"
@@ -1150,7 +1156,7 @@ const createVendorOrdersWorkflow = createWorkflow(
(input: WorkflowInput) => { (input: WorkflowInput) => {
const cart = useRemoteQueryStep({ const cart = useRemoteQueryStep({
entry_point: "cart", entry_point: "cart",
fields: ['items.*'], fields: ["items.*"],
variables: { id: input.cart_id }, variables: { id: input.cart_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
@@ -1158,28 +1164,28 @@ const createVendorOrdersWorkflow = createWorkflow(
const order = completeCartWorkflow.runAsStep({ const order = completeCartWorkflow.runAsStep({
input: { input: {
id: cart.id id: cart.id,
} },
}) })
const { vendorsItems } = groupVendorItemsStep({ const { vendorsItems } = groupVendorItemsStep({
cart cart,
}) })
const { const {
orders: vendorOrders, orders: vendorOrders,
linkDefs linkDefs,
} = createVendorOrdersStep({ } = createVendorOrdersStep({
parentOrder: order, parentOrder: order,
vendorsItems vendorsItems,
}) })
createRemoteLinkStep(linkDefs) createRemoteLinkStep(linkDefs)
return { return new WorkflowResponse({
parent_order: order, parent_order: order,
vendor_orders: vendorOrders vendor_orders: vendorOrders,
} })
} }
) )
@@ -17,8 +17,8 @@ const post = await postModuleService.createPosts({
name: "My Post", name: "My Post",
published_at: new Date(), published_at: new Date(),
metadata: { 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([ const posts = await postModuleService.createPosts([
{ {
name: "My Post", name: "My Post",
published_at: new Date() published_at: new Date(),
}, },
{ {
name: "My Other Post", 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 ```ts
await postModuleService.deletePosts([ await postModuleService.deletePosts([
"123", "123",
"321" "321",
]) ])
``` ```
@@ -37,7 +37,7 @@ To delete multiple records, pass an array of IDs as a parameter of the method.
```ts ```ts
await postModuleService.deletePosts({ 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 ```ts
const posts = await postModuleService.listPosts({ 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 ```ts
const posts = await postModuleService.listPosts({}, { 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 ```ts
const posts = await postModuleService.listPosts({}, { 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 ```ts
const posts = await postModuleService.listPosts({}, { const posts = await postModuleService.listPosts({}, {
take: 20, 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 ```ts
const posts = await postModuleService.listPosts({}, { const posts = await postModuleService.listPosts({}, {
order: { order: {
name: "ASC" name: "ASC",
} },
}) })
``` ```
@@ -27,7 +27,7 @@ If no parameters are passed, the method returns an array with two items:
```ts ```ts
const [posts, count] = await postModuleService.listAndCountPosts({ 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 ```ts
const [posts, count] = await postModuleService.listAndCountPosts({}, { const [posts, count] = await postModuleService.listAndCountPosts({}, {
relations: ["author"] relations: ["author"],
}) })
``` ```
@@ -81,7 +81,7 @@ The method returns an array with two items:
```ts ```ts
const [posts, count] = await postModuleService.listAndCountPosts({}, { 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 ```ts
const [posts, count] = await postModuleService.listAndCountPosts({}, { const [posts, count] = await postModuleService.listAndCountPosts({}, {
take: 20, take: 20,
skip: 10 skip: 10,
}) })
``` ```
@@ -130,8 +130,8 @@ The method returns an array with two items:
```ts ```ts
const [posts, count] = await postModuleService.listAndCountPosts({}, { const [posts, count] = await postModuleService.listAndCountPosts({}, {
order: { order: {
name: "ASC" name: "ASC",
} },
}) })
``` ```
@@ -28,7 +28,7 @@ For example, the returned object of the above example is:
```ts ```ts
restoredPosts = { restoredPosts = {
post_id: ["123"] post_id: ["123"],
} }
``` ```
@@ -39,7 +39,7 @@ restoredPosts = {
```ts ```ts
const restoredPosts = await postModuleService.restorePosts([ const restoredPosts = await postModuleService.restorePosts([
"123", "123",
"321" "321",
]) ])
``` ```
@@ -57,8 +57,8 @@ For example, the returned object of the above example is:
restoredPosts = { restoredPosts = {
post_id: [ post_id: [
"123", "123",
"321" "321",
] ],
} }
``` ```
@@ -68,7 +68,7 @@ restoredPosts = {
```ts ```ts
const restoredPosts = await postModuleService.restorePosts({ 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 ```ts
restoredPosts = { restoredPosts = {
post_id: [ post_id: [
"123" "123",
] ],
} }
``` ```
@@ -36,7 +36,7 @@ This applies to relations between data models of the same module. To retrieve li
```ts ```ts
const post = await postModuleService.retrievePost("123", { const post = await postModuleService.retrievePost("123", {
relations: ["author"] relations: ["author"],
}) })
``` ```
@@ -54,7 +54,7 @@ The method returns the record as an object.
```ts ```ts
const post = await postModuleService.retrievePost("123", { 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 ```ts
deletedPosts = { deletedPosts = {
post_id: ["123"] post_id: ["123"],
} }
``` ```
@@ -41,7 +41,7 @@ deletedPosts = {
```ts ```ts
const deletedPosts = await postModuleService.softDeletePosts([ const deletedPosts = await postModuleService.softDeletePosts([
"123", "123",
"321" "321",
]) ])
``` ```
@@ -59,8 +59,8 @@ For example, the returned object of the above example is:
deletedPosts = { deletedPosts = {
post_id: [ post_id: [
"123", "123",
"321" "321",
] ],
} }
``` ```
@@ -70,7 +70,7 @@ deletedPosts = {
```ts ```ts
const deletedPosts = await postModuleService.softDeletePosts({ 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 ```ts
deletedPosts = { deletedPosts = {
post_id: ["123"] post_id: ["123"],
} }
``` ```
@@ -15,7 +15,7 @@ This method updates one or more records of the data model.
```ts ```ts
const post = await postModuleService.updatePosts({ const post = await postModuleService.updatePosts({
id: "123", 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([ const posts = await postModuleService.updatePosts([
{ {
id: "123", id: "123",
name: "My Post" name: "My Post",
}, },
{ {
id: "321", 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 ```ts
const posts = await postModuleService.updatePosts({ const posts = await postModuleService.updatePosts({
selector: { selector: {
name: "My Post" name: "My Post",
}, },
data: { 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([ const posts = await postModuleService.updatePosts([
{ {
selector: { selector: {
name: "My Post" name: "My Post",
}, },
data: { data: {
published_at: new Date() published_at: new Date(),
} },
}, },
{ {
selector: { selector: {
name: "Another Post" name: "Another Post",
}, },
data: { data: {
metadata: { 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({ const posts = await postModuleService.listPosts({
views: [ views: [
50, 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({ const posts = await postModuleService.listPosts({
name: { name: {
$nin: [ $nin: [
"My Post" "My Post",
] ],
} },
}) })
``` ```
@@ -80,8 +80,8 @@ This filter only applies to text-like properties, including `id` and `enum` prop
```ts ```ts
const posts = await postModuleService.listPosts({ const posts = await postModuleService.listPosts({
name: { name: {
$like: "My%" $like: "My%",
} },
}) })
``` ```
@@ -102,8 +102,8 @@ This filter only applies to the `number` and `dateTime` properties.
```ts ```ts
const posts = await postModuleService.listPosts({ const posts = await postModuleService.listPosts({
published_at: { published_at: {
$lt: new Date() $lt: new Date(),
} },
}) })
``` ```
@@ -128,8 +128,8 @@ endToday.setHours(23, 59, 59, 59)
const posts = await postModuleService.listPosts({ const posts = await postModuleService.listPosts({
published_at: { published_at: {
$gte: startToday, $gte: startToday,
$lte: endToday $lte: endToday,
} },
}) })
``` ```
@@ -149,10 +149,10 @@ const posts = await postModuleService.listPosts({
}, },
{ {
published_at: { published_at: {
$lt: new Date() $lt: new Date(),
} },
} },
] ],
}) })
``` ```
@@ -25,7 +25,7 @@ export const fetchHighlights = [
const queryParams = new URLSearchParams({ const queryParams = new URLSearchParams({
fields: `*variants.calculated_price`, fields: `*variants.calculated_price`,
region_id: region.id, 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()}`, { fetch(`http://localhost:9000/store/products/${id}?${queryParams.toString()}`, {
@@ -135,7 +135,7 @@ export default function Product({ params: { id } }: Params) {
const queryParams = new URLSearchParams({ const queryParams = new URLSearchParams({
fields: `*variants.calculated_price`, fields: `*variants.calculated_price`,
region_id: region.id, 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()}`, { fetch(`http://localhost:9000/store/products/${id}?${queryParams.toString()}`, {