Skip to content

Commit

Permalink
docs: fixes to wishlist plugin guide
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser committed Feb 20, 2025
1 parent 3b49978 commit d33a8c3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
93 changes: 72 additions & 21 deletions www/apps/resources/app/plugins/guides/wishlist/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,12 @@ The workflow to add an item to a wishlist has the following steps:
depth: 1,
link: "/references/helper-steps/useQueryGraphStep"
},
{
type: "step",
name: "validateWishlistExistsStep",
description: "Validate that the customer's wishlist exists.",
depth: 1
},
{
type: "step",
name: "validateWishlistSalesChannelStep",
Expand Down Expand Up @@ -965,9 +971,42 @@ The workflow to add an item to a wishlist has the following steps:

The `useQueryGraphStep` is from Medusa's workflows package. So, you'll only implement the other steps.

#### validateWishlistExistsStep

The second step in the workflow validates that the customer's wishlist, retrieved in the first step, exists.

To create the step, create the file `src/workflows/steps/validate-wishlist-exists.ts` with the following content:

![Directory structure after adding the step file](https://res.cloudinary.com/dza7lstvk/image/upload/v1740071251/Medusa%20Resources/wishlist-29_bq6kcn.jpg)

```ts title="src/workflows/steps/validate-wishlist-exists.ts"
import { MedusaError } from "@medusajs/framework/utils"
import { createStep } from "@medusajs/framework/workflows-sdk"
import { InferTypeOf } from "@medusajs/framework/types"
import { Wishlist } from "../../modules/wishlist/models/wishlist"

type Input = {
wishlists?: InferTypeOf<typeof Wishlist>[]
}

export const validateWishlistExistsStep = createStep(
"validate-wishlist-exists",
async (input: Input) => {
if (!input.wishlists?.length) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
"No wishlist found for this customer"
)
}
}
)
```

This step receives an array of wishlists and throws an error if it's empty. You'll use this to stop the workflow's execution if the customer doesn't have a wishlist.

#### validateWishlistSalesChannelStep

The second step in the workflow validates that the wishlist belongs to the sales channel specified in the input.
The third step in the workflow validates that the wishlist belongs to the sales channel specified in the input.

To create the step, create the file `src/workflows/steps/validate-wishlist-sales-channel.ts` with the following content:

Expand Down Expand Up @@ -1079,7 +1118,7 @@ This step receives the variant ID, sales channel ID, and wishlist object as inpu

#### createWishlistItemStep

The fourth step in the workflow creates a wishlist item for the specified variant in the wishlist.
The fifth step in the workflow creates a wishlist item for the specified variant in the wishlist.

Create the file `src/workflows/steps/create-wishlist-item.ts` with the following content:

Expand Down Expand Up @@ -1133,12 +1172,13 @@ You can now add the `createWishlistItemWorkflow` to the plugin. Create the file
![Directory structure after adding the workflow file](https://res.cloudinary.com/dza7lstvk/image/upload/v1737470660/Medusa%20Resources/wishlist-16_ovujwp.jpg)

export const createWishlistItemWorkflowHighlights = [
["16", "useQueryGraphStep", "Retrieve the wishlist of the specified customer."],
["27", "validateWishlistSalesChannelStep", "Validate that the wishlist belongs to the specified sales channel."],
["33", "validateVariantWishlistStep", "Validate that the specified variant is not already in the wishlist."],
["39", "createWishlistItemStep", "Create the wishlist item."],
["45", "useQueryGraphStep", "Retrieve the wishlist again with the new item added."],
["54", "wishlist", "Return the wishlist with the new item."]
["17", "useQueryGraphStep", "Retrieve the wishlist of the specified customer."],
["25", "validateWishlistExistsStep", "Validate that the customer has a wishlist."],
["29", "validateWishlistSalesChannelStep", "Validate that the wishlist belongs to the specified sales channel."],
["35", "validateVariantWishlistStep", "Validate that the specified variant is not already in the wishlist."],
["41", "createWishlistItemStep", "Create the wishlist item."],
["47", "useQueryGraphStep", "Retrieve the wishlist again with the new item added."],
["56", "wishlist", "Return the wishlist with the new item."]
]

```ts title="src/workflows/create-wishlist-item.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports" highlights={createWishlistItemWorkflowHighlights}
Expand All @@ -1147,6 +1187,7 @@ import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { validateWishlistSalesChannelStep } from "./steps/validate-wishlist-sales-channel"
import { createWishlistItemStep } from "./steps/create-wishlist-item"
import { validateVariantWishlistStep } from "./steps/validate-variant-wishlist"
import { validateWishlistExistsStep } from "./steps/validate-wishlist-exists"

type CreateWishlistItemWorkflowInput = {
variant_id: string
Expand All @@ -1163,9 +1204,10 @@ export const createWishlistItemWorkflow = createWorkflow(
filters: {
customer_id: input.customer_id,
},
options: {
throwIfKeyNotFound: true,
},
})

validateWishlistExistsStep({
wishlists
})

validateWishlistSalesChannelStep({
Expand Down Expand Up @@ -1406,6 +1448,12 @@ The workflow to remove an item from a wishlist has the following steps:
depth: 1,
link: "/references/helper-steps/useQueryGraphStep"
},
{
type: "step",
name: "validateWishlistExistsStep",
description: "Validate that the customer's wishlist exists.",
depth: 1
},
{
type: "step",
name: "validateItemInWishlistStep",
Expand All @@ -1430,7 +1478,7 @@ The workflow to remove an item from a wishlist has the following steps:
hideLegend
/>

The `useQueryGraphStep` is from Medusa's workflows package. So, you'll only implement the other steps.
The `useQueryGraphStep` is from Medusa's workflows package, and you implemented the `validateWishlistExistsStep` [previously](#validatewishlistexistsstep) . So, you'll only implement the other steps.

#### validateItemInWishlistStep

Expand Down Expand Up @@ -1524,18 +1572,20 @@ You can now add the `deleteWishlistItemWorkflow` to the plugin. Create the file
![Directory structure after adding the workflow file](https://res.cloudinary.com/dza7lstvk/image/upload/v1737474872/Medusa%20Resources/wishlist-22_wt1g36.jpg)

export const deleteWishlistItemWorkflowHighlights = [
["14", "useQueryGraphStep", "Retrieve the wishlist of a customer."],
["25", "validateItemInWishlistStep", "Validate that the item is in the customer's wishlist."],
["30", "deleteWishlistItemStep", "Delete the wishlist item."],
["33", "useQueryGraphStep", "Retrieve the wishlist again with the item removed."],
["42", "wishlist", "Return the wishlist without the removed item."]
["15", "useQueryGraphStep", "Retrieve the wishlist of a customer."],
["23", "validateWishlistExistsStep", "Validate that the customer has a wishlist."],
["27", "validateItemInWishlistStep", "Validate that the item is in the customer's wishlist."],
["32", "deleteWishlistItemStep", "Delete the wishlist item."],
["35", "useQueryGraphStep", "Retrieve the wishlist again with the item removed."],
["44", "wishlist", "Return the wishlist without the removed item."]
]

```ts title="src/workflows/delete-wishlist-item.ts" highlights={deleteWishlistItemWorkflowHighlights}
import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import { deleteWishlistItemStep } from "./steps/delete-wishlist-item"
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { validateItemInWishlistStep } from "./steps/validate-item-in-wishlist"
import { validateWishlistExistsStep } from "./steps/validate-wishlist-exists"

type DeleteWishlistItemWorkflowInput = {
wishlist_item_id: string
Expand All @@ -1551,9 +1601,10 @@ export const deleteWishlistItemWorkflow = createWorkflow(
filters: {
customer_id: input.customer_id,
},
options: {
throwIfKeyNotFound: true,
},
})

validateWishlistExistsStep({
wishlists
})

validateItemInWishlistStep({
Expand All @@ -1568,7 +1619,7 @@ export const deleteWishlistItemWorkflow = createWorkflow(
entity: "wishlist",
fields: ["*", "items.*", "items.product_variant.*"],
filters: {
id: wishlists[0].wishlist.id,
id: wishlists[0].id,
},
}).config({ name: "refetch-wishlist" })

Expand Down
2 changes: 1 addition & 1 deletion www/apps/resources/generated/edit-dates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5861,7 +5861,7 @@ export const generatedEditDates = {
"references/core_flows/types/core_flows.ThrowUnlessPaymentCollectionNotePaidInput/page.mdx": "2025-01-17T16:43:25.819Z",
"references/core_flows/types/core_flows.ValidatePaymentsRefundStepInput/page.mdx": "2025-01-17T16:43:26.128Z",
"references/core_flows/types/core_flows.ValidateRefundStepInput/page.mdx": "2025-01-17T16:43:26.124Z",
"app/plugins/guides/wishlist/page.mdx": "2025-02-11T14:25:46.734Z",
"app/plugins/guides/wishlist/page.mdx": "2025-02-20T17:13:30.276Z",
"app/plugins/page.mdx": "2025-01-22T09:36:37.745Z",
"app/admin-components/components/data-table/page.mdx": "2025-01-22T16:01:01.279Z",
"references/order_models/variables/order_models.Order/page.mdx": "2025-01-27T11:43:58.788Z",
Expand Down

0 comments on commit d33a8c3

Please sign in to comment.