@labdigital/commercetools-mock 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1416,7 +1416,17 @@ class CartRepository extends AbstractResourceRepository {
1416
1416
  });
1417
1417
  }
1418
1418
 
1419
- const price = variant.prices[0];
1419
+ const currency = resource.totalPrice.currencyCode;
1420
+ const price = selectPrice({
1421
+ prices: variant.prices,
1422
+ currency,
1423
+ country: resource.country
1424
+ });
1425
+
1426
+ if (!price) {
1427
+ throw new Error(`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`);
1428
+ }
1429
+
1420
1430
  resource.lineItems.push({
1421
1431
  id: v4(),
1422
1432
  productId: product.id,
@@ -1551,17 +1561,13 @@ class CartRepository extends AbstractResourceRepository {
1551
1561
  }
1552
1562
  };
1553
1563
 
1554
- this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
1555
- var _variant$prices2;
1556
-
1564
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
1557
1565
  const {
1558
1566
  productId,
1559
- quantity
1560
- } = draftLineItem; // @ts-ignore
1561
-
1562
- let variantId = draftLineItem.variant.id; // @ts-ignore
1563
-
1564
- let sku = draftLineItem.variant.sku;
1567
+ quantity,
1568
+ variantId,
1569
+ sku
1570
+ } = draftLineItem;
1565
1571
  let product = null;
1566
1572
  let variant;
1567
1573
 
@@ -1599,11 +1605,15 @@ class CartRepository extends AbstractResourceRepository {
1599
1605
  throw new Error(sku ? `A variant with SKU '${sku}' for product '${product.id}' not found.` : `A variant with ID '${variantId}' for product '${product.id}' not found.`);
1600
1606
  }
1601
1607
 
1602
- const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
1603
1608
  const quant = quantity != null ? quantity : 1;
1609
+ const price = selectPrice({
1610
+ prices: variant.prices,
1611
+ currency,
1612
+ country
1613
+ });
1604
1614
 
1605
1615
  if (!price) {
1606
- throw new Error(`Price not set on ${productId}`);
1616
+ throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
1607
1617
  }
1608
1618
 
1609
1619
  return {
@@ -1632,12 +1642,12 @@ class CartRepository extends AbstractResourceRepository {
1632
1642
  }
1633
1643
 
1634
1644
  create(projectKey, draft) {
1635
- var _draft$lineItems;
1645
+ var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1636
1646
 
1637
- const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
1647
+ const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1638
1648
  const resource = { ...getBaseResourceProperties(),
1639
1649
  cartState: 'Active',
1640
- lineItems: lineItems != null ? lineItems : [],
1650
+ lineItems,
1641
1651
  customLineItems: [],
1642
1652
  totalPrice: {
1643
1653
  type: 'centPrecision',
@@ -1645,11 +1655,13 @@ class CartRepository extends AbstractResourceRepository {
1645
1655
  currencyCode: draft.currency,
1646
1656
  fractionDigits: 0
1647
1657
  },
1648
- taxMode: 'Platform',
1649
- taxRoundingMode: 'HalfEven',
1650
- taxCalculationMode: 'LineItemLevel',
1658
+ taxMode: (_draft$taxMode = draft.taxMode) != null ? _draft$taxMode : 'Platform',
1659
+ taxRoundingMode: (_draft$taxRoundingMod = draft.taxRoundingMode) != null ? _draft$taxRoundingMod : 'HalfEven',
1660
+ taxCalculationMode: (_draft$taxCalculation = draft.taxCalculationMode) != null ? _draft$taxCalculation : 'LineItemLevel',
1651
1661
  refusedGifts: [],
1652
- origin: 'Customer',
1662
+ locale: draft.locale,
1663
+ country: draft.country,
1664
+ origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1653
1665
  custom: createCustomFields(draft.custom, projectKey, this._storage)
1654
1666
  }; // @ts-ignore
1655
1667
 
@@ -1673,6 +1685,25 @@ class CartRepository extends AbstractResourceRepository {
1673
1685
 
1674
1686
  }
1675
1687
 
1688
+ const selectPrice = ({
1689
+ prices,
1690
+ currency,
1691
+ country
1692
+ }) => {
1693
+ if (!prices) {
1694
+ return undefined;
1695
+ } // Quick-and-dirty way of selecting price based on the given currency and country.
1696
+ // Can be improved later to give more priority to exact matches over
1697
+ // 'all country' matches, and include customer groups in the mix as well
1698
+
1699
+
1700
+ return prices.find(price => {
1701
+ const countryMatch = !price.country || price.country === country;
1702
+ const currencyMatch = price.value.currencyCode === currency;
1703
+ return countryMatch && currencyMatch;
1704
+ });
1705
+ };
1706
+
1676
1707
  const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount * lineItem.quantity;
1677
1708
 
1678
1709
  const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
@@ -1971,10 +2002,17 @@ class CartService extends AbstractService {
1971
2002
  return response.status(400).send();
1972
2003
  }
1973
2004
 
1974
- const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
2005
+ const cartDraft = { ...cartOrOrder,
1975
2006
  currency: cartOrOrder.totalPrice.currencyCode,
1976
- discountCodes: []
1977
- });
2007
+ discountCodes: [],
2008
+ lineItems: cartOrOrder.lineItems.map(lineItem => {
2009
+ return { ...lineItem,
2010
+ variantId: lineItem.variant.id,
2011
+ sku: lineItem.variant.sku
2012
+ };
2013
+ })
2014
+ };
2015
+ const newCart = this.repository.create(request.params.projectKey, cartDraft);
1978
2016
  return response.status(200).send(newCart);
1979
2017
  });
1980
2018
  }