feat: Add currency module and remove currency models from region and pricing modules (#6536)

What:
- Creates a new currency module
- Removes currency model from the pricing module
- Removes currency model from region module
This commit is contained in:
Stevche Radevski
2024-02-29 16:09:59 +01:00
committed by GitHub
parent 06f706a51a
commit dc025302a1
102 changed files with 1502 additions and 2128 deletions

View File

@@ -42,7 +42,7 @@ describe("API Keys - Admin", () => {
await createAdminUser(dbConnection, adminHeaders)
// Used for testing cross-module authentication checks
await regionService.createDefaultCountriesAndCurrencies()
await regionService.createDefaultCountries()
})
afterEach(async () => {

View File

@@ -63,7 +63,7 @@ describe("Carts workflows", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
await regionModuleService.createDefaultCountriesAndCurrencies()
await regionModuleService.createDefaultCountries()
// Here, so we don't have to create a region for each test
defaultRegion = await regionModuleService.create({

View File

@@ -59,7 +59,7 @@ describe("Store Carts API", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
await regionModuleService.createDefaultCountriesAndCurrencies()
await regionModuleService.createDefaultCountries()
// Here, so we don't have to create a region for each test
defaultRegion = await regionModuleService.create({

View File

@@ -0,0 +1,67 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICurrencyModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { DataSource } from "typeorm"
import { createAdminUser } from "../../../helpers/create-admin-user"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("Currency - Admin", () => {
let dbConnection: DataSource
let appContainer
let shutdownServer
let service: ICurrencyModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.CURRENCY)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should correctly retrieve and list currencies", async () => {
const api = useApi() as any
const listResp = await api.get("/admin/currencies", adminHeaders)
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
}),
expect.objectContaining({
code: "cad",
}),
])
)
const retrieveResp = await api.get(`/admin/currencies/aud`, adminHeaders)
expect(retrieveResp.data.currency).toEqual(
listResp.data.currencies.find((c) => c.code === "aud")
)
})
})

View File

@@ -0,0 +1,62 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICurrencyModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { DataSource } from "typeorm"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const storeHeaders = {
headers: {},
}
describe("Currency - Store", () => {
let dbConnection: DataSource
let appContainer
let shutdownServer
let service: ICurrencyModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.CURRENCY)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should correctly retrieve and list currencies", async () => {
const api = useApi() as any
const listResp = await api.get("/store/currencies", storeHeaders)
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
}),
expect.objectContaining({
code: "cad",
}),
])
)
const retrieveResp = await api.get(`/store/currencies/aud`, storeHeaders)
expect(retrieveResp.data.currency).toEqual(
listResp.data.currencies.find((c) => c.code === "aud")
)
})
})

View File

@@ -45,7 +45,7 @@ describe("Cart links", () => {
beforeEach(async () => {
// @ts-ignore
await regionModule.createDefaultCountriesAndCurrencies()
await regionModule.createDefaultCountries()
})
afterEach(async () => {

View File

@@ -35,7 +35,7 @@ describe("Link: Cart Region", () => {
beforeEach(async () => {
// @ts-ignore
await regionModule.createDefaultCountriesAndCurrencies()
await regionModule.createDefaultCountries()
})
afterEach(async () => {

View File

@@ -38,7 +38,7 @@ describe("Regions - Admin", () => {
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
await service.createDefaultCountriesAndCurrencies()
await service.createDefaultCountries()
})
afterEach(async () => {
@@ -127,25 +127,6 @@ describe("Regions - Admin", () => {
)
})
it("should throw on unknown currency in create", async () => {
const api = useApi() as any
const error = await api
.post(
`/admin/regions`,
{
currency_code: "foo",
name: "Test Region",
},
adminHeaders
)
.catch((e) => e)
expect(error.response.status).toEqual(400)
expect(error.response.data.message).toEqual(
'Currencies with codes: "foo" were not found'
)
})
it("should throw on unknown properties in create", async () => {
const api = useApi() as any
const error = await api

View File

@@ -128,5 +128,10 @@ module.exports = {
resources: "shared",
resolve: "@medusajs/tax",
},
[Modules.CURRENCY]: {
scope: "internal",
resources: "shared",
resolve: "@medusajs/currency",
},
},
}

View File

@@ -12,6 +12,7 @@
"@medusajs/api-key": "workspace:^",
"@medusajs/auth": "workspace:*",
"@medusajs/cache-inmemory": "workspace:*",
"@medusajs/currency": "workspace:^",
"@medusajs/customer": "workspace:^",
"@medusajs/event-bus-local": "workspace:*",
"@medusajs/inventory": "workspace:^",