pim-import 2.5.0 → 2.7.0

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.
@@ -6,6 +6,7 @@ import {
6
6
  import { AllProductsEntry } from "../../resources/AllProducts";
7
7
  import { ProductRelation } from "../../resources/ProductRelation";
8
8
  import { FieldItem } from "../../resources/cfFields";
9
+ import { Audit } from "../../resources/Audit";
9
10
  import {
10
11
  AvailableCatalogs,
11
12
  CfLocalizedEntryField,
@@ -1585,51 +1586,106 @@ export const setProductsColorRelationships = async (
1585
1586
  };
1586
1587
 
1587
1588
  /**
1588
- * Audit - Checks if there are products to publish or unpublish
1589
1589
  *
1590
- * TODO: È uno dei punti critici dell'import, in quanto dovento interrogare tutti i topicProduct che hanno
1591
- * un code presente tra quelli restituiti dall'endpoint audit/?fromDate=20200228T01:01:00 (no filtro per catalogo ne per famiglia)
1592
- * la query di Contenful richiede parecchio tempo e può capitare che ritorni error 429 "Too Many Request"
1590
+ * Audit - Checks if there are products to publish or unpublish
1593
1591
  *
1594
1592
  * @param {string} lastModified The last modified data. Format: 20200426T07:50:00 - yearmonthdayThour:minute:second
1595
1593
  * @param {string} catalog
1596
1594
  */
1597
1595
  export const audit = async (
1598
1596
  lastModified: string,
1599
- catalog?: AvailableCatalogs
1597
+ catalog: AvailableCatalogs,
1598
+ offset: number = 0,
1599
+ limit: number = 150,
1600
+ s3FilePath: string = ""
1600
1601
  ) => {
1601
1602
  const timeStart = new Date();
1602
1603
  log(`audit - lastModified: ${lastModified} catalog: ${catalog}`, "INFO");
1603
- const allAudit = await getAudit(lastModified);
1604
+
1605
+ // if not exists save data to s3
1606
+ if (!s3FilePath) {
1607
+ const allAudit = await getAudit(lastModified);
1608
+ if (!allAudit) {
1609
+ log(`No audits were found to process for the date ${lastModified}`);
1610
+ return {
1611
+ offset: Number(offset),
1612
+ limit: Number(limit),
1613
+ completed: true,
1614
+ s3FilePath: "",
1615
+ };
1616
+ }
1617
+
1618
+ const filename = `${lastModified}-all-audit.json`;
1619
+ const path = `audit`;
1620
+ await saveJsonToS3(allAudit, filename, path);
1621
+ const s3Path = `${path}/${filename}`;
1622
+
1623
+ return {
1624
+ offset: Number(offset),
1625
+ limit: Number(limit),
1626
+ completed: false,
1627
+ s3FilePath: s3Path,
1628
+ };
1629
+ }
1630
+
1631
+ // get data from s3
1632
+ let allAudit: Audit[] = await getFileFromS3(s3FilePath);
1633
+ const total: number = allAudit.length;
1634
+ allAudit = allAudit.slice(offset, offset + limit);
1604
1635
 
1605
1636
  if (allAudit) {
1606
1637
  const defaultEnvironmentLocaleCode =
1607
1638
  await getEnvironmentDefaultLocaleCode();
1608
- const total = allAudit.length;
1609
- let count = 0;
1610
1639
 
1611
1640
  const productCodes = allAudit.map((audit) => audit.product);
1641
+ const otherFilters = [];
1642
+ if (catalog) {
1643
+ otherFilters.push({ key: "fields.catalogs.sys.id[in]", value: catalog });
1644
+ }
1612
1645
  const entries = await getAllEntriesByCodes(
1613
1646
  productCodes,
1614
1647
  "topicProduct",
1615
- "sys,fields.code,fields.catalogs,fields.pimStatus"
1648
+ "sys,fields",
1649
+ "fields.code",
1650
+ otherFilters
1616
1651
  );
1617
1652
 
1618
- await sleep(2000, true);
1653
+ if (!entries.length) {
1654
+ log(
1655
+ `No products found between offset: ${offset} and limit: ${limit}. Total: ${total}`
1656
+ );
1657
+ const nextOffset = Number(offset) + Number(limit);
1658
+ const completed = limit === -1 || nextOffset >= total;
1659
+ if (completed) {
1660
+ log(`Audit completed`);
1661
+ }
1619
1662
 
1620
- const pageCodes = allAudit.map((audit) =>
1621
- getProductPageIdByCode(audit.product)
1663
+ const tEnd = new Date();
1664
+ const secs = secondBetweenTwoDate(timeStart, tEnd);
1665
+ log(`Execution time: ${secs} seconds`);
1666
+
1667
+ return {
1668
+ offset: nextOffset,
1669
+ limit: Number(limit),
1670
+ completed,
1671
+ s3FilePath,
1672
+ };
1673
+ }
1674
+
1675
+ const pageCodes = entries.map((entry: Entry) =>
1676
+ getProductPageIdByCode(entry.fields.code[defaultEnvironmentLocaleCode])
1622
1677
  );
1678
+
1623
1679
  const entriesPage = await getAllEntriesByCodes(
1624
1680
  pageCodes,
1625
1681
  "page",
1626
- "sys",
1682
+ "sys,fields",
1627
1683
  "sys.id"
1628
1684
  );
1629
1685
 
1686
+ let count: number = offset;
1630
1687
  for (const audit of allAudit) {
1631
- ++count;
1632
- log(`${count} of ${total}`);
1688
+ log(`${++count} of ${total}`);
1633
1689
  log(`I process the product ${audit.product} with status ${audit.what}`);
1634
1690
 
1635
1691
  log(`Search product entry...`);
@@ -1642,11 +1698,11 @@ export const audit = async (
1642
1698
  if (!productEntry) {
1643
1699
  if (catalog) {
1644
1700
  log(
1645
- `The product was not found in the CMS with catalog ${catalog}`,
1701
+ `The ${audit.product} product was not found in the CMS with catalog ${catalog}`,
1646
1702
  "WARN"
1647
1703
  );
1648
1704
  } else {
1649
- log(`The product was not found in the CMS`, "WARN");
1705
+ log(`The ${audit.product} product was not found in the CMS`, "WARN");
1650
1706
  }
1651
1707
  continue;
1652
1708
  }
@@ -1660,9 +1716,9 @@ export const audit = async (
1660
1716
  }
1661
1717
 
1662
1718
  log(`Search product page entry...`);
1663
- let pageEntry = entriesPage.find(
1664
- (currentEntry) =>
1665
- currentEntry.sys.id === getProductPageIdByCode(audit.product)
1719
+ const pageEntryFromId = getProductPageIdByCode(audit.product);
1720
+ let pageEntryFrom = entriesPage.find(
1721
+ (currentEntry) => currentEntry.sys.id === pageEntryFromId
1666
1722
  );
1667
1723
 
1668
1724
  log(`Get product catalogs...`);
@@ -1700,31 +1756,40 @@ export const audit = async (
1700
1756
  );
1701
1757
  }
1702
1758
 
1703
- if (pageEntry) {
1704
- if (pageEntry.isPublished()) {
1705
- log(`Unpublish page ${pageEntry.sys.id}`);
1706
- pageEntry = await pageEntry.unpublish(); // AUDIT_PRODUCT_STATUS
1707
- } else {
1708
- log(`The page ${pageEntry.sys.id} is already unpublish`);
1709
- }
1710
- } else {
1711
- log(`Page PAGE_${audit.product} not found`);
1712
- }
1713
- if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
1714
- log(
1715
- `Creating redirect from ${audit.product} to ${audit.upgradeProduct}`
1716
- );
1717
- const upgradeProductEntry: Entry = await getEntryByCode(
1718
- audit.upgradeProduct,
1719
- "topicProduct"
1720
- );
1721
- if (upgradeProductEntry) {
1722
- // TODO: Creare redirect
1723
- } else {
1759
+ if (pageEntryFrom?.fields) {
1760
+ if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
1724
1761
  log(
1725
- `Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the topicProduct ${audit.upgradeProduct} not found.`
1762
+ `Creating redirect from ${audit.product} to ${audit.upgradeProduct}`
1726
1763
  );
1764
+ const pageEntryToId = getProductPageIdByCode(audit.upgradeProduct);
1765
+ const pageEntryTo = await getEntryByID(
1766
+ pageEntryToId,
1767
+ "page",
1768
+ "sys.id"
1769
+ );
1770
+
1771
+ if (pageEntryTo) {
1772
+ // Save redirect
1773
+ pageEntryFrom.fields = await addToRelationFields(
1774
+ pageEntryFrom,
1775
+ "redirectTo",
1776
+ pageEntryTo.sys.id
1777
+ );
1778
+ pageEntryFrom = await pageEntryFrom.update();
1779
+ } else {
1780
+ log(
1781
+ `Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryToId} not found.`
1782
+ );
1783
+ if (pageEntryFrom.isPublished()) {
1784
+ pageEntryFrom = await pageEntryFrom.unpublish(); // AUDIT_PRODUCT_STATUS
1785
+ log(`${pageEntryFrom.sys.id} page unpublished`);
1786
+ } else {
1787
+ log(`${pageEntryFrom.sys.id} page is already unpublish`);
1788
+ }
1789
+ }
1727
1790
  }
1791
+ } else {
1792
+ log(`${pageEntryFromId} page from not found`);
1728
1793
  }
1729
1794
  } else {
1730
1795
  log(
@@ -1732,12 +1797,32 @@ export const audit = async (
1732
1797
  );
1733
1798
  }
1734
1799
  }
1800
+
1801
+ const nextOffset = Number(offset) + Number(limit);
1802
+
1803
+ const tEnd = new Date();
1804
+ const secs = secondBetweenTwoDate(timeStart, tEnd);
1805
+ log(`Execution time: ${secs} seconds`);
1806
+
1807
+ return {
1808
+ offset: nextOffset,
1809
+ limit: Number(limit),
1810
+ completed: limit === -1 || count >= total,
1811
+ s3FilePath,
1812
+ };
1735
1813
  } else {
1736
- log(`No audits were found to process for the date ${lastModified}`);
1814
+ log(`Execution completed`);
1737
1815
  }
1738
1816
  const tEnd = new Date();
1739
1817
  const secs = secondBetweenTwoDate(timeStart, tEnd);
1740
1818
  log(`Request time: ${secs} seconds`);
1819
+
1820
+ return {
1821
+ offset: Number(offset),
1822
+ limit: Number(limit),
1823
+ completed: true,
1824
+ s3FilePath,
1825
+ };
1741
1826
  };
1742
1827
 
1743
1828
  export const importProductByCode = async (
@@ -526,7 +526,7 @@ export const getSubFamilyPageSlugWithDetails = async (
526
526
  ): Promise<pageResponse> => {
527
527
  const subFamilyEntryId = subFamilyEntry?.sys?.id;
528
528
  if (!subFamilyEntryId) {
529
- log(`Invalid subFamilyEntry`, "WARN");
529
+ log(`Invalid subFamilyEntry`, "ERROR");
530
530
  }
531
531
  const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
532
532
  const catalogEntryId =
package/src/types.ts CHANGED
@@ -147,6 +147,29 @@ export type AssetPropFields = {
147
147
  };
148
148
  };
149
149
 
150
+ export type AssetPropFieldsWithoutLocaleKey = {
151
+ /** Title for this asset */
152
+ title: {
153
+ [key: string]: string;
154
+ };
155
+ /** Description for this asset */
156
+ description?: {
157
+ [key: string]: string;
158
+ };
159
+ /** File object for this asset */
160
+ file: {
161
+ fileName: string;
162
+ contentType: string;
163
+ /** Url where the file is available to be downloaded from, into the Contentful asset system. After the asset is processed this field is gone. */
164
+ upload?: string;
165
+ /** Url where the file is available at the Contentful media asset system. This field won't be available until the asset is processed. */
166
+ url?: string;
167
+ /** Details for the file, depending on file type (example: image size in bytes, etc) */
168
+ details?: Record<string, any>;
169
+ uploadFrom?: Record<string, any>;
170
+ };
171
+ };
172
+
150
173
  export type TopicProductFieldsResponse = {
151
174
  names: CfLocalizedEntryField;
152
175
  code: string;
package/src/utils.ts CHANGED
@@ -144,7 +144,7 @@ export const getPimTranslations = (
144
144
  * @returns
145
145
  */
146
146
  export const getUnwantedValues = (): (string | number)[] => {
147
- return ["N/C", "N/A", "N/S", "-1", -1];
147
+ return ["N/C", "N/A", "N/S", "-1", -1, "-1.00"];
148
148
  };
149
149
 
150
150
  /**