From 6e5912612c33a123e0a4e1f605ea6149d9fa3006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:32:25 +0100 Subject: [PATCH] fix(core-flows, dashboard): handling fulfillment rules (#11111) **What** - fix storing "true" | "false" strings from admin - use strings instead of booleans for context filtering --- .../create-shipping-options-form.tsx | 4 +- ...-shipping-options-for-cart-with-pricing.ts | 30 +++++---- .../list-shipping-options-for-cart.ts | 65 ++++++++++--------- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx b/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx index 838fae3740..07dc491ddf 100644 --- a/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx +++ b/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx @@ -151,13 +151,13 @@ export function CreateShippingOptionsForm({ rules: [ { // eslint-disable-next-line - value: isReturn ? '"true"' : '"false"', + value: isReturn ? "true" : "false", attribute: "is_return", operator: "eq", }, { // eslint-disable-next-line - value: data.enabled_in_store ? '"true"' : '"false"', + value: data.enabled_in_store ? "true" : "false", attribute: "enabled_in_store", operator: "eq", }, 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 a8b00dc037..1fc550029e 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 @@ -1,4 +1,4 @@ -import { ShippingOptionPriceType } from "@medusajs/framework/utils" +import { isDefined, ShippingOptionPriceType } from "@medusajs/framework/utils" import { createWorkflow, parallelize, @@ -6,8 +6,8 @@ import { WorkflowData, WorkflowResponse, } from "@medusajs/framework/workflows-sdk" -import { - CalculateShippingOptionPriceDTO, +import { + CalculateShippingOptionPriceDTO, ListShippingOptionsForCartWithPricingWorkflowInput, } from "@medusajs/types" @@ -44,13 +44,13 @@ export const listShippingOptionsForCartWithPricingWorkflowId = /** * This workflow lists shipping options that can be used during checkout for a cart. It also retrieves the prices * of these shipping options, including calculated prices that may be retrieved from third-party providers. - * + * * This workflow is executed in other cart-related workflows, such as {@link addShippingMethodToCartWorkflow} to retrieve the * price of the shipping method being added to the cart. - * - * You can use this workflow within your own customizations or custom workflows, allowing you to retrieve the shipping options of a cart and their prices + * + * You can use this workflow within your own customizations or custom workflows, allowing you to retrieve the shipping options of a cart and their prices * in your custom flows. - * + * * @example * const { result } = await listShippingOptionsForCartWithPricingWorkflow(container) * .run({ @@ -66,16 +66,14 @@ export const listShippingOptionsForCartWithPricingWorkflowId = * ] * } * }) - * + * * @summary - * + * * List a cart's shipping options with prices. */ export const listShippingOptionsForCartWithPricingWorkflow = createWorkflow( listShippingOptionsForCartWithPricingWorkflowId, - ( - input: WorkflowData - ) => { + (input: WorkflowData) => { const optionIds = transform({ input }, ({ input }) => (input.options ?? []).map(({ id }) => id) ) @@ -141,8 +139,12 @@ export const listShippingOptionsForCartWithPricingWorkflow = createWorkflow( { input, cart, fulfillmentSetIds }, ({ input, cart, fulfillmentSetIds }) => ({ context: { - is_return: input.is_return ?? false, - enabled_in_store: input.enabled_in_store ?? true, + is_return: input.is_return ? "true" : "false", + enabled_in_store: !isDefined(input.enabled_in_store) + ? "true" + : input.enabled_in_store + ? "true" + : "false", }, filters: { 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 18193c986a..2a177673f6 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 @@ -8,23 +8,24 @@ import { useQueryGraphStep, validatePresenceOfStep } from "../../common" import { useRemoteQueryStep } from "../../common/steps/use-remote-query" import { cartFieldsForPricingContext } from "../utils/fields" import { ListShippingOptionsForCartWorkflowInput } from "@medusajs/types" +import { isDefined } from "@medusajs/framework/utils" export const listShippingOptionsForCartWorkflowId = "list-shipping-options-for-cart" /** - * This workflow lists the shipping options of a cart. It's executed by the + * This workflow lists the shipping options of a cart. It's executed by the * [List Shipping Options Store API Route](https://docs.medusajs.com/api/store#shipping-options_getshippingoptions). - * + * * :::note - * + * * This workflow doesn't retrieve the calculated prices of the shipping options. If you need to retrieve the prices of the shipping options, * use the {@link listShippingOptionsForCartWithPricingWorkflow} workflow. - * + * * ::: - * - * You can use this workflow within your own customizations or custom workflows, allowing you to wrap custom logic around to retrieve the shipping options of a cart + * + * You can use this workflow within your own customizations or custom workflows, allowing you to wrap custom logic around to retrieve the shipping options of a cart * in your custom flows. - * + * * @example * const { result } = await listShippingOptionsForCartWorkflow(container) * .run({ @@ -33,16 +34,14 @@ export const listShippingOptionsForCartWorkflowId = * option_ids: ["so_123"] * } * }) - * + * * @summary - * + * * List a cart's shipping options. */ export const listShippingOptionsForCartWorkflow = createWorkflow( listShippingOptionsForCartWorkflowId, - ( - input: WorkflowData - ) => { + (input: WorkflowData) => { const cartQuery = useQueryGraphStep({ entity: "cart", filters: { id: input.cart_id }, @@ -92,27 +91,33 @@ export const listShippingOptionsForCartWorkflow = createWorkflow( const queryVariables = transform( { input, fulfillmentSetIds, cart }, - ({ input, fulfillmentSetIds, cart }) => ({ - id: input.option_ids, + ({ input, fulfillmentSetIds, cart }) => { + return { + id: input.option_ids, - context: { - is_return: input.is_return ?? false, - enabled_in_store: input.enabled_in_store ?? true, - }, - - filters: { - fulfillment_set_id: fulfillmentSetIds, - - address: { - country_code: cart.shipping_address?.country_code, - province_code: cart.shipping_address?.province, - city: cart.shipping_address?.city, - postal_expression: cart.shipping_address?.postal_code, + context: { + is_return: input.is_return ? "true" : "false", + enabled_in_store: !isDefined(input.enabled_in_store) + ? "true" + : input.enabled_in_store + ? "true" + : "false", }, - }, - calculated_price: { context: cart }, - }) + filters: { + fulfillment_set_id: fulfillmentSetIds, + + address: { + country_code: cart.shipping_address?.country_code, + province_code: cart.shipping_address?.province, + city: cart.shipping_address?.city, + postal_expression: cart.shipping_address?.postal_code, + }, + }, + + calculated_price: { context: cart }, + } + } ) const shippingOptions = useRemoteQueryStep({