chore: move ModuleRegistrationName to utils (#7911)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-03 06:30:56 -03:00
committed by GitHub
parent 46f15b4909
commit a7844efd09
339 changed files with 7203 additions and 7620 deletions

View File

@@ -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 products 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 products data using the `previousProductData` passed from the step to the compensation function."]
[
"39",
"",
"Revert the products 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 products 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 products 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 products 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 products details and the ERP details.
The route returns the result of the workflow, which is an object holding both the update products details and the ERP details.