-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Include address-based tax zone strategy (#3198)
- Loading branch information
1 parent
0cd0ef2
commit 5547128
Showing
4 changed files
with
112 additions
and
1 deletion.
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
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
52 changes: 52 additions & 0 deletions
52
packages/core/src/config/tax/address-based-tax-zone-strategy.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,52 @@ | ||
import { RequestContext, Zone, Channel, Order } from '@vendure/core'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { Logger } from '../logger/vendure-logger'; | ||
|
||
import { AddressBasedTaxZoneStrategy } from './address-based-tax-zone-strategy'; | ||
|
||
describe('AddressBasedTaxZoneStrategy', () => { | ||
const strategy = new AddressBasedTaxZoneStrategy(); | ||
|
||
const ctx = {} as RequestContext; | ||
const defaultZone = { name: 'Default Zone' } as Zone; | ||
const channel = { defaultTaxZone: defaultZone } as Channel; | ||
|
||
const zones: Zone[] = [ | ||
defaultZone, | ||
{ name: 'US Zone', members: [{ code: 'US' }] } as Zone, | ||
{ name: 'DE Zone', members: [{ code: 'DE' }] } as Zone, | ||
]; | ||
|
||
it('Determines zone based on billing address country', () => { | ||
const order = { | ||
billingAddress: { countryCode: 'US' }, | ||
shippingAddress: { countryCode: 'DE' }, | ||
} as Order; | ||
const result = strategy.determineTaxZone(ctx, zones, channel, order); | ||
expect(result.name).toBe('US Zone'); | ||
}); | ||
|
||
it('Determines zone based on shipping address if no billing address set', () => { | ||
const order = { shippingAddress: { countryCode: 'DE' } } as Order; | ||
const result = strategy.determineTaxZone(ctx, zones, channel, order); | ||
expect(result.name).toBe('DE Zone'); | ||
}); | ||
|
||
it('Returns the default zone if no matching zone is found', () => { | ||
const order = { billingAddress: { countryCode: 'FR' } } as Order; | ||
const result = strategy.determineTaxZone(ctx, zones, channel, order); | ||
expect(result.name).toBe('Default Zone'); | ||
}); | ||
|
||
it('Returns the default zone if no order is provided', () => { | ||
const result = strategy.determineTaxZone(ctx, zones, channel); | ||
expect(result.name).toBe('Default Zone'); | ||
}); | ||
|
||
it('Returns the default zone if no address is set on order', () => { | ||
const order = {} as Order; | ||
const result = strategy.determineTaxZone(ctx, zones, channel, order); | ||
expect(result.name).toBe('Default Zone'); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/core/src/config/tax/address-based-tax-zone-strategy.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,42 @@ | ||
import { RequestContext } from '../../api/common/request-context'; | ||
import { Channel, Order, Zone } from '../../entity'; | ||
import { Logger } from '../logger/vendure-logger'; | ||
|
||
import { TaxZoneStrategy } from './tax-zone-strategy'; | ||
|
||
const loggerCtx = 'AddressBasedTaxZoneStrategy'; | ||
|
||
/** | ||
* @description | ||
* Address based {@link TaxZoneStrategy} which tries to find the applicable {@link Zone} based on the | ||
* country of the billing address, or else the country of the shipping address of the Order. | ||
* | ||
* Returns the default {@link Channel}'s default tax zone if no applicable zone is found. | ||
* | ||
* @since 3.1.0 | ||
* | ||
* :::info | ||
* | ||
* This is configured via `taxOptions.taxZoneStrategy = new AddressBasedTaxZoneStrategy()` in | ||
* your VendureConfig. | ||
* | ||
* ::: | ||
* | ||
* @docsCategory tax | ||
*/ | ||
export class AddressBasedTaxZoneStrategy implements TaxZoneStrategy { | ||
determineTaxZone(ctx: RequestContext, zones: Zone[], channel: Channel, order?: Order): Zone { | ||
const countryCode = order?.billingAddress?.countryCode ?? order?.shippingAddress?.countryCode; | ||
if (order && countryCode) { | ||
const zone = zones.find(z => z.members?.find(member => member.code === countryCode)); | ||
if (zone) { | ||
return zone; | ||
} | ||
Logger.debug( | ||
`No tax zone found for country ${countryCode}. Returning default ${channel.defaultTaxZone.name} for order ${order.code}`, | ||
loggerCtx, | ||
); | ||
} | ||
return channel.defaultTaxZone; | ||
} | ||
} |