@faststore/api 1.10.34 → 1.11.3

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,15 @@
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.11.3](https://github.com/vtex/faststore/compare/v1.11.2...v1.11.3) (2022-08-10)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Join cart items ([#1434](https://github.com/vtex/faststore/issues/1434)) ([990b6a9](https://github.com/vtex/faststore/commit/990b6a920703f9004f7e0d6be6c2a316610db5c6))
12
+
13
+
14
+
6
15
  ## 1.10.34 (2022-08-04)
7
16
 
8
17
 
@@ -933,7 +933,7 @@ const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachme
933
933
  const getId = item => {
934
934
  var _item$itemOffered$add;
935
935
 
936
- return [item.itemOffered.sku, item.seller.identifier, item.price, (_item$itemOffered$add = item.itemOffered.additionalProperty) == null ? void 0 : _item$itemOffered$add.filter(isAttachment).map(getPropertyId).join('-')].filter(Boolean).join('::');
936
+ return [item.itemOffered.sku, item.seller.identifier, item.price < 0.01 ? 'Gift' : undefined, (_item$itemOffered$add = item.itemOffered.additionalProperty) == null ? void 0 : _item$itemOffered$add.filter(isAttachment).map(getPropertyId).join('-')].filter(Boolean).join('::');
937
937
  };
938
938
 
939
939
  const orderFormItemToOffer = (item, index) => ({
@@ -971,7 +971,12 @@ const groupById = offers => offers.reduce((acc, item) => {
971
971
  var _acc$get;
972
972
 
973
973
  const id = getId(item);
974
- acc.set(id, (_acc$get = acc.get(id)) != null ? _acc$get : item);
974
+
975
+ if (!acc.has(id)) {
976
+ acc.set(id, []);
977
+ }
978
+
979
+ (_acc$get = acc.get(id)) == null ? void 0 : _acc$get.push(item);
975
980
  return acc;
976
981
  }, new Map());
977
982
 
@@ -990,6 +995,30 @@ const equals = (storeOrder, orderForm) => {
990
995
  return isSameOrder && orderItemsAreSync;
991
996
  };
992
997
 
998
+ const joinItems = form => {
999
+ const itemsById = form.items.reduce((acc, item) => {
1000
+ const id = getId(orderFormItemToOffer(item));
1001
+
1002
+ if (!acc[id]) {
1003
+ acc[id] = [];
1004
+ }
1005
+
1006
+ acc[id].push(item);
1007
+ return acc;
1008
+ }, {});
1009
+ return { ...form,
1010
+ items: Object.values(itemsById).map(items => {
1011
+ const [item] = items;
1012
+ const quantity = items.reduce((acc, i) => acc + i.quantity, 0);
1013
+ const totalPrice = items.reduce((acc, i) => acc + i.quantity * i.sellingPrice, 0);
1014
+ return { ...item,
1015
+ quantity,
1016
+ sellingPrice: totalPrice / quantity
1017
+ };
1018
+ })
1019
+ };
1020
+ };
1021
+
993
1022
  const orderFormToCart = async (form, skuLoader) => {
994
1023
  return {
995
1024
  order: {
@@ -1092,7 +1121,7 @@ const validateCart = async (_, {
1092
1121
  const isStale = isOrderFormStale(orderForm);
1093
1122
 
1094
1123
  if (isStale === true && orderNumber) {
1095
- const newOrderForm = await setOrderFormEtag(orderForm, commerce);
1124
+ const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1096
1125
  return orderFormToCart(newOrderForm, skuLoader);
1097
1126
  }
1098
1127
  } // Step2: Process items from both browser and checkout so they have the same shape
@@ -1100,33 +1129,41 @@ const validateCart = async (_, {
1100
1129
 
1101
1130
  const browserItemsById = groupById(acceptedOffer);
1102
1131
  const originItemsById = groupById(orderForm.items.map(orderFormItemToOffer));
1103
- const browserItems = Array.from(browserItemsById.values()); // items on the user's browser
1132
+ const originItems = Array.from(originItemsById.entries()); // items on the VTEX platform backend
1104
1133
 
1105
- const originItems = Array.from(originItemsById.values()); // items on the VTEX platform backend
1134
+ const browserItems = Array.from(browserItemsById.entries()); // items on the user's browser
1106
1135
  // Step3: Compute delta changes
1107
1136
 
1108
1137
  const {
1109
1138
  itemsToAdd,
1110
1139
  itemsToUpdate
1111
- } = browserItems.reduce((acc, item) => {
1112
- const maybeOriginItem = originItemsById.get(getId(item));
1140
+ } = browserItems.reduce((acc, [id, items]) => {
1141
+ const maybeOriginItem = originItemsById.get(id); // Adding new items to cart
1113
1142
 
1114
1143
  if (!maybeOriginItem) {
1115
- acc.itemsToAdd.push(item);
1116
- } else {
1117
- acc.itemsToUpdate.push({ ...maybeOriginItem,
1118
- quantity: item.quantity
1119
- });
1120
- }
1144
+ items.forEach(item => acc.itemsToAdd.push(item));
1145
+ return acc;
1146
+ } // Update existing items
1147
+
1121
1148
 
1149
+ const [head, ...tail] = maybeOriginItem;
1150
+ const totalQuantity = items.reduce((acc, curr) => acc + curr.quantity, 0); // set total quantity to first item
1151
+
1152
+ acc.itemsToUpdate.push({ ...head,
1153
+ quantity: totalQuantity
1154
+ }); // Remove all the rest
1155
+
1156
+ tail.forEach(item => acc.itemsToUpdate.push({ ...item,
1157
+ quantity: 0
1158
+ }));
1122
1159
  return acc;
1123
1160
  }, {
1124
1161
  itemsToAdd: [],
1125
1162
  itemsToUpdate: []
1126
1163
  });
1127
- const itemsToDelete = originItems.filter(item => !browserItemsById.has(getId(item))).map(item => ({ ...item,
1164
+ const itemsToDelete = originItems.filter(([id]) => !browserItemsById.has(id)).flatMap(([, items]) => items.map(item => ({ ...item,
1128
1165
  quantity: 0
1129
- }));
1166
+ })));
1130
1167
  const changes = [...itemsToAdd, ...itemsToUpdate, ...itemsToDelete].map(offerToOrderItemInput);
1131
1168
 
1132
1169
  if (changes.length === 0) {
@@ -1139,7 +1176,7 @@ const validateCart = async (_, {
1139
1176
  id: orderForm.orderFormId,
1140
1177
  orderItems: changes
1141
1178
  }) // update orderForm etag so we know last time we touched this orderForm
1142
- .then(form => enableOrderFormSync ? setOrderFormEtag(form, commerce) : form); // Step5: If no changes detected before/after updating orderForm, the order is validated
1179
+ .then(form => enableOrderFormSync ? setOrderFormEtag(form, commerce) : form).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1143
1180
 
1144
1181
  if (equals(order, updatedOrderForm)) {
1145
1182
  return null;