feat: Add create location fulfillment set flow + API (#6945)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CreateFulfillmentSetDTO,
|
||||
IFulfillmentModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createFulfillmentSetsId = "create-fulfillment-sets"
|
||||
export const createFulfillmentSets = createStep(
|
||||
createFulfillmentSetsId,
|
||||
async (data: CreateFulfillmentSetDTO[], { container }) => {
|
||||
const service = container.resolve<IFulfillmentModuleService>(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
const createSets = await service.create(data)
|
||||
|
||||
return new StepResponse(
|
||||
createSets,
|
||||
createSets.map((createdSet) => createdSet.id)
|
||||
)
|
||||
},
|
||||
async (createSetIds, { container }) => {
|
||||
if (!createSetIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IFulfillmentModuleService>(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
await service.delete(createSetIds)
|
||||
}
|
||||
)
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./add-rules-to-fulfillment-shipping-option"
|
||||
export * from "./create-fulfillment-set"
|
||||
export * from "./remove-rules-from-fulfillment-shipping-option"
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
interface StepInput {
|
||||
input: {
|
||||
location_id: string
|
||||
fulfillment_set_ids: string[]
|
||||
}[]
|
||||
}
|
||||
|
||||
export const associateFulfillmentSetsWithLocationStepId =
|
||||
"associate-fulfillment-sets-with-location-step"
|
||||
export const associateFulfillmentSetsWithLocationStep = createStep(
|
||||
associateFulfillmentSetsWithLocationStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
if (!data.input.length) {
|
||||
return new StepResponse([], [])
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const links = data.input
|
||||
.map((link) => {
|
||||
return link.fulfillment_set_ids.map((id) => {
|
||||
return {
|
||||
[Modules.FULFILLMENT]: {
|
||||
fulfillment_set_id: id,
|
||||
},
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: link.location_id,
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
.flat()
|
||||
|
||||
const createdLinks = await remoteLink.create(links)
|
||||
|
||||
return new StepResponse(createdLinks, links)
|
||||
},
|
||||
async (links, { container }) => {
|
||||
if (!links?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
await remoteLink.dismiss(links)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
import { CreateLocationFulfillmentSetWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createFulfillmentSets } from "../../fulfillment"
|
||||
import { associateFulfillmentSetsWithLocationStep } from "../steps/associate-locations-with-fulfillment-sets"
|
||||
|
||||
export const createLocationFulfillmentSetWorkflowId =
|
||||
"create-location-fulfillment-set"
|
||||
export const createLocationFulfillmentSetWorkflow = createWorkflow(
|
||||
createLocationFulfillmentSetWorkflowId,
|
||||
(input: WorkflowData<CreateLocationFulfillmentSetWorkflowInputDTO>) => {
|
||||
const fulfillmentSet = createFulfillmentSets([
|
||||
{
|
||||
name: input.fulfillment_set_data.name,
|
||||
type: input.fulfillment_set_data.type,
|
||||
},
|
||||
])
|
||||
|
||||
const data = transform({ input, fulfillmentSet }, (data) => [
|
||||
{
|
||||
location_id: data.input.location_id,
|
||||
fulfillment_set_ids: [data.fulfillmentSet[0].id],
|
||||
},
|
||||
])
|
||||
|
||||
associateFulfillmentSetsWithLocationStep({
|
||||
input: data,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./create-location-fulfillment-set"
|
||||
export * from "./create-stock-locations"
|
||||
export * from "./update-stock-locations"
|
||||
export * from "./delete-stock-locations"
|
||||
export * from "./update-stock-locations"
|
||||
|
||||
Reference in New Issue
Block a user