diff --git a/packages/tax/integration-tests/__tests__/index.spec.ts b/packages/tax/integration-tests/__tests__/index.spec.ts index 753f9e2c4d..814b1257c4 100644 --- a/packages/tax/integration-tests/__tests__/index.spec.ts +++ b/packages/tax/integration-tests/__tests__/index.spec.ts @@ -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, }), ]) @@ -397,6 +397,7 @@ moduleIntegrationTestRunner({ expect(taxRegions).toEqual([ expect.objectContaining({ id: region.id, + country_code: "us", deleted_at: expect.any(Date), }), ]) diff --git a/packages/tax/src/services/tax-module-service.ts b/packages/tax/src/services/tax-module-service.ts index d13ce53691..7e5995130b 100644 --- a/packages/tax/src/services/tax-module-service.ts +++ b/packages/tax/src/services/tax-module-service.ts @@ -160,28 +160,10 @@ 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 | null)[], - Partial[] - ] - ) + const { defaultRates, regionData } = + this.prepareTaxRegionInputForCreate(data) + + await this.verifyProvinceToCountryMatch(regionData, sharedContext) const regions = await this.taxRegionService_.create( regionData, @@ -251,16 +233,18 @@ export default class TaxModuleService< calculationContext: TaxTypes.TaxCalculationContext, @MedusaContext() sharedContext: Context = {} ): Promise<(TaxTypes.ItemTaxLineDTO | TaxTypes.ShippingTaxLineDTO)[]> { + const normalizedContext = + this.normalizeTaxCalculationContext(calculationContext) const regions = await this.taxRegionService_.list( { $or: [ { - country_code: calculationContext.address.country_code, + country_code: normalizedContext.address.country_code, province_code: null, }, { - country_code: calculationContext.address.country_code, - province_code: calculationContext.address.province_code, + country_code: normalizedContext.address.country_code, + province_code: normalizedContext.address.province_code, }, ], }, @@ -355,6 +339,56 @@ export default class TaxModuleService< return itemTaxLines } + private normalizeTaxCalculationContext( + context: TaxTypes.TaxCalculationContext + ): TaxTypes.TaxCalculationContext { + return { + ...context, + address: { + ...context.address, + country_code: this.normalizeRegionCodes(context.address.country_code), + province_code: context.address.province_code + ? this.normalizeRegionCodes(context.address.province_code) + : null, + }, + } + } + + private prepareTaxRegionInputForCreate( + data: TaxTypes.CreateTaxRegionDTO | TaxTypes.CreateTaxRegionDTO[] + ) { + const regionsWithDefaultRate = Array.isArray(data) ? data : [data] + + const defaultRates: (Omit< + TaxTypes.CreateTaxRateDTO, + "tax_region_id" + > | null)[] = [] + const regionData: TaxTypes.CreateTaxRegionDTO[] = [] + + for (const region of regionsWithDefaultRate) { + const { default_tax_rate, ...rest } = region + if (!default_tax_rate) { + defaultRates.push(null) + } else { + defaultRates.push({ + ...default_tax_rate, + is_default: true, + created_by: region.created_by, + }) + } + + regionData.push({ + ...rest, + province_code: rest.province_code + ? this.normalizeRegionCodes(rest.province_code) + : null, + country_code: this.normalizeRegionCodes(rest.country_code), + }) + } + + return { defaultRates, regionData } + } + private async verifyProvinceToCountryMatch( regionsToVerify: TaxTypes.CreateTaxRegionDTO[], sharedContext: Context = {} @@ -531,4 +565,8 @@ export default class TaxModuleService< (a, b) => (a as any).priority_score - (b as any).priority_score ) } + + private normalizeRegionCodes(code: string) { + return code.toLowerCase() + } } diff --git a/packages/types/src/tax/mutations.ts b/packages/types/src/tax/mutations.ts index 95b02433a7..476209b4f8 100644 --- a/packages/types/src/tax/mutations.ts +++ b/packages/types/src/tax/mutations.ts @@ -15,7 +15,7 @@ export interface CreateTaxRegionDTO { parent_id?: string | null metadata?: Record created_by?: string - default_tax_rate: { + default_tax_rate?: { rate?: number | null code?: string | null name: string