fix: Tax region + rates clean up (#9279)
What - Require `code` on Tax Rates - Update dashboard to account for non-nullable code on Tax Rates - Include `automatic_taxes` in API Route response Closes CC-524 CC-525
This commit is contained in:
@@ -2,7 +2,7 @@ import { IEventBusModuleService } from "@medusajs/types"
|
||||
import { CommonEvents, Modules } from "@medusajs/utils"
|
||||
import FormData from "form-data"
|
||||
import fs from "fs/promises"
|
||||
import { TestEventUtils, medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { medusaIntegrationTestRunner, TestEventUtils } from "medusa-test-utils"
|
||||
import path from "path"
|
||||
import {
|
||||
adminHeaders,
|
||||
|
||||
@@ -55,9 +55,17 @@ medusaIntegrationTestRunner({
|
||||
expect(response.data.regions).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "United Kingdom",
|
||||
id: region1.id,
|
||||
currency_code: "gbp",
|
||||
automatic_taxes: region1.automatic_taxes,
|
||||
countries: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "United States",
|
||||
id: region2.id,
|
||||
currency_code: "usd",
|
||||
automatic_taxes: region2.automatic_taxes,
|
||||
countries: [],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
@@ -46,7 +46,11 @@ export const setupTaxStructure = async (service: ITaxModuleService) => {
|
||||
},
|
||||
{
|
||||
country_code: "CA",
|
||||
default_tax_rate: { name: "Canada Default Rate", rate: 5 },
|
||||
default_tax_rate: {
|
||||
name: "Canada Default Rate",
|
||||
rate: 5,
|
||||
code: "CA_DEF",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
|
||||
@@ -234,6 +234,29 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to create a tax rate without a code", async () => {
|
||||
const errResponse = await api
|
||||
.post(
|
||||
`/admin/tax-regions`,
|
||||
{
|
||||
country_code: "us",
|
||||
default_tax_rate: {
|
||||
rate: 2,
|
||||
name: "default rate",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(errResponse.response.status).toEqual(400)
|
||||
expect(errResponse.response.data).toEqual({
|
||||
message:
|
||||
"Invalid request: Field 'default_tax_rate, code' is required",
|
||||
type: "invalid_data",
|
||||
})
|
||||
})
|
||||
|
||||
it("can create a tax rate and update it", async () => {
|
||||
const regionRes = await api.post(
|
||||
`/admin/tax-regions`,
|
||||
|
||||
+1
@@ -59,6 +59,7 @@ export const EditRegionForm = ({
|
||||
await updateRegion(
|
||||
{
|
||||
name: values.name,
|
||||
automatic_taxes: values.automatic_taxes,
|
||||
currency_code: values.currency_code.toLowerCase(),
|
||||
payment_providers: values.payment_providers,
|
||||
is_tax_inclusive: values.is_tax_inclusive,
|
||||
|
||||
+14
-14
@@ -21,12 +21,10 @@ type TaxRegionCreateFormProps = {
|
||||
const TaxRegionCreateSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
code: z.string().optional(),
|
||||
rate: z
|
||||
.object({
|
||||
float: z.number().optional(),
|
||||
value: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
rate: z.object({
|
||||
float: z.number().optional(),
|
||||
value: z.string().optional(),
|
||||
}),
|
||||
country_code: z.string().min(1),
|
||||
})
|
||||
|
||||
@@ -49,14 +47,16 @@ export const TaxRegionCreateForm = ({ parentId }: TaxRegionCreateFormProps) => {
|
||||
const { mutateAsync, isPending } = useCreateTaxRegion()
|
||||
|
||||
const handleSubmit = form.handleSubmit(async (values) => {
|
||||
const defaultRate =
|
||||
values.name && values.rate?.float
|
||||
? {
|
||||
name: values.name,
|
||||
rate: values.rate.float,
|
||||
code: values.code,
|
||||
}
|
||||
: undefined
|
||||
const defaultRate = values.name
|
||||
? {
|
||||
name: values.name,
|
||||
rate:
|
||||
values.rate?.value === ""
|
||||
? undefined
|
||||
: parseFloat(values.rate.value!),
|
||||
code: values.code,
|
||||
}
|
||||
: undefined
|
||||
|
||||
await mutateAsync(
|
||||
{
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ type TaxRegionProvinceCreateFormProps = {
|
||||
const CreateTaxRegionProvinceSchema = z.object({
|
||||
province_code: z.string().min(1),
|
||||
name: z.string().optional(),
|
||||
code: z.string().optional(),
|
||||
code: z.string().min(1),
|
||||
rate: z
|
||||
.object({
|
||||
float: z.number().optional(),
|
||||
|
||||
+2
-2
@@ -38,7 +38,7 @@ import { createTaxRulePayload } from "../../../common/utils"
|
||||
|
||||
const TaxRegionCreateTaxOverrideSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
code: z.string().optional(),
|
||||
code: z.string().min(1),
|
||||
rate: z
|
||||
.object({
|
||||
float: z.number().optional(),
|
||||
@@ -408,7 +408,7 @@ export const TaxRegionCreateTaxOverrideForm = ({
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<Form.Item>
|
||||
<Form.Label optional>
|
||||
<Form.Label>
|
||||
{t("taxRegions.fields.taxCode")}
|
||||
</Form.Label>
|
||||
<Form.Control>
|
||||
|
||||
+2
-2
@@ -48,7 +48,7 @@ const getStackedModalId = (type: TaxRateRuleReferenceType) =>
|
||||
|
||||
const TaxRegionTaxRateEditSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
code: z.string().optional(),
|
||||
code: z.string().min(1),
|
||||
rate: z.object({
|
||||
float: z.number().optional(),
|
||||
value: z.string().optional(),
|
||||
@@ -80,7 +80,7 @@ export const TaxRegionTaxOverrideEditForm = ({
|
||||
const form = useForm<z.infer<typeof TaxRegionTaxRateEditSchema>>({
|
||||
defaultValues: {
|
||||
name: taxRate.name,
|
||||
code: taxRate.code || "",
|
||||
code: taxRate.code,
|
||||
rate: {
|
||||
value: taxRate.rate?.toString() || "",
|
||||
},
|
||||
|
||||
+2
-2
@@ -20,7 +20,7 @@ type TaxRegionTaxRateCreateFormProps = {
|
||||
|
||||
const TaxRegionTaxRateCreateSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
code: z.string().optional(),
|
||||
code: z.string().min(1),
|
||||
rate: z
|
||||
.object({
|
||||
float: z.number().optional(),
|
||||
@@ -57,7 +57,7 @@ export const TaxRegionTaxRateCreateForm = ({
|
||||
tax_region_id: taxRegion.id,
|
||||
is_default: true,
|
||||
name: values.name,
|
||||
code: values.code || undefined,
|
||||
code: values.code,
|
||||
rate: values.rate?.float,
|
||||
is_combinable: values.is_combinable,
|
||||
},
|
||||
|
||||
+2
-2
@@ -36,7 +36,7 @@ export const TaxRegionTaxRateEditForm = ({
|
||||
const form = useForm<z.infer<typeof TaxRegionTaxRateEditSchema>>({
|
||||
defaultValues: {
|
||||
name: taxRate.name,
|
||||
code: taxRate.code || "",
|
||||
code: taxRate.code,
|
||||
rate: {
|
||||
value: taxRate.rate?.toString() || "",
|
||||
},
|
||||
@@ -51,7 +51,7 @@ export const TaxRegionTaxRateEditForm = ({
|
||||
await mutateAsync(
|
||||
{
|
||||
name: values.name,
|
||||
code: values.code || null,
|
||||
code: values.code,
|
||||
rate: values.rate?.float,
|
||||
is_combinable: values.is_combinable,
|
||||
},
|
||||
|
||||
@@ -8,7 +8,7 @@ export interface AdminTaxRateRule {
|
||||
export interface AdminTaxRate {
|
||||
id: string
|
||||
rate: number | null
|
||||
code: string | null
|
||||
code: string
|
||||
name: string
|
||||
metadata: Record<string, unknown> | null
|
||||
tax_region_id: string
|
||||
|
||||
@@ -7,7 +7,7 @@ export interface AdminCreateTaxRate {
|
||||
name: string
|
||||
tax_region_id: string
|
||||
rate?: number
|
||||
code?: string
|
||||
code: string
|
||||
rules?: AdminCreateTaxRateRule[]
|
||||
is_default?: boolean
|
||||
is_combinable?: boolean
|
||||
@@ -17,7 +17,7 @@ export interface AdminCreateTaxRate {
|
||||
export interface AdminUpdateTaxRate {
|
||||
name?: string
|
||||
rate?: number
|
||||
code?: string | null
|
||||
code: string
|
||||
rules?: AdminCreateTaxRateRule[]
|
||||
is_default?: boolean
|
||||
is_combinable?: boolean
|
||||
|
||||
@@ -4,7 +4,7 @@ export interface AdminCreateTaxRegion {
|
||||
parent_id?: string
|
||||
default_tax_rate?: {
|
||||
rate?: number
|
||||
code?: string
|
||||
code: string
|
||||
name: string
|
||||
is_combinable?: boolean
|
||||
metadata?: Record<string, unknown>
|
||||
|
||||
@@ -5,6 +5,7 @@ export const defaultAdminRegionFields = [
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"automatic_taxes",
|
||||
"metadata",
|
||||
"*countries",
|
||||
]
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { createRegionsWorkflow } from "@medusajs/core-flows"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { refetchRegion } from "./helpers"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.AdminRegionFilters>,
|
||||
|
||||
@@ -36,7 +36,7 @@ export const AdminCreateTaxRateRule = z.object({
|
||||
export type AdminCreateTaxRateType = z.infer<typeof AdminCreateTaxRate>
|
||||
export const AdminCreateTaxRate = z.object({
|
||||
rate: z.number().optional(),
|
||||
code: z.string().optional(),
|
||||
code: z.string(),
|
||||
rules: z.array(AdminCreateTaxRateRule).optional(),
|
||||
name: z.string(),
|
||||
is_default: z.boolean().optional(),
|
||||
@@ -48,7 +48,7 @@ export const AdminCreateTaxRate = z.object({
|
||||
export type AdminUpdateTaxRateType = z.infer<typeof AdminUpdateTaxRate>
|
||||
export const AdminUpdateTaxRate = z.object({
|
||||
rate: z.number().optional(),
|
||||
code: z.string().nullish(),
|
||||
code: z.string().optional(),
|
||||
rules: z.array(AdminCreateTaxRateRule).optional(),
|
||||
name: z.string().optional(),
|
||||
is_default: z.boolean().optional(),
|
||||
|
||||
@@ -46,7 +46,7 @@ export const AdminCreateTaxRegion = z.object({
|
||||
default_tax_rate: z
|
||||
.object({
|
||||
rate: z.number().optional(),
|
||||
code: z.string().optional(),
|
||||
code: z.string(),
|
||||
name: z.string(),
|
||||
is_combinable: z.boolean().optional(),
|
||||
metadata: z.record(z.unknown()).nullish(),
|
||||
|
||||
@@ -72,6 +72,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -90,6 +91,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -99,6 +101,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -125,6 +128,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -135,6 +139,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -161,6 +166,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -171,6 +177,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -181,6 +188,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -215,6 +223,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -224,6 +233,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
.catch((e) => e)
|
||||
@@ -240,6 +250,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -250,6 +261,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST",
|
||||
},
|
||||
})
|
||||
.catch((e) => e)
|
||||
@@ -265,6 +277,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST-CODE",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -301,7 +314,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
tax_region_id: region.id,
|
||||
rate: 2,
|
||||
name: "Test Rate",
|
||||
code: null,
|
||||
code: "TEST-CODE",
|
||||
is_default: true,
|
||||
}),
|
||||
])
|
||||
@@ -313,6 +326,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
tax_region_id: region.id,
|
||||
rate: 2,
|
||||
name: "Test Rate",
|
||||
code: "TEST-CODE",
|
||||
is_default: true,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
@@ -332,6 +346,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST-CODE",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -413,6 +428,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
const region = await service.createTaxRegions({
|
||||
country_code: "US",
|
||||
default_tax_rate: {
|
||||
code: "TEST",
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
},
|
||||
@@ -424,6 +440,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
province_code: "CA",
|
||||
parent_id: region.id,
|
||||
default_tax_rate: {
|
||||
code: "TEST",
|
||||
name: "CA Rate",
|
||||
rate: 8.25,
|
||||
},
|
||||
@@ -472,6 +489,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
{
|
||||
country_code: "US",
|
||||
default_tax_rate: {
|
||||
code: "TEST",
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
},
|
||||
@@ -482,6 +500,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
tax_region_id: region.id,
|
||||
name: "Shipping Rate",
|
||||
rate: 8.23,
|
||||
code: "TEST-CODE",
|
||||
})
|
||||
|
||||
await service.createTaxRateRules([
|
||||
@@ -1036,6 +1055,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
code: "TEST-CODE",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1045,6 +1065,7 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
name: "Shipping Rate",
|
||||
rate: 8.23,
|
||||
is_default: true,
|
||||
code: "TEST-CODE-2",
|
||||
})
|
||||
).rejects.toThrowError(
|
||||
/Tax rate with tax_region_id: .*?, already exists./
|
||||
|
||||
@@ -27,11 +27,11 @@ export const setupTaxStructure = async (service: ITaxModuleService) => {
|
||||
const [us, dk, de, ca] = await service.createTaxRegions([
|
||||
{
|
||||
country_code: "US",
|
||||
default_tax_rate: { name: "US Default Rate", rate: 2 },
|
||||
default_tax_rate: { name: "US Default Rate", rate: 2, code: "US" },
|
||||
},
|
||||
{
|
||||
country_code: "DK",
|
||||
default_tax_rate: { name: "Denmark Default Rate", rate: 25 },
|
||||
default_tax_rate: { name: "Denmark Default Rate", rate: 25, code: "DK" },
|
||||
},
|
||||
{
|
||||
country_code: "DE",
|
||||
@@ -43,7 +43,7 @@ export const setupTaxStructure = async (service: ITaxModuleService) => {
|
||||
},
|
||||
{
|
||||
country_code: "CA",
|
||||
default_tax_rate: { name: "Canada Default Rate", rate: 5 },
|
||||
default_tax_rate: { name: "Canada Default Rate", rate: 5, code: "CA" },
|
||||
},
|
||||
])
|
||||
|
||||
|
||||
@@ -246,7 +246,7 @@
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"nullable": false,
|
||||
"mappedType": "float"
|
||||
},
|
||||
"code": {
|
||||
@@ -255,7 +255,7 @@
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"nullable": false,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"name": {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class Migration20240924114005 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
this.addSql(
|
||||
`UPDATE "tax_rate" SET code = 'default' WHERE code IS NULL;`
|
||||
)
|
||||
this.addSql(
|
||||
`ALTER TABLE IF EXISTS "tax_rate" ALTER COLUMN "code" SET NOT NULL;`
|
||||
)
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql(
|
||||
`ALTER TABLE IF EXISTS "tax_rate" ALTER COLUMN "code" DROP NOT NULL;`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -61,8 +61,8 @@ export default class TaxRate {
|
||||
rate: number | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
code: string | null = null
|
||||
@Property({ columnType: "text" })
|
||||
code: string
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text" })
|
||||
|
||||
Reference in New Issue
Block a user