From d33a8c33bab4b22eedbf35aa1259756044d97de2 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 20 Feb 2025 19:15:05 +0200 Subject: [PATCH] docs: fixes to wishlist plugin guide --- .../app/plugins/guides/wishlist/page.mdx | 93 ++++++++++++++----- www/apps/resources/generated/edit-dates.mjs | 2 +- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/www/apps/resources/app/plugins/guides/wishlist/page.mdx b/www/apps/resources/app/plugins/guides/wishlist/page.mdx index 4860f2c96352c..293a6db628209 100644 --- a/www/apps/resources/app/plugins/guides/wishlist/page.mdx +++ b/www/apps/resources/app/plugins/guides/wishlist/page.mdx @@ -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", @@ -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[] +} + +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: @@ -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: @@ -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} @@ -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 @@ -1163,9 +1204,10 @@ export const createWishlistItemWorkflow = createWorkflow( filters: { customer_id: input.customer_id, }, - options: { - throwIfKeyNotFound: true, - }, + }) + + validateWishlistExistsStep({ + wishlists }) validateWishlistSalesChannelStep({ @@ -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", @@ -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 @@ -1524,11 +1572,12 @@ 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} @@ -1536,6 +1585,7 @@ import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows- 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 @@ -1551,9 +1601,10 @@ export const deleteWishlistItemWorkflow = createWorkflow( filters: { customer_id: input.customer_id, }, - options: { - throwIfKeyNotFound: true, - }, + }) + + validateWishlistExistsStep({ + wishlists }) validateItemInWishlistStep({ @@ -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" }) diff --git a/www/apps/resources/generated/edit-dates.mjs b/www/apps/resources/generated/edit-dates.mjs index b5281f4e85401..b643dc432ee4a 100644 --- a/www/apps/resources/generated/edit-dates.mjs +++ b/www/apps/resources/generated/edit-dates.mjs @@ -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",