feat(medusa, core-flows, fulfillment): calculate SO price endpoint (#10532)
**What** - endpoint + flow for fetching calculated price for a shipping option --- CLOSES CMRC-777
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import {
|
||||
CalculateShippingOptionPriceDTO,
|
||||
IFulfillmentModuleService,
|
||||
} from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
export const calculateShippingOptionsPricesStepId =
|
||||
"calculate-shipping-options-prices"
|
||||
/**
|
||||
* This step calculates the prices for one or more shipping options.
|
||||
*/
|
||||
export const calculateShippingOptionsPricesStep = createStep(
|
||||
calculateShippingOptionsPricesStepId,
|
||||
async (input: CalculateShippingOptionPriceDTO[], { container }) => {
|
||||
const service = container.resolve<IFulfillmentModuleService>(
|
||||
Modules.FULFILLMENT
|
||||
)
|
||||
|
||||
const prices = await service.calculateShippingOptionsPrices(input)
|
||||
|
||||
return new StepResponse(prices)
|
||||
}
|
||||
)
|
||||
@@ -15,3 +15,4 @@ export * from "./update-fulfillment"
|
||||
export * from "./update-shipping-profiles"
|
||||
export * from "./upsert-shipping-options"
|
||||
export * from "./validate-shipment"
|
||||
export * from "./calculate-shipping-options-prices"
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
import { FulfillmentWorkflow } from "@medusajs/framework/types"
|
||||
import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { calculateShippingOptionsPricesStep } from "../steps"
|
||||
import { useQueryGraphStep } from "../../common"
|
||||
|
||||
export const calculateShippingOptionsPricesWorkflowId =
|
||||
"calculate-shipping-options-prices-workflow"
|
||||
/**
|
||||
* This workflow calculates the prices for one or more shipping options.
|
||||
*/
|
||||
export const calculateShippingOptionsPricesWorkflow = createWorkflow(
|
||||
calculateShippingOptionsPricesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<FulfillmentWorkflow.CalculateShippingOptionsPricesWorkflowInput>
|
||||
): WorkflowResponse<FulfillmentWorkflow.CalculateShippingOptionsPricesWorkflowOutput> => {
|
||||
const ids = transform({ input }, ({ input }) =>
|
||||
input.shipping_options.map((so) => so.id)
|
||||
)
|
||||
|
||||
const shippingOptionsQuery = useQueryGraphStep({
|
||||
entity: "shipping_option",
|
||||
filters: { id: ids },
|
||||
fields: ["id", "provider_id", "data"],
|
||||
}).config({ name: "shipping-options-query" })
|
||||
|
||||
const cartQuery = useQueryGraphStep({
|
||||
entity: "cart",
|
||||
filters: { id: input.cart_id },
|
||||
fields: ["id", "items.*", "shipping_address.*"],
|
||||
}).config({ name: "cart-query" })
|
||||
|
||||
const data = transform(
|
||||
{ shippingOptionsQuery, cartQuery, input },
|
||||
({ shippingOptionsQuery, cartQuery, input }) => {
|
||||
const shippingOptions = shippingOptionsQuery.data
|
||||
const cart = cartQuery.data[0]
|
||||
|
||||
const shippingOptionDataMap = new Map(
|
||||
input.shipping_options.map((so) => [so.id, so.data])
|
||||
)
|
||||
|
||||
return shippingOptions.map((shippingOption) => ({
|
||||
id: shippingOption.id,
|
||||
provider_id: shippingOption.provider_id,
|
||||
optionData: shippingOption.data,
|
||||
data: shippingOptionDataMap.get(shippingOption.id) ?? {},
|
||||
context: {
|
||||
cart,
|
||||
},
|
||||
}))
|
||||
}
|
||||
)
|
||||
|
||||
const prices = calculateShippingOptionsPricesStep(data)
|
||||
|
||||
return new WorkflowResponse(prices)
|
||||
}
|
||||
)
|
||||
@@ -14,3 +14,4 @@ export * from "./update-fulfillment"
|
||||
export * from "./update-service-zones"
|
||||
export * from "./update-shipping-options"
|
||||
export * from "./update-shipping-profiles"
|
||||
export * from "./calculate-shipping-options-prices"
|
||||
|
||||
Reference in New Issue
Block a user