@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.
@@ -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
 
@@ -1486,6 +1486,24 @@ class CartRepository extends AbstractResourceRepository {
1486
1486
  }) => {
1487
1487
  resource.billingAddress = address;
1488
1488
  },
1489
+ setShippingMethod: (projectKey, resource, {
1490
+ shippingMethod
1491
+ }) => {
1492
+ const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1493
+ shippingMethod);
1494
+
1495
+ if (!resolvedType) {
1496
+ throw new Error(`Type ${shippingMethod} not found`);
1497
+ } //@ts-ignore
1498
+
1499
+
1500
+ resource.shippingInfo = {
1501
+ shippingMethod: {
1502
+ typeId: 'shipping-method',
1503
+ id: resolvedType.id
1504
+ }
1505
+ };
1506
+ },
1489
1507
  setCountry: (projectKey, resource, {
1490
1508
  country
1491
1509
  }) => {
@@ -1539,6 +1557,81 @@ class CartRepository extends AbstractResourceRepository {
1539
1557
  resource.shippingAddress = address;
1540
1558
  }
1541
1559
  };
1560
+
1561
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
1562
+ var _variant$prices2;
1563
+
1564
+ const {
1565
+ productId,
1566
+ quantity
1567
+ } = draftLineItem; // @ts-ignore
1568
+
1569
+ let variantId = draftLineItem.variant.id; // @ts-ignore
1570
+
1571
+ let sku = draftLineItem.variant.sku;
1572
+ let product = null;
1573
+ let variant;
1574
+
1575
+ if (productId && variantId) {
1576
+ // Fetch product and variant by ID
1577
+ product = this._storage.get(projectKey, 'product', productId, {});
1578
+ } else if (sku) {
1579
+ // Fetch product and variant by SKU
1580
+ const items = this._storage.query(projectKey, 'product', {
1581
+ where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1582
+ });
1583
+
1584
+ if (items.count === 1) {
1585
+ product = items.results[0];
1586
+ }
1587
+ }
1588
+
1589
+ if (!product) {
1590
+ // Check if product is found
1591
+ throw new CommercetoolsError({
1592
+ code: 'General',
1593
+ message: sku ? `A product containing a variant with SKU '${sku}' not found.` : `A product with ID '${productId}' not found.`
1594
+ });
1595
+ } // Find matching variant
1596
+
1597
+
1598
+ variant = [product.masterData.current.masterVariant, ...product.masterData.current.variants].find(x => {
1599
+ if (sku) return x.sku === sku;
1600
+ if (variantId) return x.id === variantId;
1601
+ return false;
1602
+ });
1603
+
1604
+ if (!variant) {
1605
+ // Check if variant is found
1606
+ 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
+ }
1608
+
1609
+ const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
1610
+ const quant = quantity != null ? quantity : 1;
1611
+
1612
+ if (!price) {
1613
+ throw new Error(`Price not set on ${productId}`);
1614
+ }
1615
+
1616
+ return {
1617
+ id: uuid.v4(),
1618
+ productId: product.id,
1619
+ productKey: product.key,
1620
+ name: product.masterData.current.name,
1621
+ productSlug: product.masterData.current.slug,
1622
+ productType: product.productType,
1623
+ variant,
1624
+ price: price,
1625
+ totalPrice: { ...price.value,
1626
+ centAmount: price.value.centAmount * quant
1627
+ },
1628
+ quantity: quant,
1629
+ discountedPricePerQuantity: [],
1630
+ lineItemMode: 'Standard',
1631
+ priceMode: 'Platform',
1632
+ state: []
1633
+ };
1634
+ };
1542
1635
  }
1543
1636
 
1544
1637
  getTypeId() {
@@ -1546,9 +1639,12 @@ class CartRepository extends AbstractResourceRepository {
1546
1639
  }
1547
1640
 
1548
1641
  create(projectKey, draft) {
1642
+ var _draft$lineItems;
1643
+
1644
+ const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
1549
1645
  const resource = { ...getBaseResourceProperties(),
1550
1646
  cartState: 'Active',
1551
- lineItems: [],
1647
+ lineItems: lineItems != null ? lineItems : [],
1552
1648
  customLineItems: [],
1553
1649
  totalPrice: {
1554
1650
  type: 'centPrecision',
@@ -1562,7 +1658,9 @@ class CartRepository extends AbstractResourceRepository {
1562
1658
  refusedGifts: [],
1563
1659
  origin: 'Customer',
1564
1660
  custom: createCustomFields(draft.custom, projectKey, this._storage)
1565
- };
1661
+ }; // @ts-ignore
1662
+
1663
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1566
1664
  this.save(projectKey, resource);
1567
1665
  return resource;
1568
1666
  }