@faststore/api 1.10.4 → 1.10.6

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 1.10.6 (2022-07-05)
7
+
8
+
9
+ ### Features
10
+
11
+ * Support for cross selling API ([#1396](https://github.com/vtex/faststore/issues/1396)) ([98eb7e2](https://github.com/vtex/faststore/commit/98eb7e2cc6670bcb05d00418f901e26a2e9db8f0))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [1.10.4](https://github.com/vtex/faststore/compare/v1.10.3...v1.10.4) (2022-07-04)
7
18
 
8
19
 
@@ -44,6 +44,19 @@ const VtexCommerce = ({
44
44
  },
45
45
  portal: {
46
46
  pagetype: slug => fetchAPI(`${base}/api/catalog_system/pub/portal/pagetype/${slug}`)
47
+ },
48
+ products: {
49
+ crossselling: ({
50
+ type,
51
+ productId,
52
+ groupByProduct = true
53
+ }) => {
54
+ const params = new URLSearchParams({
55
+ sc: ctx.storage.channel.salesChannel,
56
+ groupByProduct: groupByProduct.toString()
57
+ });
58
+ return fetchAPI(`${base}/api/catalog_system/pub/products/crossselling/${type}/${productId}?${params}`);
59
+ }
47
60
  }
48
61
  },
49
62
  checkout: {
@@ -641,6 +654,14 @@ class ChannelMarshal {
641
654
 
642
655
  }
643
656
 
657
+ const FACET_CROSS_SELLING_MAP = {
658
+ buy: "whoboughtalsobought",
659
+ view: "whosawalsosaw",
660
+ similars: "similars",
661
+ viewAndBought: "whosawalsobought",
662
+ accessories: "accessories",
663
+ suggestions: "suggestions"
664
+ };
644
665
  /**
645
666
  * Transform facets from the store to VTEX platform facets.
646
667
  * For instance, the channel in Store becomes trade-policy and regionId in VTEX's realm
@@ -682,6 +703,16 @@ const transformSelectedFacet = ({
682
703
  };
683
704
  }
684
705
 
706
+ case "buy":
707
+ case "view":
708
+ case "similars":
709
+ case "viewAndBought":
710
+ case "accessories":
711
+ case "suggestions":
712
+ {
713
+ return []; // remove this facet from search
714
+ }
715
+
685
716
  default:
686
717
  return {
687
718
  key,
@@ -698,6 +729,18 @@ const parseRange = range => {
698
729
 
699
730
  return splitted;
700
731
  };
732
+ const isCrossSelling = x => typeof FACET_CROSS_SELLING_MAP[x] === "string";
733
+ const findCrossSelling = facets => {
734
+ var _filtered$;
735
+
736
+ const filtered = facets == null ? void 0 : facets.filter(x => isCrossSelling(x.key));
737
+
738
+ if (Array.isArray(filtered) && filtered.length > 1) {
739
+ throw new BadRequestError(`You passed ${filtered.length} cross selling facets but only one is allowed. Please leave one of the following facet: ${filtered.map(x => x.key).join(',')}`);
740
+ }
741
+
742
+ return (_filtered$ = filtered == null ? void 0 : filtered[0]) != null ? _filtered$ : null;
743
+ };
701
744
  const findSlug = facets => {
702
745
  var _facets$find$value, _facets$find;
703
746
 
@@ -1565,6 +1608,7 @@ const Query = {
1565
1608
  // Insert channel in context for later usage
1566
1609
  const channel = findChannel(selectedFacets);
1567
1610
  const locale = findLocale(selectedFacets);
1611
+ const crossSelling = findCrossSelling(selectedFacets);
1568
1612
 
1569
1613
  if (channel) {
1570
1614
  mutateChannelContext(ctx, channel);
@@ -1574,11 +1618,30 @@ const Query = {
1574
1618
  mutateLocaleContext(ctx, locale);
1575
1619
  }
1576
1620
 
1621
+ let query = term;
1622
+ /**
1623
+ * In case we are using crossSelling, we need to modify the search
1624
+ * we will be performing on our search engine. The idea in here
1625
+ * is to use the cross selling API for fetching the productIds our
1626
+ * search will return for us.
1627
+ * Doing this two request workflow makes it possible to have cross
1628
+ * selling with Search features, like pagination, internationalization
1629
+ * etc
1630
+ */
1631
+
1632
+ if (crossSelling) {
1633
+ const products = await ctx.clients.commerce.catalog.products.crossselling({
1634
+ type: FACET_CROSS_SELLING_MAP[crossSelling.key],
1635
+ productId: crossSelling.value
1636
+ });
1637
+ query = `product:${products.map(x => x.productId).slice(0, first).join(";")}`;
1638
+ }
1639
+
1577
1640
  const after = maybeAfter ? Number(maybeAfter) : 0;
1578
1641
  const searchArgs = {
1579
1642
  page: Math.ceil(after / first),
1580
1643
  count: first,
1581
- query: term,
1644
+ query,
1582
1645
  sort: SORT_MAP[sort != null ? sort : 'score_desc'],
1583
1646
  selectedFacets: (_selectedFacets$flatM = selectedFacets == null ? void 0 : selectedFacets.flatMap(transformSelectedFacet)) != null ? _selectedFacets$flatM : []
1584
1647
  };