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

View File

@@ -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: [],
},
}
)
})
})
})

View File

@@ -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

View File

@@ -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 => {