@faststore/api 2.1.9 → 2.1.24

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.
@@ -22,7 +22,7 @@ var api = require('@opentelemetry/api');
22
22
  var apiLogs = require('@opentelemetry/api-logs');
23
23
 
24
24
  var name = "@faststore/api";
25
- var version = "2.1.8";
25
+ var version = "2.1.9";
26
26
  var license = "MIT";
27
27
  var main = "dist/index.js";
28
28
  var typings = "dist/index.d.ts";
@@ -58,8 +58,8 @@ var dependencies = {
58
58
  };
59
59
  var devDependencies = {
60
60
  "@envelop/core": "^2.6.0",
61
- "@faststore/eslint-config": "^2.1.1",
62
- "@faststore/shared": "^2.1.1",
61
+ "@faststore/eslint-config": "^2.1.9",
62
+ "@faststore/shared": "^2.1.9",
63
63
  "@graphql-codegen/cli": "2.2.0",
64
64
  "@graphql-codegen/typescript": "2.2.2",
65
65
  "@types/express": "^4.17.16",
@@ -179,60 +179,37 @@ const VtexCommerce = ({
179
179
  }
180
180
  }) : Promise.resolve(undefined);
181
181
  },
182
- getDeliveryWindows: ({
183
- id,
184
- index,
185
- deliveryMode,
186
- body
187
- }, incrementedAddress) => {
188
- var _body$selectedAddress;
189
- const mappedBody = {
190
- logisticsInfo: Array.from({
191
- length: index
192
- }, (_, itemIndex) => ({
193
- itemIndex,
194
- selectedDeliveryChannel: (deliveryMode == null ? void 0 : deliveryMode.deliveryChannel) || null,
195
- selectedSla: (deliveryMode == null ? void 0 : deliveryMode.deliveryMethod) || null
196
- })),
197
- selectedAddresses: body == null ? void 0 : (_body$selectedAddress = body.selectedAddresses) == null ? void 0 : _body$selectedAddress.map(address => {
198
- var _address$geoCoordinat, _address$geoCoordinat2;
199
- const selectedAddress = {
200
- addressType: address.addressType || null,
201
- receiverName: address.receiverName || null,
202
- postalCode: address.postalCode || (incrementedAddress == null ? void 0 : incrementedAddress.postalCode) || null,
203
- city: (incrementedAddress == null ? void 0 : incrementedAddress.city) || null,
204
- state: (incrementedAddress == null ? void 0 : incrementedAddress.state) || null,
205
- country: address.country || (incrementedAddress == null ? void 0 : incrementedAddress.country) || null,
206
- street: (incrementedAddress == null ? void 0 : incrementedAddress.street) || null,
207
- number: (incrementedAddress == null ? void 0 : incrementedAddress.number) || null,
208
- neighborhood: (incrementedAddress == null ? void 0 : incrementedAddress.neighborhood) || null,
209
- complement: (incrementedAddress == null ? void 0 : incrementedAddress.complement) || null,
210
- reference: (incrementedAddress == null ? void 0 : incrementedAddress.reference) || null,
211
- geoCoordinates: []
212
- };
213
- const longitude = (address == null ? void 0 : address.geoCoordinates) instanceof Array ? null : (address == null ? void 0 : (_address$geoCoordinat = address.geoCoordinates) == null ? void 0 : _address$geoCoordinat.longitude) || null;
214
- const latitude = (address == null ? void 0 : address.geoCoordinates) instanceof Array ? null : (address == null ? void 0 : (_address$geoCoordinat2 = address.geoCoordinates) == null ? void 0 : _address$geoCoordinat2.latitude) || null;
215
- selectedAddress.geoCoordinates = longitude && latitude ? {
216
- longitude,
217
- latitude
218
- } : (incrementedAddress == null ? void 0 : incrementedAddress.geoCoordinates) || [];
219
- return selectedAddress;
220
- })
221
- };
222
- return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
223
- ...BASE_INIT,
224
- body: JSON.stringify(mappedBody)
225
- });
226
- },
227
182
  shippingData: ({
228
183
  id,
229
184
  index,
230
185
  deliveryMode,
231
186
  body
232
- }, incrementedAddress) => {
233
- var _deliveryMode$deliver, _deliveryMode$deliver2, _body$selectedAddress2;
234
- const hasDeliveryWindow = deliveryMode != null && deliveryMode.deliveryWindow ? true : false;
235
- const deliveryWindow = hasDeliveryWindow ? {
187
+ }, incrementedAddress, setDeliveryWindow) => {
188
+ var _body$selectedAddress, _deliveryMode$deliver, _deliveryMode$deliver2;
189
+ const addressSession = body == null ? void 0 : (_body$selectedAddress = body.selectedAddresses) == null ? void 0 : _body$selectedAddress.map(address => {
190
+ const addressSession = {
191
+ addressType: address.addressType || null,
192
+ receiverName: address.receiverName || null,
193
+ postalCode: address.postalCode || (incrementedAddress == null ? void 0 : incrementedAddress.postalCode) || null,
194
+ city: (incrementedAddress == null ? void 0 : incrementedAddress.city) || null,
195
+ state: (incrementedAddress == null ? void 0 : incrementedAddress.state) || null,
196
+ country: address.country || (incrementedAddress == null ? void 0 : incrementedAddress.country) || null,
197
+ street: (incrementedAddress == null ? void 0 : incrementedAddress.street) || null,
198
+ number: (incrementedAddress == null ? void 0 : incrementedAddress.number) || null,
199
+ neighborhood: (incrementedAddress == null ? void 0 : incrementedAddress.neighborhood) || null,
200
+ complement: (incrementedAddress == null ? void 0 : incrementedAddress.complement) || null,
201
+ reference: (incrementedAddress == null ? void 0 : incrementedAddress.reference) || null,
202
+ geoCoordinates: []
203
+ };
204
+ const geoCoordinates = address == null ? void 0 : address.geoCoordinates;
205
+ if (geoCoordinates) {
206
+ const latitude = typeof geoCoordinates === 'object' && 'latitude' in geoCoordinates ? geoCoordinates.latitude : null;
207
+ const longitude = typeof geoCoordinates === 'object' && 'longitude' in geoCoordinates ? geoCoordinates.longitude : null;
208
+ addressSession.geoCoordinates = latitude !== null && longitude !== null ? [longitude, latitude] : (incrementedAddress == null ? void 0 : incrementedAddress.geoCoordinates) || [];
209
+ }
210
+ return addressSession;
211
+ });
212
+ const deliveryWindow = setDeliveryWindow ? {
236
213
  startDateUtc: deliveryMode == null ? void 0 : (_deliveryMode$deliver = deliveryMode.deliveryWindow) == null ? void 0 : _deliveryMode$deliver.startDate,
237
214
  endDateUtc: deliveryMode == null ? void 0 : (_deliveryMode$deliver2 = deliveryMode.deliveryWindow) == null ? void 0 : _deliveryMode$deliver2.endDate
238
215
  } : null;
@@ -245,27 +222,8 @@ const VtexCommerce = ({
245
222
  selectedSla: (deliveryMode == null ? void 0 : deliveryMode.deliveryMethod) || null,
246
223
  deliveryWindow: deliveryWindow
247
224
  })),
248
- selectedAddresses: body == null ? void 0 : (_body$selectedAddress2 = body.selectedAddresses) == null ? void 0 : _body$selectedAddress2.map(address => {
249
- var _address$geoCoordinat3, _address$geoCoordinat4;
250
- const selectedAddress = {
251
- addressType: address.addressType || null,
252
- receiverName: address.receiverName || null,
253
- postalCode: address.postalCode || (incrementedAddress == null ? void 0 : incrementedAddress.postalCode) || null,
254
- city: (incrementedAddress == null ? void 0 : incrementedAddress.city) || null,
255
- state: (incrementedAddress == null ? void 0 : incrementedAddress.state) || null,
256
- country: address.country || (incrementedAddress == null ? void 0 : incrementedAddress.country) || null,
257
- street: (incrementedAddress == null ? void 0 : incrementedAddress.street) || null,
258
- number: (incrementedAddress == null ? void 0 : incrementedAddress.number) || null,
259
- neighborhood: (incrementedAddress == null ? void 0 : incrementedAddress.neighborhood) || null,
260
- complement: (incrementedAddress == null ? void 0 : incrementedAddress.complement) || null,
261
- reference: (incrementedAddress == null ? void 0 : incrementedAddress.reference) || null,
262
- geoCoordinates: []
263
- };
264
- const longitude = (address == null ? void 0 : address.geoCoordinates) instanceof Array ? null : (address == null ? void 0 : (_address$geoCoordinat3 = address.geoCoordinates) == null ? void 0 : _address$geoCoordinat3.longitude) || null;
265
- const latitude = (address == null ? void 0 : address.geoCoordinates) instanceof Array ? null : (address == null ? void 0 : (_address$geoCoordinat4 = address.geoCoordinates) == null ? void 0 : _address$geoCoordinat4.latitude) || null;
266
- selectedAddress.geoCoordinates = longitude && latitude ? [longitude, latitude] : (incrementedAddress == null ? void 0 : incrementedAddress.geoCoordinates) || [];
267
- return selectedAddress;
268
- })
225
+ selectedAddresses: addressSession,
226
+ address: addressSession
269
227
  };
270
228
  return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
271
229
  ...BASE_INIT,
@@ -1101,6 +1059,119 @@ function getPropertyId(item) {
1101
1059
  return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
1102
1060
  }
1103
1061
 
1062
+ const shouldUpdateShippingData = (orderForm, session) => {
1063
+ var _orderForm$shippingDa;
1064
+ if (!hasSessionPostalCodeOrGeoCoordinates(session)) {
1065
+ return false;
1066
+ }
1067
+ const {
1068
+ address
1069
+ } = (_orderForm$shippingDa = orderForm.shippingData) != null ? _orderForm$shippingDa : {
1070
+ address: null
1071
+ };
1072
+ if (checkPostalCode(address, session.postalCode)) {
1073
+ return true;
1074
+ }
1075
+ if (checkGeoCoordinates(address, session.geoCoordinates)) {
1076
+ return true;
1077
+ }
1078
+ if (!hasItems(orderForm)) {
1079
+ return false;
1080
+ }
1081
+ // The logisticsInfo will always exist if there´s at least one item inside the cart
1082
+ const {
1083
+ logisticsInfo
1084
+ } = orderForm.shippingData;
1085
+ if (shouldUpdateDeliveryChannel(logisticsInfo, session)) {
1086
+ return true;
1087
+ }
1088
+ if (shouldUpdateDeliveryMethod(logisticsInfo, session)) {
1089
+ return true;
1090
+ }
1091
+ if (shouldUpdateDeliveryWindow(logisticsInfo, session)) {
1092
+ return true;
1093
+ }
1094
+ return false;
1095
+ };
1096
+ // Validate if theres any postal Code or GeoCoordinates set at the session
1097
+ const hasSessionPostalCodeOrGeoCoordinates = session => {
1098
+ return !!session.postalCode || session.geoCoordinates && session.geoCoordinates.latitude && session.geoCoordinates.longitude;
1099
+ };
1100
+ // Validate if theres a difference between the session postal code and orderForm postal code
1101
+ const checkPostalCode = (address, postalCode) => {
1102
+ return typeof postalCode === 'string' && (address == null ? void 0 : address.postalCode) !== postalCode;
1103
+ };
1104
+ // Validate if theres a difference between the session geoCoords and orderForm geoCoords
1105
+ const checkGeoCoordinates = (address, geoCoordinates) => {
1106
+ return typeof (geoCoordinates == null ? void 0 : geoCoordinates.latitude) === 'number' && typeof (geoCoordinates == null ? void 0 : geoCoordinates.longitude) === 'number' && ((address == null ? void 0 : address.geoCoordinates[0]) !== (geoCoordinates == null ? void 0 : geoCoordinates.longitude) || (address == null ? void 0 : address.geoCoordinates[1]) !== (geoCoordinates == null ? void 0 : geoCoordinates.latitude));
1107
+ };
1108
+ // Validate if theres any item inside the orderForm
1109
+ const hasItems = orderForm => {
1110
+ return orderForm.items.length !== 0;
1111
+ };
1112
+ // Validate if the deliveryChannel from the session is different from the selected delivery channel
1113
+ // and if so needs to validate if the deliveryChannel for the session is available inside the slas for the item
1114
+ const shouldUpdateDeliveryChannel = (logisticsInfo, session) => {
1115
+ var _session$deliveryMode;
1116
+ if (!(session != null && (_session$deliveryMode = session.deliveryMode) != null && _session$deliveryMode.deliveryChannel)) {
1117
+ return false;
1118
+ }
1119
+ const {
1120
+ deliveryChannel
1121
+ } = session.deliveryMode;
1122
+ for (const item of logisticsInfo) {
1123
+ if (item.selectedDeliveryChannel !== deliveryChannel) {
1124
+ const matchingSla = item.slas.find(sla => sla.deliveryChannel === deliveryChannel);
1125
+ if (matchingSla) {
1126
+ return true;
1127
+ }
1128
+ }
1129
+ }
1130
+ return false;
1131
+ };
1132
+ // Validate if the deliveryMethod from the session is different from the selectedSLA
1133
+ // and if so needs to validate if the deliveryMethod for the session is available inside the slas for the item
1134
+ const shouldUpdateDeliveryMethod = (logisticsInfo, session) => {
1135
+ var _session$deliveryMode2;
1136
+ if (!(session != null && (_session$deliveryMode2 = session.deliveryMode) != null && _session$deliveryMode2.deliveryMethod)) {
1137
+ return false;
1138
+ }
1139
+ const {
1140
+ deliveryMethod
1141
+ } = session.deliveryMode;
1142
+ for (const item of logisticsInfo) {
1143
+ if (item.selectedSla !== deliveryMethod) {
1144
+ const matchingSla = item.slas.find(sla => sla.id === deliveryMethod);
1145
+ if (matchingSla) {
1146
+ return true;
1147
+ }
1148
+ }
1149
+ }
1150
+ return false;
1151
+ };
1152
+ // Validate if the deliveryWindow from the session is different from the deliveryWindow of the SLA
1153
+ // and if so needs to validate if the deliveryWindow for the session is available inside the availableDeliveryWindows for the item
1154
+ const shouldUpdateDeliveryWindow = (logisticsInfo, session) => {
1155
+ var _session$deliveryMode3, _session$deliveryMode4, _session$deliveryMode5, _session$deliveryMode6;
1156
+ if (!(session != null && (_session$deliveryMode3 = session.deliveryMode) != null && (_session$deliveryMode4 = _session$deliveryMode3.deliveryWindow) != null && _session$deliveryMode4.startDate) || !(session != null && (_session$deliveryMode5 = session.deliveryMode) != null && (_session$deliveryMode6 = _session$deliveryMode5.deliveryWindow) != null && _session$deliveryMode6.endDate)) {
1157
+ return false;
1158
+ }
1159
+ const {
1160
+ startDate,
1161
+ endDate
1162
+ } = session.deliveryMode.deliveryWindow;
1163
+ for (const item of logisticsInfo) {
1164
+ for (const sla of item.slas) {
1165
+ var _sla$availableDeliver;
1166
+ const matchingWindow = (_sla$availableDeliver = sla.availableDeliveryWindows) == null ? void 0 : _sla$availableDeliver.some(window => window.startDateUtc === startDate && window.endDateUtc === endDate);
1167
+ if (matchingWindow) {
1168
+ return true;
1169
+ }
1170
+ }
1171
+ }
1172
+ return false;
1173
+ };
1174
+
1104
1175
  const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
1105
1176
  const getId = item => {
1106
1177
  var _item$itemOffered$add;
@@ -1247,7 +1318,6 @@ const getOrderForm = async (id, session, {
1247
1318
  commerce
1248
1319
  }
1249
1320
  }) => {
1250
- var _orderForm$shippingDa, _orderForm$shippingDa2, _session$geoCoordinat, _orderForm$shippingDa3, _orderForm$shippingDa4, _orderForm$shippingDa5, _orderForm$shippingDa6;
1251
1321
  const orderForm = await commerce.checkout.orderForm({
1252
1322
  id
1253
1323
  });
@@ -1259,8 +1329,8 @@ const getOrderForm = async (id, session, {
1259
1329
  if (!session) {
1260
1330
  return orderForm;
1261
1331
  }
1262
- const shouldUpdateShippingData = orderForm.items.length > 0 && typeof session.postalCode === 'string' && ((_orderForm$shippingDa = orderForm.shippingData) == null ? void 0 : (_orderForm$shippingDa2 = _orderForm$shippingDa.address) == null ? void 0 : _orderForm$shippingDa2.postalCode) !== session.postalCode || typeof session.geoCoordinates === 'object' && typeof ((_session$geoCoordinat = session.geoCoordinates) == null ? void 0 : _session$geoCoordinat.latitude) === 'number' && typeof session.geoCoordinates.longitude === 'number' && (((_orderForm$shippingDa3 = orderForm.shippingData) == null ? void 0 : (_orderForm$shippingDa4 = _orderForm$shippingDa3.address) == null ? void 0 : _orderForm$shippingDa4.geoCoordinates[0]) !== session.geoCoordinates.longitude || ((_orderForm$shippingDa5 = orderForm.shippingData) == null ? void 0 : (_orderForm$shippingDa6 = _orderForm$shippingDa5.address) == null ? void 0 : _orderForm$shippingDa6.geoCoordinates[1]) !== session.geoCoordinates.latitude);
1263
- if (shouldUpdateShippingData) {
1332
+ const updateShipping = shouldUpdateShippingData(orderForm, session);
1333
+ if (updateShipping) {
1264
1334
  var _session$deliveryMode;
1265
1335
  let incrementedAddress;
1266
1336
  if (session.postalCode) {
@@ -1269,14 +1339,14 @@ const getOrderForm = async (id, session, {
1269
1339
  const hasDeliveryWindow = (_session$deliveryMode = session.deliveryMode) != null && _session$deliveryMode.deliveryWindow ? true : false;
1270
1340
  if (hasDeliveryWindow) {
1271
1341
  // if you have a Delivery Window you have to first get the delivery window to set the desired after
1272
- await commerce.checkout.getDeliveryWindows({
1342
+ await commerce.checkout.shippingData({
1273
1343
  id: orderForm.orderFormId,
1274
1344
  index: orderForm.items.length,
1275
1345
  deliveryMode: session.deliveryMode,
1276
1346
  body: {
1277
1347
  selectedAddresses: [session]
1278
1348
  }
1279
- }, incrementedAddress);
1349
+ }, incrementedAddress, false);
1280
1350
  }
1281
1351
  return commerce.checkout.shippingData({
1282
1352
  id: orderForm.orderFormId,
@@ -1285,7 +1355,7 @@ const getOrderForm = async (id, session, {
1285
1355
  body: {
1286
1356
  selectedAddresses: [session]
1287
1357
  }
1288
- }, incrementedAddress);
1358
+ }, incrementedAddress, true);
1289
1359
  }
1290
1360
  return orderForm;
1291
1361
  };
@@ -1335,7 +1405,15 @@ const validateCart = async (_, {
1335
1405
  const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : '';
1336
1406
  // Step1: Get OrderForm from VTEX Commerce
1337
1407
  const orderForm = await getOrderForm(orderNumber, session, ctx);
1338
- // Step1.5: Check if another system changed the orderForm with this orderNumber
1408
+ // Step1.1: Checks if the orderForm id has changed. There are three cases for this:
1409
+ // Social Selling: the vtex_session cookie contains a new orderForm id with Social Selling data
1410
+ // My Orders: the customer clicks on reordering through generating a new cart and when returning to the faststore, this information needs to be returned by vtex_session cookie.
1411
+ // New session: a new user enters the website and has no orderForm attributed to it (has no relation to the vtex_session cookie).
1412
+ // In all cases, the origin orderForm should replace the copy that's in the browser
1413
+ if (orderForm.orderFormId != orderNumberFromCart) {
1414
+ return orderFormToCart(orderForm, skuLoader);
1415
+ }
1416
+ // Step1.2: Check if another system changed the orderForm with this orderNumber
1339
1417
  // If so, this means the user interacted with this cart elsewhere and expects
1340
1418
  // to see this new cart state instead of what's stored on the user's browser.
1341
1419
  const isStale = isOrderFormStale(orderForm);