@labdigital/commercetools-mock 0.5.22 → 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.
@@ -926,8 +926,8 @@ class ProjectAPI {
926
926
  }
927
927
  }
928
928
 
929
- get(typeId, id) {
930
- return this._storage.get(this.projectKey, typeId, id, {});
929
+ get(typeId, id, params) {
930
+ return this._storage.get(this.projectKey, typeId, id, params);
931
931
  } // TODO: Not sure if we want to expose this...
932
932
 
933
933
 
@@ -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,
@@ -1486,6 +1496,24 @@ class CartRepository extends AbstractResourceRepository {
1486
1496
  }) => {
1487
1497
  resource.billingAddress = address;
1488
1498
  },
1499
+ setShippingMethod: (projectKey, resource, {
1500
+ shippingMethod
1501
+ }) => {
1502
+ const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1503
+ shippingMethod);
1504
+
1505
+ if (!resolvedType) {
1506
+ throw new Error(`Type ${shippingMethod} not found`);
1507
+ } //@ts-ignore
1508
+
1509
+
1510
+ resource.shippingInfo = {
1511
+ shippingMethod: {
1512
+ typeId: 'shipping-method',
1513
+ id: resolvedType.id
1514
+ }
1515
+ };
1516
+ },
1489
1517
  setCountry: (projectKey, resource, {
1490
1518
  country
1491
1519
  }) => {
@@ -1539,6 +1567,81 @@ class CartRepository extends AbstractResourceRepository {
1539
1567
  resource.shippingAddress = address;
1540
1568
  }
1541
1569
  };
1570
+
1571
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
1572
+ const {
1573
+ productId,
1574
+ quantity,
1575
+ variantId,
1576
+ sku
1577
+ } = draftLineItem;
1578
+ let product = null;
1579
+ let variant;
1580
+
1581
+ if (productId && variantId) {
1582
+ // Fetch product and variant by ID
1583
+ product = this._storage.get(projectKey, 'product', productId, {});
1584
+ } else if (sku) {
1585
+ // Fetch product and variant by SKU
1586
+ const items = this._storage.query(projectKey, 'product', {
1587
+ where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1588
+ });
1589
+
1590
+ if (items.count === 1) {
1591
+ product = items.results[0];
1592
+ }
1593
+ }
1594
+
1595
+ if (!product) {
1596
+ // Check if product is found
1597
+ throw new CommercetoolsError({
1598
+ code: 'General',
1599
+ message: sku ? `A product containing a variant with SKU '${sku}' not found.` : `A product with ID '${productId}' not found.`
1600
+ });
1601
+ } // Find matching variant
1602
+
1603
+
1604
+ variant = [product.masterData.current.masterVariant, ...product.masterData.current.variants].find(x => {
1605
+ if (sku) return x.sku === sku;
1606
+ if (variantId) return x.id === variantId;
1607
+ return false;
1608
+ });
1609
+
1610
+ if (!variant) {
1611
+ // Check if variant is found
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.`);
1613
+ }
1614
+
1615
+ const quant = quantity != null ? quantity : 1;
1616
+ const price = selectPrice({
1617
+ prices: variant.prices,
1618
+ currency,
1619
+ country
1620
+ });
1621
+
1622
+ if (!price) {
1623
+ throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
1624
+ }
1625
+
1626
+ return {
1627
+ id: uuid.v4(),
1628
+ productId: product.id,
1629
+ productKey: product.key,
1630
+ name: product.masterData.current.name,
1631
+ productSlug: product.masterData.current.slug,
1632
+ productType: product.productType,
1633
+ variant,
1634
+ price: price,
1635
+ totalPrice: { ...price.value,
1636
+ centAmount: price.value.centAmount * quant
1637
+ },
1638
+ quantity: quant,
1639
+ discountedPricePerQuantity: [],
1640
+ lineItemMode: 'Standard',
1641
+ priceMode: 'Platform',
1642
+ state: []
1643
+ };
1644
+ };
1542
1645
  }
1543
1646
 
1544
1647
  getTypeId() {
@@ -1546,9 +1649,12 @@ class CartRepository extends AbstractResourceRepository {
1546
1649
  }
1547
1650
 
1548
1651
  create(projectKey, draft) {
1652
+ var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1653
+
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 : [];
1549
1655
  const resource = { ...getBaseResourceProperties(),
1550
1656
  cartState: 'Active',
1551
- lineItems: [],
1657
+ lineItems,
1552
1658
  customLineItems: [],
1553
1659
  totalPrice: {
1554
1660
  type: 'centPrecision',
@@ -1556,13 +1662,17 @@ class CartRepository extends AbstractResourceRepository {
1556
1662
  currencyCode: draft.currency,
1557
1663
  fractionDigits: 0
1558
1664
  },
1559
- taxMode: 'Platform',
1560
- taxRoundingMode: 'HalfEven',
1561
- 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',
1562
1668
  refusedGifts: [],
1563
- origin: 'Customer',
1669
+ locale: draft.locale,
1670
+ country: draft.country,
1671
+ origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1564
1672
  custom: createCustomFields(draft.custom, projectKey, this._storage)
1565
- };
1673
+ }; // @ts-ignore
1674
+
1675
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1566
1676
  this.save(projectKey, resource);
1567
1677
  return resource;
1568
1678
  }
@@ -1582,6 +1692,25 @@ class CartRepository extends AbstractResourceRepository {
1582
1692
 
1583
1693
  }
1584
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
+
1585
1714
  const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount * lineItem.quantity;
1586
1715
 
1587
1716
  const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
@@ -1880,10 +2009,17 @@ class CartService extends AbstractService {
1880
2009
  return response.status(400).send();
1881
2010
  }
1882
2011
 
1883
- const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
2012
+ const cartDraft = { ...cartOrOrder,
1884
2013
  currency: cartOrOrder.totalPrice.currencyCode,
1885
- discountCodes: []
1886
- });
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);
1887
2023
  return response.status(200).send(newCart);
1888
2024
  });
1889
2025
  }
@@ -2117,7 +2253,7 @@ class CustomerRepository extends AbstractResourceRepository {
2117
2253
  create(projectKey, draft) {
2118
2254
  const resource = { ...getBaseResourceProperties(),
2119
2255
  email: draft.email,
2120
- password: Buffer.from(draft.password).toString('base64'),
2256
+ password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2121
2257
  isEmailVerified: draft.isEmailVerified || false,
2122
2258
  addresses: []
2123
2259
  };