Merge pull request #1475 from medusajs/fix-cartServiceCreateLostShippingAddress
This commit is contained in:
@@ -98,7 +98,7 @@ Object {
|
|||||||
"payment_authorized_at": null,
|
"payment_authorized_at": null,
|
||||||
"payment_id": null,
|
"payment_id": null,
|
||||||
"region_id": "test-region",
|
"region_id": "test-region",
|
||||||
"shipping_address_id": StringMatching /\\^addr_\\*/,
|
"shipping_address_id": "test-shipping-address",
|
||||||
"type": "swap",
|
"type": "swap",
|
||||||
"updated_at": Any<String>,
|
"updated_at": Any<String>,
|
||||||
},
|
},
|
||||||
@@ -274,7 +274,7 @@ Object {
|
|||||||
"payment_authorized_at": null,
|
"payment_authorized_at": null,
|
||||||
"payment_id": null,
|
"payment_id": null,
|
||||||
"region_id": "test-region",
|
"region_id": "test-region",
|
||||||
"shipping_address_id": StringMatching /\\^addr_\\*/,
|
"shipping_address_id": "test-shipping-address",
|
||||||
"type": "swap",
|
"type": "swap",
|
||||||
"updated_at": Any<String>,
|
"updated_at": Any<String>,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,18 +1,7 @@
|
|||||||
const path = require("path")
|
const path = require("path")
|
||||||
const {
|
const {
|
||||||
Region,
|
|
||||||
Order,
|
|
||||||
Customer,
|
|
||||||
ShippingProfile,
|
ShippingProfile,
|
||||||
Product,
|
|
||||||
ProductVariant,
|
|
||||||
MoneyAmount,
|
|
||||||
LineItem,
|
|
||||||
Payment,
|
|
||||||
Cart,
|
|
||||||
ShippingMethod,
|
|
||||||
ShippingOption,
|
ShippingOption,
|
||||||
Swap,
|
|
||||||
} = require("@medusajs/medusa")
|
} = require("@medusajs/medusa")
|
||||||
|
|
||||||
const setupServer = require("../../../helpers/setup-server")
|
const setupServer = require("../../../helpers/setup-server")
|
||||||
@@ -137,7 +126,7 @@ describe("/store/carts", () => {
|
|||||||
type: "swap",
|
type: "swap",
|
||||||
created_at: expect.any(String),
|
created_at: expect.any(String),
|
||||||
updated_at: expect.any(String),
|
updated_at: expect.any(String),
|
||||||
shipping_address_id: expect.stringMatching(/^addr_*/),
|
shipping_address_id: "test-shipping-address",
|
||||||
metadata: {
|
metadata: {
|
||||||
swap_id: expect.stringMatching(/^swap_*/),
|
swap_id: expect.stringMatching(/^swap_*/),
|
||||||
},
|
},
|
||||||
@@ -221,7 +210,7 @@ describe("/store/carts", () => {
|
|||||||
cart: {
|
cart: {
|
||||||
id: expect.stringMatching(/^cart_*/),
|
id: expect.stringMatching(/^cart_*/),
|
||||||
billing_address_id: "test-billing-address",
|
billing_address_id: "test-billing-address",
|
||||||
shipping_address_id: expect.stringMatching(/^addr_*/),
|
shipping_address_id: "test-shipping-address",
|
||||||
type: "swap",
|
type: "swap",
|
||||||
created_at: expect.any(String),
|
created_at: expect.any(String),
|
||||||
updated_at: expect.any(String),
|
updated_at: expect.any(String),
|
||||||
|
|||||||
+2
-2
@@ -2389,9 +2389,9 @@ Object {
|
|||||||
"created_at": Any<Date>,
|
"created_at": Any<Date>,
|
||||||
"customer_id": null,
|
"customer_id": null,
|
||||||
"deleted_at": null,
|
"deleted_at": null,
|
||||||
"first_name": null,
|
"first_name": "Chyna",
|
||||||
"id": Any<String>,
|
"id": Any<String>,
|
||||||
"last_name": null,
|
"last_name": "Osinski",
|
||||||
"metadata": null,
|
"metadata": null,
|
||||||
"phone": "12353245",
|
"phone": "12353245",
|
||||||
"postal_code": "1234",
|
"postal_code": "1234",
|
||||||
|
|||||||
@@ -204,7 +204,21 @@ describe("CartService", () => {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const addressRepository = MockRepository({ create: (c) => c })
|
const addressRepository = MockRepository({
|
||||||
|
create: (c) => c,
|
||||||
|
findOne: (id) => {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
first_name: "LeBron",
|
||||||
|
last_name: "James",
|
||||||
|
address_1: "Dunk St",
|
||||||
|
city: "Dunkville",
|
||||||
|
province: "CA",
|
||||||
|
postal_code: "12345",
|
||||||
|
country_code: "us",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
const cartRepository = MockRepository()
|
const cartRepository = MockRepository()
|
||||||
const customerService = {
|
const customerService = {
|
||||||
retrieveByEmail: jest.fn().mockReturnValue(
|
retrieveByEmail: jest.fn().mockReturnValue(
|
||||||
@@ -262,7 +276,7 @@ describe("CartService", () => {
|
|||||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("creates a cart with a prefilled shipping address", async () => {
|
it("should throw shipping country not in region", async () => {
|
||||||
const res = cartService.create({
|
const res = cartService.create({
|
||||||
region_id: IdMap.getId("testRegion"),
|
region_id: IdMap.getId("testRegion"),
|
||||||
shipping_address: {
|
shipping_address: {
|
||||||
@@ -279,7 +293,7 @@ describe("CartService", () => {
|
|||||||
await expect(res).rejects.toThrow("Shipping country not in region")
|
await expect(res).rejects.toThrow("Shipping country not in region")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("creates a cart with a prefilled shipping address", async () => {
|
it("a cart with a prefilled shipping address", async () => {
|
||||||
await cartService.create({
|
await cartService.create({
|
||||||
region_id: IdMap.getId("testRegion"),
|
region_id: IdMap.getId("testRegion"),
|
||||||
shipping_address: {
|
shipping_address: {
|
||||||
|
|||||||
@@ -362,41 +362,32 @@ class CartService extends TransactionBaseService<CartService> {
|
|||||||
|
|
||||||
rawCart.region_id = region.id
|
rawCart.region_id = region.id
|
||||||
|
|
||||||
if (data.shipping_address_id !== undefined) {
|
if (!data.shipping_address && !data.shipping_address_id) {
|
||||||
const shippingAddress = data.shipping_address_id
|
|
||||||
? await addressRepo.findOne(data.shipping_address_id)
|
|
||||||
: null
|
|
||||||
|
|
||||||
if (
|
|
||||||
shippingAddress &&
|
|
||||||
!regCountries.includes(shippingAddress.country_code)
|
|
||||||
) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.NOT_ALLOWED,
|
|
||||||
"Shipping country not in region"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
rawCart.shipping_address = shippingAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!data.shipping_address) {
|
|
||||||
if (region.countries.length === 1) {
|
if (region.countries.length === 1) {
|
||||||
// Preselect the country if the region only has 1
|
|
||||||
// and create address entity
|
|
||||||
rawCart.shipping_address = addressRepo.create({
|
rawCart.shipping_address = addressRepo.create({
|
||||||
country_code: regCountries[0],
|
country_code: regCountries[0],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!regCountries.includes(data.shipping_address.country_code)) {
|
if (data.shipping_address) {
|
||||||
throw new MedusaError(
|
if (!regCountries.includes(data.shipping_address.country_code)) {
|
||||||
MedusaError.Types.NOT_ALLOWED,
|
throw new MedusaError(
|
||||||
"Shipping country not in region"
|
MedusaError.Types.NOT_ALLOWED,
|
||||||
)
|
"Shipping country not in region"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rawCart.shipping_address = data.shipping_address
|
||||||
|
}
|
||||||
|
if (data.shipping_address_id) {
|
||||||
|
const addr = await addressRepo.findOne(data.shipping_address_id)
|
||||||
|
if (addr && !regCountries.includes(addr.country_code)) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.NOT_ALLOWED,
|
||||||
|
"Shipping country not in region"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rawCart.shipping_address_id = data.shipping_address_id
|
||||||
}
|
}
|
||||||
|
|
||||||
rawCart.shipping_address = data.shipping_address
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const remainingFields: (keyof Cart)[] = [
|
const remainingFields: (keyof Cart)[] = [
|
||||||
|
|||||||
Reference in New Issue
Block a user