fix: infer MA currency on PL create (#2232)

**What**
- a MoneyAmount record can be created with either providing region or currency. MA records cannot be inserted in the DB without currency due to not null constraints therefore the currency needs to be inferred from provided region

**How**
- by using the same utility that fixes this issue on PL update

**Testing**
- extend the "create PL" integration test to handle a MA with a region

---

FIXES CORE-525
This commit is contained in:
Frane Polić
2022-09-19 18:30:31 +00:00
committed by GitHub
parent e1fe5ed094
commit de85a971c6
3 changed files with 33 additions and 10 deletions
@@ -1,13 +1,16 @@
const path = require("path")
const { Region } = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server")
const startServerWithEnvironment = require("../../../helpers/start-server-with-environment").default
const startServerWithEnvironment =
require("../../../helpers/start-server-with-environment").default
const { useApi } = require("../../../helpers/use-api")
const { useDb, initDb } = require("../../../helpers/use-db")
const {
simpleProductFactory,
simplePriceListFactory,
simpleRegionFactory,
} = require("../../factories")
const adminSeeder = require("../../helpers/admin-seeder")
const customerSeeder = require("../../helpers/customer-seeder")
@@ -53,6 +56,11 @@ describe("/admin/price-lists", () => {
it("creates a price list", async () => {
const api = useApi()
const region = await simpleRegionFactory(dbConnection, {
id: "region-pl-infer-currency",
currency_code: "hrk",
})
const payload = {
name: "VIP Summer sale",
description: "Summer sale for VIP customers. 25% off selected items.",
@@ -71,6 +79,11 @@ describe("/admin/price-lists", () => {
currency_code: "usd",
variant_id: "test-variant",
},
{
amount: 105,
region_id: region.id,
variant_id: "test-variant",
},
],
}
@@ -106,6 +119,12 @@ describe("/admin/price-lists", () => {
currency_code: "usd",
variant_id: "test-variant",
}),
expect.objectContaining({
id: expect.any(String),
amount: 105,
currency_code: region.currency_code,
variant_id: "test-variant",
}),
],
})
)
@@ -1157,7 +1176,7 @@ describe("/admin/price-lists", () => {
amount: 150,
price_list_id: "test-list",
}),
],)
]),
}),
expect.objectContaining({
id: "test-variant-2",
@@ -1209,9 +1228,7 @@ describe("/admin/price-lists", () => {
expect(response.data.count).toEqual(1)
expect(response.data.products).toHaveLength(1)
expect(response.data.products).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: "test-prod-2" }),
])
expect.arrayContaining([expect.objectContaining({ id: "test-prod-2" })])
)
})
@@ -1487,9 +1504,10 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/price-lists", () => {
response = await api
.post(
`/admin/price-lists/${priceListIncludesTaxId}`,
{ includes_tax: true, },
{ includes_tax: true },
adminReqConfig
).catch((err) => {
)
.catch((err) => {
console.log(err)
})
@@ -44,6 +44,7 @@ describe("PriceListService", () => {
priceListRepository,
moneyAmountRepository,
featureFlagRouter: new FlagRouter({}),
regionService: RegionServiceMock,
})
beforeEach(async () => {
@@ -129,7 +130,8 @@ describe("PriceListService", () => {
updateRelatedMoneyAmountRepository.save = jest
.fn()
.mockImplementation(() => Promise.resolve())
updateRelatedMoneyAmountRepository.updatePriceListPrices = new MoneyAmountRepository().updatePriceListPrices
updateRelatedMoneyAmountRepository.updatePriceListPrices =
new MoneyAmountRepository().updatePriceListPrices
const updateRelatedPriceListService = new PriceListService({
manager: MockManager,
+5 -2
View File
@@ -140,7 +140,8 @@ class PriceListService extends TransactionBaseService {
const priceList = await priceListRepo.save(entity)
if (prices) {
await moneyAmountRepo.addPriceListPrices(priceList.id, prices)
const prices_ = await this.addCurrencyFromRegion(prices)
await moneyAmountRepo.addPriceListPrices(priceList.id, prices_)
}
if (customer_groups) {
@@ -507,7 +508,9 @@ class PriceListService extends TransactionBaseService {
const prices_: typeof prices = []
const regionServiceTx = this.regionService_.withTransaction(this.manager_)
for (const p of prices) {
for (const price of prices) {
const p = { ...price }
if (p.region_id) {
const region = await regionServiceTx.retrieve(p.region_id)