cart service: setRegion - filter items that don't have region price

This commit is contained in:
Sebastian Rindom
2020-02-08 18:35:56 +01:00
parent 64afe158bb
commit d114b806d6
3 changed files with 38 additions and 6 deletions
@@ -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: [],
},
}
)
})
}) })
}) })
+16 -5
View File
@@ -213,6 +213,13 @@ class CartService extends BaseService {
const validatedLineItem = this.validateLineItem_(lineItem) const validatedLineItem = this.validateLineItem_(lineItem)
const cart = await this.retrieve(cartId) 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 => const currentItem = cart.items.find(line =>
_.isEqual(line.content, validatedLineItem.content) _.isEqual(line.content, validatedLineItem.content)
) )
@@ -406,15 +413,19 @@ class CartService extends BaseService {
if (cart.items.length) { if (cart.items.length) {
const newItems = await Promise.all( const newItems = await Promise.all(
cart.items.map(async lineItem => { cart.items.map(async lineItem => {
lineItem.content = await this.updateContentPrice_( try {
lineItem.content, lineItem.content = await this.updateContentPrice_(
region._id lineItem.content,
) region._id
)
} catch (err) {
return null
}
return lineItem 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 // If the country code of a shipping address is set we need to clear it
@@ -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) { if (variant.prices.length) {
let foundDefault = false let foundDefault = false
const newPrices = variant.prices.map(moneyAmount => { const newPrices = variant.prices.map(moneyAmount => {