feat(regions): Add support for updating countries in a region (#6432)

**What**
Add missing support for updating countries on the POST /admin/regions/:id route.
Furthermore, the PR refactors how the normalization and validation was handled in both create and update scenarios.
Finally, the PR adds a unique index on the country entity, which ensures at the DB level that we cannot have a duplicate country in the DB for a single region.
This commit is contained in:
Stevche Radevski
2024-02-20 12:46:42 +01:00
committed by GitHub
parent c319edb8e0
commit cfefd59249
10 changed files with 436 additions and 132 deletions

View File

@@ -52,6 +52,8 @@ describe("Regions - Admin", () => {
{
name: "Test Region",
currency_code: "usd",
countries: ["us", "ca"],
metadata: { foo: "bar" },
},
adminHeaders
)
@@ -62,14 +64,21 @@ describe("Regions - Admin", () => {
id: created.data.region.id,
name: "Test Region",
currency_code: "usd",
metadata: { foo: "bar" },
})
)
expect(created.data.region.countries.map((c) => c.iso_2)).toEqual([
"us",
"ca",
])
const updated = await api.post(
`/admin/regions`,
`/admin/regions/${created.data.region.id}`,
{
name: "United States",
currency_code: "usd",
countries: ["us"],
metadata: { foo: "baz" },
},
adminHeaders
)
@@ -78,9 +87,12 @@ describe("Regions - Admin", () => {
expect(updated.data.region).toEqual(
expect.objectContaining({
id: updated.data.region.id,
name: "United States",
currency_code: "usd",
metadata: { foo: "baz" },
})
)
expect(updated.data.region.countries.map((c) => c.iso_2)).toEqual(["us"])
const deleted = await api.delete(
`/admin/regions/${updated.data.region.id}`,
@@ -129,7 +141,7 @@ describe("Regions - Admin", () => {
expect(error.response.status).toEqual(400)
expect(error.response.data.message).toEqual(
"Currency with code: foo was not found"
'Currencies with codes: "foo" were not found'
)
})
@@ -180,6 +192,8 @@ describe("Regions - Admin", () => {
{
name: "Test",
currency_code: "usd",
countries: ["jp"],
metadata: { foo: "bar" },
},
])
@@ -192,8 +206,12 @@ describe("Regions - Admin", () => {
id: expect.any(String),
name: "Test",
currency_code: "usd",
metadata: { foo: "bar" },
}),
])
expect(response.data.regions[0].countries.map((c) => c.iso_2)).toEqual([
"jp",
])
})
it("should get a region", async () => {
@@ -201,6 +219,8 @@ describe("Regions - Admin", () => {
{
name: "Test",
currency_code: "usd",
countries: ["jp"],
metadata: { foo: "bar" },
},
])
@@ -213,7 +233,9 @@ describe("Regions - Admin", () => {
id: region.id,
name: "Test",
currency_code: "usd",
metadata: { foo: "bar" },
})
)
expect(response.data.region.countries.map((c) => c.iso_2)).toEqual(["jp"])
})
})