From 520867b074c9b1b77ad78388d6d326058c7d8195 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Fri, 3 May 2024 17:44:22 +0200 Subject: [PATCH] feat(fulfillment, core-flows): Apply correct address and context + cleanup (#7230) The tests will be added in subsequent requests where the worklows are being worked on --- .changeset/quick-beans-share.md | 6 ++ .../list-shipping-options-for-cart.ts | 2 +- .../services/fulfillment-module-service.ts | 59 ++++++------------- 3 files changed, 25 insertions(+), 42 deletions(-) create mode 100644 .changeset/quick-beans-share.md diff --git a/.changeset/quick-beans-share.md b/.changeset/quick-beans-share.md new file mode 100644 index 0000000000..fc76d78465 --- /dev/null +++ b/.changeset/quick-beans-share.md @@ -0,0 +1,6 @@ +--- +"@medusajs/fulfillment": patch +"@medusajs/core-flows": patch +--- + +feat(fulfillment, core-flows): Apply correct address and context + cleanup diff --git a/packages/core/core-flows/src/definition/cart/workflows/list-shipping-options-for-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/list-shipping-options-for-cart.ts index 44e5552f60..377039d72f 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/list-shipping-options-for-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/list-shipping-options-for-cart.ts @@ -46,7 +46,7 @@ export const listShippingOptionsForCartWorkflow = createWorkflow( variables: { id: input.sales_channel_id, "stock_locations.fulfillment_sets.service_zones.shipping_options": { - context: { + filters: { address: { city: input.shipping_address?.city, country_code: input.shipping_address?.country_code, diff --git a/packages/modules/fulfillment/src/services/fulfillment-module-service.ts b/packages/modules/fulfillment/src/services/fulfillment-module-service.ts index 85ebae3f1e..0aa40c53c9 100644 --- a/packages/modules/fulfillment/src/services/fulfillment-module-service.ts +++ b/packages/modules/fulfillment/src/services/fulfillment-module-service.ts @@ -137,29 +137,6 @@ export default class FulfillmentModuleService< return joinerConfig } - private setupShippingOptionsConfig_( - filters, - config - ): - | FulfillmentTypes.FilterableShippingOptionForContextProps["context"] - | undefined { - const fieldIdx = config.relations?.indexOf("shipping_options_context") - const shouldCalculatePrice = fieldIdx > -1 - - const shippingOptionsContext = filters.context ?? {} - - delete filters.context - - if (!shouldCalculatePrice) { - return - } - - // cleanup virtual field "shipping_options_context" - config.relations?.splice(fieldIdx, 1) - - return shippingOptionsContext - } - @InjectManager("baseRepository_") // @ts-ignore async listShippingOptions( @@ -167,11 +144,9 @@ export default class FulfillmentModuleService< config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise { - const optionsContext = this.setupShippingOptionsConfig_(filters, config) - - if (optionsContext) { - filters.context = optionsContext - + // Eventually, we could call normalizeListShippingOptionsForContextParams to translate the address and make a and condition with the other filters + // In that case we could remote the address check below + if (filters?.context || filters?.address) { return await this.listShippingOptionsForContext( filters, config, @@ -1698,21 +1673,23 @@ export default class FulfillmentModuleService< const geoZoneConstraints = FulfillmentModuleService.buildGeoZoneConstraintsFromAddress(address) - normalizedFilters = { - ...normalizedFilters, - service_zone: { - ...(normalizedFilters.service_zone ?? {}), - geo_zones: { - $or: geoZoneConstraints.map((geoZoneConstraint) => ({ - // Apply eventually provided constraints on the geo zone along side the address constraints - ...(normalizedFilters.service_zone?.geo_zones ?? {}), - ...geoZoneConstraint, - })), + if (geoZoneConstraints.length) { + normalizedFilters = { + ...normalizedFilters, + service_zone: { + ...(normalizedFilters.service_zone ?? {}), + geo_zones: { + $or: geoZoneConstraints.map((geoZoneConstraint) => ({ + // Apply eventually provided constraints on the geo zone along side the address constraints + ...(normalizedFilters.service_zone?.geo_zones ?? {}), + ...geoZoneConstraint, + })), + }, }, - }, - } + } - normalizedConfig.relations.push("service_zone.geo_zones") + normalizedConfig.relations.push("service_zone.geo_zones") + } } normalizedConfig.relations = Array.from(new Set(normalizedConfig.relations))