Add support for tax inclusivity to region and store (#7808)

This also includes rework of the currency model for the Store module.

This change is breaking as existing stores won't have any supported currencies set, so users would need to go to the store settings again and choose the supported currencies there.
This commit is contained in:
Stevche Radevski
2024-06-24 17:25:44 +02:00
committed by GitHub
parent 79d90fadc4
commit e8d6025374
45 changed files with 580 additions and 408 deletions

View File

@@ -29,8 +29,9 @@ export const seedStorefrontDefaults = async (
store = await storeModule.updateStores(store.id, {
default_region_id: region.id,
supported_currency_codes: [region.currency_code],
default_currency_code: region.currency_code,
supported_currencies: [
{ currency_code: region.currency_code, is_default: true },
],
})
return {

View File

@@ -97,8 +97,10 @@ medusaIntegrationTestRunner({
store = await storeModule.createStores({
name: "New store",
supported_currency_codes: ["usd", "dkk"],
default_currency_code: "usd",
supported_currencies: [
{ currency_code: "usd", is_default: true },
{ currency_code: "dkk" },
],
})
})
@@ -570,8 +572,10 @@ medusaIntegrationTestRunner({
}
await service.createStores({
supported_currency_codes: ["usd", "dkk"],
default_currency_code: "usd",
supported_currencies: [
{ currency_code: "usd", is_default: true },
{ currency_code: "dkk" },
],
default_sales_channel_id: defaultSalesChannel.id,
})
})
@@ -1139,8 +1143,10 @@ medusaIntegrationTestRunner({
}
await service.createStores({
supported_currency_codes: ["usd", "dkk"],
default_currency_code: "usd",
supported_currencies: [
{ currency_code: "usd", is_default: true },
{ currency_code: "dkk" },
],
default_sales_channel_id: defaultSalesChannel.id,
})
})

View File

@@ -30,8 +30,10 @@ medusaIntegrationTestRunner({
store = await storeModule.createStores({
name: "New store",
supported_currency_codes: ["usd", "dkk"],
default_currency_code: "usd",
supported_currencies: [
{ currency_code: "usd", is_default: true },
{ currency_code: "dkk" },
],
default_sales_channel_id: "sc_12345",
})
})
@@ -48,9 +50,14 @@ medusaIntegrationTestRunner({
expect.objectContaining({
id: expect.any(String),
name: "New store",
default_currency_code: "usd",
default_sales_channel_id: expect.any(String),
supported_currency_codes: ["usd", "dkk"],
supported_currencies: [
expect.objectContaining({
currency_code: "usd",
is_default: true,
}),
expect.objectContaining({ currency_code: "dkk" }),
],
created_at: expect.any(String),
updated_at: expect.any(String),
})
@@ -59,12 +66,12 @@ medusaIntegrationTestRunner({
})
describe("POST /admin/stores", () => {
it("fails to update default currency if not in store currencies", async () => {
it("fails to update default currencies if there is no default one", async () => {
const err = await api
.post(
`/admin/stores/${store.id}`,
{
default_currency_code: "eur",
supported_currencies: [{ currency_code: "eur" }],
},
adminHeaders
)
@@ -74,17 +81,19 @@ medusaIntegrationTestRunner({
expect(err.response.data).toEqual(
expect.objectContaining({
type: "invalid_data",
message: "Store does not have currency: eur",
message: "There should be a default currency set for the store",
})
)
})
// BREAKING: `currencies` was renamed to `supported_currency_codes`
// BREAKING: `currencies` was renamed to `supported_currencies`
it("fails to remove default currency from currencies without replacing it", async () => {
const err = await api
.post(
`/admin/stores/${store.id}`,
{ supported_currency_codes: ["dkk"] },
{
supported_currencies: [{ currency_code: "dkk" }],
},
adminHeaders
)
.catch((e) => e)
@@ -93,41 +102,19 @@ medusaIntegrationTestRunner({
expect(err.response.data).toEqual(
expect.objectContaining({
type: "invalid_data",
message:
"You are not allowed to remove default currency from store currencies without replacing it as well",
message: "There should be a default currency set for the store",
})
)
})
it("successfully updates default currency code", async () => {
const response = await api
.post(
`/admin/stores/${store.id}`,
{
default_currency_code: "dkk",
},
adminHeaders
)
.catch((err) => console.log(err))
expect(response.status).toEqual(200)
expect(response.data.store).toEqual(
expect.objectContaining({
id: expect.any(String),
name: "New store",
default_currency_code: "dkk",
created_at: expect.any(String),
updated_at: expect.any(String),
})
)
})
it("successfully updates default currency and store currencies", async () => {
it("successfully updates currencies and default currency", async () => {
const response = await api.post(
`/admin/stores/${store.id}`,
{
default_currency_code: "jpy",
supported_currency_codes: ["jpy", "usd"],
supported_currencies: [
{ currency_code: "usd" },
{ currency_code: "dkk", is_default: true },
],
},
adminHeaders
)
@@ -137,9 +124,15 @@ medusaIntegrationTestRunner({
expect.objectContaining({
id: expect.any(String),
name: "New store",
default_sales_channel_id: expect.any(String),
supported_currency_codes: ["jpy", "usd"],
default_currency_code: "jpy",
supported_currencies: [
expect.objectContaining({
currency_code: "usd",
}),
expect.objectContaining({
currency_code: "dkk",
is_default: true,
}),
],
created_at: expect.any(String),
updated_at: expect.any(String),
})
@@ -150,7 +143,10 @@ medusaIntegrationTestRunner({
const response = await api.post(
`/admin/stores/${store.id}`,
{
supported_currency_codes: ["jpy", "usd"],
supported_currencies: [
{ currency_code: "jpy", is_default: true },
{ currency_code: "usd" },
],
},
adminHeaders
)
@@ -161,8 +157,15 @@ medusaIntegrationTestRunner({
id: expect.any(String),
default_sales_channel_id: expect.any(String),
name: "New store",
supported_currency_codes: ["jpy", "usd"],
default_currency_code: "usd",
supported_currencies: [
expect.objectContaining({
currency_code: "jpy",
is_default: true,
}),
expect.objectContaining({
currency_code: "usd",
}),
],
created_at: expect.any(String),
updated_at: expect.any(String),
})

View File

@@ -1,5 +1,6 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICurrencyModuleService, IStoreModuleService } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -27,24 +28,29 @@ medusaIntegrationTestRunner({
it("should query store and default currency with remote query", async () => {
const store = await storeModuleService.createStores({
name: "Store",
default_currency_code: "usd",
supported_currency_codes: ["usd"],
supported_currencies: [{ currency_code: "usd", is_default: true }],
})
const stores = await remoteQuery({
store: {
fields: ["id"],
default_currency: {
fields: ["code"],
},
},
const query = remoteQueryObjectFromString({
entryPoint: "store",
fields: [
"id",
"supported_currencies.*",
"supported_currencies.currency.*",
],
})
const stores = await remoteQuery(query)
expect(stores).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: store.id,
default_currency: expect.objectContaining({ code: "usd" }),
supported_currencies: expect.arrayContaining([
expect.objectContaining({
currency: expect.objectContaining({ code: "usd" }),
currency_code: "usd",
}),
]),
}),
])
)

View File

@@ -29,13 +29,18 @@ medusaIntegrationTestRunner({
it("should correctly implement the entire lifecycle of a store", async () => {
const createdStore = await service.createStores({
name: "Test store",
supported_currency_codes: ["usd"],
supported_currencies: [{ currency_code: "usd", is_default: true }],
})
expect(createdStore).toEqual(
expect.objectContaining({
id: createdStore.id,
supported_currency_codes: ["usd"],
supported_currencies: [
expect.objectContaining({
currency_code: "usd",
is_default: true,
}),
],
name: "Test store",
})
)