From 33aa3edb805c217cf8e024af4c5be1835eb397dd Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:03:54 +0100 Subject: [PATCH] fix(medusa): keep country_code when partially updating cart addresses (#2683) **What** - Fixes a bug where `country_code` was dropped when partially updating a shipping or billing address on a cart. --- .changeset/serious-glasses-impress.md | 5 +++ .../api/__tests__/store/cart/cart.js | 38 +++++++++++++++++++ .../__snapshots__/index.js.snap | 4 +- .../medusa/src/services/__tests__/cart.js | 6 +-- packages/medusa/src/services/cart.ts | 8 ++-- 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 .changeset/serious-glasses-impress.md diff --git a/.changeset/serious-glasses-impress.md b/.changeset/serious-glasses-impress.md new file mode 100644 index 0000000000..e39813605e --- /dev/null +++ b/.changeset/serious-glasses-impress.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix: Fixes a bug where `country_code` was dropped when partially updating a shipping and/or billing address on a cart. diff --git a/integration-tests/api/__tests__/store/cart/cart.js b/integration-tests/api/__tests__/store/cart/cart.js index 8227ba24f3..a27273dc03 100644 --- a/integration-tests/api/__tests__/store/cart/cart.js +++ b/integration-tests/api/__tests__/store/cart/cart.js @@ -1642,6 +1642,44 @@ describe("/store/carts", () => { expect(response.status).toEqual(200) }) + it("partially updates shipping and billing address while retaining the addresses country codes", async () => { + const api = useApi() + + // Partially update the shipping address + await api + .post("/store/carts/test-cart", { + shipping_address: { + last_name: "james", + }, + }) + .catch((error) => console.log(error)) + + // Partially update the billing address + const response = await api.post("/store/carts/test-cart", { + billing_address: { + first_name: "bruce", + last_name: "banner", + country_code: "us", + }, + }) + + expect(response.status).toEqual(200) + expect(response.data.cart).toEqual( + expect.objectContaining({ + shipping_address: expect.objectContaining({ + first_name: "lebron", + last_name: "james", + country_code: "us", + }), + billing_address: expect.objectContaining({ + first_name: "bruce", + last_name: "banner", + country_code: "us", + }), + }) + ) + }) + it("adds free shipping to cart then removes it again", async () => { const api = useApi() diff --git a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap index b12a9dbfee..0f2cb0fe6a 100644 --- a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap +++ b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap @@ -2415,7 +2415,7 @@ Object { "address_2": null, "city": "ville la something", "company": null, - "country_code": null, + "country_code": "us", "created_at": Any, "customer_id": null, "deleted_at": null, @@ -2476,4 +2476,4 @@ Object { "tracking_links": Array [], "tracking_number": "", } -`; \ No newline at end of file +`; diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 0cb3fd25f5..ba0ded1d45 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -1,12 +1,12 @@ import _ from "lodash" import { MedusaError } from "medusa-core-utils" import { IdMap, MockManager, MockRepository } from "medusa-test-utils" +import { FlagRouter } from "../../utils/flag-router" import CartService from "../cart" import { InventoryServiceMock } from "../__mocks__/inventory" import { LineItemAdjustmentServiceMock } from "../__mocks__/line-item-adjustment" -import { FlagRouter } from "../../utils/flag-router" -import { taxProviderServiceMock } from "../__mocks__/tax-provider" import { newTotalsServiceMock } from "../__mocks__/new-totals" +import { taxProviderServiceMock } from "../__mocks__/tax-provider" const eventBusService = { emit: jest.fn(), @@ -1100,7 +1100,7 @@ describe("CartService", () => { last_name: "James", address_1: "24 Dunks Drive", city: "Los Angeles", - country_code: "US", + country_code: "us", province: "CA", postal_code: "93011", phone: "+1 (222) 333 4444", diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index 769bb49093..bd6d1a9dbc 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -1238,8 +1238,6 @@ class CartService extends TransactionBaseService { address = addressOrId as Address } - address.country_code = address.country_code?.toLowerCase() ?? null - if (address.id) { cart.billing_address = await addrRepo.save(address) } else { @@ -1284,11 +1282,11 @@ class CartService extends TransactionBaseService { address = addressOrId as Address } - address.country_code = address.country_code?.toLowerCase() ?? null - if ( address.country_code && - !cart.region.countries.find(({ iso_2 }) => address.country_code === iso_2) + !cart.region.countries.find( + ({ iso_2 }) => address.country_code?.toLowerCase() === iso_2 + ) ) { throw new MedusaError( MedusaError.Types.INVALID_DATA,