fix(medusa): Upserting tax rates (#4189)

* fix(medusa): Upserting tax rates

* Create orange-bikes-sparkle.md

* add more tests

* add explicit null type
This commit is contained in:
Oliver Windall Juhl
2023-05-29 13:31:39 +02:00
committed by GitHub
parent ded667f568
commit 6998666c6e
5 changed files with 132 additions and 44 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Upserting tax rates
@@ -1,17 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/admin/tax-rates creates a tax rate 1`] = `
Object {
"code": "tricks",
"created_at": Any<String>,
"id": StringMatching /\\^txr_\\*/,
"name": "special",
"rate": null,
"region_id": "test-region",
"updated_at": Any<String>,
}
`;
exports[`/admin/tax-rates deletes a tax rate 1`] = `
Object {
"deleted": true,
@@ -38,15 +26,3 @@ Object {
"region_id": Any<String>,
}
`;
exports[`/admin/tax-rates updates a tax rate 1`] = `
Object {
"code": "something new",
"created_at": Any<String>,
"id": StringMatching /\\^txr_\\*/,
"name": "special",
"rate": 10,
"region_id": "test-region",
"updated_at": Any<String>,
}
`;
@@ -397,11 +397,84 @@ describe("/admin/tax-rates", () => {
)
expect(response.status).toEqual(200)
expect(response.data.tax_rate).toMatchSnapshot({
expect(response.data.tax_rate).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^txr_*/),
created_at: expect.any(String),
updated_at: expect.any(String),
name: "special",
code: "tricks",
rate: null,
})
)
})
test("creates a tax rate with a rate", async () => {
await adminSeeder(dbConnection)
const api = useApi()
await simpleRegionFactory(dbConnection, {
id: "test-region",
})
const response = await api.post(
`/admin/tax-rates`,
{
name: "special",
code: "tricks",
region_id: "test-region",
rate: 2,
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
expect(response.status).toEqual(200)
expect(response.data.tax_rate).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^txr_*/),
name: "special",
code: "tricks",
region_id: "test-region",
rate: 2,
})
)
})
test("creates a tax rate with a null rate", async () => {
await adminSeeder(dbConnection)
const api = useApi()
await simpleRegionFactory(dbConnection, {
id: "test-region",
})
const response = await api.post(
`/admin/tax-rates`,
{
name: "special",
code: "tricks",
region_id: "test-region",
rate: null,
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
expect(response.status).toEqual(200)
expect(response.data.tax_rate).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^txr_*/),
name: "special",
code: "tricks",
region_id: "test-region",
rate: null,
})
)
})
test("creates a tax rate and assigns products", async () => {
@@ -453,6 +526,7 @@ describe("/admin/tax-rates", () => {
{
name: "special",
code: "something new",
rate: 5,
},
{
headers: {
@@ -462,13 +536,48 @@ describe("/admin/tax-rates", () => {
)
expect(response.status).toEqual(200)
expect(response.data.tax_rate).toMatchSnapshot({
expect(response.data.tax_rate).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^txr_*/),
code: "special",
name: "special",
code: "something new",
created_at: expect.any(String),
updated_at: expect.any(String),
rate: 5,
})
)
})
test("updates a tax rate with null rate", async () => {
await adminSeeder(dbConnection)
await simpleRegionFactory(dbConnection, { id: "test-region" })
const rate = await simpleTaxRateFactory(dbConnection, {
name: "test",
code: "something",
rate: 10,
region_id: "test-region",
})
const api = useApi()
const response = await api.post(
`/admin/tax-rates/${rate.id}`,
{
rate: null,
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
expect(response.status).toEqual(200)
expect(response.data.tax_rate).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^txr_*/),
rate: null,
})
)
})
test("deletes a tax rate", async () => {
@@ -1,12 +1,11 @@
import { IsArray, IsOptional, IsString } from "class-validator"
import { IsArray, IsNumber, IsOptional, IsString } from "class-validator"
import { getRetrieveConfig, pickByConfig } from "./utils/get-query-config"
import { EntityManager } from "typeorm"
import { IsType } from "../../../../utils/validators/is-type"
import { omit } from "lodash"
import { isDefined, MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { TaxRate } from "../../../.."
import { TaxRateService } from "../../../../services"
import { omit } from "lodash"
import { validator } from "../../../../utils/validator"
/**
@@ -187,7 +186,7 @@ export class AdminPostTaxRatesReq {
region_id: string
@IsOptional()
@IsType([Number, null])
@IsNumber()
rate?: number | null
@IsOptional()
@@ -1,13 +1,12 @@
import { IsArray, IsOptional, IsString } from "class-validator"
import { IsArray, IsNumber, IsOptional, IsString } from "class-validator"
import { getRetrieveConfig, pickByConfig } from "./utils/get-query-config"
import { omit } from "lodash"
import { isDefined } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { IsType } from "../../../../utils/validators/is-type"
import { TaxRate } from "../../../.."
import { TaxRateService } from "../../../../services"
import { omit } from "lodash"
import { validator } from "../../../../utils/validator"
import { isDefined } from "medusa-core-utils"
/**
* @oas [post] /admin/tax-rates/{id}
@@ -183,7 +182,7 @@ export class AdminPostTaxRatesTaxRateReq {
region_id?: string
@IsOptional()
@IsType([Number, null])
@IsNumber()
rate?: number | null
@IsOptional()