feat: CartRegion link, definition + workflow (#6392)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const findOneOrAnyRegionStepId = "find-one-or-any-region"
|
||||
export const findOneOrAnyRegionStep = createStep(
|
||||
findOneOrAnyRegionStepId,
|
||||
async (data: { regionId?: string }, { container }) => {
|
||||
const service = container.resolve<IRegionModuleService>(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
if (!data.regionId) {
|
||||
const regions = await service.list({})
|
||||
|
||||
if (!regions?.length) {
|
||||
throw Error("No regions found")
|
||||
}
|
||||
|
||||
return new StepResponse(regions[0])
|
||||
}
|
||||
|
||||
const region = await service.retrieve(data.regionId)
|
||||
|
||||
return new StepResponse(region)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./create-carts"
|
||||
export * from "./find-one-or-any-region"
|
||||
export * from "./update-carts"
|
||||
|
||||
|
||||
@@ -1,13 +1,31 @@
|
||||
import { CartDTO, CreateCartDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createCartsStep } from "../steps"
|
||||
import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createCartsStep, findOneOrAnyRegionStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { cartData: CreateCartDTO[] }
|
||||
type WorkflowInput = CreateCartWorkflowInputDTO
|
||||
|
||||
export const createCartsWorkflowId = "create-carts"
|
||||
export const createCartsWorkflow = createWorkflow(
|
||||
createCartsWorkflowId,
|
||||
export const createCartWorkflowId = "create-cart"
|
||||
export const createCartWorkflow = createWorkflow(
|
||||
createCartWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CartDTO[]> => {
|
||||
return createCartsStep(input.cartData)
|
||||
const region = findOneOrAnyRegionStep({
|
||||
regionId: input.region_id,
|
||||
})
|
||||
|
||||
const cartInput = transform({ input, region }, (data) => {
|
||||
return {
|
||||
...data.input,
|
||||
currency_code: data?.input.currency_code || data.region.currency_code,
|
||||
region_id: data.region.id,
|
||||
}
|
||||
})
|
||||
|
||||
const cart = createCartsStep([cartInput])
|
||||
|
||||
return cart
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user