feat(medusa,fulfillment): pass stock location data to fulfillment provider (#9322)
**What** - Fetches the stock location's details when creating a fulfillment and return fulfillment. - Passes the data to the fulfillment module, which in turn passes it to the fulfillment provider. **Why** - When creating a fulfillment in a multi-location setup the fulfillment provider will need to know where the package is being sent from (so the shipping service can pick it up). - Previously, we didn't pass anything but the location id to the fulfillment provider. Because the fulfillment provider can't have a dependency on the stock location module this was not sufficient. - This change ensures there is enough data passed to the fulfillment provider to build integrations properly.
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import { FulfillmentDTO, FulfillmentWorkflow } from "@medusajs/framework/types"
|
||||
import {
|
||||
FulfillmentDTO,
|
||||
FulfillmentWorkflow,
|
||||
StockLocationDTO,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { createFulfillmentStep } from "../steps"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
|
||||
export const createFulfillmentWorkflowId = "create-fulfillment-workflow"
|
||||
/**
|
||||
@@ -15,6 +21,47 @@ export const createFulfillmentWorkflow = createWorkflow(
|
||||
(
|
||||
input: WorkflowData<FulfillmentWorkflow.CreateFulfillmentWorkflowInput>
|
||||
): WorkflowResponse<FulfillmentDTO> => {
|
||||
return new WorkflowResponse(createFulfillmentStep(input))
|
||||
const location: StockLocationDTO = useRemoteQueryStep({
|
||||
entry_point: "stock_location",
|
||||
fields: [
|
||||
"id",
|
||||
"name",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"address.id",
|
||||
"address.address_1",
|
||||
"address.address_2",
|
||||
"address.city",
|
||||
"address.country_code",
|
||||
"address.phone",
|
||||
"address.province",
|
||||
"address.postal_code",
|
||||
"address.metadata",
|
||||
],
|
||||
variables: { id: input.location_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "get-location" })
|
||||
|
||||
const stepInput = transform({ input, location }, ({ input, location }) => {
|
||||
return {
|
||||
...input,
|
||||
location,
|
||||
}
|
||||
})
|
||||
|
||||
// When we have support for hooks with a return this would be a great
|
||||
// place to put a hook for people to collect additional data they would
|
||||
// like to pass down to the provider.
|
||||
//
|
||||
// const providerDataHook = createHook("getProviderData", stepInput)
|
||||
//
|
||||
// The collected provider data would be passed to createFulfillment in a
|
||||
// additional_provider_data: Record<string, unknown> field.
|
||||
|
||||
const result = createFulfillmentStep(stepInput)
|
||||
|
||||
return new WorkflowResponse(result)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { FulfillmentDTO, FulfillmentWorkflow } from "@medusajs/framework/types"
|
||||
import {
|
||||
FulfillmentDTO,
|
||||
FulfillmentWorkflow,
|
||||
StockLocationDTO,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { createReturnFulfillmentStep } from "../steps"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
|
||||
export const createReturnFulfillmentWorkflowId =
|
||||
"create-return-fulfillment-workflow"
|
||||
@@ -16,6 +22,38 @@ export const createReturnFulfillmentWorkflow = createWorkflow(
|
||||
(
|
||||
input: WorkflowData<FulfillmentWorkflow.CreateFulfillmentWorkflowInput>
|
||||
): WorkflowResponse<FulfillmentDTO> => {
|
||||
return new WorkflowResponse(createReturnFulfillmentStep(input))
|
||||
const location: StockLocationDTO = useRemoteQueryStep({
|
||||
entry_point: "stock_location",
|
||||
fields: [
|
||||
"id",
|
||||
"name",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"address.id",
|
||||
"address.address_1",
|
||||
"address.address_2",
|
||||
"address.city",
|
||||
"address.country_code",
|
||||
"address.phone",
|
||||
"address.province",
|
||||
"address.postal_code",
|
||||
"address.metadata",
|
||||
],
|
||||
variables: { id: input.location_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "get-location" })
|
||||
|
||||
const stepInput = transform({ input, location }, ({ input, location }) => {
|
||||
return {
|
||||
...input,
|
||||
location,
|
||||
}
|
||||
})
|
||||
|
||||
const result = createReturnFulfillmentStep(stepInput)
|
||||
|
||||
return new WorkflowResponse(result)
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user