fix(medusa): Proper fix of the cart service
This commit is contained in:
1 parent
32a65d27fc
commit
f5edaf51ea
2 files changed
+22
-31
No files matched your search
@@ -276,7 +276,7 @@ describe("CartService", () => {
|
|||||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
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({
|
await cartService.create({
|
||||||
region_id: IdMap.getId("testRegion"),
|
region_id: IdMap.getId("testRegion"),
|
||||||
email: "email@test.com",
|
email: "email@test.com",
|
||||||
@@ -309,7 +309,7 @@ describe("CartService", () => {
|
|||||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
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({
|
const res = cartService.create({
|
||||||
region_id: IdMap.getId("testRegion"),
|
region_id: IdMap.getId("testRegion"),
|
||||||
shipping_address: {
|
shipping_address: {
|
||||||
|
|||||||
@@ -393,41 +393,32 @@ class CartService extends BaseService {
|
|||||||
|
|
||||||
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
|
if (region.countries.length === 1) {
|
||||||
? 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
|
|
||||||
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 = addr
|
||||||
}
|
}
|
||||||
|
|
||||||
rawCart.shipping_address = data.shipping_address
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const remainingFields: (keyof Cart)[] = [
|
const remainingFields: (keyof Cart)[] = [
|
||||||
|
|||||||
Reference in new issue
Block a user