fix(medusa): Proper fix of the cart service
This commit is contained in:
@@ -276,7 +276,7 @@ describe("CartService", () => {
|
||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("successfully creates a cart with a shipping address id", async () => {
|
||||
it("successfully creates a cart with a shipping address id but no shippings_address", async () => {
|
||||
await cartService.create({
|
||||
region_id: IdMap.getId("testRegion"),
|
||||
email: "email@test.com",
|
||||
@@ -309,7 +309,7 @@ describe("CartService", () => {
|
||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("creates a cart with a prefilled shipping address", async () => {
|
||||
it("creates a cart with a prefilled shipping address but a country not part of the region", async () => {
|
||||
const res = cartService.create({
|
||||
region_id: IdMap.getId("testRegion"),
|
||||
shipping_address: {
|
||||
|
||||
@@ -393,41 +393,32 @@ class CartService extends BaseService {
|
||||
|
||||
rawCart.region_id = region.id
|
||||
|
||||
if (data.shipping_address_id !== undefined) {
|
||||
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 (!rawCart.shipping_address && region.countries.length === 1) {
|
||||
// Preselect the country if the region only has 1
|
||||
// and create address entity
|
||||
if (!data.shipping_address && !data.shipping_address_id) {
|
||||
if (region.countries.length === 1) {
|
||||
rawCart.shipping_address = addressRepo.create({
|
||||
country_code: regCountries[0],
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (!regCountries.includes(data.shipping_address.country_code)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"Shipping country not in region"
|
||||
)
|
||||
if (data.shipping_address) {
|
||||
if (!regCountries.includes(data.shipping_address.country_code)) {
|
||||
throw new MedusaError(
|
||||
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 = addr
|
||||
}
|
||||
|
||||
rawCart.shipping_address = data.shipping_address
|
||||
}
|
||||
|
||||
const remainingFields: (keyof Cart)[] = [
|
||||
|
||||
Reference in New Issue
Block a user