fix: Updating store currencies (#984)
* fix: Adds default currency to store currencies on create * fix: Adds integration tests + currency update error handling * add test * Lowercase currency codes * fix tests * fix: Await drop database to stop Jest from complaining * revert itnegration tests fix * fix store tests
This commit is contained in:
1 parent
3bf32e5dc9
commit
59bb413245
5 files changed
+412
-16
No files matched your search
@@ -53,7 +53,11 @@ describe("StoreService", () => {
|
||||
describe("update", () => {
|
||||
const storeRepository = MockRepository({
|
||||
findOne: () =>
|
||||
Promise.resolve({ id: IdMap.getId("store"), name: "Medusa" }),
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("store"),
|
||||
name: "Medusa",
|
||||
default_currency_code: "usd",
|
||||
}),
|
||||
})
|
||||
|
||||
const currencyRepository = MockRepository({})
|
||||
@@ -79,13 +83,14 @@ describe("StoreService", () => {
|
||||
expect(storeRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("store"),
|
||||
name: "Medusa Commerce",
|
||||
default_currency_code: "usd",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails if currency not ok", async () => {
|
||||
await expect(
|
||||
storeService.update({
|
||||
currencies: ["1cd"],
|
||||
currencies: ["1cd", "usd"],
|
||||
})
|
||||
).rejects.toThrow("Invalid currency 1cd")
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ class StoreService extends BaseService {
|
||||
this.currencyRepository_
|
||||
)
|
||||
|
||||
const store = await this.retrieve()
|
||||
const store = await this.retrieve(["currencies"])
|
||||
|
||||
const {
|
||||
metadata,
|
||||
@@ -125,23 +125,20 @@ class StoreService extends BaseService {
|
||||
store.metadata = this.setMetadata_(store.id, metadata)
|
||||
}
|
||||
|
||||
if (default_currency_code) {
|
||||
const curr = await currencyRepository.findOne({
|
||||
code: default_currency_code.toLowerCase(),
|
||||
})
|
||||
if (storeCurrencies) {
|
||||
const defaultCurr = default_currency_code ?? store.default_currency_code
|
||||
const hasDefCurrency = storeCurrencies.find(
|
||||
(c) => c.toLowerCase() === defaultCurr.toLowerCase()
|
||||
)
|
||||
|
||||
if (!curr) {
|
||||
// throw if we are trying to remove a currency from store currently used as default
|
||||
if (!hasDefCurrency) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Currency ${default_currency_code} not found`
|
||||
`You are not allowed to remove default currency from store currencies without replacing it as well`
|
||||
)
|
||||
}
|
||||
|
||||
store.default_currency = curr
|
||||
store.default_currency_code = curr.code
|
||||
}
|
||||
|
||||
if (storeCurrencies) {
|
||||
store.currencies = await Promise.all(
|
||||
storeCurrencies.map(async (curr) => {
|
||||
const currency = await currencyRepository.findOne({
|
||||
@@ -160,6 +157,35 @@ class StoreService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
if (default_currency_code) {
|
||||
const storeCurrCodes = store.currencies.map((c) => c.code)
|
||||
const hasDefCurrency = storeCurrCodes.find(
|
||||
(c) => c === default_currency_code.toLowerCase()
|
||||
)
|
||||
|
||||
// throw if store currencies does not have default currency
|
||||
if (!hasDefCurrency) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Store does not have currency: ${default_currency_code}`
|
||||
)
|
||||
}
|
||||
|
||||
const curr = await currencyRepository.findOne({
|
||||
code: default_currency_code.toLowerCase(),
|
||||
})
|
||||
|
||||
if (!curr) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Currency ${default_currency_code} not found`
|
||||
)
|
||||
}
|
||||
|
||||
store.default_currency = curr
|
||||
store.default_currency_code = curr.code
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(rest)) {
|
||||
store[key] = value
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user