Skip to content

Commit

Permalink
docs: fixes to customization docs (medusajs#9236)
Browse files Browse the repository at this point in the history
Closes medusajs#9225, medusajs#9224, medusajs#9226, medusajs#9227

Closes DOCS-948, DOCS-947, DOCS-945, DOCS-946
  • Loading branch information
shahednasser authored and panalgin committed Oct 7, 2024
1 parent 55cf556 commit 087e0a5
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export default defineMiddlewares({
method: "POST",
matcher: "/admin/products",
additionalDataValidator: {
brand: z.string().optional()
}
}
]
brand: z.string().optional(),
},
},
],
})
```

Expand Down Expand Up @@ -154,14 +154,14 @@ createProductsWorkflow.hooks.productsCreated(
...product,
metadata: {
...product.metadata,
brand: additional_data.brand
}
brand: additional_data.brand,
},
}))
)

return new StepResponse(products, {
products,
additional_data
additional_data,
})
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const updateAssociationHighlights = [
const product = await helloModuleService.retrieveProduct(
"123",
{
relations: ["orders"]
relations: ["orders"],
}
)

Expand All @@ -169,7 +169,7 @@ const updatedProduct = await helloModuleService.updateProducts({
// other properties...
orders: [
...product.orders.map((order) => order.id),
"321"
"321",
],
})
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ import { model } from "@medusajs/utils"
const Order = model.define("order", {
id: model.id().primaryKey(),
products: model.manyToMany(() => Product, {
mappedBy: "orders"
mappedBy: "orders",
}),
})

const Product = model.define("product", {
id: model.id().primaryKey(),
orders: model.manyToMany(() => Order, {
mappedBy: "products"
mappedBy: "products",
}),
})
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const step1 = createStep(
async () => {
return new StepResponse(
`Hello from step one!`,
{ message: "Oops! Rolling back my changes..."}
{ message: "Oops! Rolling back my changes..." }
)
},
async ({ message }) => {
Expand Down Expand Up @@ -180,7 +180,7 @@ const step1 = createStep(
async () => {
return new StepResponse(
`Hello from step one!`,
{ message: "Oops! Rolling back my changes..."}
{ message: "Oops! Rolling back my changes..." }
)
},
async ({ message }, { container }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ export const successStatusHighlights = [
import {
Modules,
TransactionHandlerType,
} from "@medusajs/utils";
} from "@medusajs/utils"
import {
StepResponse,
createStep
} from "@medusajs/workflows-sdk";
createStep,
} from "@medusajs/workflows-sdk"

type SetStepSuccessStepInput = {
transactionId: string
Expand All @@ -132,7 +132,7 @@ export const setStepSuccessStep = createStep(
) {
const workflowEngineService = container.resolve(
Modules.WORKFLOW_ENGINE
);
)

await workflowEngineService.setStepSuccess({
idempotencyKey: {
Expand All @@ -145,9 +145,9 @@ export const setStepSuccessStep = createStep(
options: {
container,
},
});
})
}
);
)
```

In this step (which you use in a workflow other than the long-running workflow), you resolve the Workflow Engine Module's main service and set `step-2` of the previous workflow as successful.
Expand Down Expand Up @@ -232,11 +232,11 @@ export const failureStatusHighlights = [
import {
Modules,
TransactionHandlerType,
} from "@medusajs/utils";
} from "@medusajs/utils"
import {
StepResponse,
createStep
} from "@medusajs/workflows-sdk";
createStep,
} from "@medusajs/workflows-sdk"

type SetStepFailureStepInput = {
transactionId: string
Expand All @@ -250,7 +250,7 @@ export const setStepFailureStep = createStep(
) {
const workflowEngineService = container.resolve(
Modules.WORKFLOW_ENGINE
);
)

await workflowEngineService.setStepFailure({
idempotencyKey: {
Expand All @@ -263,9 +263,9 @@ export const setStepFailureStep = createStep(
options: {
container,
},
});
})
}
);
)
```

You use this step in another workflow that changes the status of an async step in a long-running workflow's execution to failed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ The [createProductsWorkflow](!resources!/references/medusa-workflows/createProdu
So, to consume the `productsCreated` hook, create the file `src/workflows/hooks/created-product.ts` with the following content:

export const hookHighlights = [
["6", "productsCreated", "Access the hook in the `hooks` property."],
["8", "", "Only proceed if the brand ID is passed in the additional data."],
["17", "retrieveBrand", "Try to retrieve the brand to ensure it exists."],
["21", "links", "Define an array to store the links in."],
["25", "push", "Add a link to be created."],
["35", "create", "Create the links."]
["7", "productsCreated", "Access the hook in the `hooks` property."],
["9", "", "Only proceed if the brand ID is passed in the additional data."],
["18", "retrieveBrand", "Try to retrieve the brand to ensure it exists."],
["27", "links", "Define an array to store the links in."],
["31", "push", "Add a link to be created."],
["41", "create", "Create the links."]
]

```ts title="src/workflows/hooks/created-product.ts" highlights={hookHighlights}
import { createProductsWorkflow } from "@medusajs/core-flows"
import { Modules } from "@medusajs/utils"
import { StepResponse } from "@medusajs/workflows-sdk"
import { Modules, ContainerRegistrationKeys } from "@medusajs/utils"
import { BRAND_MODULE } from "../../modules/brand"
import BrandModuleService from "../../modules/brand/service"

Expand All @@ -104,7 +105,12 @@ createProductsWorkflow.hooks.productsCreated(
// if the brand doesn't exist, an error is thrown.
await brandModuleService.retrieveBrand(additional_data.brand_id as string)

const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
const logger = container.resolve(
ContainerRegistrationKeys.LOGGER
)

const links = []

Expand All @@ -122,8 +128,10 @@ createProductsWorkflow.hooks.productsCreated(

await remoteLink.create(links)

logger.info("Linked brand to products")

return new StepResponse(links, links)
})
})
)
```

Expand All @@ -139,8 +147,8 @@ Add the following compensation function as a second parameter:

```ts title="src/workflows/hooks/created-product.ts"
createProductsWorkflow.hooks.productsCreated(
// ...
(async ({ links }, { container }) => {
// ...
(async (links, { container }) => {
if (!links.length) {
return
}
Expand All @@ -149,7 +157,7 @@ createProductsWorkflow.hooks.productsCreated(
ContainerRegistrationKeys.REMOTE_LINK
)

await remoteLink.dimiss(links)
await remoteLink.dismiss(links)
})
)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Make sure to replace the email and password with your user's credentials.
Then, send a `GET` request to `/admin/products/:id/brand`:

```bash
curl 'http://localhost:9000/admin/product/prod_123/brand' \
curl 'http://localhost:9000/admin/products/prod_123/brand' \
-H 'Authorization: Bearer {token}'
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ import { syncBrandToSystemWorkflow } from "../workflows/sync-brand-to-system"
export default async function brandCreatedHandler({
event: { data },
container,
}: SubscriberArgs<Record<string, string>>) {
}: SubscriberArgs<{ id: string }>) {
await syncBrandToSystemWorkflow(container).run({
input: data,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class BrandClient {

const moduleDef = configModule.modules[BRAND_MODULE]
if (typeof moduleDef !== "boolean") {
this.options_ = moduleDef.options
this.options_ = moduleDef.options as BrandClientOptions
}
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export class BrandClient {
}`)
}

async createBrand(brand: Record<string, string>) {
async createBrand(brand: Record<string, any>) {
await this.sendRequest("/brands", "POST", brand)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ Next, create the file `instrumentation.js` with the following content:
```js title="instrumentation.js"
const { registerOtel } = require("@medusajs/medusa")
// If using an exporter other than Zipkin, require it here.
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')
const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin")

// If using an exporter other than Zipkin, initialize it here.
const exporter = new ZipkinExporter({
serviceName: 'my-medusa-project',
serviceName: "my-medusa-project",
})

export function register() {
registerOtel({
serviceName: 'medusajs',
serviceName: "medusajs",
// pass exporter
exporter,
instrument: {
http: true,
workflows: true,
remoteQuery: true
remoteQuery: true,
},
})
}
Expand Down

0 comments on commit 087e0a5

Please sign in to comment.