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
This commit is contained in:
Adrien de Peretti
2024-05-03 15:44:22 +00:00
committed by GitHub
parent 4fe28f5a95
commit 520867b074
3 changed files with 25 additions and 42 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/fulfillment": patch
"@medusajs/core-flows": patch
---
feat(fulfillment, core-flows): Apply correct address and context + cleanup
@@ -46,7 +46,7 @@ export const listShippingOptionsForCartWorkflow = createWorkflow(
variables: { variables: {
id: input.sales_channel_id, id: input.sales_channel_id,
"stock_locations.fulfillment_sets.service_zones.shipping_options": { "stock_locations.fulfillment_sets.service_zones.shipping_options": {
context: { filters: {
address: { address: {
city: input.shipping_address?.city, city: input.shipping_address?.city,
country_code: input.shipping_address?.country_code, country_code: input.shipping_address?.country_code,
@@ -137,29 +137,6 @@ export default class FulfillmentModuleService<
return joinerConfig 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_") @InjectManager("baseRepository_")
// @ts-ignore // @ts-ignore
async listShippingOptions( async listShippingOptions(
@@ -167,11 +144,9 @@ export default class FulfillmentModuleService<
config: FindConfig<FulfillmentTypes.ShippingOptionDTO> = {}, config: FindConfig<FulfillmentTypes.ShippingOptionDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<FulfillmentTypes.ShippingOptionDTO[]> { ): Promise<FulfillmentTypes.ShippingOptionDTO[]> {
const optionsContext = this.setupShippingOptionsConfig_(filters, config) // 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 (optionsContext) { if (filters?.context || filters?.address) {
filters.context = optionsContext
return await this.listShippingOptionsForContext( return await this.listShippingOptionsForContext(
filters, filters,
config, config,
@@ -1698,21 +1673,23 @@ export default class FulfillmentModuleService<
const geoZoneConstraints = const geoZoneConstraints =
FulfillmentModuleService.buildGeoZoneConstraintsFromAddress(address) FulfillmentModuleService.buildGeoZoneConstraintsFromAddress(address)
normalizedFilters = { if (geoZoneConstraints.length) {
...normalizedFilters, normalizedFilters = {
service_zone: { ...normalizedFilters,
...(normalizedFilters.service_zone ?? {}), service_zone: {
geo_zones: { ...(normalizedFilters.service_zone ?? {}),
$or: geoZoneConstraints.map((geoZoneConstraint) => ({ geo_zones: {
// Apply eventually provided constraints on the geo zone along side the address constraints $or: geoZoneConstraints.map((geoZoneConstraint) => ({
...(normalizedFilters.service_zone?.geo_zones ?? {}), // Apply eventually provided constraints on the geo zone along side the address constraints
...geoZoneConstraint, ...(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)) normalizedConfig.relations = Array.from(new Set(normalizedConfig.relations))