docs: fixes to type errors in guides (#13797)

This commit is contained in:
Shahed Nasser
2025-10-21 16:12:35 +03:00
committed by GitHub
parent d9249be6e6
commit 0d83918348
19 changed files with 922 additions and 682 deletions
@@ -646,7 +646,7 @@ const createSubscriptionStep = createStep(
}, {
subscription: subscription[0],
})
}, async ({ subscription }, { container }) => {
}, async (data, { container }) => {
// TODO implement compensation
}
)
@@ -706,10 +706,13 @@ This adds links between:
The step also has a compensation function to undo the steps changes if an error occurs. So, replace the second `TODO` with the following:
```ts title="src/workflows/create-subscription/steps/create-subscription.ts"
if (!data) {
return
}
const subscriptionModuleService: SubscriptionModuleService =
container.resolve(SUBSCRIPTION_MODULE)
container.resolve(SUBSCRIPTION_MODULE)
await subscriptionModuleService.cancelSubscriptions(subscription.id)
await subscriptionModuleService.cancelSubscriptions(data.subscription.id)
```
The compensation function receives the subscription as a parameter. It cancels the subscription.
@@ -815,7 +818,7 @@ const createSubscriptionWorkflow = createWorkflow(
const { subscription, linkDefs } = createSubscriptionStep({
cart_id: input.cart_id,
order_id: orders[0].id,
customer_id: orders[0].customer_id,
customer_id: orders[0].customer_id!,
subscription_data: input.subscription_data,
})
@@ -859,9 +862,9 @@ In this step, youll create a custom API route similar to the [Complete Cart A
Create the file `src/api/store/carts/[id]/subscribe/route.ts` with the following content:
export const completeCartHighlights = [
["17", "graph", "Retrieve the cart to retrieve the subscription details from the `metadata`."],
["29", "", "If the subscription data isn't set in the cart's `metadata`, throw an error"],
["36", "createSubscriptionWorkflow", "Execute the workflow created in the previous step."]
["18", "graph", "Retrieve the cart to retrieve the subscription details from the `metadata`."],
["30", "", "If the subscription data isn't set in the cart's `metadata`, throw an error"],
["37", "createSubscriptionWorkflow", "Execute the workflow created in the previous step."]
]
```ts title="src/api/store/carts/[id]/subscribe/route.ts" highlights={completeCartHighlights} collapsibleLines="1-10" expandMoreLabel="Show Imports"
@@ -874,6 +877,7 @@ import {
MedusaError,
} from "@medusajs/framework/utils"
import createSubscriptionWorkflow from "../../../../../workflows/create-subscription"
import { SubscriptionInterval } from "../../../../../modules/subscription/types"
export const POST = async (
req: MedusaRequest,
@@ -906,8 +910,8 @@ export const POST = async (
input: {
cart_id: req.params.id,
subscription_data: {
interval: metadata.subscription_interval,
period: metadata.subscription_period,
interval: metadata.subscription_interval as SubscriptionInterval,
period: metadata.subscription_period as number,
},
},
})
@@ -1247,7 +1251,7 @@ export const GET = async (
const {
data: subscriptions,
metadata: { count, take, skip },
metadata: { count, take, skip } = {},
} = await query.graph({
entity: "subscription",
...req.queryConfig,
@@ -1743,7 +1747,7 @@ import { AccountHolderDTO, CustomerDTO, PaymentMethodDTO } from "@medusajs/frame
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
export interface GetPaymentMethodStepInput {
customer: CustomerDTO & {
customer?: CustomerDTO & {
account_holder: AccountHolderDTO
}
}
@@ -1776,7 +1780,7 @@ export const getPaymentMethodStep = createStep(
async ({ customer }: GetPaymentMethodStepInput, { container }) => {
const paymentModuleService = container.resolve(Modules.PAYMENT)
if (!customer.account_holder) {
if (!customer?.account_holder) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"No account holder found for the customer while retrieving payment method"
@@ -1840,7 +1844,7 @@ import { createOrderWorkflow } from "@medusajs/medusa/core-flows"
import { SubscriptionData } from "../../../modules/subscription/types"
import { SUBSCRIPTION_MODULE } from "../../../modules/subscription"
type StepInput = {
export type CreateSubscriptionOrderStepInput = {
subscription: SubscriptionData
cart: CartWorkflowDTO
payment_collection: PaymentCollectionDTO
@@ -1854,7 +1858,7 @@ const createSubscriptionOrderStep = createStep(
"create-subscription-order",
async ({
subscription, cart, payment_collection,
}: StepInput,
}: CreateSubscriptionOrderStepInput,
{ container, context }) => {
const linkDefs: LinkDefinition[] = []
@@ -1873,7 +1877,7 @@ const createSubscriptionOrderStep = createStep(
order,
})
},
async ({ order }, { container }) => {
async (data, { container }) => {
// TODO add compensation function
}
)
@@ -1904,20 +1908,20 @@ function getOrderData(cart: CartWorkflowDTO) {
id: null,
},
items: cart.items,
shipping_methods: cart.shipping_methods.map((method) => ({
shipping_methods: cart.shipping_methods?.map((method) => ({
name: method.name,
amount: method.amount,
is_tax_inclusive: method.is_tax_inclusive,
shipping_option_id: method.shipping_option_id,
data: method.data,
tax_lines: method.tax_lines.map((taxLine) => ({
tax_lines: method.tax_lines?.map((taxLine) => ({
description: taxLine.description,
tax_rate_id: taxLine.tax_rate_id,
code: taxLine.code,
rate: taxLine.rate,
provider_id: taxLine.provider_id,
})),
adjustments: method.adjustments.map((adjustment) => ({
adjustments: method.adjustments?.map((adjustment) => ({
code: adjustment.code,
amount: adjustment.amount,
description: adjustment.description,
@@ -1962,11 +1966,14 @@ This adds links to be created into the `linkDefs` array between the new order an
Finally, replace the `TODO` in the compensation function to cancel the order in case of an error:
```ts title="src/workflows/create-subscription-order/steps/create-subscription-order.ts"
if (!data) {
return
}
const orderModuleService: IOrderModuleService = container.resolve(
Modules.ORDER
)
await orderModuleService.cancel(order.id)
await orderModuleService.cancel(data.order.id)
```
### Create updateSubscriptionStep (Ninth Step)
@@ -2046,7 +2053,7 @@ const updateSubscriptionStep = createStep(
})
},
async ({
prev_data,
data,
}, { container }) => {
// TODO add compensation
}
@@ -2062,15 +2069,18 @@ Before updating the subscription, the step retrieves the old data and passes it
So, replace the `TODO` in the compensation function with the following:
```ts title="src/workflows/create-subscription-order/steps/update-subscription.ts"
if (!data) {
return
}
const subscriptionModuleService: SubscriptionModuleService =
container.resolve(
SUBSCRIPTION_MODULE
)
await subscriptionModuleService.updateSubscriptions({
id: prev_data.id,
last_order_date: prev_data.last_order_date,
next_order_date: prev_data.next_order_date,
id: data.prev_data.id,
last_order_date: data.prev_data.last_order_date,
next_order_date: data.prev_data.next_order_date,
})
```
@@ -2081,19 +2091,19 @@ This updates the subscriptions `last_order_date` and `next_order_date` proper
Finally, create the file `src/workflows/create-subscription-order/index.ts` with the following content:
export const createSubscriptionOrderWorkflowHighlights = [
["26", "useQueryGraphStep", "Retrieve the cart linked to the subscription."],
["63", "createPaymentCollectionsStep", "Create a payment collection using the same information in the cart."],
["67", "getPaymentMethodStep", "Get the customer's saved payment method."],
["80", "data", "Pass data required by Stripe to capture the payment."],
["89", "createPaymentSessionsWorkflow", "Create a payment session in the payment collection from the previous step."],
["93", "authorizePaymentSessionStep", "Authorize the payment session created from the first step."],
["98", "createSubscriptionOrderStep", "Create the new order for the subscription."],
["104", "createRemoteLinkStep", "Create links returned by the previous step."],
["106", "capturePaymentStep", "Capture the orders payment."],
["111", "updateSubscriptionStep", "Update the subscriptions `last_order_date` and `next_order_date`."]
["31", "useQueryGraphStep", "Retrieve the cart linked to the subscription."],
["68", "createPaymentCollectionsStep", "Create a payment collection using the same information in the cart."],
["72", "getPaymentMethodStep", "Get the customer's saved payment method."],
["85", "data", "Pass data required by Stripe to capture the payment."],
["94", "createPaymentSessionsWorkflow", "Create a payment session in the payment collection from the previous step."],
["98", "authorizePaymentSessionStep", "Authorize the payment session created from the first step."],
["103", "createSubscriptionOrderStep", "Create the new order for the subscription."],
["109", "createRemoteLinkStep", "Create links returned by the previous step."],
["111", "capturePaymentStep", "Capture the orders payment."],
["116", "updateSubscriptionStep", "Update the subscriptions `last_order_date` and `next_order_date`."]
]
```ts title="src/workflows/create-subscription-order/index.ts" highlights={createSubscriptionOrderWorkflowHighlights} collapsibleLines="1-18" expandMoreLabel="Show Imports"
```ts title="src/workflows/create-subscription-order/index.ts" highlights={createSubscriptionOrderWorkflowHighlights} collapsibleLines="1-23" expandMoreLabel="Show Imports"
import { createWorkflow, transform, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
import {
useQueryGraphStep,
@@ -2108,9 +2118,14 @@ import {
authorizePaymentSessionStep,
createPaymentCollectionsStep,
} from "@medusajs/medusa/core-flows"
import createSubscriptionOrderStep from "./steps/create-subscription-order"
import createSubscriptionOrderStep, {
CreateSubscriptionOrderStepInput,
} from "./steps/create-subscription-order"
import updateSubscriptionStep from "./steps/update-subscription"
import { getPaymentMethodStep } from "./steps/get-payment-method"
import {
getPaymentMethodStep,
GetPaymentMethodStepInput,
} from "./steps/get-payment-method"
type WorkflowInput = {
subscription: SubscriptionData
@@ -2150,9 +2165,9 @@ const createSubscriptionOrderWorkflow = createWorkflow(
}, (data) => {
const cart = data.subscriptions[0].cart
return {
currency_code: cart.currency_code,
amount: cart.payment_collection.amount,
metadata: cart.payment_collection.metadata,
currency_code: cart?.currency_code || "",
amount: cart?.payment_collection?.amount || 0,
metadata: cart?.payment_collection?.metadata || undefined,
}
})
@@ -2172,7 +2187,7 @@ const createSubscriptionOrderWorkflow = createWorkflow(
return {
payment_collection_id: data.payment_collection.id,
provider_id: "pp_stripe_stripe",
customer_id: data.subscriptions[0].cart.customer.id,
customer_id: data.subscriptions[0].cart?.customer?.id,
data: {
payment_method: data.defaultPaymentMethod.id,
off_session: true,
@@ -2195,7 +2210,7 @@ const createSubscriptionOrderWorkflow = createWorkflow(
subscription: input.subscription,
cart: carts[0],
payment_collection,
})
} as unknown as CreateSubscriptionOrderStepInput)
createRemoteLinkStep(linkDefs)