@azzas/azzas-tracker-web 1.0.79 → 1.0.81

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/dist/mod.js CHANGED
@@ -312,6 +312,19 @@ var paymentTypeMap = [
312
312
  ["deb", "cart\xE3o"]
313
313
  ];
314
314
  var toNum = (val) => typeof val === "string" ? Number(val.replace(/[^\d,]/g, "").replace(",", ".")) : val;
315
+ var setLocalStorage = (key, value) => {
316
+ localStorage.setItem(key, value);
317
+ };
318
+ var getLocalStorage = (key) => {
319
+ return localStorage.getItem(key);
320
+ };
321
+ var documentToHash = async (document2) => {
322
+ const encoder = new TextEncoder();
323
+ const data = encoder.encode(document2);
324
+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);
325
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
326
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
327
+ };
315
328
 
316
329
  // src/adapters/dito.ts
317
330
  function pushToDito(event, ecomm) {
@@ -589,10 +602,158 @@ async function itemsFromOrderForm(context) {
589
602
  );
590
603
  }
591
604
 
605
+ // src/params/utils/itemListAttribution.ts
606
+ var LAST_SELECTED_ITEM_KEY = "last_selected_item";
607
+ var STORAGE_VIEWED = "last_viewed_item";
608
+ var CART_LIST_HISTORY_KEY = "cart_list_history";
609
+ var DAY_IN_MS = 24 * 60 * 60 * 1e3;
610
+ function getStorage() {
611
+ try {
612
+ if (typeof window === "undefined" || !window.localStorage) return null;
613
+ return window.localStorage;
614
+ } catch (e) {
615
+ return null;
616
+ }
617
+ }
618
+ function parseJSON(value) {
619
+ if (!value) return null;
620
+ try {
621
+ return JSON.parse(value);
622
+ } catch (e) {
623
+ return null;
624
+ }
625
+ }
626
+ function isFreshTimestamp(timestamp) {
627
+ const elapsed = Date.now() - timestamp;
628
+ return elapsed >= 0 && elapsed < DAY_IN_MS;
629
+ }
630
+ function isStoredItemMemory(value) {
631
+ if (!value || typeof value !== "object") return false;
632
+ const candidate = value;
633
+ return typeof candidate.id === "string" && typeof candidate.list === "string" && typeof candidate.timestamp === "number";
634
+ }
635
+ function resolveIdFromUnknownItem(item) {
636
+ var _a, _b, _c, _d, _e;
637
+ const id = (_e = (_d = (_c = (_b = (_a = item == null ? void 0 : item.productID) != null ? _a : item == null ? void 0 : item.productId) != null ? _b : item == null ? void 0 : item.item_group_id) != null ? _c : item == null ? void 0 : item.item_id) != null ? _d : item == null ? void 0 : item.id) != null ? _e : null;
638
+ return id == null ? null : String(id);
639
+ }
640
+ function resolveItemId(item) {
641
+ return resolveIdFromUnknownItem(item);
642
+ }
643
+ function readStoredItem(key) {
644
+ const storage = getStorage();
645
+ if (!storage) return null;
646
+ try {
647
+ const parsed = parseJSON(storage.getItem(key));
648
+ return isStoredItemMemory(parsed) ? parsed : null;
649
+ } catch (e) {
650
+ return null;
651
+ }
652
+ }
653
+ function writeStoredItem(key, value) {
654
+ const storage = getStorage();
655
+ if (!storage) return;
656
+ try {
657
+ storage.setItem(key, JSON.stringify(value));
658
+ } catch (e) {
659
+ }
660
+ }
661
+ function readCartHistory() {
662
+ const storage = getStorage();
663
+ if (!storage) return [];
664
+ try {
665
+ const parsed = parseJSON(storage.getItem(CART_LIST_HISTORY_KEY));
666
+ if (!Array.isArray(parsed)) return [];
667
+ return parsed.filter((entry) => !!entry && typeof entry === "object" && typeof entry.id === "string" && typeof entry.list === "string");
668
+ } catch (e) {
669
+ return [];
670
+ }
671
+ }
672
+ function writeCartHistory(history) {
673
+ const storage = getStorage();
674
+ if (!storage) return;
675
+ try {
676
+ storage.setItem(CART_LIST_HISTORY_KEY, JSON.stringify(history));
677
+ } catch (e) {
678
+ }
679
+ }
680
+ function saveSelectedItem(productId, listName) {
681
+ if (productId == null || !listName) return;
682
+ const payload = {
683
+ id: String(productId),
684
+ list: String(listName),
685
+ timestamp: Date.now()
686
+ };
687
+ writeStoredItem(LAST_SELECTED_ITEM_KEY, payload);
688
+ }
689
+ function saveViewedItem(productId, listName) {
690
+ if (productId == null) return;
691
+ const payload = {
692
+ id: String(productId),
693
+ list: String(listName != null ? listName : "PDP"),
694
+ timestamp: Date.now()
695
+ };
696
+ writeStoredItem(STORAGE_VIEWED, payload);
697
+ }
698
+ function resolveItemListName(params) {
699
+ const { event, item, itemListName } = params;
700
+ const currentListName = itemListName != null ? itemListName : null;
701
+ const isResolvableEvent = event === "VIEW_ITEM" || event === "ADD_TO_CART";
702
+ if (!isResolvableEvent) return currentListName;
703
+ const currentItemId = resolveIdFromUnknownItem(item);
704
+ if (event === "VIEW_ITEM") {
705
+ if (!currentItemId) return "PDP";
706
+ const selectedItem = readStoredItem(LAST_SELECTED_ITEM_KEY);
707
+ if (!selectedItem) return "PDP";
708
+ if (selectedItem.id === currentItemId && isFreshTimestamp(selectedItem.timestamp)) {
709
+ return selectedItem.list;
710
+ }
711
+ return "PDP";
712
+ }
713
+ if (currentListName !== "PDP" && currentListName !== null) return currentListName;
714
+ if (!currentItemId) return currentListName;
715
+ const viewedItem = readStoredItem(STORAGE_VIEWED);
716
+ if (viewedItem && viewedItem.id === currentItemId && isFreshTimestamp(viewedItem.timestamp)) {
717
+ return viewedItem.list;
718
+ }
719
+ return currentListName === "PDP" ? "PDP" : null;
720
+ }
721
+ function appendCartHistory(productId, listName) {
722
+ if (productId == null) return;
723
+ const history = readCartHistory();
724
+ history.push({
725
+ id: String(productId),
726
+ list: listName ? String(listName) : "PDP"
727
+ });
728
+ writeCartHistory(history);
729
+ }
730
+ function pickSelectedItemFromResolvedItems(items, source) {
731
+ var _a;
732
+ if (!Array.isArray(items) || items.length === 0) return null;
733
+ if (typeof (source == null ? void 0 : source.index) === "number") {
734
+ const indexed = (_a = items.find((item) => (item == null ? void 0 : item.index) === source.index)) != null ? _a : items[source.index];
735
+ if (indexed && typeof indexed === "object") return indexed;
736
+ }
737
+ return items[0];
738
+ }
739
+ function persistSelectItemOriginFromResolvedItems(items, source) {
740
+ var _a, _b, _c;
741
+ const selectedItem = pickSelectedItemFromResolvedItems(items, source);
742
+ if (!selectedItem) return;
743
+ const productId = resolveIdFromUnknownItem(selectedItem);
744
+ const listName = (_c = (_b = (_a = selectedItem == null ? void 0 : selectedItem.item_list_name) != null ? _a : source == null ? void 0 : source.itemListName) != null ? _b : source == null ? void 0 : source.item_list_name) != null ? _c : null;
745
+ saveSelectedItem(productId, listName);
746
+ }
747
+
592
748
  // src/params/resolvers/items/fromItem.ts
593
749
  async function itemsFromItem(context, event) {
594
750
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
595
751
  const { item, orderForm, itemListName } = context;
752
+ const resolvedListName = resolveItemListName({ event, item, itemListName });
753
+ const resolvedItemId = resolveItemId(item);
754
+ if (event === "ADD_TO_CART") {
755
+ appendCartHistory(resolvedItemId, resolvedListName);
756
+ }
596
757
  return [
597
758
  {
598
759
  index: typeof (item == null ? void 0 : item.index) === "number" ? item.index : null,
@@ -610,7 +771,7 @@ async function itemsFromItem(context, event) {
610
771
  item_variant: ((item == null ? void 0 : item.skuName) || (item == null ? void 0 : item.item_variant) || (item == null ? void 0 : item.name) || (item == null ? void 0 : item.itemOffered.name)).split(/[-/]/)[0].trim() || null,
611
772
  item_variant2: (item == null ? void 0 : item.size) || ((_n = item == null ? void 0 : item.itemAttributes) == null ? void 0 : _n.size) || ((_p = (_o = (item == null ? void 0 : item.skuName) || (item == null ? void 0 : item.name) || (item == null ? void 0 : item.itemOffered.name)) == null ? void 0 : _o.split(/[-/]/).pop()) == null ? void 0 : _p.trim()) || null,
612
773
  discount: getDiscount(item),
613
- item_list_name: itemListName || null,
774
+ item_list_name: resolvedListName,
614
775
  item_url: (item == null ? void 0 : item.item_url) || null,
615
776
  // add_to_cart dito
616
777
  image_url: resizeVtexImage(item == null ? void 0 : item.image) || resizeVtexImage(item == null ? void 0 : item.imageUrl) || null
@@ -640,9 +801,14 @@ async function itemsFromList(context) {
640
801
  }
641
802
 
642
803
  // src/params/resolvers/items/fromPdp.ts
643
- async function itemsFromPDP(context) {
804
+ async function itemsFromPDP(context, event) {
644
805
  var _a, _b, _c, _d;
645
806
  const { item, value, brand } = context;
807
+ const resolvedListName = resolveItemListName({ event, item, itemListName: null });
808
+ const resolvedItemId = resolveItemId(item);
809
+ if (event === "VIEW_ITEM") {
810
+ saveViewedItem(resolvedItemId, resolvedListName);
811
+ }
646
812
  return [
647
813
  {
648
814
  price: value || 0,
@@ -659,7 +825,8 @@ async function itemsFromPDP(context) {
659
825
  item_sku: (item == null ? void 0 : item.sku) || (item == null ? void 0 : item.productId) || null,
660
826
  item_url: (item == null ? void 0 : item.url) || (item == null ? void 0 : item.item_url) || (item == null ? void 0 : item.link) || null,
661
827
  image_url: resizeVtexImage(item == null ? void 0 : item.image) || resizeVtexImage(item == null ? void 0 : item.imageUrl) || null,
662
- seller_id: (item == null ? void 0 : item.seller) || null
828
+ seller_id: (item == null ? void 0 : item.seller) || null,
829
+ item_list_name: resolvedListName
663
830
  }
664
831
  ];
665
832
  }
@@ -702,11 +869,15 @@ function getItems(context, eventName) {
702
869
  case "REMOVE_FROM_CART":
703
870
  return itemsFromItem(context, eventName);
704
871
  case "SELECT_ITEM":
872
+ return itemsFromList(context).then((items) => {
873
+ persistSelectItemOriginFromResolvedItems(items, context);
874
+ return items;
875
+ });
705
876
  case "VIEW_ITEM_LIST":
706
877
  return itemsFromList(context);
707
878
  case "VIEW_ITEM":
708
879
  case "CUSTOM_VIEW_ITEM":
709
- return itemsFromPDP(context);
880
+ return itemsFromPDP(context, eventName);
710
881
  case "VIEW_PROMOTION":
711
882
  case "SELECT_PROMOTION":
712
883
  return itemsFromBanner(context);
@@ -798,7 +969,16 @@ var paramGetters = {
798
969
  if (response.status !== 200) {
799
970
  return { customer: { email: context.userEmail, id: context == null ? void 0 : context.userId }, address: null };
800
971
  }
801
- return await response.json();
972
+ const res = await response.json();
973
+ const cpfFromStorage = getLocalStorage("user_doc_dt");
974
+ if (cpfFromStorage) {
975
+ return { ...res, customer: { ...res.customer, cpf: cpfFromStorage } };
976
+ } else {
977
+ const hashedDocument = await documentToHash(res.customer.document);
978
+ console.log("[DT] set localstorage");
979
+ setLocalStorage("user_doc_dt", hashedDocument);
980
+ return { ...res, customer: { ...res.customer, cpf: hashedDocument } };
981
+ }
802
982
  } catch (_) {
803
983
  return { customer: { email: context.userEmail, id: context == null ? void 0 : context.userId }, address: null };
804
984
  }