diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index daa05f4f0c..634910629a 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -567,5 +567,25 @@ describe("CartService", () => { } ) }) + + it("filters items that don't have region prices", async () => { + await cartService.setRegion( + IdMap.getId("cartWithLine"), + IdMap.getId("testRegion") + ) + + expect(CartModelMock.updateOne).toHaveBeenCalledTimes(1) + expect(CartModelMock.updateOne).toHaveBeenCalledWith( + { + _id: IdMap.getId("cartWithLine"), + }, + { + $set: { + region_id: IdMap.getId("testRegion"), + items: [], + }, + } + ) + }) }) }) diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index 8d54e126dd..81d04af642 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -213,6 +213,13 @@ class CartService extends BaseService { const validatedLineItem = this.validateLineItem_(lineItem) const cart = await this.retrieve(cartId) + if (!cart) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + "The cart was not found" + ) + } + const currentItem = cart.items.find(line => _.isEqual(line.content, validatedLineItem.content) ) @@ -406,15 +413,19 @@ class CartService extends BaseService { if (cart.items.length) { const newItems = await Promise.all( cart.items.map(async lineItem => { - lineItem.content = await this.updateContentPrice_( - lineItem.content, - region._id - ) + try { + lineItem.content = await this.updateContentPrice_( + lineItem.content, + region._id + ) + } catch (err) { + return null + } return lineItem }) ) - update.items = newItems + update.items = newItems.filter(i => !!i) } // If the country code of a shipping address is set we need to clear it diff --git a/packages/medusa/src/services/product-variant.js b/packages/medusa/src/services/product-variant.js index c9624eca17..8b5cfaa18f 100644 --- a/packages/medusa/src/services/product-variant.js +++ b/packages/medusa/src/services/product-variant.js @@ -143,7 +143,8 @@ class ProductVariantService extends BaseService { ) } - // If prices already exist we need to update all prices with the same currency + // If prices already exist we need to update all prices with the same + // currency if (variant.prices.length) { let foundDefault = false const newPrices = variant.prices.map(moneyAmount => {