@labdigital/commercetools-mock 0.5.22 → 0.5.23

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.
@@ -919,8 +919,8 @@ class ProjectAPI {
919
919
  }
920
920
  }
921
921
 
922
- get(typeId, id) {
923
- return this._storage.get(this.projectKey, typeId, id, {});
922
+ get(typeId, id, params) {
923
+ return this._storage.get(this.projectKey, typeId, id, params);
924
924
  } // TODO: Not sure if we want to expose this...
925
925
 
926
926
 
@@ -1479,6 +1479,24 @@ class CartRepository extends AbstractResourceRepository {
1479
1479
  }) => {
1480
1480
  resource.billingAddress = address;
1481
1481
  },
1482
+ setShippingMethod: (projectKey, resource, {
1483
+ shippingMethod
1484
+ }) => {
1485
+ const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1486
+ shippingMethod);
1487
+
1488
+ if (!resolvedType) {
1489
+ throw new Error(`Type ${shippingMethod} not found`);
1490
+ } //@ts-ignore
1491
+
1492
+
1493
+ resource.shippingInfo = {
1494
+ shippingMethod: {
1495
+ typeId: 'shipping-method',
1496
+ id: resolvedType.id
1497
+ }
1498
+ };
1499
+ },
1482
1500
  setCountry: (projectKey, resource, {
1483
1501
  country
1484
1502
  }) => {
@@ -1532,6 +1550,81 @@ class CartRepository extends AbstractResourceRepository {
1532
1550
  resource.shippingAddress = address;
1533
1551
  }
1534
1552
  };
1553
+
1554
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
1555
+ var _variant$prices2;
1556
+
1557
+ const {
1558
+ productId,
1559
+ quantity
1560
+ } = draftLineItem; // @ts-ignore
1561
+
1562
+ let variantId = draftLineItem.variant.id; // @ts-ignore
1563
+
1564
+ let sku = draftLineItem.variant.sku;
1565
+ let product = null;
1566
+ let variant;
1567
+
1568
+ if (productId && variantId) {
1569
+ // Fetch product and variant by ID
1570
+ product = this._storage.get(projectKey, 'product', productId, {});
1571
+ } else if (sku) {
1572
+ // Fetch product and variant by SKU
1573
+ const items = this._storage.query(projectKey, 'product', {
1574
+ where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1575
+ });
1576
+
1577
+ if (items.count === 1) {
1578
+ product = items.results[0];
1579
+ }
1580
+ }
1581
+
1582
+ if (!product) {
1583
+ // Check if product is found
1584
+ throw new CommercetoolsError({
1585
+ code: 'General',
1586
+ message: sku ? `A product containing a variant with SKU '${sku}' not found.` : `A product with ID '${productId}' not found.`
1587
+ });
1588
+ } // Find matching variant
1589
+
1590
+
1591
+ variant = [product.masterData.current.masterVariant, ...product.masterData.current.variants].find(x => {
1592
+ if (sku) return x.sku === sku;
1593
+ if (variantId) return x.id === variantId;
1594
+ return false;
1595
+ });
1596
+
1597
+ if (!variant) {
1598
+ // Check if variant is found
1599
+ 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
+ }
1601
+
1602
+ const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
1603
+ const quant = quantity != null ? quantity : 1;
1604
+
1605
+ if (!price) {
1606
+ throw new Error(`Price not set on ${productId}`);
1607
+ }
1608
+
1609
+ return {
1610
+ id: v4(),
1611
+ productId: product.id,
1612
+ productKey: product.key,
1613
+ name: product.masterData.current.name,
1614
+ productSlug: product.masterData.current.slug,
1615
+ productType: product.productType,
1616
+ variant,
1617
+ price: price,
1618
+ totalPrice: { ...price.value,
1619
+ centAmount: price.value.centAmount * quant
1620
+ },
1621
+ quantity: quant,
1622
+ discountedPricePerQuantity: [],
1623
+ lineItemMode: 'Standard',
1624
+ priceMode: 'Platform',
1625
+ state: []
1626
+ };
1627
+ };
1535
1628
  }
1536
1629
 
1537
1630
  getTypeId() {
@@ -1539,9 +1632,12 @@ class CartRepository extends AbstractResourceRepository {
1539
1632
  }
1540
1633
 
1541
1634
  create(projectKey, draft) {
1635
+ var _draft$lineItems;
1636
+
1637
+ const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
1542
1638
  const resource = { ...getBaseResourceProperties(),
1543
1639
  cartState: 'Active',
1544
- lineItems: [],
1640
+ lineItems: lineItems != null ? lineItems : [],
1545
1641
  customLineItems: [],
1546
1642
  totalPrice: {
1547
1643
  type: 'centPrecision',
@@ -1555,7 +1651,9 @@ class CartRepository extends AbstractResourceRepository {
1555
1651
  refusedGifts: [],
1556
1652
  origin: 'Customer',
1557
1653
  custom: createCustomFields(draft.custom, projectKey, this._storage)
1558
- };
1654
+ }; // @ts-ignore
1655
+
1656
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1559
1657
  this.save(projectKey, resource);
1560
1658
  return resource;
1561
1659
  }