From 048620884b5d458b109ac496e7a2056c202bd459 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Wed, 18 Dec 2024 18:12:31 +0100 Subject: [PATCH 1/3] chore(types): Keep intelisense while being more loosen (#10657) **What** Keep the autocompletion for the fields API in Query APIs while being more flexible in order to prevent limitation from our depth limit but also to be more flexible when assigning string[] coming from the API for example. --- .changeset/pink-ways-count.md | 5 ++++ .../__tests__/remote-query.spec.ts | 29 ++++++++++--------- .../remote-query-object-from-string.ts | 16 ++++++---- 3 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 .changeset/pink-ways-count.md diff --git a/.changeset/pink-ways-count.md b/.changeset/pink-ways-count.md new file mode 100644 index 0000000000000..b68038ea58326 --- /dev/null +++ b/.changeset/pink-ways-count.md @@ -0,0 +1,5 @@ +--- +"@medusajs/types": patch +--- + +chore(types): Keep intelisense while being more loosen diff --git a/packages/core/types/src/modules-sdk/__tests__/remote-query.spec.ts b/packages/core/types/src/modules-sdk/__tests__/remote-query.spec.ts index 96614a45b9997..31fb3680861db 100644 --- a/packages/core/types/src/modules-sdk/__tests__/remote-query.spec.ts +++ b/packages/core/types/src/modules-sdk/__tests__/remote-query.spec.ts @@ -14,20 +14,21 @@ describe("RemoteQuery", () => { it("should return the correct type for fields when using a string entry point", () => { type Result = RemoteQueryObjectConfig<"simple_product">["fields"] expectTypeOf().toEqualTypeOf< - ( - | "*" - | "id" - | "handle" - | "title" - | "variants.id" - | "variants.*" - | "sales_channels.id" - | "sales_channels.*" - | "sales_channels.name" - | "sales_channels_link.*" - | "sales_channels_link.product_id" - | "sales_channels_link.sales_channel_id" - )[] + | ( + | "*" + | "id" + | "handle" + | "title" + | "variants.id" + | "variants.*" + | "sales_channels.id" + | "sales_channels.*" + | "sales_channels.name" + | "sales_channels_link.*" + | "sales_channels_link.product_id" + | "sales_channels_link.sales_channel_id" + )[] + | string[] >() }) diff --git a/packages/core/types/src/modules-sdk/remote-query-object-from-string.ts b/packages/core/types/src/modules-sdk/remote-query-object-from-string.ts index 405f142ff3a03..d91279133a877 100644 --- a/packages/core/types/src/modules-sdk/remote-query-object-from-string.ts +++ b/packages/core/types/src/modules-sdk/remote-query-object-from-string.ts @@ -11,9 +11,11 @@ export type RemoteQueryObjectConfig = { RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints] > extends never ? string[] - : ObjectToRemoteQueryFields< - RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints] - >[] + : + | ObjectToRemoteQueryFields< + RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints] + >[] + | string[] } export type RemoteQueryObjectFromStringResult< @@ -36,9 +38,11 @@ export type RemoteQueryInput = { RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints] > extends never ? string[] - : ObjectToRemoteQueryFields< - RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints] - >[] + : + | ObjectToRemoteQueryFields< + RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints] + >[] + | string[] /** * Pagination configurations for the returned list of items. */ From 9d85e663b8bac2240ec3e3bf99377dd0eac72160 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Wed, 18 Dec 2024 19:18:51 +0200 Subject: [PATCH 2/3] fix(core-flows): use useQueryGraphStep instead of useQueryStep (#10643) Replace usage of `useQueryStep` with `useGraphQueryStep` and remove `useQueryStep`, as it's not exported by the package and seems to be a duplicate of `useGraphQueryStep` --- .changeset/heavy-spies-thank.md | 5 +++++ .../src/cart/workflows/complete-cart.ts | 4 ++-- .../core-flows/src/common/steps/use-query.ts | 21 ------------------- .../src/payment/workflows/process-payment.ts | 8 +++---- 4 files changed, 11 insertions(+), 27 deletions(-) create mode 100644 .changeset/heavy-spies-thank.md delete mode 100644 packages/core/core-flows/src/common/steps/use-query.ts diff --git a/.changeset/heavy-spies-thank.md b/.changeset/heavy-spies-thank.md new file mode 100644 index 0000000000000..dbfa3b0872264 --- /dev/null +++ b/.changeset/heavy-spies-thank.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix(core-flows): use useQueryGraphStep instead of useQueryStep diff --git a/packages/core/core-flows/src/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/cart/workflows/complete-cart.ts index b0d199c860eac..0ae1264d86674 100644 --- a/packages/core/core-flows/src/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/complete-cart.ts @@ -18,9 +18,9 @@ import { import { createRemoteLinkStep, emitEventStep, + useQueryGraphStep, useRemoteQueryStep, } from "../../common" -import { useQueryStep } from "../../common/steps/use-query" import { createOrdersStep } from "../../order/steps/create-orders" import { authorizePaymentSessionStep } from "../../payment/steps/authorize-payment-session" import { registerUsageStep } from "../../promotion/steps/register-usage" @@ -55,7 +55,7 @@ export const completeCartWorkflow = createWorkflow( ( input: WorkflowData ): WorkflowResponse<{ id: string }> => { - const orderCart = useQueryStep({ + const orderCart = useQueryGraphStep({ entity: "order_cart", fields: ["cart_id", "order_id"], filters: { cart_id: input.id }, diff --git a/packages/core/core-flows/src/common/steps/use-query.ts b/packages/core/core-flows/src/common/steps/use-query.ts deleted file mode 100644 index 6d28317d79b30..0000000000000 --- a/packages/core/core-flows/src/common/steps/use-query.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { ContainerRegistrationKeys } from "@medusajs/utils" -import { createStep, StepResponse } from "@medusajs/workflows-sdk" - -interface QueryInput { - entity: string - fields: string[] - filters?: Record - context?: any -} - -export const useQueryStepId = "use-query" -export const useQueryStep = createStep( - useQueryStepId, - async (data: QueryInput, { container }) => { - const query = container.resolve(ContainerRegistrationKeys.QUERY) - - const result = await query.graph(data) - - return new StepResponse(result) - } -) diff --git a/packages/core/core-flows/src/payment/workflows/process-payment.ts b/packages/core/core-flows/src/payment/workflows/process-payment.ts index 5cfdf5ee82caf..c2af91e8c1ca5 100644 --- a/packages/core/core-flows/src/payment/workflows/process-payment.ts +++ b/packages/core/core-flows/src/payment/workflows/process-payment.ts @@ -2,9 +2,9 @@ import { WebhookActionResult } from "@medusajs/types" import { PaymentActions } from "@medusajs/utils" import { createWorkflow, when } from "@medusajs/workflows-sdk" import { completeCartWorkflow } from "../../cart/workflows/complete-cart" -import { useQueryStep } from "../../common/steps/use-query" import { authorizePaymentSessionStep } from "../steps" import { capturePaymentWorkflow } from "./capture-payment" +import { useQueryGraphStep } from "../../common" interface ProcessPaymentWorkflowInput extends WebhookActionResult {} @@ -12,7 +12,7 @@ export const processPaymentWorkflowId = "process-payment-workflow" export const processPaymentWorkflow = createWorkflow( processPaymentWorkflowId, (input: ProcessPaymentWorkflowInput) => { - const paymentData = useQueryStep({ + const paymentData = useQueryGraphStep({ entity: "payment", fields: ["id"], filters: { payment_session_id: input.data?.session_id }, @@ -20,7 +20,7 @@ export const processPaymentWorkflow = createWorkflow( name: "payment-query", }) - const paymentSessionResult = useQueryStep({ + const paymentSessionResult = useQueryGraphStep({ entity: "payment_session", fields: ["payment_collection_id"], filters: { id: input.data?.session_id }, @@ -28,7 +28,7 @@ export const processPaymentWorkflow = createWorkflow( name: "payment-session-query", }) - const cartPaymentCollection = useQueryStep({ + const cartPaymentCollection = useQueryGraphStep({ entity: "cart_payment_collection", fields: ["cart_id"], filters: { From 3dba551ad2c2bbcd5c443a769779e4a5fc0ed6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Wed, 18 Dec 2024 21:23:06 +0100 Subject: [PATCH 3/3] fix(core-flows): data passed to to fulfillment provider context (#10660) * fix: cart data passed to context, from location in validation * refactor: extract common fields to a const --- .../core/core-flows/src/cart/utils/fields.ts | 26 +++++++++++++++++ .../workflows/add-shipping-method-to-cart.ts | 6 +++- ...-shipping-options-for-cart-with-pricing.ts | 29 +++++++++++++------ .../list-shipping-options-for-cart.ts | 13 ++------- .../calculate-shipping-options-prices.ts | 3 +- 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/packages/core/core-flows/src/cart/utils/fields.ts b/packages/core/core-flows/src/cart/utils/fields.ts index 410e4067a2366..4a8d54476ae66 100644 --- a/packages/core/core-flows/src/cart/utils/fields.ts +++ b/packages/core/core-flows/src/cart/utils/fields.ts @@ -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", @@ -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.*", @@ -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.*", +] diff --git a/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts b/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts index 65329c7a6549e..7c0f15282f441 100644 --- a/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts @@ -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( diff --git a/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart-with-pricing.ts b/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart-with-pricing.ts index 5c5597d08bdf5..a0f05eea0f9ce 100644 --- a/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart-with-pricing.ts +++ b/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart-with-pricing.ts @@ -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", @@ -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", ], @@ -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 @@ -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 + ], } }), ] diff --git a/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts b/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts index ac41df245b479..dbcb3084b4409 100644 --- a/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts @@ -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() - 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, } } ) @@ -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, } }) ) diff --git a/packages/core/core-flows/src/fulfillment/workflows/calculate-shipping-options-prices.ts b/packages/core/core-flows/src/fulfillment/workflows/calculate-shipping-options-prices.ts index 48e0aab43877f..9087eb8ce9324 100644 --- a/packages/core/core-flows/src/fulfillment/workflows/calculate-shipping-options-prices.ts +++ b/packages/core/core-flows/src/fulfillment/workflows/calculate-shipping-options-prices.ts @@ -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" @@ -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(