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.
This commit is contained in:
Kasper Fabricius Kristensen
2022-12-12 15:03:54 +01:00
committed by GitHub
parent d68e81fb3d
commit 33aa3edb80
5 changed files with 51 additions and 10 deletions

View File

@@ -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.

View File

@@ -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()

View File

@@ -2415,7 +2415,7 @@ Object {
"address_2": null,
"city": "ville la something",
"company": null,
"country_code": null,
"country_code": "us",
"created_at": Any<Date>,
"customer_id": null,
"deleted_at": null,
@@ -2476,4 +2476,4 @@ Object {
"tracking_links": Array [],
"tracking_number": "",
}
`;
`;

View File

@@ -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",

View File

@@ -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,