chore: move ModuleRegistrationName to utils (#7911)
This commit is contained in:
@@ -26,7 +26,6 @@ In the file, add the type of the expected workflow input:
|
||||
import { UpdateProductDTO } from "@medusajs/types"
|
||||
|
||||
export type UpdateProductAndErpWorkflowInput = UpdateProductDTO
|
||||
|
||||
```
|
||||
|
||||
The expected input is the data to update in the product along with the product’s ID.
|
||||
@@ -41,18 +40,23 @@ Create the file `src/workflows/update-product-erp/steps/update-product.ts` with
|
||||
|
||||
export const updateProductHighlights = [
|
||||
["13", "resolve", "Resolve the `ProductService` from the Medusa container."],
|
||||
["16", "previousProductData", "Retrieve the `previousProductData` to pass it to the compensation function."],
|
||||
[
|
||||
"16",
|
||||
"previousProductData",
|
||||
"Retrieve the `previousProductData` to pass it to the compensation function.",
|
||||
],
|
||||
["19", "", "Update the product."],
|
||||
["39", "", "Revert the product’s data using the `previousProductData` passed from the step to the compensation function."]
|
||||
[
|
||||
"39",
|
||||
"",
|
||||
"Revert the product’s data using the `previousProductData` passed from the step to the compensation function.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/update-product-erp/steps/update-product.ts" highlights={updateProductHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { UpdateProductAndErpWorkflowInput } from ".."
|
||||
|
||||
const updateProduct = createStep(
|
||||
@@ -62,8 +66,7 @@ const updateProduct = createStep(
|
||||
context.container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
const { id } = input
|
||||
const previousProductData =
|
||||
await productModuleService.retrieve(id)
|
||||
const previousProductData = await productModuleService.retrieve(id)
|
||||
|
||||
const product = await productModuleService.update(id, input)
|
||||
|
||||
@@ -77,37 +80,28 @@ const updateProduct = createStep(
|
||||
const productModuleService: IProductModuleService =
|
||||
context.container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
const {
|
||||
id,
|
||||
type,
|
||||
options,
|
||||
variants,
|
||||
...previousData
|
||||
} = previousProductData
|
||||
const { id, type, options, variants, ...previousData } = previousProductData
|
||||
|
||||
await productModuleService.update(
|
||||
id,
|
||||
{
|
||||
...previousData,
|
||||
variants: variants.map((variant) => {
|
||||
const variantOptions = {}
|
||||
await productModuleService.update(id, {
|
||||
...previousData,
|
||||
variants: variants.map((variant) => {
|
||||
const variantOptions = {}
|
||||
|
||||
variant.options.forEach((option) => {
|
||||
variantOptions[option.option.title] = option.value
|
||||
})
|
||||
variant.options.forEach((option) => {
|
||||
variantOptions[option.option.title] = option.value
|
||||
})
|
||||
|
||||
return {
|
||||
...variant,
|
||||
options: variantOptions,
|
||||
}
|
||||
}),
|
||||
options: options.map((option) => ({
|
||||
...option,
|
||||
values: option.values.map((value) => value.value),
|
||||
})),
|
||||
type_id: type.id,
|
||||
}
|
||||
)
|
||||
return {
|
||||
...variant,
|
||||
options: variantOptions,
|
||||
}
|
||||
}),
|
||||
options: options.map((option) => ({
|
||||
...option,
|
||||
values: option.values.map((value) => value.value),
|
||||
})),
|
||||
type_id: type.id,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -139,17 +133,30 @@ The `ErpModuleService` used is assumed to be created in a module.
|
||||
Create the file `src/workflows/update-product-erp/steps/update-erp.ts` with the following content:
|
||||
|
||||
export const updateErpHighlights = [
|
||||
["12", "resolve", "Resolve the `erpModuleService` from the Medusa container."],
|
||||
["17", "previousErpData", "Retrieve the `previousErpData` to pass it to the compensation function."],
|
||||
["21", "updateProductErpData", "Update the product’s ERP data and return the data from the ERP system."],
|
||||
["37", "updateProductErpData", "Revert the product's data in the ERP system to its previous state using the `previousErpData`."]
|
||||
[
|
||||
"12",
|
||||
"resolve",
|
||||
"Resolve the `erpModuleService` from the Medusa container.",
|
||||
],
|
||||
[
|
||||
"17",
|
||||
"previousErpData",
|
||||
"Retrieve the `previousErpData` to pass it to the compensation function.",
|
||||
],
|
||||
[
|
||||
"21",
|
||||
"updateProductErpData",
|
||||
"Update the product’s ERP data and return the data from the ERP system.",
|
||||
],
|
||||
[
|
||||
"37",
|
||||
"updateProductErpData",
|
||||
"Revert the product's data in the ERP system to its previous state using the `previousErpData`.",
|
||||
],
|
||||
]
|
||||
|
||||
```ts title="src/workflows/update-product-erp/steps/update-erp.ts" highlights={updateErpHighlights} collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { UpdateProductAndErpWorkflowInput } from ".."
|
||||
import ErpModuleService from "../../../modules/erp/service"
|
||||
|
||||
@@ -162,14 +169,12 @@ const updateErp = createStep(
|
||||
const { id, ...updatedData } = input
|
||||
|
||||
// get previous ERP data
|
||||
const previousErpData =
|
||||
await erpModuleService.retrieveProductErpDetails(id)
|
||||
const previousErpData = await erpModuleService.retrieveProductErpDetails(id)
|
||||
|
||||
const updatedErpData =
|
||||
await erpModuleService.updateProductErpData(
|
||||
id,
|
||||
updatedData
|
||||
)
|
||||
const updatedErpData = await erpModuleService.updateProductErpData(
|
||||
id,
|
||||
updatedData
|
||||
)
|
||||
|
||||
return new StepResponse(updatedErpData, {
|
||||
// pass to compensation function
|
||||
@@ -179,13 +184,9 @@ const updateErp = createStep(
|
||||
},
|
||||
// compensation function
|
||||
async ({ previousErpData, productId }, context) => {
|
||||
const erpService: ErpModuleService =
|
||||
context.container.resolve("erpService")
|
||||
const erpService: ErpModuleService = context.container.resolve("erpService")
|
||||
|
||||
await erpService.updateProductErpData(
|
||||
productId,
|
||||
previousErpData
|
||||
)
|
||||
await erpService.updateProductErpData(productId, previousErpData)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -253,10 +254,7 @@ import updateProductAndErpWorkflow, {
|
||||
UpdateProductAndErpWorkflowInput,
|
||||
} from "../../../../../workflows/update-product-erp"
|
||||
|
||||
type ProductErpReq = Omit<
|
||||
UpdateProductAndErpWorkflowInput,
|
||||
"id"
|
||||
>
|
||||
type ProductErpReq = Omit<UpdateProductAndErpWorkflowInput, "id">
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<ProductErpReq>,
|
||||
@@ -268,9 +266,7 @@ export const POST = async (
|
||||
...req.body,
|
||||
}
|
||||
|
||||
const { result } = await updateProductAndErpWorkflow(
|
||||
req.scope
|
||||
).run({
|
||||
const { result } = await updateProductAndErpWorkflow(req.scope).run({
|
||||
input: productData,
|
||||
})
|
||||
|
||||
@@ -280,4 +276,4 @@ export const POST = async (
|
||||
|
||||
In this `POST` API route, you retrieve the product’s ID from the path parameter and the data to update from the request body. You then execute the workflow by passing it the retrieved data as an input.
|
||||
|
||||
The route returns the result of the workflow, which is an object holding both the update product’s details and the ERP details.
|
||||
The route returns the result of the workflow, which is an object holding both the update product’s details and the ERP details.
|
||||
|
||||
@@ -23,10 +23,7 @@ A workflow is considered long-running if at least one step has its `async` confi
|
||||
For example, consider the following workflow and steps:
|
||||
|
||||
```ts title="src/workflows/hello-world.ts" highlights={[["13"]]} collapsibleLines="1-10" expandButtonLabel="Show More"
|
||||
import {
|
||||
createStep,
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStep, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
|
||||
const step1 = createStep("step-1", async () => {
|
||||
// ...
|
||||
@@ -50,19 +47,18 @@ type WorkflowOutput = {
|
||||
message: string
|
||||
}
|
||||
|
||||
const myWorkflow = createWorkflow<
|
||||
{},
|
||||
WorkflowOutput
|
||||
>({
|
||||
name: "hello-world",
|
||||
}, function () {
|
||||
step1()
|
||||
step2()
|
||||
step3()
|
||||
})
|
||||
const myWorkflow = createWorkflow<{}, WorkflowOutput>(
|
||||
{
|
||||
name: "hello-world",
|
||||
},
|
||||
function () {
|
||||
step1()
|
||||
step2()
|
||||
step3()
|
||||
}
|
||||
)
|
||||
|
||||
export default myWorkflow
|
||||
|
||||
```
|
||||
|
||||
The second step has in its configuration object `async` set to true. This indicates that this step is an asynchronous step.
|
||||
@@ -89,26 +85,15 @@ export const highlights = [
|
||||
]
|
||||
|
||||
```ts title="src/api/store/workflows/route.ts" highlights={highlights} collapsibleLines="1-11" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import myWorkflow from "../../../workflows/hello-world"
|
||||
import {
|
||||
IWorkflowEngineService,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const { transaction, result } = await myWorkflow(req.scope)
|
||||
.run()
|
||||
export async function GET(req: MedusaRequest, res: MedusaResponse) {
|
||||
const { transaction, result } = await myWorkflow(req.scope).run()
|
||||
|
||||
const workflowEngine = req.scope.resolve<
|
||||
IWorkflowEngineService
|
||||
>(
|
||||
const workflowEngine = req.scope.resolve<IWorkflowEngineService>(
|
||||
ModuleRegistrationName.WORKFLOW_ENGINE
|
||||
)
|
||||
|
||||
@@ -137,18 +122,20 @@ The `subscribe` method accepts an object having three properties:
|
||||
{
|
||||
name: "workflowId",
|
||||
type: "`string`",
|
||||
description: "The name of the workflow."
|
||||
description: "The name of the workflow.",
|
||||
},
|
||||
{
|
||||
name: "transactionId",
|
||||
type: "`string`",
|
||||
description: "The ID of the workflow exection's transaction. The transaction's details are returned in the response of the workflow execution."
|
||||
description:
|
||||
"The ID of the workflow exection's transaction. The transaction's details are returned in the response of the workflow execution.",
|
||||
},
|
||||
{
|
||||
name: "subscriber",
|
||||
type: "`string`",
|
||||
description: "The function executed when the workflow execution's status changes. The function receives a data object. It has an `eventType` property, which you use to check the status of the workflow execution."
|
||||
}
|
||||
description:
|
||||
"The function executed when the workflow execution's status changes. The function receives a data object. It has an `eventType` property, which you use to check the status of the workflow execution.",
|
||||
},
|
||||
]}
|
||||
sectionTitle="Access Long-Running Workflow Status and Result"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user