Skip to content

Commit

Permalink
feat(fulfillment, core-flows): Apply correct address and context + cl…
Browse files Browse the repository at this point in the history
…eanup (#7230)

The tests will be added in subsequent requests where the worklows are being worked on
  • Loading branch information
adrien2p committed May 3, 2024
1 parent 4fe28f5 commit 520867b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
6 changes: 6 additions & 0 deletions .changeset/quick-beans-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/fulfillment": patch
"@medusajs/core-flows": patch
---

feat(fulfillment, core-flows): Apply correct address and context + cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,41 +137,16 @@ 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(
filters: FulfillmentTypes.FilterableShippingOptionForContextProps = {},
config: FindConfig<FulfillmentTypes.ShippingOptionDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<FulfillmentTypes.ShippingOptionDTO[]> {
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,
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 520867b

Please sign in to comment.