From 67efc1350abefda8fa0fa41f3af30f75a4a3bbb4 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 26 Feb 2024 16:03:20 +0100 Subject: [PATCH 1/4] fix: normalize region codes in db --- .../integration-tests/__tests__/index.spec.ts | 7 +++-- .../tax/src/services/tax-module-service.ts | 31 +++++++++++++++---- packages/types/src/tax/mutations.ts | 2 +- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/tax/integration-tests/__tests__/index.spec.ts b/packages/tax/integration-tests/__tests__/index.spec.ts index 753f9e2c4dfbc..814b1257c4fbc 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 a1af013d14ca3..e4cd46ce132d3 100644 --- a/packages/tax/src/services/tax-module-service.ts +++ b/packages/tax/src/services/tax-module-service.ts @@ -152,7 +152,6 @@ export default class TaxModuleService< @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 @@ -165,15 +164,23 @@ export default class TaxModuleService< created_by: region.created_by, }) } - acc[1].push(rest) + 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 | null)[], - Partial[] + TaxTypes.CreateTaxRegionDTO[] ] ) + await this.verifyProvinceToCountryMatch(regionData, sharedContext) + const regions = await this.taxRegionService_.create( regionData, sharedContext @@ -246,12 +253,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, }, ], }, @@ -479,4 +494,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 95b02433a7b56..476209b4f8e6c 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 From efc6f3a240da70dd3b20dd01bae2a7c501e9b1ec Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 26 Feb 2024 16:07:40 +0100 Subject: [PATCH 2/4] chore: add prepare region data internal function --- .../tax/src/services/tax-module-service.ts | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/packages/tax/src/services/tax-module-service.ts b/packages/tax/src/services/tax-module-service.ts index e4cd46ce132d3..20e5d406eae57 100644 --- a/packages/tax/src/services/tax-module-service.ts +++ b/packages/tax/src/services/tax-module-service.ts @@ -151,33 +151,7 @@ export default class TaxModuleService< data: TaxTypes.CreateTaxRegionDTO | TaxTypes.CreateTaxRegionDTO[], @MedusaContext() sharedContext: Context = {} ) { - const input = Array.isArray(data) ? data : [data] - 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, - province_code: rest.province_code - ? this.normalizeRegionCodes(rest.province_code) - : null, - country_code: this.normalizeRegionCodes(rest.country_code), - }) - return acc - }, - [[], []] as [ - (Omit | null)[], - TaxTypes.CreateTaxRegionDTO[] - ] - ) + const [defaultRates, regionData] = this.prepareTaxRegionInputForCreate(data) await this.verifyProvinceToCountryMatch(regionData, sharedContext) @@ -293,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 | null)[], + TaxTypes.CreateTaxRegionDTO[] + ] + ) + } + private async verifyProvinceToCountryMatch( regionsToVerify: TaxTypes.CreateTaxRegionDTO[], sharedContext: Context = {} From 44e560cdd841b005699bc5a69ef2217e281543c2 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 26 Feb 2024 19:04:09 +0100 Subject: [PATCH 3/4] cleanup --- .../tax/src/services/tax-module-service.ts | 86 +++++++++++-------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/packages/tax/src/services/tax-module-service.ts b/packages/tax/src/services/tax-module-service.ts index 20e5d406eae57..60915add358e4 100644 --- a/packages/tax/src/services/tax-module-service.ts +++ b/packages/tax/src/services/tax-module-service.ts @@ -223,24 +223,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: this.normalizeRegionCodes( - calculationContext.address.country_code - ), + country_code: normalizedContext.address.country_code, province_code: null, }, { - country_code: this.normalizeRegionCodes( - calculationContext.address.country_code - ), - province_code: calculationContext.address.province_code - ? this.normalizeRegionCodes( - calculationContext.address.province_code - ) - : null, + country_code: normalizedContext.address.country_code, + province_code: normalizedContext.address.province_code, }, ], }, @@ -267,36 +261,54 @@ export default class TaxModuleService< return toReturn.flat() } + 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 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), + 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, }) - return acc - }, - [[], []] as [ - (Omit | null)[], - TaxTypes.CreateTaxRegionDTO[] - ] - ) + } + + 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( From 6662a4ff1adfa70373097c823edcff0b24d6c056 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 26 Feb 2024 19:20:16 +0100 Subject: [PATCH 4/4] cleanup --- packages/tax/src/services/tax-module-service.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/tax/src/services/tax-module-service.ts b/packages/tax/src/services/tax-module-service.ts index 60915add358e4..b2ec5cd219e7c 100644 --- a/packages/tax/src/services/tax-module-service.ts +++ b/packages/tax/src/services/tax-module-service.ts @@ -151,7 +151,8 @@ export default class TaxModuleService< data: TaxTypes.CreateTaxRegionDTO | TaxTypes.CreateTaxRegionDTO[], @MedusaContext() sharedContext: Context = {} ) { - const [defaultRates, regionData] = this.prepareTaxRegionInputForCreate(data) + const { defaultRates, regionData } = + this.prepareTaxRegionInputForCreate(data) await this.verifyProvinceToCountryMatch(regionData, sharedContext) @@ -308,7 +309,7 @@ export default class TaxModuleService< }) } - return [defaultRates, regionData] + return { defaultRates, regionData } } private async verifyProvinceToCountryMatch(