Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa): keep country_code when partially updating cart addresses #2683

Merged
merged 8 commits into from
Dec 12, 2022
5 changes: 5 additions & 0 deletions .changeset/serious-glasses-impress.md
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions integration-tests/api/__tests__/store/cart/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2476,4 +2476,4 @@ Object {
"tracking_links": Array [],
"tracking_number": "",
}
`;
`;
6 changes: 3 additions & 3 deletions packages/medusa/src/services/__tests__/cart.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 3 additions & 5 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down