feat(tax): add support for updating tax rates (#6516)

**What**
- Adds update
This commit is contained in:
Sebastian Rindom
2024-02-27 10:55:22 +01:00
committed by GitHub
parent d03b72ecdd
commit ca463ae9a9
4 changed files with 137 additions and 0 deletions

View File

@@ -8,6 +8,73 @@ moduleIntegrationTestRunner({
moduleName: Modules.TAX,
testSuite: ({ service }: SuiteOptions<ITaxModuleService>) => {
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",

View File

@@ -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<TaxTypes.TaxRateDTO>
async update(
ids: string[],
data: TaxTypes.UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO[]>
async update(
selector: TaxTypes.FilterableTaxRateProps,
data: TaxTypes.UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO[]>
@InjectManager("baseRepository_")
async update(
selector: string | string[] | TaxTypes.FilterableTaxRateProps,
data: TaxTypes.UpdateTaxRateDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<TaxTypes.TaxRateDTO | TaxTypes.TaxRateDTO[]> {
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

View File

@@ -9,6 +9,15 @@ export interface CreateTaxRateDTO {
metadata?: Record<string, unknown>
}
export interface UpdateTaxRateDTO {
rate?: number | null
code?: string | null
name?: string
is_default?: boolean
created_by?: string
metadata?: Record<string, unknown>
}
export interface CreateTaxRegionDTO {
country_code: string
province_code?: string | null

View File

@@ -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<TaxRateDTO[]>
create(data: CreateTaxRateDTO, sharedContext?: Context): Promise<TaxRateDTO>
update(
taxRateId: string,
data: UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxRateDTO>
update(
taxRateIds: string[],
data: UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxRateDTO[]>
update(
selector: FilterableTaxRateProps,
data: UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxRateDTO[]>
delete(taxRateIds: string[], sharedContext?: Context): Promise<void>
delete(taxRateId: string, sharedContext?: Context): Promise<void>