Skip to content

Commit

Permalink
fix(core-flows): data passed to to fulfillment provider context (#10660)
Browse files Browse the repository at this point in the history
* fix: cart data passed to context, from location in validation

* refactor: extract common fields to a const
  • Loading branch information
fPolic authored Dec 18, 2024
1 parent 9d85e66 commit 3dba551
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
26 changes: 26 additions & 0 deletions packages/core/core-flows/src/cart/utils/fields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Always ensure that cartFieldsForPricingContext is present in cartFieldsForRefreshSteps
// Always ensure that cartFieldsForCalculateShippingOptionsPrices is present in cartFieldsForRefreshSteps
export const cartFieldsForRefreshSteps = [
"id",
"currency_code",
Expand All @@ -18,6 +19,13 @@ export const cartFieldsForRefreshSteps = [
"items.product.collection_id",
"items.product.categories.id",
"items.product.tags.id",
"items.variant.id",
"items.variant.product.id",
"items.variant.weight",
"items.variant.length",
"items.variant.height",
"items.variant.width",
"items.variant.material",
"items.adjustments.*",
"items.tax_lines.*",
"shipping_address.*",
Expand Down Expand Up @@ -146,3 +154,21 @@ export const productVariantsFields = [
"inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
"inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
]

// ensure that at least these fields are present when fetching cart for caluclating shipping options prices
export const cartFieldsForCalculateShippingOptionsPrices = [
"id",
"items.*",
"items.variant.id",
"items.variant.product.id",
"items.variant.weight",
"items.variant.length",
"items.variant.height",
"items.variant.width",
"items.variant.material",
"items.product.id",
"items.product.collection_id",
"items.product.categories.id",
"items.product.tags.id",
"shipping_address.*",
]
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export const addShippingMethodToCartWorkflow = createWorkflow(
)

const shippingMethodInput = transform(
{ input, shippingOptions, validatedMethodData },
{
input,
shippingOptions,
validatedMethodData,
},
(data) => {
const options = (data.input.options ?? []).map((option) => {
const shippingOption = data.shippingOptions.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { CalculateShippingOptionPriceDTO } from "@medusajs/types"

import { useQueryGraphStep, validatePresenceOfStep } from "../../common"
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
import { calculateShippingOptionsPricesStep } from "../../fulfillment"
import { CalculateShippingOptionPriceDTO } from "@medusajs/types"
import { cartFieldsForCalculateShippingOptionsPrices } from "../utils/fields"

const COMMON_OPTIONS_FIELDS = [
"id",
Expand Down Expand Up @@ -57,15 +59,10 @@ export const listShippingOptionsForCartWithPricingWorkflow = createWorkflow(
entity: "cart",
filters: { id: input.cart_id },
fields: [
"id",
...cartFieldsForCalculateShippingOptionsPrices,
"sales_channel_id",
"currency_code",
"region_id",
"shipping_address.city",
"shipping_address.country_code",
"shipping_address.province",
"shipping_address.postal_code",
"items.*",
"item_total",
"total",
],
Expand Down Expand Up @@ -245,8 +242,18 @@ export const listShippingOptionsForCartWithPricingWorkflow = createWorkflow(
)

const shippingOptionsWithPrice = transform(
{ shippingOptionsFlatRate, shippingOptionsCalculated, prices },
({ shippingOptionsFlatRate, shippingOptionsCalculated, prices }) => {
{
shippingOptionsFlatRate,
shippingOptionsCalculated,
prices,
fulfillmentSetLocationMap,
},
({
shippingOptionsFlatRate,
shippingOptionsCalculated,
prices,
fulfillmentSetLocationMap,
}) => {
return [
...shippingOptionsFlatRate.map((shippingOption) => {
const price = shippingOption.calculated_price
Expand All @@ -264,6 +271,10 @@ export const listShippingOptionsForCartWithPricingWorkflow = createWorkflow(
is_tax_inclusive:
prices[index]?.is_calculated_price_tax_inclusive,
calculated_price: prices[index],
stock_location:
fulfillmentSetLocationMap[
shippingOption.service_zone.fulfillment_set_id
],
}
}),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,19 @@ export const listShippingOptionsForCartWorkflow = createWorkflow(
({ scFulfillmentSetQuery }) => scFulfillmentSetQuery.data[0]
)

const { fulfillmentSetIds, fulfillmentSetLocationMap } = transform(
const { fulfillmentSetIds } = transform(
{ scFulfillmentSets },
({ scFulfillmentSets }) => {
const fulfillmentSetIds = new Set<string>()
const fulfillmentSetLocationMap = {}

scFulfillmentSets.stock_locations.forEach((stockLocation) => {
stockLocation.fulfillment_sets.forEach((fulfillmentSet) => {
fulfillmentSetLocationMap[fulfillmentSet.id] = stockLocation
fulfillmentSetIds.add(fulfillmentSet.id)
})
})

return {
fulfillmentSetIds: Array.from(fulfillmentSetIds),
fulfillmentSetLocationMap,
}
}
)
Expand Down Expand Up @@ -130,19 +127,15 @@ export const listShippingOptionsForCartWorkflow = createWorkflow(
}).config({ name: "shipping-options-query" })

const shippingOptionsWithPrice = transform(
{ shippingOptions, fulfillmentSetLocationMap },
({ shippingOptions, fulfillmentSetLocationMap }) =>
{ shippingOptions },
({ shippingOptions }) =>
shippingOptions.map((shippingOption) => {
const price = shippingOption.calculated_price
const fulfillmentSetId =
shippingOption.service_zone.fulfillment_set_id
const stockLocation = fulfillmentSetLocationMap[fulfillmentSetId]

return {
...shippingOption,
amount: price?.calculated_amount,
is_tax_inclusive: !!price?.is_calculated_price_tax_inclusive,
stock_location: stockLocation,
}
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@medusajs/framework/workflows-sdk"
import { calculateShippingOptionsPricesStep } from "../steps"
import { useQueryGraphStep } from "../../common"
import { cartFieldsForCalculateShippingOptionsPrices } from "../../cart/utils/fields"

export const calculateShippingOptionsPricesWorkflowId =
"calculate-shipping-options-prices-workflow"
Expand All @@ -31,7 +32,7 @@ export const calculateShippingOptionsPricesWorkflow = createWorkflow(
const cartQuery = useQueryGraphStep({
entity: "cart",
filters: { id: input.cart_id },
fields: ["id", "items.*", "shipping_address.*"],
fields: cartFieldsForCalculateShippingOptionsPrices,
}).config({ name: "cart-query" })

const fulfillmentSetId = transform(
Expand Down

0 comments on commit 3dba551

Please sign in to comment.