From de85a971c6af6202c87a696762b831a05316c62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Mon, 19 Sep 2022 20:30:31 +0200 Subject: [PATCH] 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 --- .../api/__tests__/admin/price-list.js | 32 +++++++++++++++---- .../src/services/__tests__/price-list.js | 4 ++- packages/medusa/src/services/price-list.ts | 7 ++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/integration-tests/api/__tests__/admin/price-list.js b/integration-tests/api/__tests__/admin/price-list.js index e9b953c917..7b3c1cc3f3 100644 --- a/integration-tests/api/__tests__/admin/price-list.js +++ b/integration-tests/api/__tests__/admin/price-list.js @@ -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) }) diff --git a/packages/medusa/src/services/__tests__/price-list.js b/packages/medusa/src/services/__tests__/price-list.js index 5a6a2ffb8c..3c385f024d 100644 --- a/packages/medusa/src/services/__tests__/price-list.js +++ b/packages/medusa/src/services/__tests__/price-list.js @@ -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, diff --git a/packages/medusa/src/services/price-list.ts b/packages/medusa/src/services/price-list.ts index e7083b7791..1f564d6f37 100644 --- a/packages/medusa/src/services/price-list.ts +++ b/packages/medusa/src/services/price-list.ts @@ -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)