@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.
@@ -1423,7 +1423,17 @@ class CartRepository extends AbstractResourceRepository {
1423
1423
  });
1424
1424
  }
1425
1425
 
1426
- const price = variant.prices[0];
1426
+ const currency = resource.totalPrice.currencyCode;
1427
+ const price = selectPrice({
1428
+ prices: variant.prices,
1429
+ currency,
1430
+ country: resource.country
1431
+ });
1432
+
1433
+ if (!price) {
1434
+ throw new Error(`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`);
1435
+ }
1436
+
1427
1437
  resource.lineItems.push({
1428
1438
  id: uuid.v4(),
1429
1439
  productId: product.id,
@@ -1558,17 +1568,13 @@ class CartRepository extends AbstractResourceRepository {
1558
1568
  }
1559
1569
  };
1560
1570
 
1561
- this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
1562
- var _variant$prices2;
1563
-
1571
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
1564
1572
  const {
1565
1573
  productId,
1566
- quantity
1567
- } = draftLineItem; // @ts-ignore
1568
-
1569
- let variantId = draftLineItem.variant.id; // @ts-ignore
1570
-
1571
- let sku = draftLineItem.variant.sku;
1574
+ quantity,
1575
+ variantId,
1576
+ sku
1577
+ } = draftLineItem;
1572
1578
  let product = null;
1573
1579
  let variant;
1574
1580
 
@@ -1606,11 +1612,15 @@ class CartRepository extends AbstractResourceRepository {
1606
1612
  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.`);
1607
1613
  }
1608
1614
 
1609
- const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
1610
1615
  const quant = quantity != null ? quantity : 1;
1616
+ const price = selectPrice({
1617
+ prices: variant.prices,
1618
+ currency,
1619
+ country
1620
+ });
1611
1621
 
1612
1622
  if (!price) {
1613
- throw new Error(`Price not set on ${productId}`);
1623
+ throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
1614
1624
  }
1615
1625
 
1616
1626
  return {
@@ -1639,12 +1649,12 @@ class CartRepository extends AbstractResourceRepository {
1639
1649
  }
1640
1650
 
1641
1651
  create(projectKey, draft) {
1642
- var _draft$lineItems;
1652
+ var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1643
1653
 
1644
- const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
1654
+ 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 : [];
1645
1655
  const resource = { ...getBaseResourceProperties(),
1646
1656
  cartState: 'Active',
1647
- lineItems: lineItems != null ? lineItems : [],
1657
+ lineItems,
1648
1658
  customLineItems: [],
1649
1659
  totalPrice: {
1650
1660
  type: 'centPrecision',
@@ -1652,11 +1662,13 @@ class CartRepository extends AbstractResourceRepository {
1652
1662
  currencyCode: draft.currency,
1653
1663
  fractionDigits: 0
1654
1664
  },
1655
- taxMode: 'Platform',
1656
- taxRoundingMode: 'HalfEven',
1657
- taxCalculationMode: 'LineItemLevel',
1665
+ taxMode: (_draft$taxMode = draft.taxMode) != null ? _draft$taxMode : 'Platform',
1666
+ taxRoundingMode: (_draft$taxRoundingMod = draft.taxRoundingMode) != null ? _draft$taxRoundingMod : 'HalfEven',
1667
+ taxCalculationMode: (_draft$taxCalculation = draft.taxCalculationMode) != null ? _draft$taxCalculation : 'LineItemLevel',
1658
1668
  refusedGifts: [],
1659
- origin: 'Customer',
1669
+ locale: draft.locale,
1670
+ country: draft.country,
1671
+ origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1660
1672
  custom: createCustomFields(draft.custom, projectKey, this._storage)
1661
1673
  }; // @ts-ignore
1662
1674
 
@@ -1680,6 +1692,25 @@ class CartRepository extends AbstractResourceRepository {
1680
1692
 
1681
1693
  }
1682
1694
 
1695
+ const selectPrice = ({
1696
+ prices,
1697
+ currency,
1698
+ country
1699
+ }) => {
1700
+ if (!prices) {
1701
+ return undefined;
1702
+ } // Quick-and-dirty way of selecting price based on the given currency and country.
1703
+ // Can be improved later to give more priority to exact matches over
1704
+ // 'all country' matches, and include customer groups in the mix as well
1705
+
1706
+
1707
+ return prices.find(price => {
1708
+ const countryMatch = !price.country || price.country === country;
1709
+ const currencyMatch = price.value.currencyCode === currency;
1710
+ return countryMatch && currencyMatch;
1711
+ });
1712
+ };
1713
+
1683
1714
  const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount * lineItem.quantity;
1684
1715
 
1685
1716
  const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
@@ -1978,10 +2009,17 @@ class CartService extends AbstractService {
1978
2009
  return response.status(400).send();
1979
2010
  }
1980
2011
 
1981
- const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
2012
+ const cartDraft = { ...cartOrOrder,
1982
2013
  currency: cartOrOrder.totalPrice.currencyCode,
1983
- discountCodes: []
1984
- });
2014
+ discountCodes: [],
2015
+ lineItems: cartOrOrder.lineItems.map(lineItem => {
2016
+ return { ...lineItem,
2017
+ variantId: lineItem.variant.id,
2018
+ sku: lineItem.variant.sku
2019
+ };
2020
+ })
2021
+ };
2022
+ const newCart = this.repository.create(request.params.projectKey, cartDraft);
1985
2023
  return response.status(200).send(newCart);
1986
2024
  });
1987
2025
  }