From 85de2039cc0b47d2f026d7225ccc8c9dc9bcb676 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Thu, 23 Sep 2021 10:52:46 +0200 Subject: [PATCH] hotfix: bug related to region update with no shipping address (#415) --- integration-tests/api/__tests__/store/cart.js | 18 ++++++++++++++++++ integration-tests/api/helpers/cart-seeder.js | 16 ++++++++++++++++ packages/medusa/src/services/__tests__/cart.js | 3 +++ packages/medusa/src/services/cart.js | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index afd35f4e69..215f77d194 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -112,6 +112,24 @@ describe("/store/carts", () => { await doAfterEach() }) + // We were experiencing some issues when having created a cart in a region + // containing multiple countries. At this point, the cart does not have a shipping + // address. Therefore, on subsequent requests to update the cart, the server + // would throw a 500 due to missing shipping address id on insertion. + it("updates a cart, that does not have a shipping address", async () => { + const api = useApi() + + const response = await api.post("/store/carts", { + region_id: "test-region-multiple", + }) + + const getRes = await api.post(`/store/carts/${response.data.cart.id}`, { + region_id: "test-region", + }) + + expect(getRes.status).toEqual(200) + }) + it("fails on apply discount if limit has been reached", async () => { const api = useApi() diff --git a/integration-tests/api/helpers/cart-seeder.js b/integration-tests/api/helpers/cart-seeder.js index c59ea96e3f..7a75d418b9 100644 --- a/integration-tests/api/helpers/cart-seeder.js +++ b/integration-tests/api/helpers/cart-seeder.js @@ -40,6 +40,22 @@ module.exports = async (connection, data = {}) => { tax_rate: 0, }) + // Region with multiple countries + const regionWithMultipleCoutries = manager.create(Region, { + id: "test-region-multiple", + name: "Test Region", + currency_code: "eur", + tax_rate: 0, + }) + + await manager.save(regionWithMultipleCoutries) + await manager.query( + `UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'no'` + ) + await manager.query( + `UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'dk'` + ) + const freeRule = manager.create(DiscountRule, { id: "free-shipping-rule", description: "Free shipping rule", diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 18919d6b29..f887a6a9d5 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -1083,6 +1083,9 @@ describe("CartService", () => { id: "region", countries: [{ iso_2: "us" }], }, + shipping_address: { + country_code: "us", + }, items: [IdMap.getId("testitem"), null], payment_session: null, payment_sessions: [], diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index 1dfba8d51c..de1f3ab164 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -1446,7 +1446,7 @@ class CartService extends BaseService { } } - await addrRepo.save(updated) + await this.updateShippingAddress_(cart, updated, addrRepo) } // Shipping methods are determined by region so the user needs to find a