Skip to content

Commit

Permalink
docs: fixes and improvements to Sanity guide (#10414)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Dec 4, 2024
1 parent 3407695 commit 665eea8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
84 changes: 38 additions & 46 deletions www/apps/resources/app/integrations/guides/sanity/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
import { ProductDTO } from "@medusajs/framework/types"
import {
ContainerRegistrationKeys,
promiseAll,
} from "@medusajs/framework/utils"
import SanityModuleService from "../../../modules/sanity/service"
import { SANITY_MODULE } from "../../../modules/sanity"
Expand All @@ -694,15 +695,15 @@ export const syncStep = createStep(
const sanityModule: SanityModuleService = container.resolve(SANITY_MODULE)
const query = container.resolve(ContainerRegistrationKeys.QUERY)

const total = 0
let total = 0;
const upsertMap: {
before: any
after: any
}[] = []

const batchSize = 200
const hasMore = true
const offset = 0
const batchSize = 200;
let hasMore = true;
let offset = 0;
let filters = input.product_ids ? {
id: input.product_ids
} : {}
Expand Down Expand Up @@ -760,54 +761,39 @@ Notice that you pass `sanity_product.*` in the `fields` array. Medusa will retri
}
```

Next, you want to sync the retrieved products. So, replace the `TODO` with the following:
Next, you want to sync the retrieved products. So, replace the `TODO` in the `while` loop with the following:

```ts title="src/workflows/sanity-sync-products/steps/sync.ts"
// other imports...
import {
while (hasMore) {
// ...
promiseAll,
} from "@medusajs/framework/utils"

export const syncStep = createStep(
{ name: "sync-step", async: true },
async (input: SyncStepInput, { container }) => {
// ...

while (hasMore) {
// ...
try {
await promiseAll(
products.map(async (prod) => {
const after = await sanityModule.upsertSyncDocument(
"product",
prod as ProductDTO
);

upsertMap.push({
// @ts-ignore
before: prod.sanity_product,
after
})

return after
}),
)
} catch (e) {
return StepResponse.permanentFailure(
`An error occurred while syncing documents: ${e}`,
upsertMap
)
}
try {
await promiseAll(
products.map(async (prod) => {
const after = await sanityModule.upsertSyncDocument(
"product",
prod as ProductDTO
);

offset += batchSize
hasMore = offset < count
total += products.length
}
upsertMap.push({
// @ts-ignore
before: prod.sanity_product,
after
})

return new StepResponse({ total }, upsertMap)
return after
}),
)
} catch (e) {
return StepResponse.permanentFailure(
`An error occurred while syncing documents: ${e}`,
upsertMap
)
}
)

offset += batchSize
hasMore = offset < count
total += products.length
}
```

In the `while` loop, you loop over the array of products to sync them to Sanity. You use the `promiseAll` Medusa utility that loops over an array of promises and ensures that all transactions within these promises are rolled back in case an error occurs.
Expand All @@ -816,6 +802,12 @@ For each product, you upsert it into Sanity, then push its document before and a

You also wrap the `promiseAll` function within a try-catch block. In the catch block, you invoke and return `StepResponse.permanentFailure` which indicates that the step has failed but still invokes the rollback mechanism that you'll implement in a bit. The first parameter of `permanentFailure` is the error message, and the second is the data to use in the rollback mechanism.

Finally, after the `while` loop and at the end of the step, add the following return statement:

```ts title="src/workflows/sanity-sync-products/steps/sync.ts"
return new StepResponse({ total }, upsertMap);
```

If no errors occur, the step returns an instance of `StepResponse`, which must be returned by any step. It accepts as a first parameter the data to return to the workflow that executed this step.

#### Add Compensation Function
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 @@ -3245,7 +3245,7 @@ export const generatedEditDates = {
"references/types/HttpTypes/interfaces/types.HttpTypes.AdminBatchProductVariantRequest/page.mdx": "2024-11-25T17:49:26.851Z",
"references/types/WorkflowTypes/ProductWorkflow/interfaces/types.WorkflowTypes.ProductWorkflow.ExportProductsDTO/page.mdx": "2024-11-12T09:36:24.232Z",
"app/contribution-guidelines/admin-translations/page.mdx": "2024-11-14T08:54:15.369Z",
"app/integrations/guides/sanity/page.mdx": "2024-11-27T10:10:12.100Z",
"app/integrations/guides/sanity/page.mdx": "2024-12-03T14:14:11.347Z",
"references/api_key/types/api_key.FindConfigOrder/page.mdx": "2024-11-25T17:49:28.715Z",
"references/auth/types/auth.FindConfigOrder/page.mdx": "2024-11-25T17:49:28.887Z",
"references/cart/types/cart.FindConfigOrder/page.mdx": "2024-11-25T17:49:29.455Z",
Expand Down

0 comments on commit 665eea8

Please sign in to comment.