Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tax): normalize country and province code #6513

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/tax/integration-tests/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ moduleIntegrationTestRunner({
expect.arrayContaining([
expect.objectContaining({
id: region.id,
country_code: "US",
country_code: "us",
province_code: null,
parent_id: null,
}),
expect.objectContaining({
id: provinceRegion.id,
country_code: "US",
province_code: "CA",
country_code: "us",
province_code: "ca",
parent_id: region.id,
}),
])
Expand Down Expand Up @@ -397,6 +397,7 @@ moduleIntegrationTestRunner({
expect(taxRegions).toEqual([
expect.objectContaining({
id: region.id,
country_code: "us",
deleted_at: expect.any(Date),
}),
])
Expand Down
75 changes: 50 additions & 25 deletions packages/tax/src/services/tax-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,9 @@ export default class TaxModuleService<
data: TaxTypes.CreateTaxRegionDTO | TaxTypes.CreateTaxRegionDTO[],
@MedusaContext() sharedContext: Context = {}
) {
const input = Array.isArray(data) ? data : [data]
await this.verifyProvinceToCountryMatch(input, sharedContext)
const [defaultRates, regionData] = input.reduce(
(acc, region) => {
const { default_tax_rate, ...rest } = region
if (!default_tax_rate) {
acc[0].push(null)
} else {
acc[0].push({
...default_tax_rate,
is_default: true,
created_by: region.created_by,
})
}
acc[1].push(rest)
return acc
},
[[], []] as [
(Omit<TaxTypes.CreateTaxRateDTO, "tax_region_id"> | null)[],
Partial<TaxTypes.CreateTaxRegionDTO>[]
]
)
const [defaultRates, regionData] = this.prepareTaxRegionInputForCreate(data)

await this.verifyProvinceToCountryMatch(regionData, sharedContext)

const regions = await this.taxRegionService_.create(
regionData,
Expand Down Expand Up @@ -246,12 +227,20 @@ export default class TaxModuleService<
{
$or: [
{
country_code: calculationContext.address.country_code,
country_code: this.normalizeRegionCodes(
calculationContext.address.country_code
),
province_code: null,
},
{
country_code: calculationContext.address.country_code,
province_code: calculationContext.address.province_code,
country_code: this.normalizeRegionCodes(
calculationContext.address.country_code
),
province_code: calculationContext.address.province_code
? this.normalizeRegionCodes(
calculationContext.address.province_code
)
: null,
},
],
},
Expand All @@ -278,6 +267,38 @@ export default class TaxModuleService<
return toReturn.flat()
}

private prepareTaxRegionInputForCreate(
data: TaxTypes.CreateTaxRegionDTO | TaxTypes.CreateTaxRegionDTO[]
) {
const input = Array.isArray(data) ? data : [data]
return input.reduce(
(acc, region) => {
const { default_tax_rate, ...rest } = region
if (!default_tax_rate) {
acc[0].push(null)
} else {
acc[0].push({
...default_tax_rate,
is_default: true,
created_by: region.created_by,
})
}
acc[1].push({
...rest,
province_code: rest.province_code
? this.normalizeRegionCodes(rest.province_code)
: null,
country_code: this.normalizeRegionCodes(rest.country_code),
})
return acc
},
[[], []] as [
(Omit<TaxTypes.CreateTaxRateDTO, "tax_region_id"> | null)[],
TaxTypes.CreateTaxRegionDTO[]
]
)
}

private async verifyProvinceToCountryMatch(
regionsToVerify: TaxTypes.CreateTaxRegionDTO[],
sharedContext: Context = {}
Expand Down Expand Up @@ -479,4 +500,8 @@ export default class TaxModuleService<
(a, b) => (a as any).priority_score - (b as any).priority_score
)
}

private normalizeRegionCodes(code: string) {
return code.toLowerCase()
}
}
2 changes: 1 addition & 1 deletion packages/types/src/tax/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface CreateTaxRegionDTO {
parent_id?: string | null
metadata?: Record<string, unknown>
created_by?: string
default_tax_rate: {
default_tax_rate?: {
rate?: number | null
code?: string | null
name: string
Expand Down
Loading