diff --git a/packages/tax/integration-tests/__tests__/index.spec.ts b/packages/tax/integration-tests/__tests__/index.spec.ts index 814b1257c4..cd28f2f9ae 100644 --- a/packages/tax/integration-tests/__tests__/index.spec.ts +++ b/packages/tax/integration-tests/__tests__/index.spec.ts @@ -8,6 +8,73 @@ moduleIntegrationTestRunner({ moduleName: Modules.TAX, testSuite: ({ service }: SuiteOptions) => { describe("TaxModuleService", function () { + it("should create tax rates and update them", async () => { + const region = await service.createTaxRegions({ + country_code: "US", + default_tax_rate: { + name: "Test Rate", + rate: 0.2, + }, + }) + + const rate = await service.create({ + tax_region_id: region.id, + name: "Shipping Rate", + code: "test", + rate: 8.23, + }) + + const updatedRate = await service.update(rate.id, { + name: "Updated Rate", + code: "TEST", + rate: 8.25, + }) + + expect(updatedRate).toEqual( + expect.objectContaining({ + tax_region_id: region.id, + rate: 8.25, + name: "Updated Rate", + code: "TEST", + is_default: false, + }) + ) + + const updatedDefaultRate = await service.update( + { tax_region_id: region.id, is_default: true }, + { rate: 2 } + ) + + expect(updatedDefaultRate).toEqual([ + expect.objectContaining({ + tax_region_id: region.id, + rate: 2, + name: "Test Rate", + code: null, + is_default: true, + }), + ]) + + const rates = await service.list() + expect(rates).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + tax_region_id: region.id, + rate: 2, + name: "Test Rate", + is_default: true, + }), + expect.objectContaining({ + tax_region_id: region.id, + rate: 8.25, + name: "Updated Rate", + code: "TEST", + is_default: false, + }), + ]) + ) + }) + it("should create a tax region", async () => { const region = await service.createTaxRegions({ country_code: "US", diff --git a/packages/tax/src/services/tax-module-service.ts b/packages/tax/src/services/tax-module-service.ts index 7e5995130b..7f643db9d6 100644 --- a/packages/tax/src/services/tax-module-service.ts +++ b/packages/tax/src/services/tax-module-service.ts @@ -15,6 +15,7 @@ import { MedusaError, ModulesSdkUtils, isDefined, + isString, promiseAll, } from "@medusajs/utils" import { TaxRate, TaxRegion, TaxRateRule } from "@models" @@ -145,6 +146,49 @@ export default class TaxModuleService< }) } + async update( + id: string, + data: TaxTypes.UpdateTaxRateDTO, + sharedContext?: Context + ): Promise + async update( + ids: string[], + data: TaxTypes.UpdateTaxRateDTO, + sharedContext?: Context + ): Promise + async update( + selector: TaxTypes.FilterableTaxRateProps, + data: TaxTypes.UpdateTaxRateDTO, + sharedContext?: Context + ): Promise + + @InjectManager("baseRepository_") + async update( + selector: string | string[] | TaxTypes.FilterableTaxRateProps, + data: TaxTypes.UpdateTaxRateDTO, + @MedusaContext() sharedContext: Context = {} + ): Promise { + const rates = await this.update_(selector, data, sharedContext) + const serialized = await this.baseRepository_.serialize< + TaxTypes.TaxRateDTO[] + >(rates, { populate: true }) + return isString(selector) ? serialized[0] : serialized + } + + @InjectTransactionManager("baseRepository_") + protected async update_( + idOrSelector: string | string[] | TaxTypes.FilterableTaxRateProps, + data: TaxTypes.UpdateTaxRateDTO, + @MedusaContext() sharedContext: Context = {} + ) { + const selector = + Array.isArray(idOrSelector) || isString(idOrSelector) + ? { id: idOrSelector } + : idOrSelector + + return await this.taxRateService_.update({ selector, data }, sharedContext) + } + createTaxRegions( data: TaxTypes.CreateTaxRegionDTO, sharedContext?: Context diff --git a/packages/types/src/tax/mutations.ts b/packages/types/src/tax/mutations.ts index 476209b4f8..9414e8eebd 100644 --- a/packages/types/src/tax/mutations.ts +++ b/packages/types/src/tax/mutations.ts @@ -9,6 +9,15 @@ export interface CreateTaxRateDTO { metadata?: Record } +export interface UpdateTaxRateDTO { + rate?: number | null + code?: string | null + name?: string + is_default?: boolean + created_by?: string + metadata?: Record +} + export interface CreateTaxRegionDTO { country_code: string province_code?: string | null diff --git a/packages/types/src/tax/service.ts b/packages/types/src/tax/service.ts index 5f639a4b7c..aa8e925067 100644 --- a/packages/types/src/tax/service.ts +++ b/packages/types/src/tax/service.ts @@ -19,6 +19,7 @@ import { CreateTaxRateRuleDTO, CreateTaxRateDTO, CreateTaxRegionDTO, + UpdateTaxRateDTO, } from "./mutations" export interface ITaxModuleService extends IModuleService { @@ -46,6 +47,22 @@ export interface ITaxModuleService extends IModuleService { ): Promise create(data: CreateTaxRateDTO, sharedContext?: Context): Promise + update( + taxRateId: string, + data: UpdateTaxRateDTO, + sharedContext?: Context + ): Promise + update( + taxRateIds: string[], + data: UpdateTaxRateDTO, + sharedContext?: Context + ): Promise + update( + selector: FilterableTaxRateProps, + data: UpdateTaxRateDTO, + sharedContext?: Context + ): Promise + delete(taxRateIds: string[], sharedContext?: Context): Promise delete(taxRateId: string, sharedContext?: Context): Promise