Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] develop from medusajs:develop #5

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-spies-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---

fix(core-flows): use useQueryGraphStep instead of useQueryStep
5 changes: 5 additions & 0 deletions .changeset/pink-ways-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/types": patch
---

chore(types): Keep intelisense while being more loosen
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
4 changes: 2 additions & 2 deletions packages/core/core-flows/src/cart/workflows/complete-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -55,7 +55,7 @@ export const completeCartWorkflow = createWorkflow(
(
input: WorkflowData<CompleteCartWorkflowInput>
): WorkflowResponse<{ id: string }> => {
const orderCart = useQueryStep({
const orderCart = useQueryGraphStep({
entity: "order_cart",
fields: ["cart_id", "order_id"],
filters: { cart_id: input.id },
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
21 changes: 0 additions & 21 deletions packages/core/core-flows/src/common/steps/use-query.ts

This file was deleted.

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ 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 {}

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 },
}).config({
name: "payment-query",
})

const paymentSessionResult = useQueryStep({
const paymentSessionResult = useQueryGraphStep({
entity: "payment_session",
fields: ["payment_collection_id"],
filters: { id: input.data?.session_id },
}).config({
name: "payment-session-query",
})

const cartPaymentCollection = useQueryStep({
const cartPaymentCollection = useQueryGraphStep({
entity: "cart_payment_collection",
fields: ["cart_id"],
filters: {
Expand Down
29 changes: 15 additions & 14 deletions packages/core/types/src/modules-sdk/__tests__/remote-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result>().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[]
>()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export type RemoteQueryObjectConfig<TEntry extends string> = {
RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints]
> extends never
? string[]
: ObjectToRemoteQueryFields<
RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints]
>[]
:
| ObjectToRemoteQueryFields<
RemoteQueryEntryPoints[TEntry & keyof RemoteQueryEntryPoints]
>[]
| string[]
}

export type RemoteQueryObjectFromStringResult<
Expand All @@ -36,9 +38,11 @@ export type RemoteQueryInput<TEntry extends string> = {
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.
*/
Expand Down
Loading