docs: change useRemoteQueryStep to useQueryGraphStep (#10051)

* docs: change useRemoteQueryStep to useQueryGraphStep

* fix lint errors
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-11-13 11:00:55 +02:00
1 parent 823552ecd9
commit 868b1c190f
17 files changed
+241 -266

No files matched your search

@@ -55,7 +55,7 @@ import { DetailWidgetProps, HttpTypes } from "@medusajs/framework/types"
const ProductWidget = () => { const ProductWidget = () => {
const { data, isLoading } = useQuery({ const { data, isLoading } = useQuery({
queryFn: () => sdk.admin.product.list(), queryFn: () => sdk.admin.product.list(),
queryKey: ["products"] queryKey: ["products"],
}) })
return ( return (
@@ -95,17 +95,17 @@ import { sdk } from "../lib/config"
import { DetailWidgetProps, HttpTypes } from "@medusajs/framework/types" import { DetailWidgetProps, HttpTypes } from "@medusajs/framework/types"
const ProductWidget = ({ const ProductWidget = ({
data: productData data: productData,
}: DetailWidgetProps<HttpTypes.AdminProduct>) => { }: DetailWidgetProps<HttpTypes.AdminProduct>) => {
const { mutateAsync } = useMutation({ const { mutateAsync } = useMutation({
mutationFn: (payload: HttpTypes.AdminUpdateProduct) => mutationFn: (payload: HttpTypes.AdminUpdateProduct) =>
sdk.admin.product.update(productData.id, payload), sdk.admin.product.update(productData.id, payload),
onSuccess: () => alert("updated product") onSuccess: () => alert("updated product"),
}) })
const handleUpdate = () => { const handleUpdate = () => {
mutateAsync({ mutateAsync({
title: "New Product Title" title: "New Product Title",
}) })
} }
@@ -57,7 +57,7 @@ import {
LoaderOptions, LoaderOptions,
} from "@medusajs/framework/types" } from "@medusajs/framework/types"
import { import {
ContainerRegistrationKeys ContainerRegistrationKeys,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
export default async function helloWorldLoader({ export default async function helloWorldLoader({
@@ -30,7 +30,7 @@ module.exports = defineConfig({
capitalize: true, capitalize: true,
}, },
}, },
] ],
}) })
``` ```
@@ -145,7 +145,7 @@ const myWorkflow = createWorkflow(
function (input: WorkflowInput) { function (input: WorkflowInput) {
const message = transform( const message = transform(
{ {
input input,
}, },
(data) => data.input.message || "hello" (data) => data.input.message || "hello"
) )
@@ -179,7 +179,7 @@ const myWorkflow = createWorkflow(
"hello-world", "hello-world",
function (input) { function (input) {
validateHasStr1({ validateHasStr1({
input input,
}) })
// workflow continues its execution only if // workflow continues its execution only if
@@ -135,8 +135,8 @@ module.exports = defineConfig({
modules: [ modules: [
{ {
resolve: "./src/modules/hello", resolve: "./src/modules/hello",
} },
] ],
}) })
``` ```
@@ -212,11 +212,11 @@ export async function GET(
) )
const my_custom = await helloModuleService.createMyCustoms({ const my_custom = await helloModuleService.createMyCustoms({
name: "test" name: "test",
}) })
res.json({ res.json({
my_custom my_custom,
}) })
} }
``` ```
@@ -105,8 +105,8 @@ module.exports = defineConfig({
modules: [ modules: [
{ {
resolve: "./src/modules/brand", resolve: "./src/modules/brand",
} },
] ],
}) })
``` ```
@@ -192,7 +192,7 @@ module.exports = defineConfig({
apiKey: process.env.BRAND_API_KEY || "temp", apiKey: process.env.BRAND_API_KEY || "temp",
}, },
}, },
] ],
}) })
``` ```
@@ -349,7 +349,7 @@ You add a step that deletes the manager using the `deleteManagers` method of the
Next, in the same file, add the workflow that deletes a manager: Next, in the same file, add the workflow that deletes a manager:
export const deleteHighlights = [ export const deleteHighlights = [
["30", "manager_id", "If your actor type has a different name, such as vendor, change it to be `{actor_type}_id`."] ["29", "manager_id", "If your actor type has a different name, such as vendor, change it to be `{actor_type}_id`."]
] ]
```ts title="src/workflows/delete-manager.ts" collapsibleLines="1-15" expandButtonLabel="Show Imports" highlights={deleteHighlights} ```ts title="src/workflows/delete-manager.ts" collapsibleLines="1-15" expandButtonLabel="Show Imports" highlights={deleteHighlights}
@@ -363,7 +363,7 @@ import {
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { import {
setAuthAppMetadataStep, setAuthAppMetadataStep,
useRemoteQueryStep, useQueryGraphStep,
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
// ... // ...
@@ -375,17 +375,15 @@ export const deleteManagerWorkflow = createWorkflow(
): WorkflowResponse<string> => { ): WorkflowResponse<string> => {
deleteManagerStep(input) deleteManagerStep(input)
const authIdentities = useRemoteQueryStep({ const { data: authIdentities } = useQueryGraphStep({
entry_point: "auth_identity", entity: "auth_identity",
fields: ["id"], fields: ["id"],
variables: { filters: {
filters: { app_metadata: {
app_metadata: { // the ID is of the format `{actor_type}_id`.
// the ID is of the format `{actor_type}_id`. manager_id: input.id,
manager_id: input.id, }
}, }
},
},
}) })
const authIdentity = transform( const authIdentity = transform(
@@ -492,7 +492,7 @@ Finally, you'll create the workflow. Create the file `src/workflows/update-custo
```ts title="src/workflows/update-custom-from-cart/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports" ```ts title="src/workflows/update-custom-from-cart/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports"
import { CartDTO } from "@medusajs/framework/types" import { CartDTO } from "@medusajs/framework/types"
import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk" import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, dismissRemoteLinkStep, useRemoteQueryStep } from "@medusajs/medusa/core-flows" import { createRemoteLinkStep, dismissRemoteLinkStep, useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { createCustomStep } from "../create-custom-from-cart/steps/create-custom" import { createCustomStep } from "../create-custom-from-cart/steps/create-custom"
import { Modules } from "@medusajs/framework/utils" import { Modules } from "@medusajs/framework/utils"
import { HELLO_MODULE } from "../../modules/hello" import { HELLO_MODULE } from "../../modules/hello"
@@ -509,15 +509,12 @@ export type UpdateCustomFromCartStepInput = {
export const updateCustomFromCartWorkflow = createWorkflow( export const updateCustomFromCartWorkflow = createWorkflow(
"update-custom-from-cart", "update-custom-from-cart",
(input: UpdateCustomFromCartStepInput) => { (input: UpdateCustomFromCartStepInput) => {
const cartData = useRemoteQueryStep({ const { data: carts } = useQueryGraphStep({
entry_point: "cart", entity: "cart",
fields: ["custom.*"], fields: ["custom.*"],
variables: { filters: {
filters: { id: input.cart.id
id: input.cart.id, }
},
},
list: false,
}) })
// TODO create, update, or delete Custom record // TODO create, update, or delete Custom record
@@ -534,9 +531,9 @@ Next, replace the `TODO` with the following:
```ts title="src/workflows/update-custom-from-cart/index.ts" ```ts title="src/workflows/update-custom-from-cart/index.ts"
const created = when({ const created = when({
input, input,
cartData, carts,
}, (data) => }, (data) =>
!data.cartData.custom && !data.carts[0].custom &&
data.input.additional_data?.custom_name?.length > 0 data.input.additional_data?.custom_name?.length > 0
) )
.then(() => { .then(() => {
@@ -568,25 +565,25 @@ Next, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-cart/index.ts" ```ts title="src/workflows/update-custom-from-cart/index.ts"
const deleted = when({ const deleted = when({
input, input,
cartData, carts,
}, (data) => }, (data) =>
data.cartData.custom && ( data.carts[0].custom && (
data.input.additional_data?.custom_name === null || data.input.additional_data?.custom_name === null ||
data.input.additional_data?.custom_name.length === 0 data.input.additional_data?.custom_name.length === 0
) )
) )
.then(() => { .then(() => {
deleteCustomStep({ deleteCustomStep({
custom: cartData.custom, custom: carts[0].custom,
}) })
dismissRemoteLinkStep({ dismissRemoteLinkStep({
[HELLO_MODULE]: { [HELLO_MODULE]: {
custom_id: cartData.custom.id, custom_id: carts[0].custom.id,
}, },
}) })
return cartData.custom.id return carts[0].custom.id
}) })
// TODO delete Custom record // TODO delete Custom record
@@ -599,11 +596,11 @@ Finally, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-cart/index.ts" ```ts title="src/workflows/update-custom-from-cart/index.ts"
const updated = when({ const updated = when({
input, input,
cartData, carts,
}, (data) => data.cartData.custom && data.input.additional_data?.custom_name?.length > 0) }, (data) => data.carts[0].custom && data.input.additional_data?.custom_name?.length > 0)
.then(() => { .then(() => {
const custom = updateCustomStep({ const custom = updateCustomStep({
id: cartData.custom.id, id: carts[0].custom.id,
custom_name: input.additional_data.custom_name, custom_name: input.additional_data.custom_name,
}) })
@@ -504,7 +504,7 @@ Finally, you'll create the workflow. Create the file `src/workflows/update-custo
```ts title="src/workflows/update-custom-from-customer/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports" ```ts title="src/workflows/update-custom-from-customer/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports"
import { CustomerDTO } from "@medusajs/framework/types" import { CustomerDTO } from "@medusajs/framework/types"
import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk" import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, dismissRemoteLinkStep, useRemoteQueryStep } from "@medusajs/medusa/core-flows" import { createRemoteLinkStep, dismissRemoteLinkStep, useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { createCustomStep } from "../create-custom-from-customer/steps/create-custom" import { createCustomStep } from "../create-custom-from-customer/steps/create-custom"
import { Modules } from "@medusajs/framework/utils" import { Modules } from "@medusajs/framework/utils"
import { HELLO_MODULE } from "../../modules/hello" import { HELLO_MODULE } from "../../modules/hello"
@@ -521,15 +521,12 @@ export type UpdateCustomFromCustomerStepInput = {
export const updateCustomFromCustomerWorkflow = createWorkflow( export const updateCustomFromCustomerWorkflow = createWorkflow(
"update-custom-from-customer", "update-custom-from-customer",
(input: UpdateCustomFromCustomerStepInput) => { (input: UpdateCustomFromCustomerStepInput) => {
const customerData = useRemoteQueryStep({ const { data: customers } = useQueryGraphStep({
entry_point: "customer", entity: "customer",
fields: ["custom.*"], fields: ["custom.*"],
variables: { filters: {
filters: { id: input.customer.id,
id: input.customer.id, }
},
},
list: false,
}) })
// TODO create, update, or delete Custom record // TODO create, update, or delete Custom record
@@ -546,9 +543,9 @@ Next, replace the `TODO` with the following:
```ts title="src/workflows/update-custom-from-customer/index.ts" ```ts title="src/workflows/update-custom-from-customer/index.ts"
const created = when({ const created = when({
input, input,
customerData, customers,
}, (data) => }, (data) =>
!data.customerData.custom && !data.customers[0].custom &&
data.input.additional_data?.custom_name?.length > 0 data.input.additional_data?.custom_name?.length > 0
) )
.then(() => { .then(() => {
@@ -580,25 +577,25 @@ Next, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-customer/index.ts" ```ts title="src/workflows/update-custom-from-customer/index.ts"
const deleted = when({ const deleted = when({
input, input,
customerData, customers,
}, (data) => }, (data) =>
data.customerData.custom && ( data.customers[0].custom && (
data.input.additional_data?.custom_name === null || data.input.additional_data?.custom_name === null ||
data.input.additional_data?.custom_name.length === 0 data.input.additional_data?.custom_name.length === 0
) )
) )
.then(() => { .then(() => {
deleteCustomStep({ deleteCustomStep({
custom: customerData.custom, custom: customers[0].custom,
}) })
dismissRemoteLinkStep({ dismissRemoteLinkStep({
[HELLO_MODULE]: { [HELLO_MODULE]: {
custom_id: customerData.custom.id, custom_id: customers[0].custom.id,
}, },
}) })
return customerData.custom.id return customers[0].custom.id
}) })
// TODO delete Custom record // TODO delete Custom record
@@ -611,11 +608,11 @@ Finally, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-customer/index.ts" ```ts title="src/workflows/update-custom-from-customer/index.ts"
const updated = when({ const updated = when({
input, input,
customerData, customers,
}, (data) => data.customerData.custom && data.input.additional_data?.custom_name?.length > 0) }, (data) => data.customers[0].custom && data.input.additional_data?.custom_name?.length > 0)
.then(() => { .then(() => {
const custom = updateCustomStep({ const custom = updateCustomStep({
id: customerData.custom.id, id: customers[0].custom.id,
custom_name: input.additional_data.custom_name, custom_name: input.additional_data.custom_name,
}) })
@@ -510,7 +510,7 @@ Finally, you'll create the workflow. Create the file `src/workflows/update-custo
```ts title="src/workflows/update-custom-from-product/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports" ```ts title="src/workflows/update-custom-from-product/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports"
import { ProductDTO } from "@medusajs/framework/types" import { ProductDTO } from "@medusajs/framework/types"
import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk" import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, dismissRemoteLinkStep, useRemoteQueryStep } from "@medusajs/medusa/core-flows" import { createRemoteLinkStep, dismissRemoteLinkStep, useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { createCustomStep } from "../create-custom-from-cart/steps/create-custom" import { createCustomStep } from "../create-custom-from-cart/steps/create-custom"
import { Modules } from "@medusajs/framework/utils" import { Modules } from "@medusajs/framework/utils"
import { HELLO_MODULE } from "../../modules/hello" import { HELLO_MODULE } from "../../modules/hello"
@@ -527,15 +527,12 @@ export type UpdateCustomFromProductStepInput = {
export const updateCustomFromProductWorkflow = createWorkflow( export const updateCustomFromProductWorkflow = createWorkflow(
"update-custom-from-product", "update-custom-from-product",
(input: UpdateCustomFromProductStepInput) => { (input: UpdateCustomFromProductStepInput) => {
const productData = useRemoteQueryStep({ const { data: products } = useQueryGraphStep({
entry_point: "product", entity: "product",
fields: ["custom.*"], fields: ["custom.*"],
variables: { filters: {
filters: { id: input.product.id,
id: input.product.id }
}
},
list: false
}) })
// TODO create, update, or delete Custom record // TODO create, update, or delete Custom record
@@ -552,9 +549,9 @@ Next, replace the `TODO` with the following:
```ts title="src/workflows/update-custom-from-product/index.ts" ```ts title="src/workflows/update-custom-from-product/index.ts"
const created = when({ const created = when({
input, input,
productData products
}, (data) => }, (data) =>
!data.productData.custom && !data.products[0].custom &&
data.input.additional_data?.custom_name?.length > 0 data.input.additional_data?.custom_name?.length > 0
) )
.then(() => { .then(() => {
@@ -586,25 +583,25 @@ Next, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-product/index.ts" ```ts title="src/workflows/update-custom-from-product/index.ts"
const deleted = when({ const deleted = when({
input, input,
productData products
}, (data) => }, (data) =>
data.productData.custom && ( data.products[0].custom && (
data.input.additional_data?.custom_name === null || data.input.additional_data?.custom_name === null ||
data.input.additional_data?.custom_name.length === 0 data.input.additional_data?.custom_name.length === 0
) )
) )
.then(() => { .then(() => {
deleteCustomStep({ deleteCustomStep({
custom: productData.custom custom: products[0].custom
}) })
dismissRemoteLinkStep({ dismissRemoteLinkStep({
[HELLO_MODULE]: { [HELLO_MODULE]: {
custom_id: productData.custom.id custom_id: products[0].custom.id
} }
}) })
return productData.custom.id return products[0].custom.id
}) })
// TODO delete Custom record // TODO delete Custom record
@@ -617,11 +614,11 @@ Finally, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-product/index.ts" ```ts title="src/workflows/update-custom-from-product/index.ts"
const updated = when({ const updated = when({
input, input,
productData products
}, (data) => data.productData.custom && data.input.additional_data?.custom_name?.length > 0) }, (data) => data.products[0].custom && data.input.additional_data?.custom_name?.length > 0)
.then(() => { .then(() => {
const custom = updateCustomStep({ const custom = updateCustomStep({
id: productData.custom.id, id: products[0].custom.id,
custom_name: input.additional_data.custom_name custom_name: input.additional_data.custom_name
}) })
@@ -516,7 +516,7 @@ Finally, you'll create the workflow. Create the file `src/workflows/update-custo
```ts title="src/workflows/update-custom-from-promotion/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports" ```ts title="src/workflows/update-custom-from-promotion/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports"
import { PromotionDTO } from "@medusajs/framework/types" import { PromotionDTO } from "@medusajs/framework/types"
import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk" import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, dismissRemoteLinkStep, useRemoteQueryStep } from "@medusajs/medusa/core-flows" import { createRemoteLinkStep, dismissRemoteLinkStep, useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { createCustomStep } from "../create-custom-from-cart/steps/create-custom" import { createCustomStep } from "../create-custom-from-cart/steps/create-custom"
import { Modules } from "@medusajs/framework/utils" import { Modules } from "@medusajs/framework/utils"
import { HELLO_MODULE } from "../../modules/hello" import { HELLO_MODULE } from "../../modules/hello"
@@ -533,15 +533,12 @@ export type UpdateCustomFromPromotionStepInput = {
export const updateCustomFromPromotionWorkflow = createWorkflow( export const updateCustomFromPromotionWorkflow = createWorkflow(
"update-custom-from-promotion", "update-custom-from-promotion",
(input: UpdateCustomFromPromotionStepInput) => { (input: UpdateCustomFromPromotionStepInput) => {
const promotionData = useRemoteQueryStep({ const { data: promotions } = useQueryGraphStep({
entry_point: "promotion", entity: "promotion",
fields: ["custom.*"], fields: ["custom.*"],
variables: { filters: {
filters: { id: input.promotion.id,
id: input.promotion.id }
}
},
list: false
}) })
// TODO create, update, or delete Custom record // TODO create, update, or delete Custom record
@@ -558,9 +555,9 @@ Next, replace the `TODO` with the following:
```ts title="src/workflows/update-custom-from-promotion/index.ts" ```ts title="src/workflows/update-custom-from-promotion/index.ts"
const created = when({ const created = when({
input, input,
promotionData promotions
}, (data) => }, (data) =>
!data.promotionData.custom && !data.promotions[0].custom &&
data.input.additional_data?.custom_name?.length > 0 data.input.additional_data?.custom_name?.length > 0
) )
.then(() => { .then(() => {
@@ -592,25 +589,25 @@ Next, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-promotion/index.ts" ```ts title="src/workflows/update-custom-from-promotion/index.ts"
const deleted = when({ const deleted = when({
input, input,
promotionData promotions
}, (data) => }, (data) =>
data.promotionData.custom && ( data.promotions[0].custom && (
data.input.additional_data?.custom_name === null || data.input.additional_data?.custom_name === null ||
data.input.additional_data?.custom_name.length === 0 data.input.additional_data?.custom_name.length === 0
) )
) )
.then(() => { .then(() => {
deleteCustomStep({ deleteCustomStep({
custom: promotionData.custom custom: promotions[0].custom
}) })
dismissRemoteLinkStep({ dismissRemoteLinkStep({
[HELLO_MODULE]: { [HELLO_MODULE]: {
custom_id: promotionData.custom.id custom_id: promotions[0].custom.id
} }
}) })
return promotionData.custom.id return promotions[0].custom.id
}) })
// TODO delete Custom record // TODO delete Custom record
@@ -623,11 +620,11 @@ Finally, replace the new `TODO` with the following:
```ts title="src/workflows/update-custom-from-promotion/index.ts" ```ts title="src/workflows/update-custom-from-promotion/index.ts"
const updated = when({ const updated = when({
input, input,
promotionData promotions
}, (data) => data.promotionData.custom && data.input.additional_data?.custom_name?.length > 0) }, (data) => data.promotions[0].custom && data.input.additional_data?.custom_name?.length > 0)
.then(() => { .then(() => {
const custom = updateCustomStep({ const custom = updateCustomStep({
id: promotionData.custom.id, id: promotions[0].custom.id,
custom_name: input.additional_data.custom_name custom_name: input.additional_data.custom_name
}) })
@@ -1567,8 +1567,8 @@ To customize the cart completion flow, you’ll create a workflow and then use t
```mermaid ```mermaid
graph TD graph TD
completeCartWorkflow["completeCartWorkflow (Medusa)"] --> useRemoteQueryStep["useRemoteQueryStep (Medusa)"] completeCartWorkflow["completeCartWorkflow (Medusa)"] --> useQueryGraphStep["useQueryGraphStep (Medusa)"]
useRemoteQueryStep --> when{order has digital products?} useQueryGraphStep --> when{order has digital products?}
when -->|Yes| createDigitalProductOrderStep when -->|Yes| createDigitalProductOrderStep
createDigitalProductOrderStep --> createRemoteLinkStep["createRemoteLinkStep (Medusa)"] createDigitalProductOrderStep --> createRemoteLinkStep["createRemoteLinkStep (Medusa)"]
createRemoteLinkStep --> createOrderFulfillmentWorkflow["createOrderFulfillmentWorkflow (Medusa)"] createRemoteLinkStep --> createOrderFulfillmentWorkflow["createOrderFulfillmentWorkflow (Medusa)"]
@@ -1580,7 +1580,7 @@ graph TD
The workflow has the following steps: The workflow has the following steps:
1. `completeCartWorkflow` to create a Medusa order from the cart. Medusa provides this workflow through the `@medusajs/medusa/core-flows` package and you can use it as a step. 1. `completeCartWorkflow` to create a Medusa order from the cart. Medusa provides this workflow through the `@medusajs/medusa/core-flows` package and you can use it as a step.
2. `useRemoteQueryStep` to retrieve the order’s items with the digital products associated with the purchased product variants. Medusa provides this step through the `@medusajs/medusa/core-flows` package. 2. `useQueryGraphStep` to retrieve the order’s items with the digital products associated with the purchased product variants. Medusa provides this step through the `@medusajs/medusa/core-flows` package.
3. If the order has digital products, you: 3. If the order has digital products, you:
1. create the digital product order. 1. create the digital product order.
2. link the digital product order with the Medusa order. Medusa provides a `createRemoteLinkStep` in the `@medusajs/medusa/core-flows` package that can be used here. 2. link the digital product order with the Medusa order. Medusa provides a `createRemoteLinkStep` in the `@medusajs/medusa/core-flows` package that can be used here.
@@ -1665,14 +1665,14 @@ In the compensation function, you delete the digital product order.
Create the file `src/workflows/create-digital-product-order/index.ts` with the following content: Create the file `src/workflows/create-digital-product-order/index.ts` with the following content:
export const createDpoWorkflowHighlights = [ export const createDpoWorkflowHighlights = [
["25", "completeCartWorkflow", "Create an order for the cart."], ["27", "completeCartWorkflow", "Create an order for the cart."],
["31", "useRemoteQueryStep", "Retrieve the order's items and their associated variants and linked digital products."], ["33", "useQueryGraphStep", "Retrieve the order's items and their associated variants and linked digital products."],
["56", "when", "Check whether the order has any digital products."], ["57", "when", "Check whether the order has any digital products."],
["61", "then", "Perform the callback function if an order has digital products."], ["60", "then", "Perform the callback function if an order has digital products."],
["64", "createDigitalProductOrderStep", "Create the digital product order."], ["63", "createDigitalProductOrderStep", "Create the digital product order."],
["66", "createRemoteLinkStep", "Link the digital product order to the Medusa order."], ["67", "createRemoteLinkStep", "Link the digital product order to the Medusa order."],
["75", "createOrderFulfillmentWorkflow", "Create a fulfillment for the digital products in the order."], ["76", "createOrderFulfillmentWorkflow", "Create a fulfillment for the digital products in the order."],
["89", "emitEventStep", "Emit the `digital_product_order.created` event."] ["90", "emitEventStep", "Emit the `digital_product_order.created` event."]
] ]
```ts title="src/workflows/create-digital-product-order/index.ts" highlights={createDpoWorkflowHighlights} collapsibleLines="1-17" expandMoreLabel="Show Imports" ```ts title="src/workflows/create-digital-product-order/index.ts" highlights={createDpoWorkflowHighlights} collapsibleLines="1-17" expandMoreLabel="Show Imports"
@@ -1680,16 +1680,18 @@ import {
createWorkflow, createWorkflow,
transform, transform,
when, when,
WorkflowResponse, WorkflowResponse
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { import {
completeCartWorkflow, completeCartWorkflow,
useRemoteQueryStep, useQueryGraphStep,
createRemoteLinkStep, createRemoteLinkStep,
createOrderFulfillmentWorkflow, createOrderFulfillmentWorkflow,
emitEventStep, emitEventStep
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import { Modules } from "@medusajs/framework/utils" import {
Modules
} from "@medusajs/framework/utils"
import createDigitalProductOrderStep from "./steps/create-digital-product-order" import createDigitalProductOrderStep from "./steps/create-digital-product-order"
import { DIGITAL_PRODUCT_MODULE } from "../../modules/digital-product" import { DIGITAL_PRODUCT_MODULE } from "../../modules/digital-product"
@@ -1700,35 +1702,34 @@ type WorkflowInput = {
const createDigitalProductOrderWorkflow = createWorkflow( const createDigitalProductOrderWorkflow = createWorkflow(
"create-digital-product-order", "create-digital-product-order",
(input: WorkflowInput) => { (input: WorkflowInput) => {
const order = completeCartWorkflow.runAsStep({ const { id } = completeCartWorkflow.runAsStep({
input: { input: {
id: input.cart_id, id: input.cart_id
}, }
}) })
const { items } = useRemoteQueryStep({ const { data: orders } = useQueryGraphStep({
entry_point: "order", entity: "order",
fields: [ fields: [
"*", "*",
"items.*", "items.*",
"items.variant.*", "items.variant.*",
"items.variant.digital_product.*", "items.variant.digital_product.*"
], ],
variables: { filters: {
filters: { id
id: order.id,
},
}, },
throw_if_key_not_found: true, options: {
list: false, throwIfKeyNotFound: true
}
}) })
const itemsWithDigitalProducts = transform({ const itemsWithDigitalProducts = transform({
items, orders
}, },
(data) => { (data) => {
return data.items.filter((item) => item.variant.digital_product !== undefined) return data.orders[0].items.filter((item) => item.variant.digital_product !== undefined)
} }
) )
const digital_product_order = when(itemsWithDigitalProducts, (itemsWithDigitalProducts) => { const digital_product_order = when(itemsWithDigitalProducts, (itemsWithDigitalProducts) => {
@@ -1737,44 +1738,46 @@ const createDigitalProductOrderWorkflow = createWorkflow(
.then(() => { .then(() => {
const { const {
digital_product_order, digital_product_order,
} = createDigitalProductOrderStep({ items }) } = createDigitalProductOrderStep({
items: orders[0].items
})
createRemoteLinkStep([{ createRemoteLinkStep([{
[DIGITAL_PRODUCT_MODULE]: { [DIGITAL_PRODUCT_MODULE]: {
digital_product_order_id: digital_product_order.id, digital_product_order_id: digital_product_order.id
}, },
[Modules.ORDER]: { [Modules.ORDER]: {
order_id: order.id, order_id: id
}, }
}]) }])
createOrderFulfillmentWorkflow.runAsStep({ createOrderFulfillmentWorkflow.runAsStep({
input: { input: {
order_id: order.id, order_id: id,
items: transform({ items: transform({
itemsWithDigitalProducts, itemsWithDigitalProducts
}, (data) => { }, (data) => {
return data.itemsWithDigitalProducts.map((item) => ({ return data.itemsWithDigitalProducts.map((item) => ({
id: item.id, id: item.id,
quantity: item.quantity, quantity: item.quantity
})) }))
}), })
}, }
}) })
emitEventStep({ emitEventStep({
eventName: "digital_product_order.created", eventName: "digital_product_order.created",
data: { data: {
id: digital_product_order.id, id: digital_product_order.id
}, }
}) })
return digital_product_order return digital_product_order
}) })
return new WorkflowResponse({ return new WorkflowResponse({
order, order: orders[0],
digital_product_order, digital_product_order
}) })
} }
) )
@@ -1785,7 +1788,7 @@ export default createDigitalProductOrderWorkflow
This creates the workflow `createDigitalProductOrderWorkflow`. It runs the following steps: This creates the workflow `createDigitalProductOrderWorkflow`. It runs the following steps:
1. `completeCartWorkflow` as a step to create the Medusa order. 1. `completeCartWorkflow` as a step to create the Medusa order.
2. `useRemoteQueryStep` to retrieve the order’s items with their associated variants and linked digital products. 2. `useQueryGraphStep` to retrieve the order’s items with their associated variants and linked digital products.
3. Use `when` to check whether the order has digital products. If so: 3. Use `when` to check whether the order has digital products. If so:
1. Use the `createDigitalProductOrderStep` to create the digital product order. 1. Use the `createDigitalProductOrderStep` to create the digital product order.
2. Use the `createRemoteLinkStep` to link the digital product order to the Medusa order. 2. Use the `createRemoteLinkStep` to link the digital product order to the Medusa order.
@@ -1842,7 +1845,7 @@ In this step, you'll create a workflow that fulfills a digital order by sending
The workflow has the following steps: The workflow has the following steps:
1. Retrieve the digital product order's details. For this, you'll use the `useRemoteQueryStep` imported from `@medusajs/medusa/core-flows`. 1. Retrieve the digital product order's details. For this, you'll use the `useQueryGraphStep` imported from `@medusajs/medusa/core-flows`.
2. Send a notification to the customer with the digital products to download. 2. Send a notification to the customer with the digital products to download.
So, you only need to implement the second step. So, you only need to implement the second step.
@@ -1959,17 +1962,17 @@ You use the `createNotifications` method of the Notification Module's main servi
Create the workflow in the file `src/workflows/fulfill-digital-order/index.ts`: Create the workflow in the file `src/workflows/fulfill-digital-order/index.ts`:
export const fulfillWorkflowHighlights = [ export const fulfillWorkflowHighlights = [
["17", "useRemoteQueryStep", "Retrieve the digital product order's details."], ["17", "useQueryGraphStep", "Retrieve the digital product order's details."],
["33", "sendDigitalOrderNotificationStep", "Send a notification to the customer."] ["33", "sendDigitalOrderNotificationStep", "Send a notification to the customer."]
] ]
```ts title="src/workflows/fulfill-digital-order/index.ts" highlights={fulfillWorkflowHighlights} collapsibleLines="1-10" expandMoreLabel="Show Imports" ```ts title="src/workflows/fulfill-digital-order/index.ts" highlights={fulfillWorkflowHighlights} collapsibleLines="1-10" expandMoreLabel="Show Imports"
import { import {
createWorkflow, createWorkflow,
WorkflowResponse, WorkflowResponse
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { import {
useRemoteQueryStep, useQueryGraphStep,
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import { sendDigitalOrderNotificationStep } from "./steps/send-digital-order-notification" import { sendDigitalOrderNotificationStep } from "./steps/send-digital-order-notification"
@@ -1980,29 +1983,28 @@ type FulfillDigitalOrderWorkflowInput = {
export const fulfillDigitalOrderWorkflow = createWorkflow( export const fulfillDigitalOrderWorkflow = createWorkflow(
"fulfill-digital-order", "fulfill-digital-order",
({ id }: FulfillDigitalOrderWorkflowInput) => { ({ id }: FulfillDigitalOrderWorkflowInput) => {
const digitalProductOrder = useRemoteQueryStep({ const { data: digitalProductOrders } = useQueryGraphStep({
entry_point: "digital_product_order", entity: "digital_product_order",
fields: [ fields: [
"*", "*",
"products.*", "products.*",
"products.medias.*", "products.medias.*",
"order.*", "order.*"
], ],
variables: { filters: {
filters: { id,
id,
},
}, },
list: false, options: {
throw_if_key_not_found: true, throwIfKeyNotFound: true
}
}) })
sendDigitalOrderNotificationStep({ sendDigitalOrderNotificationStep({
digital_product_order: digitalProductOrder, digital_product_order: digitalProductOrders[0]
}) })
return new WorkflowResponse( return new WorkflowResponse(
digitalProductOrder digitalProductOrders[0]
) )
} }
) )
@@ -2010,7 +2012,7 @@ export const fulfillDigitalOrderWorkflow = createWorkflow(
In the workflow, you: In the workflow, you:
1. Retrieve the digital product order's details using the `useRemoteQueryStep` imported from `@medusajs/medusa/core-flows`. 1. Retrieve the digital product order's details using the `useQueryGraphStep` imported from `@medusajs/medusa/core-flows`.
2. Send a notification to the customer with the digital product download links using the `sendDigitalOrderNotificationStep`. 2. Send a notification to the customer with the digital product download links using the `sendDigitalOrderNotificationStep`.
### Configure Notification Module Provider ### Configure Notification Module Provider
@@ -1119,7 +1119,7 @@ import {
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { import {
setAuthAppMetadataStep, setAuthAppMetadataStep,
useRemoteQueryStep, useQueryGraphStep,
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import { deleteRestaurantAdminStep } from "../steps/delete-restaurant-admin" import { deleteRestaurantAdminStep } from "../steps/delete-restaurant-admin"
@@ -1144,14 +1144,12 @@ So far, you only use the `deleteRestaurantAdminStep` in the workflow, which dele
Replace the `TODO` with the following: Replace the `TODO` with the following:
```ts title="restaurant-marketplace/src/workflows/restaurant/workflows/delete-restaurant-admin.ts" ```ts title="restaurant-marketplace/src/workflows/restaurant/workflows/delete-restaurant-admin.ts"
const authIdentities = useRemoteQueryStep({ const { data: authIdentities } = useQueryGraphStep({
entry_point: "auth_identity", entity: "auth_identity",
fields: ["id"], fields: ["id"],
variables: { filters: {
filters: { app_metadata: {
app_metadata: { restaurant_id: input.id,
restaurant_id: input.id,
},
}, },
}, },
}) })
@@ -803,14 +803,14 @@ In this step, you’ll create a workflow that’s executed when the customer pla
```mermaid ```mermaid
graph TD graph TD
retrieveCartStep["Retrieve Cart (useRemoteQueryStep from Medusa)"] --> completeCartWorkflow["completeCartWorkflow (Medusa)"] retrieveCartStep["Retrieve Cart (useQueryGraphStep from Medusa)"] --> completeCartWorkflow["completeCartWorkflow (Medusa)"]
completeCartWorkflow["completeCartWorkflow (Medusa)"] --> groupVendorItemsStep completeCartWorkflow["completeCartWorkflow (Medusa)"] --> groupVendorItemsStep
groupVendorItemsStep --> getOrderDetailWorkflow groupVendorItemsStep --> getOrderDetailWorkflow
getOrderDetailWorkflow --> createVendorOrdersStep getOrderDetailWorkflow --> createVendorOrdersStep
createVendorOrdersStep --> createRemoteLinkStep["Create Links (createRemoteLinkStep from Medusa)"] createVendorOrdersStep --> createRemoteLinkStep["Create Links (createRemoteLinkStep from Medusa)"]
``` ```
1. Retrieve the cart using its ID. Medusa provides a `useRemoteQueryStep` in the `@medusajs/medusa/core-flows` package that you can use. 1. Retrieve the cart using its ID. Medusa provides a `useQueryGraphStep` in the `@medusajs/medusa/core-flows` package that you can use.
2. Create a parent order for the cart and its items. Medusa also has a `completeCartWorkflow` in the `@medusajs/medusa/core-flows` package that you can use as a step. 2. Create a parent order for the cart and its items. Medusa also has a `completeCartWorkflow` in the `@medusajs/medusa/core-flows` package that you can use as a step.
3. Group the cart items by their product’s associated vendor. 3. Group the cart items by their product’s associated vendor.
4. Retrieve the order's details using Medusa's `getOrderDetailWorkflow` exported by the `@medusajs/medusa/core-flows` package. 4. Retrieve the order's details using Medusa's `getOrderDetailWorkflow` exported by the `@medusajs/medusa/core-flows` package.
@@ -1135,25 +1135,24 @@ 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 = [
["21", "useRemoteQueryStep", "Retrieve the cart's details."], ["21", "useQueryGraphStep", "Retrieve the cart's details."],
["29", "completeCartWorkflow", "Create the parent order from the cart."], ["30", "completeCartWorkflow", "Create the parent order from the cart."],
["35", "groupVendorItemsStep", "Group the items by their vendor."], ["36", "groupVendorItemsStep", "Group the items by their vendor."],
["42", "createVendorOrdersStep", "Create child orders for each vendor"], ["59", "createVendorOrdersStep", "Create child orders for each vendor"],
["47", "createRemoteLinkStep", "Create the links returned by the previous step."] ["64", "createRemoteLinkStep", "Create the links returned by the previous step."]
] ]
```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-13" expandMoreLabel="Show Imports" ```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-13" expandMoreLabel="Show Imports"
import { import {
createWorkflow, createWorkflow,
WorkflowResponse, WorkflowResponse
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { import {
useRemoteQueryStep, useQueryGraphStep,
createRemoteLinkStep, createRemoteLinkStep,
completeCartWorkflow, completeCartWorkflow,
getOrderDetailWorkflow getOrderDetailWorkflow
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import { CartDTO } from "@medusajs/framework/types"
import groupVendorItemsStep from "./steps/group-vendor-items" import groupVendorItemsStep from "./steps/group-vendor-items"
import createVendorOrdersStep from "./steps/create-vendor-orders" import createVendorOrdersStep from "./steps/create-vendor-orders"
@@ -1164,24 +1163,25 @@ type WorkflowInput = {
const createVendorOrdersWorkflow = createWorkflow( const createVendorOrdersWorkflow = createWorkflow(
"create-vendor-order", "create-vendor-order",
(input: WorkflowInput) => { (input: WorkflowInput) => {
const cart = useRemoteQueryStep({ const { data: carts } = useQueryGraphStep({
entry_point: "cart", entity: "cart",
fields: ["items.*"], fields: ['items.*'],
variables: { id: input.cart_id }, filters: { id: input.cart_id },
list: false, options: {
throw_if_key_not_found: true, throwIfKeyNotFound: true
}) as CartDTO }
})
const { id: orderId } = completeCartWorkflow.runAsStep({ const { id: orderId } = completeCartWorkflow.runAsStep({
input: { input: {
id: cart.id id: carts[0].id
} }
}) })
const { vendorsItems } = groupVendorItemsStep({ const { vendorsItems } = groupVendorItemsStep({
cart, cart: carts[0].id
}) })
const order = getOrderDetailWorkflow.runAsStep({ const order = getOrderDetailWorkflow.runAsStep({
input: { input: {
order_id: orderId, order_id: orderId,
@@ -1200,17 +1200,17 @@ const createVendorOrdersWorkflow = createWorkflow(
const { const {
orders: vendorOrders, orders: vendorOrders,
linkDefs, linkDefs
} = createVendorOrdersStep({ } = createVendorOrdersStep({
parentOrder: order, parentOrder: order,
vendorsItems, vendorsItems
}) })
createRemoteLinkStep(linkDefs) createRemoteLinkStep(linkDefs)
return new WorkflowResponse({ return new WorkflowResponse({
parent_order: order, parent_order: order,
vendor_orders: vendorOrders, vendor_orders: vendorOrders
}) })
} }
) )
@@ -1220,11 +1220,12 @@ export default createVendorOrdersWorkflow
In the workflow, you run the following steps: In the workflow, you run the following steps:
1. `useRemoteQueryStep` to retrieve the cart's details. 1. `useQueryGraphStep` to retrieve the cart's details.
2. `completeCartWorkflow` to complete the cart and create a parent order. 2. `completeCartWorkflow` to complete the cart and create a parent order.
3. `groupVendorItemsStep` to group the order's items by their vendor. 3. `groupVendorItemsStep` to group the order's items by their vendor.
4. `createVendorOrdersStep` to create child orders for each vendor's items. 4. `getOrderDetailWorkflow` to retrieve an order's details.
5. `createRemoteLinkStep` to create the links returned by the previous step. 5. `createVendorOrdersStep` to create child orders for each vendor's items.
6. `createRemoteLinkStep` to create the links returned by the previous step.
You return the parent and vendor orders. You return the parent and vendor orders.
@@ -592,23 +592,23 @@ Create the file `src/workflows/create-subscription/index.ts` with the following
export const createSubscriptionWorkflowHighlights = [ export const createSubscriptionWorkflowHighlights = [
["26", "completeCartWorkflow", "Complete the cart and create the order."], ["26", "completeCartWorkflow", "Complete the cart and create the order."],
["32", "useRemoteQueryStep", "Retrieve the order's details."], ["32", "useQueryGraphStep", "Retrieve the order's details."],
["44", "createSubscriptionStep", "Create the subscription."], ["43", "createSubscriptionStep", "Create the subscription."],
["51", "createRemoteLinkStep", "Create the links returned by the previous step."] ["50", "createRemoteLinkStep", "Create the links returned by the previous step."]
] ]
```ts title="src/workflows/create-subscription/index.ts" highlights={createSubscriptionWorkflowHighlights} collapsibleLines="1-13" expandMoreLabel="Show Imports" ```ts title="src/workflows/create-subscription/index.ts" highlights={createSubscriptionWorkflowHighlights} collapsibleLines="1-13" expandMoreLabel="Show Imports"
import { import {
createWorkflow, createWorkflow,
WorkflowResponse, WorkflowResponse
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { import {
createRemoteLinkStep, createRemoteLinkStep,
completeCartWorkflow, completeCartWorkflow,
useRemoteQueryStep useQueryGraphStep
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import { import {
SubscriptionInterval, SubscriptionInterval
} from "../../modules/subscription/types" } from "../../modules/subscription/types"
import createSubscriptionStep from "./steps/create-subscription" import createSubscriptionStep from "./steps/create-subscription"
@@ -629,30 +629,29 @@ const createSubscriptionWorkflow = createWorkflow(
} }
}) })
const order = useRemoteQueryStep({ const { data: orders } = useQueryGraphStep({
entry_point: "order", entity: "order",
fields: ["*", "id", "customer_id"], fields: ["*", "id", "customer_id"],
variables: { filters: {
filters: { id
id
}
}, },
list: false, options: {
throw_if_key_not_found: true throwIfKeyNotFound: true
}
}) })
const { subscription, linkDefs } = createSubscriptionStep({ const { subscription, linkDefs } = createSubscriptionStep({
cart_id: input.cart_id, cart_id: input.cart_id,
order_id: order.id, order_id: orders[0].id,
customer_id: order.customer_id, customer_id: orders[0].customer_id,
subscription_data: input.subscription_data, subscription_data: input.subscription_data
}) })
createRemoteLinkStep(linkDefs) createRemoteLinkStep(linkDefs)
return new WorkflowResponse({ return new WorkflowResponse({
subscription: subscription, subscription: subscription,
order: order, order: orders[0]
}) })
} }
) )
@@ -663,7 +662,7 @@ export default createSubscriptionWorkflow
This workflow accepts the cart’s ID, along with the subscription details. It executes the following steps: This workflow accepts the cart’s ID, along with the subscription details. It executes the following steps:
1. `completeCartWorkflow` from `@medusajs/medusa/core-flows` that completes a cart and creates an order. 1. `completeCartWorkflow` from `@medusajs/medusa/core-flows` that completes a cart and creates an order.
2. `useRemoteQueryStep` from `@medusajs/medusa/core-flows` to retrieve the order's details. 2. `useQueryGraphStep` from `@medusajs/medusa/core-flows` to retrieve the order's details.
3. `createSubscriptionStep`, which is the step you created previously. 3. `createSubscriptionStep`, which is the step you created previously.
4. `createRemoteLinkStep` from `@medusajs/medusa/core-flows`, which accepts links to create. These links are in the `linkDefs` array returned by the previous step. 4. `createRemoteLinkStep` from `@medusajs/medusa/core-flows`, which accepts links to create. These links are in the `linkDefs` array returned by the previous step.
@@ -1478,7 +1477,7 @@ The workflow has eight steps:
```mermaid ```mermaid
graph TD graph TD
useRemoteQueryStep["Retrieve Cart (useRemoteQueryStep by Medusa)"] --> createPaymentCollectionStep["createPaymentCollectionStep (Medusa)"] useQueryGraphStep["Retrieve Cart (useQueryGraphStep by Medusa)"] --> createPaymentCollectionStep["createPaymentCollectionStep (Medusa)"]
createPaymentCollectionStep["createPaymentCollectionStep (Medusa)"] --> createPaymentSessionsWorkflow["createPaymentSessionsWorkflow (Medusa)"] createPaymentCollectionStep["createPaymentCollectionStep (Medusa)"] --> createPaymentSessionsWorkflow["createPaymentSessionsWorkflow (Medusa)"]
createPaymentSessionsWorkflow["createPaymentSessionsWorkflow (Medusa)"] --> authorizePaymentSessionStep["authorizePaymentSessionStep (Medusa)"] createPaymentSessionsWorkflow["createPaymentSessionsWorkflow (Medusa)"] --> authorizePaymentSessionStep["authorizePaymentSessionStep (Medusa)"]
authorizePaymentSessionStep["authorizePaymentSessionStep (Medusa)"] --> createSubscriptionOrderStep authorizePaymentSessionStep["authorizePaymentSessionStep (Medusa)"] --> createSubscriptionOrderStep
@@ -1487,7 +1486,7 @@ graph TD
capturePaymentStep["capturePaymentStep (Medusa)"] --> updateSubscriptionStep capturePaymentStep["capturePaymentStep (Medusa)"] --> updateSubscriptionStep
``` ```
1. Retrieve the subscription’s linked cart. Medusa provides a `useRemoteQueryStep` in the `@medusajs/medusa/core-flows` package that can be used as a step. 1. Retrieve the subscription’s linked cart. Medusa provides a `useQueryGraphStep` in the `@medusajs/medusa/core-flows` package that can be used as a step.
2. Create a payment collection for the new order. Medusa provides a `createPaymentCollectionsStep` in the `@medusajs/medusa/core-flows` package that you can use. 2. Create a payment collection for the new order. Medusa provides a `createPaymentCollectionsStep` in the `@medusajs/medusa/core-flows` package that you can use.
3. Create payment sessions in the payment collection. Medusa provides a `createPaymentSessionsWorkflow` in the `@medusajs/medusa/core-flows` package that can be used as a step. 3. Create payment sessions in the payment collection. Medusa provides a `createPaymentSessionsWorkflow` in the `@medusajs/medusa/core-flows` package that can be used as a step.
4. Authorize the payment session. Medusa also provides the `authorizePaymentSessionStep` in the `@medusajs/medusa/core-flows` package, which can be used. 4. Authorize the payment session. Medusa also provides the `authorizePaymentSessionStep` in the `@medusajs/medusa/core-flows` package, which can be used.
@@ -1771,38 +1770,30 @@ This updates the subscription’s `last_order_date` and `next_order_date` proper
Finally, create the file `src/workflows/create-subscription-order/index.ts` with the following content: Finally, create the file `src/workflows/create-subscription-order/index.ts` with the following content:
export const createSubscriptionOrderWorkflowHighlights = [ export const createSubscriptionOrderWorkflowHighlights = [
["33", "useRemoteQueryStep", "Retrieve the cart linked to the subscription."], ["25", "useQueryGraphStep", "Retrieve the cart linked to the subscription."],
["60", "createPaymentCollectionsStep", "Create a payment collection using the same information in the cart."], ["49", "createPaymentCollectionsStep", "Create a payment collection using the same information in the cart."],
["67", "createPaymentSessionsWorkflow", "Create a payment session in the payment collection from the previous step."], ["56", "createPaymentSessionsWorkflow", "Create a payment session in the payment collection from the previous step."],
["76", "authorizePaymentSessionStep", "Authorize the payment session created from the first step."], ["65", "authorizePaymentSessionStep", "Authorize the payment session created from the first step."],
["81", "createSubscriptionOrderStep", "Create the new order for the subscription."], ["70", "createSubscriptionOrderStep", "Create the new order for the subscription."],
["87", "createRemoteLinkStep", "Create links returned by the previous step."], ["76", "createRemoteLinkStep", "Create links returned by the previous step."],
["89", "capturePaymentStep", "Capture the order’s payment."], ["78", "capturePaymentStep", "Capture the order’s payment."],
["94", "updateSubscriptionStep", "Update the subscription’s `last_order_date` and `next_order_date`."] ["83", "updateSubscriptionStep", "Update the subscription’s `last_order_date` and `next_order_date`."]
] ]
```ts title="src/workflows/create-subscription-order/index.ts" highlights={createSubscriptionOrderWorkflowHighlights} collapsibleLines="1-25" expandMoreLabel="Show Imports" ```ts title="src/workflows/create-subscription-order/index.ts" highlights={createSubscriptionOrderWorkflowHighlights} collapsibleLines="1-25" expandMoreLabel="Show Imports"
import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import { import {
createWorkflow, useQueryGraphStep,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import {
useRemoteQueryStep,
createPaymentSessionsWorkflow, createPaymentSessionsWorkflow,
createRemoteLinkStep, createRemoteLinkStep,
capturePaymentStep, capturePaymentStep
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import { import {
CartWorkflowDTO, SubscriptionData
} from "@medusajs/framework/types"
import {
SubscriptionData,
} from "../../modules/subscription/types" } from "../../modules/subscription/types"
import { import {
authorizePaymentSessionStep, authorizePaymentSessionStep,
} from "@medusajs/medusa/core-flows" createPaymentCollectionsStep
import {
createPaymentCollectionsStep,
} from "@medusajs/medusa/core-flows" } from "@medusajs/medusa/core-flows"
import createSubscriptionOrderStep from "./steps/create-subscription-order" import createSubscriptionOrderStep from "./steps/create-subscription-order"
import updateSubscriptionStep from "./steps/update-subscription" import updateSubscriptionStep from "./steps/update-subscription"
@@ -1814,8 +1805,8 @@ type WorkflowInput = {
const createSubscriptionOrderWorkflow = createWorkflow( const createSubscriptionOrderWorkflow = createWorkflow(
"create-subscription-order", "create-subscription-order",
(input: WorkflowInput) => { (input: WorkflowInput) => {
const { cart } = useRemoteQueryStep({ const { data: carts } = useQueryGraphStep({
entry_point: "subscription", entity: "subscription",
fields: [ fields: [
"*", "*",
"cart.*", "cart.*",
@@ -1828,59 +1819,56 @@ const createSubscriptionOrderWorkflow = createWorkflow(
"cart.shipping_methods.tax_lines.*", "cart.shipping_methods.tax_lines.*",
"cart.shipping_methods.adjustments.*", "cart.shipping_methods.adjustments.*",
"cart.payment_collection.*", "cart.payment_collection.*",
"cart.payment_collection.payment_sessions.*", "cart.payment_collection.payment_sessions.*"
], ],
variables: { filters: {
filters: { id: [input.subscription.id]
id: [input.subscription.id],
},
}, },
list: false, options: {
throw_if_key_not_found: true, throwIfKeyNotFound: true
}) as { }
cart: CartWorkflowDTO })
}
const payment_collection = createPaymentCollectionsStep([{ const payment_collection = createPaymentCollectionsStep([{
region_id: cart.region_id, region_id: carts[0].region_id,
currency_code: cart.currency_code, currency_code: carts[0].currency_code,
amount: cart.payment_collection.amount, amount: carts[0].payment_collection.amount,
metadata: cart.payment_collection.metadata, metadata: carts[0].payment_collection.metadata
}])[0] }])[0]
const paymentSession = createPaymentSessionsWorkflow.runAsStep({ const paymentSession = createPaymentSessionsWorkflow.runAsStep({
input: { input: {
payment_collection_id: payment_collection.id, payment_collection_id: payment_collection.id,
provider_id: cart.payment_collection.payment_sessions[0].provider_id, provider_id: carts[0].payment_collection.payment_sessions[0].provider_id,
data: cart.payment_collection.payment_sessions[0].data, data: carts[0].payment_collection.payment_sessions[0].data,
context: cart.payment_collection.payment_sessions[0].context, context: carts[0].payment_collection.payment_sessions[0].context
}, }
}) })
const payment = authorizePaymentSessionStep({ const payment = authorizePaymentSessionStep({
id: paymentSession.id, id: paymentSession.id,
context: paymentSession.context, context: paymentSession.context
}) })
const { order, linkDefs } = createSubscriptionOrderStep({ const { order, linkDefs } = createSubscriptionOrderStep({
subscription: input.subscription, subscription: input.subscription,
cart, cart: carts[0],
payment_collection, payment_collection
}) })
createRemoteLinkStep(linkDefs) createRemoteLinkStep(linkDefs)
capturePaymentStep({ capturePaymentStep({
payment_id: payment.id, payment_id: payment.id,
amount: payment.amount, amount: payment.amount
}) })
updateSubscriptionStep({ updateSubscriptionStep({
subscription_id: input.subscription.id, subscription_id: input.subscription.id
}) })
return new WorkflowResponse({ return new WorkflowResponse({
order, order
}) })
} }
) )
@@ -1890,7 +1878,7 @@ export default createSubscriptionOrderWorkflow
The workflow runs the following steps: The workflow runs the following steps:
1. `useRemoteQueryStep` to retrieve the details of the cart linked to the subscription. 1. `useQueryGraphStep` to retrieve the details of the cart linked to the subscription.
2. `createPaymentCollectionsStep` to create a payment collection using the same information in the cart. 2. `createPaymentCollectionsStep` to create a payment collection using the same information in the cart.
3. `createPaymentSessionsWorkflow` to create a payment session in the payment collection from the previous step. 3. `createPaymentSessionsWorkflow` to create a payment session in the payment collection from the previous step.
4. `authorizePaymentSessionStep` to authorize the payment session created from the first step. 4. `authorizePaymentSessionStep` to authorize the payment session created from the first step.