-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shipping-extensions): added checker for facets and country
- Loading branch information
1 parent
58df601
commit b4e17b0
Showing
16 changed files
with
1,012 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/vendure-plugin-shipping-extensions/src/config/facet-and-country-checker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
CountryService, | ||
FacetValueChecker, | ||
Injector, | ||
LanguageCode, | ||
RequestContext, | ||
ShippingEligibilityChecker, | ||
} from '@vendure/core'; | ||
import { isEligibleForCountry } from './util'; | ||
|
||
let injector: Injector; | ||
/** | ||
* Checks if an order only has items with given facets | ||
*/ | ||
export const facetAndCountryChecker = new ShippingEligibilityChecker({ | ||
code: 'shipping-by-facets-and-country', | ||
description: [ | ||
{ | ||
languageCode: LanguageCode.en, | ||
value: 'Check by facets and country', | ||
}, | ||
], | ||
args: { | ||
facets: { | ||
type: 'ID', | ||
list: true, | ||
label: [{ languageCode: LanguageCode.en, value: `Facets` }], | ||
description: [ | ||
{ | ||
languageCode: LanguageCode.en, | ||
value: `All items in order should have all of the facets`, | ||
}, | ||
], | ||
ui: { | ||
component: 'facet-value-form-input', | ||
}, | ||
}, | ||
countries: { | ||
type: 'string', | ||
list: true, | ||
ui: { | ||
component: 'select-form-input', | ||
options: [ | ||
{ | ||
value: 'nl', | ||
label: [{ languageCode: LanguageCode.en, value: 'Nederland' }], | ||
}, | ||
], | ||
}, | ||
}, | ||
excludeCountries: { | ||
type: 'boolean', | ||
description: [ | ||
{ | ||
languageCode: LanguageCode.en, | ||
value: 'Eligible for all countries except the ones listed above', | ||
}, | ||
], | ||
ui: { | ||
component: 'boolean-form-input', | ||
}, | ||
}, | ||
}, | ||
async init(_injector) { | ||
injector = _injector; | ||
const ctx = RequestContext.empty(); | ||
// Populate the countries arg list | ||
const countries = await _injector.get(CountryService).findAll(ctx); | ||
this.args.countries.ui.options = countries.items.map((c) => ({ | ||
value: c.code, | ||
label: [ | ||
{ | ||
languageCode: LanguageCode.en, | ||
value: c.name, | ||
}, | ||
], | ||
})); | ||
}, | ||
async check(ctx, order, { facets, countries, excludeCountries }) { | ||
const isEligibleByCountry = isEligibleForCountry( | ||
order, | ||
countries, | ||
excludeCountries | ||
); | ||
if (isEligibleByCountry === false) { | ||
return false; | ||
} | ||
// Shipping country is allowed, continue checking facets | ||
for (const line of order.lines) { | ||
const hasFacetValues = await injector | ||
.get(FacetValueChecker) | ||
.hasFacetValues(line, facets); | ||
if (!hasFacetValues) { | ||
// One of the lines doesn't have the facetValue, no need to check any more | ||
return false; | ||
} | ||
} | ||
return true; | ||
}, | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/vendure-plugin-shipping-extensions/src/config/util.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { isEligibleForCountry } from './util'; | ||
import { Order } from '@vendure/core'; | ||
|
||
describe('isEligibleForCountry', () => { | ||
it('Is eligible if the order country is in the list and excludeCountries is false', () => { | ||
const order = { | ||
shippingAddress: { | ||
countryCode: 'US', | ||
}, | ||
} as Order; | ||
const result = isEligibleForCountry(order, ['US', 'CA'], false); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('Is not eligible if the order country is in the list and excludeCountries is true', () => { | ||
const order = { | ||
shippingAddress: { | ||
countryCode: 'US', | ||
}, | ||
} as Order; | ||
const result = isEligibleForCountry(order, ['US', 'CA'], true); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('Is not eligible if the order country is not in the list', () => { | ||
const order = { | ||
shippingAddress: { | ||
countryCode: 'MX', | ||
}, | ||
} as Order; | ||
const result = isEligibleForCountry(order, ['US', 'CA'], false); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true if the order country is not in the list and excludeCountries is true', () => { | ||
const order = { | ||
shippingAddress: { | ||
countryCode: 'MX', | ||
}, | ||
} as Order; | ||
const result = isEligibleForCountry(order, ['US', 'CA'], true); | ||
expect(result).toBe(true); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/vendure-plugin-shipping-extensions/src/config/util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Order } from '@vendure/core'; | ||
|
||
/** | ||
* Checks if an order is eligible for the given country codes. | ||
* When excludedCountries=true: The order is eligible if it's country is NOT in the configured countryCodes | ||
*/ | ||
export function isEligibleForCountry( | ||
order: Order, | ||
countryCodes: string[], | ||
excludeCountries: boolean | ||
): boolean { | ||
const shippingCountry = order.shippingAddress.countryCode; | ||
const orderIsInSelectedCountry = shippingCountry | ||
? countryCodes.includes(shippingCountry) | ||
: false; | ||
if (orderIsInSelectedCountry && excludeCountries) { | ||
// Not eligible, because order.country is in our excluded-country-list | ||
return false; | ||
} | ||
if (!orderIsInSelectedCountry && !excludeCountries) { | ||
// Not eligible, because order.country is not in our list, but it should be | ||
return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.