fix: Investigate geo zones with address checks (#8066)
* fix: Investigate geo zones with address checks * rm debug * fix constraint building checks * add more explanation
This commit is contained in:
1 parent
8a548cbc2f
commit
143847ace4
2 files changed
+45
-24
No files matched your search
+40
@@ -226,6 +226,10 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
|||||||
{
|
{
|
||||||
name: "test",
|
name: "test",
|
||||||
geo_zones: [
|
geo_zones: [
|
||||||
|
{
|
||||||
|
type: GeoZoneType.COUNTRY,
|
||||||
|
country_code: "fr",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: GeoZoneType.ZIP,
|
type: GeoZoneType.ZIP,
|
||||||
country_code: "fr",
|
country_code: "fr",
|
||||||
@@ -304,6 +308,42 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
|
|||||||
country_code: "fr",
|
country_code: "fr",
|
||||||
province_code: "rhone",
|
province_code: "rhone",
|
||||||
city: "paris",
|
city: "paris",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(shippingOptions).toHaveLength(3)
|
||||||
|
|
||||||
|
shippingOptions = await service.listShippingOptionsForContext({
|
||||||
|
address: {
|
||||||
|
country_code: "fr",
|
||||||
|
province_code: "rhone",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(shippingOptions).toHaveLength(3)
|
||||||
|
|
||||||
|
shippingOptions = await service.listShippingOptionsForContext({
|
||||||
|
address: {
|
||||||
|
country_code: "fr",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(shippingOptions).toHaveLength(3)
|
||||||
|
|
||||||
|
shippingOptions = await service.listShippingOptionsForContext({
|
||||||
|
address: {
|
||||||
|
country_code: "fr",
|
||||||
|
postal_expression: "75006",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(shippingOptions).toHaveLength(3)
|
||||||
|
|
||||||
|
shippingOptions = await service.listShippingOptionsForContext({
|
||||||
|
address: {
|
||||||
|
country_code: "us",
|
||||||
|
province_code: "rhone",
|
||||||
|
city: "paris",
|
||||||
postal_expression: "75001",
|
postal_expression: "75001",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2189,37 +2189,18 @@ export default class FulfillmentModuleService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate that the address has the required properties for the geo zones
|
* The following changes assume that the lowest level check (e.g postal expression) can't exist multiple times in the higher level (e.g country)
|
||||||
* constraints to build after. We are going from the narrowest to the broadest
|
* In case we encounter situations where it is possible to have multiple postal expressions for the same country we need to change the logic back
|
||||||
|
* to this pr https://github.com/medusajs/medusa/pull/8066
|
||||||
*/
|
*/
|
||||||
Object.entries(geoZoneRequirePropertyHierarchy).forEach(
|
|
||||||
([prop, requiredProps]) => {
|
|
||||||
if (address![prop]) {
|
|
||||||
for (const requiredProp of requiredProps) {
|
|
||||||
if (!address![requiredProp]) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.INVALID_DATA,
|
|
||||||
`Missing required property ${requiredProp} for address when property ${prop} is set`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const geoZoneConstraints = Object.entries(geoZoneRequirePropertyHierarchy)
|
const geoZoneConstraints = Object.entries(geoZoneRequirePropertyHierarchy)
|
||||||
.map(([prop, requiredProps]) => {
|
.map(([prop, requiredProps]) => {
|
||||||
if (address![prop]) {
|
if (address![prop]) {
|
||||||
return requiredProps.reduce((geoZoneConstraint, prop) => {
|
return requiredProps.reduce((geoZoneConstraint, prop) => {
|
||||||
geoZoneConstraint.type =
|
if (isPresent(address![prop])) {
|
||||||
prop === "postal_expression"
|
|
||||||
? "zip"
|
|
||||||
: prop === "city"
|
|
||||||
? "city"
|
|
||||||
: prop === "province_code"
|
|
||||||
? "province"
|
|
||||||
: "country"
|
|
||||||
geoZoneConstraint[prop] = address![prop]
|
geoZoneConstraint[prop] = address![prop]
|
||||||
|
}
|
||||||
return geoZoneConstraint
|
return geoZoneConstraint
|
||||||
}, {} as Record<string, string | undefined>)
|
}, {} as Record<string, string | undefined>)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user