pim-import 4.10.0 → 4.12.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.
Files changed (38) hide show
  1. package/dist/algolia/projects.js +8 -0
  2. package/dist/algolia/projects.js.map +1 -1
  3. package/dist/index.js +3 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/libs/contentful.js +1 -1
  6. package/dist/libs/contentful.js.map +1 -1
  7. package/dist/pim/methods/families.js +7 -0
  8. package/dist/pim/methods/families.js.map +1 -1
  9. package/dist/pim/methods/models.js +8 -1
  10. package/dist/pim/methods/models.js.map +1 -1
  11. package/dist/pim/methods/products.js +279 -100
  12. package/dist/pim/methods/products.js.map +1 -1
  13. package/dist/pim/methods/subfamilies.js +7 -0
  14. package/dist/pim/methods/subfamilies.js.map +1 -1
  15. package/dist/pim/methods/submodels.js +7 -0
  16. package/dist/pim/methods/submodels.js.map +1 -1
  17. package/package.json +1 -1
  18. package/src/algolia/projects.ts +10 -0
  19. package/src/index.ts +1 -0
  20. package/src/libs/contentful.ts +1 -1
  21. package/src/pim/methods/families.ts +8 -0
  22. package/src/pim/methods/models.ts +11 -2
  23. package/src/pim/methods/products.ts +408 -171
  24. package/src/pim/methods/subfamilies.ts +10 -0
  25. package/src/pim/methods/submodels.ts +9 -0
  26. package/src/resources/Audit.ts +1 -0
  27. package/src/resources/CollectionModels.ts +1 -0
  28. package/src/resources/CollectionSubFamilies.ts +1 -0
  29. package/src/resources/CollectionSubModels.ts +1 -0
  30. package/src/resources/FamilyDetails.ts +1 -0
  31. package/types/algolia/projects.d.ts +2 -0
  32. package/types/index.d.ts +1 -1
  33. package/types/pim/methods/products.d.ts +5 -0
  34. package/types/resources/Audit.d.ts +1 -0
  35. package/types/resources/CollectionModels.d.ts +1 -0
  36. package/types/resources/CollectionSubFamilies.d.ts +1 -0
  37. package/types/resources/CollectionSubModels.d.ts +1 -0
  38. package/types/resources/FamilyDetails.d.ts +1 -0
@@ -1065,6 +1065,14 @@ const getProductData = async (
1065
1065
  data.fields
1066
1066
  );
1067
1067
 
1068
+ // Destinations
1069
+ if(productDetails?.destinations?.length){
1070
+ log(`set destinations`);
1071
+ data.fields = await addFieldValue(data, "destinations", productDetails.destinations.map(item => item.code));
1072
+ }else{
1073
+ log(`destinations not found`, "WARN");
1074
+ }
1075
+
1068
1076
  // endpoint payload - used into setProductRelationships
1069
1077
  const productEntryId = getTopicProductIdByCode(productDetails.code);
1070
1078
  const { path, fileName } = getProductPayloadS3Details(productEntryId);
@@ -1623,6 +1631,312 @@ export const getAllProductEntriesByCatalog = async (
1623
1631
  return allItems;
1624
1632
  };
1625
1633
 
1634
+ const productAudit = async (audit: Audit, productEntries: Entry[], catalog: AvailableCatalogs, defaultEnvironmentLocaleCode: string, current: number, allAudit: Audit[], productPageEntries: Entry[]) => {
1635
+ log(`Search product entry with id ${audit.product}...`);
1636
+ let productEntry = productEntries.find(
1637
+ (currentEntry) =>
1638
+ currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1639
+ audit.product
1640
+ );
1641
+
1642
+ if (!productEntry) {
1643
+ let logMessage = "";
1644
+ if (catalog) {
1645
+ logMessage = `The ${audit.product} product was not found in the CMS with catalog ${catalog}`;
1646
+ } else {
1647
+ logMessage = `The ${audit.product} product was not found in the CMS`;
1648
+ }
1649
+ log(logMessage, "WARN");
1650
+
1651
+ if (serverUtils) {
1652
+ serverUtils.log(logMessage);
1653
+ const progress = Math.floor((++current / allAudit.length) * 100);
1654
+ serverUtils.updateProgress(progress);
1655
+ }
1656
+ return {current, continue: true}
1657
+ }
1658
+
1659
+ log(`Founded product with id ${productEntry.sys.id}`);
1660
+
1661
+ const pageEntryFromId = getProductPageIdByCode(audit.product);
1662
+ log(`Search product page entry with id ${pageEntryFromId}...`);
1663
+ let pageEntryFrom = productPageEntries.find(
1664
+ (currentEntry) => currentEntry.sys.id === pageEntryFromId
1665
+ );
1666
+
1667
+ if (pageEntryFrom) {
1668
+ log(`Founded product page with id ${pageEntryFrom.sys.id}`);
1669
+ } else {
1670
+ log(`Product page with id ${pageEntryFromId} not found`, "WARN");
1671
+ }
1672
+
1673
+ log(`Get product catalogs...`);
1674
+ const productCatalogs: AvailableCatalogs[] =
1675
+ productEntry?.fields?.catalogs?.[defaultEnvironmentLocaleCode].map(
1676
+ (entryCatalog: FieldItem) => {
1677
+ return entryCatalog.sys.id;
1678
+ }
1679
+ );
1680
+
1681
+ if (catalog && !productCatalogs.includes(catalog)) {
1682
+ log(
1683
+ `Product ${audit.product} does not belong to the ${catalog} catalog`
1684
+ );
1685
+ } else if (["PRODUCT_UNPUBLISH", "PRODUCT_WEBOFF"].includes(audit.what)) {
1686
+ log(`Set the product status as archive...`);
1687
+ // Set the product status as archive
1688
+ productEntry = await archiveEntry(productEntry, true);
1689
+ if (!pageEntryFrom) {
1690
+ log(`${pageEntryFromId} page from not found`);
1691
+ } else if (pageEntryFrom.isArchived()) {
1692
+ log(
1693
+ `Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryFrom.sys.id} is archived.`,
1694
+ "WARN"
1695
+ );
1696
+ } else if (pageEntryFrom?.fields) {
1697
+ if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
1698
+ log(
1699
+ `Creating redirect from ${audit.product} to ${audit.upgradeProduct}`
1700
+ );
1701
+ const pageEntryToId = getProductPageIdByCode(audit.upgradeProduct);
1702
+ const pageEntryTo = await getEntryByID(
1703
+ pageEntryToId,
1704
+ "page",
1705
+ "sys.id"
1706
+ );
1707
+
1708
+ if (pageEntryTo) {
1709
+ // Save redirect
1710
+ pageEntryFrom.fields = await addToRelationFields(
1711
+ pageEntryFrom,
1712
+ "redirectTo",
1713
+ pageEntryTo.sys.id
1714
+ );
1715
+ pageEntryFrom = await pageEntryFrom.update();
1716
+ } else {
1717
+ log(
1718
+ `Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryToId} not found.`,
1719
+ "WARN"
1720
+ );
1721
+ }
1722
+ }
1723
+
1724
+ pageEntryFrom = await archiveEntry(pageEntryFrom);
1725
+ }
1726
+ } else if (
1727
+ audit.what === "REMOVE_PRODUCT_RELATIONS" &&
1728
+ !productEntry.isArchived()
1729
+ ) {
1730
+ for (const item of audit?.catalogs) {
1731
+ if (!item?.where) {
1732
+ log(
1733
+ `catalogs.where field not exists. Product: ${audit.product} `,
1734
+ "WARN"
1735
+ );
1736
+ } else {
1737
+ if (item?.catalogCode) {
1738
+ productEntry.fields = await removeFromRelationFields(
1739
+ productEntry,
1740
+ "catalogs",
1741
+ item.catalogCode,
1742
+ true
1743
+ );
1744
+ }
1745
+ if (item?.categoryCode) {
1746
+ const categoriesFieldKey =
1747
+ "categories" + capitalizeFirstLetter(item.where);
1748
+ productEntry.fields = await removeFromRelationFields(
1749
+ productEntry,
1750
+ categoriesFieldKey,
1751
+ item.categoryCode,
1752
+ true
1753
+ );
1754
+ }
1755
+ if (item?.subfamilyCode) {
1756
+ const subFamiliesFieldKey =
1757
+ "subFamilies" + capitalizeFirstLetter(item.where);
1758
+ productEntry.fields = await removeFromRelationFields(
1759
+ productEntry,
1760
+ subFamiliesFieldKey,
1761
+ item.subfamilyCode,
1762
+ true
1763
+ );
1764
+ }
1765
+ if (item?.models) {
1766
+ const modelsFieldKey =
1767
+ "models" + capitalizeFirstLetter(item.where);
1768
+ productEntry.fields = await removeFromRelationFields(
1769
+ productEntry,
1770
+ modelsFieldKey,
1771
+ item.models,
1772
+ true
1773
+ );
1774
+ }
1775
+ if (item?.subModels) {
1776
+ const subModelsFieldKey =
1777
+ "subModels" + capitalizeFirstLetter(item.where);
1778
+ productEntry.fields = await removeFromRelationFields(
1779
+ productEntry,
1780
+ subModelsFieldKey,
1781
+ item.subModels,
1782
+ true
1783
+ );
1784
+ }
1785
+ // lastPimSyncDate
1786
+ productEntry.fields = await addFieldValue(
1787
+ productEntry,
1788
+ "lastPimSyncDate",
1789
+ getLocalISOTime()
1790
+ );
1791
+ productEntry = await productEntry.update();
1792
+ }
1793
+ }
1794
+ } else {
1795
+ log(
1796
+ `It has not yet been defined how to process the state ${audit.what}`
1797
+ );
1798
+ }
1799
+
1800
+ return {current, continue: false}
1801
+ }
1802
+
1803
+ const familyAudit = async (audit: Audit, familyEntries: Entry[], catalog: AvailableCatalogs, defaultEnvironmentLocaleCode: string, current: number, allAudit: Audit[]) => {
1804
+ log(`Search family entry with id ${audit.product}...`);
1805
+ let familyEntry = familyEntries.find(
1806
+ (currentEntry) =>
1807
+ currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1808
+ audit.product
1809
+ );
1810
+
1811
+ if (!familyEntry) {
1812
+ let logMessage = "";
1813
+ if (catalog) {
1814
+ logMessage = `The ${audit.product} family was not found in the CMS with catalog ${catalog}`;
1815
+ } else {
1816
+ logMessage = `The ${audit.product} family was not found in the CMS`;
1817
+ }
1818
+ log(logMessage, "WARN");
1819
+
1820
+ if (serverUtils) {
1821
+ serverUtils.log(logMessage);
1822
+ const progress = Math.floor((++current / allAudit.length) * 100);
1823
+ serverUtils.updateProgress(progress);
1824
+ }
1825
+ return {current, continue: true}
1826
+ }
1827
+
1828
+ log(`Founded family with id ${familyEntry.sys.id}`);
1829
+
1830
+ log(`Get family catalogs...`);
1831
+ const familyCatalogs: AvailableCatalogs[] =
1832
+ familyEntry?.fields?.catalogs?.[defaultEnvironmentLocaleCode].map(
1833
+ (entryCatalog: FieldItem) => {
1834
+ return entryCatalog.sys.id;
1835
+ }
1836
+ );
1837
+
1838
+ if (catalog && !familyCatalogs.includes(catalog)) {
1839
+ log(
1840
+ `Family ${audit.product} does not belong to the ${catalog} catalog`
1841
+ );
1842
+ } else if (audit.what === "REMOVE_FAMILY_RELATIONS") {
1843
+ for (const item of audit?.catalogs) {
1844
+ if (!item?.where) {
1845
+ log(
1846
+ `catalogs.where field not exists. Family: ${audit.product} `,
1847
+ "WARN"
1848
+ );
1849
+ } else {
1850
+ if(item.where === "DESIGNERS" && item?.codes){
1851
+ const designers = item.codes.split(',')
1852
+ for(const designer of designers){
1853
+ log(`Remove ${designer} designer to ${familyEntry.sys.id} family if exists`)
1854
+ familyEntry.fields = await removeFromRelationFields(
1855
+ familyEntry,
1856
+ "designer",
1857
+ designer,
1858
+ false
1859
+ );
1860
+ }
1861
+ familyEntry = await familyEntry.update();
1862
+ }
1863
+ }
1864
+ }
1865
+ } else {
1866
+ log(
1867
+ `It has not yet been defined how to process the state ${audit.what}`
1868
+ );
1869
+ }
1870
+
1871
+ return {current, continue: false}
1872
+ }
1873
+
1874
+ const subFamilyAudit = async (audit: Audit, subfamilyEntries: Entry[], catalog: AvailableCatalogs, defaultEnvironmentLocaleCode: string, current: number, allAudit: Audit[]) => {
1875
+ log(`Search subFamily entry with id ${audit.product}...`);
1876
+ let subFamilyEntry = subfamilyEntries.find(
1877
+ (currentEntry) =>
1878
+ currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1879
+ audit.product
1880
+ );
1881
+
1882
+ if (!subFamilyEntry) {
1883
+ let logMessage = "";
1884
+ if (catalog) {
1885
+ logMessage = `The ${audit.product} subFamily was not found in the CMS with catalog ${catalog}`;
1886
+ } else {
1887
+ logMessage = `The ${audit.product} subFamily was not found in the CMS`;
1888
+ }
1889
+ log(logMessage, "WARN");
1890
+
1891
+ if (serverUtils) {
1892
+ serverUtils.log(logMessage);
1893
+ const progress = Math.floor((++current / allAudit.length) * 100);
1894
+ serverUtils.updateProgress(progress);
1895
+ }
1896
+ return {current, continue: true}
1897
+ }
1898
+
1899
+ log(`Founded subFamily with id ${subFamilyEntry.sys.id}`);
1900
+
1901
+ log(`Get subFamily catalogs...`);
1902
+ const subFamilyCatalog: AvailableCatalogs = subFamilyEntry?.fields?.catalog?.[defaultEnvironmentLocaleCode].sys.id;
1903
+
1904
+ if (catalog && subFamilyCatalog !== catalog) {
1905
+ log(
1906
+ `SubFamily ${audit.product} does not belong to the ${catalog} catalog`
1907
+ );
1908
+ } else if (audit.what === "REMOVE_SUBFAMILY_RELATIONS") {
1909
+ for (const item of audit?.catalogs) {
1910
+ if (!item?.where) {
1911
+ log(
1912
+ `catalogs.where field not exists. SubFamily: ${audit.product} `,
1913
+ "WARN"
1914
+ );
1915
+ } else {
1916
+ if(item.where === "DESIGNERS" && item?.codes){
1917
+ const designers = item.codes.split(',')
1918
+ for(const designer of designers){
1919
+ log(`Remove ${designer} designer to ${subFamilyEntry.sys.id} subFamily if exists`)
1920
+ subFamilyEntry.fields = await removeFromRelationFields(
1921
+ subFamilyEntry,
1922
+ "designer",
1923
+ designer,
1924
+ false
1925
+ );
1926
+ }
1927
+ subFamilyEntry = await subFamilyEntry.update();
1928
+ }
1929
+ }
1930
+ }
1931
+ } else {
1932
+ log(
1933
+ `It has not yet been defined how to process the state ${audit.what}`
1934
+ );
1935
+ }
1936
+
1937
+ return {current, continue: false}
1938
+ }
1939
+
1626
1940
  /**
1627
1941
  *
1628
1942
  * Audit - Checks if there are products to publish or unpublish
@@ -1688,24 +2002,51 @@ export const audit = async (
1688
2002
  const defaultEnvironmentLocaleCode =
1689
2003
  await getEnvironmentDefaultLocaleCode();
1690
2004
 
1691
- const productCodes = allAudit.map((audit) => audit.product);
2005
+ const productAuditWhat = ["PRODUCT_UNPUBLISH", "PRODUCT_WEBOFF","REMOVE_PRODUCT_RELATIONS"]
2006
+ const productCodes = allAudit.filter(item => productAuditWhat.includes(item.what)).map((audit) => audit.product);
2007
+ const familyCodes = allAudit.filter(item => item.what === "REMOVE_FAMILY_RELATIONS").map((audit) => audit.product);
2008
+ const subFamilyCodes = allAudit.filter(item => item.what === "REMOVE_SUBFAMILY_RELATIONS").map((audit) => audit.product);
1692
2009
  const otherFilters = [];
1693
2010
  if (catalog) {
1694
2011
  otherFilters.push({ key: "fields.catalogs.sys.id[in]", value: catalog });
1695
2012
  }
1696
2013
  log(`Get ${productCodes.length} product entry from Contentful`);
1697
- const entries = await getAllEntriesByCodes(
2014
+ const productEntries = await getAllEntriesByCodes(
1698
2015
  productCodes,
1699
2016
  "topicProduct",
1700
2017
  "sys,fields",
1701
2018
  "fields.code",
1702
2019
  otherFilters
1703
2020
  );
1704
- log(`Founded ${entries.length} topicProduct`);
2021
+ log(`Founded ${productEntries.length} topicProduct`);
2022
+
2023
+ let familyEntries: Entry[] = []
2024
+ if(familyCodes.length){
2025
+ familyEntries = await getAllEntriesByCodes(
2026
+ familyCodes,
2027
+ "topicFamily",
2028
+ "sys,fields",
2029
+ "fields.code",
2030
+ otherFilters
2031
+ );
2032
+ }
2033
+ log(`Founded ${familyEntries.length} topicFamily`);
2034
+
2035
+ let subFamilyEntries: Entry[] = []
2036
+ if(subFamilyCodes.length){
2037
+ subFamilyEntries = await getAllEntriesByCodes(
2038
+ subFamilyCodes,
2039
+ "topicSubFamily",
2040
+ "sys,fields",
2041
+ "fields.code",
2042
+ otherFilters
2043
+ );
2044
+ }
2045
+ log(`Founded ${subFamilyEntries.length} topicSubFamily`);
1705
2046
 
1706
- if (!entries.length) {
2047
+ if (!productEntries.length && !familyEntries.length && !subFamilyEntries.length) {
1707
2048
  log(
1708
- `No products found between offset: ${offset} and limit: ${limit}. Total: ${total}`
2049
+ `No items found between offset: ${offset} and limit: ${limit}. Total: ${total}`
1709
2050
  );
1710
2051
  const nextOffset = offset + limit;
1711
2052
  const completed = limit === -1 || offset >= total;
@@ -1732,188 +2073,43 @@ export const audit = async (
1732
2073
  };
1733
2074
  }
1734
2075
 
1735
- const pageCodes = entries.map((entry: Entry) =>
2076
+ const pageCodes = productEntries.map((entry: Entry) =>
1736
2077
  getProductPageIdByCode(entry.fields.code[defaultEnvironmentLocaleCode])
1737
2078
  );
1738
2079
 
1739
2080
  log(`Get ${pageCodes.length} product page entry from Contentful`);
1740
- const entriesPage = await getAllEntriesByCodes(
2081
+ const productPageEntries = await getAllEntriesByCodes(
1741
2082
  pageCodes,
1742
2083
  "page",
1743
2084
  "sys,fields",
1744
2085
  "sys.id"
1745
2086
  );
1746
- log(`Founded ${entriesPage.length} topicProduct pages`);
2087
+ log(`Founded ${productPageEntries.length} topicProduct pages`);
1747
2088
 
1748
2089
  let count: number = offset;
1749
2090
  let current: number = 0;
1750
2091
  for (const audit of allAudit) {
1751
2092
  log(`${++count} of ${total}`);
1752
- log(`I process the product ${audit.product} with status ${audit.what}`);
1753
-
1754
- log(`Search product entry with id ${audit.product}...`);
1755
- let productEntry = entries.find(
1756
- (currentEntry) =>
1757
- currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1758
- audit.product
1759
- );
1760
-
1761
- if (!productEntry) {
1762
- let logMessage = "";
1763
- if (catalog) {
1764
- logMessage = `The ${audit.product} product was not found in the CMS with catalog ${catalog}`;
1765
- } else {
1766
- logMessage = `The ${audit.product} product was not found in the CMS`;
1767
- }
1768
- log(logMessage, "WARN");
2093
+ log(`I process the item ${audit.product} with status ${audit.what}`);
1769
2094
 
1770
- if (serverUtils) {
1771
- serverUtils.log(logMessage);
1772
- const progress = Math.floor((++current / allAudit.length) * 100);
1773
- serverUtils.updateProgress(progress);
2095
+ if(productAuditWhat.includes(audit.what)){
2096
+ const result = await productAudit(audit, productEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit, productPageEntries)
2097
+ current = result.current
2098
+ if(result.continue){
2099
+ continue
1774
2100
  }
1775
- continue;
1776
- }
1777
-
1778
- log(`Founded product with id ${productEntry.sys.id}`);
1779
-
1780
- const pageEntryFromId = getProductPageIdByCode(audit.product);
1781
- log(`Search product page entry with id ${pageEntryFromId}...`);
1782
- let pageEntryFrom = entriesPage.find(
1783
- (currentEntry) => currentEntry.sys.id === pageEntryFromId
1784
- );
1785
-
1786
- if (pageEntryFrom) {
1787
- log(`Founded product page with id ${pageEntryFrom.sys.id}`);
1788
- } else {
1789
- log(`Product page with id ${pageEntryFromId} not found`, "WARN");
1790
- }
1791
-
1792
- log(`Get product catalogs...`);
1793
- const productCatalogs: AvailableCatalogs[] =
1794
- productEntry?.fields?.catalogs?.[defaultEnvironmentLocaleCode].map(
1795
- (entryCatalog: FieldItem) => {
1796
- return entryCatalog.sys.id;
1797
- }
1798
- );
1799
-
1800
- if (catalog && !productCatalogs.includes(catalog)) {
1801
- log(
1802
- `Product ${audit.product} does not belong to the ${catalog} catalog`
1803
- );
1804
- } else if (["PRODUCT_UNPUBLISH", "PRODUCT_WEBOFF"].includes(audit.what)) {
1805
- log(`Set the product status as archive...`);
1806
- // Set the product status as archive
1807
- productEntry = await archiveEntry(productEntry, true);
1808
- if (!pageEntryFrom) {
1809
- log(`${pageEntryFromId} page from not found`);
1810
- } else if (pageEntryFrom.isArchived()) {
1811
- log(
1812
- `Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryFrom.sys.id} is archived.`,
1813
- "WARN"
1814
- );
1815
- } else if (pageEntryFrom?.fields) {
1816
- if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
1817
- log(
1818
- `Creating redirect from ${audit.product} to ${audit.upgradeProduct}`
1819
- );
1820
- const pageEntryToId = getProductPageIdByCode(audit.upgradeProduct);
1821
- const pageEntryTo = await getEntryByID(
1822
- pageEntryToId,
1823
- "page",
1824
- "sys.id"
1825
- );
1826
-
1827
- if (pageEntryTo) {
1828
- // Save redirect
1829
- pageEntryFrom.fields = await addToRelationFields(
1830
- pageEntryFrom,
1831
- "redirectTo",
1832
- pageEntryTo.sys.id
1833
- );
1834
- pageEntryFrom = await pageEntryFrom.update();
1835
- } else {
1836
- log(
1837
- `Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryToId} not found.`,
1838
- "WARN"
1839
- );
1840
- }
1841
- }
1842
-
1843
- pageEntryFrom = await archiveEntry(pageEntryFrom);
2101
+ }else if(audit.what === "REMOVE_FAMILY_RELATIONS"){
2102
+ const result = await familyAudit(audit, familyEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit)
2103
+ current = result.current
2104
+ if(result.continue){
2105
+ continue
1844
2106
  }
1845
- } else if (
1846
- audit.what === "REMOVE_PRODUCT_RELATIONS" &&
1847
- !productEntry.isArchived()
1848
- ) {
1849
- for (const item of audit?.catalogs) {
1850
- if (!item?.where) {
1851
- log(
1852
- `catalogs.where field not exists. Product: ${audit.product} `,
1853
- "WARN"
1854
- );
1855
- } else {
1856
- if (item?.catalogCode) {
1857
- productEntry.fields = await removeFromRelationFields(
1858
- productEntry,
1859
- "catalogs",
1860
- item.catalogCode,
1861
- true
1862
- );
1863
- }
1864
- if (item?.categoryCode) {
1865
- const categoriesFieldKey =
1866
- "categories" + capitalizeFirstLetter(item.where);
1867
- productEntry.fields = await removeFromRelationFields(
1868
- productEntry,
1869
- categoriesFieldKey,
1870
- item.categoryCode,
1871
- true
1872
- );
1873
- }
1874
- if (item?.subfamilyCode) {
1875
- const subFamiliesFieldKey =
1876
- "subFamilies" + capitalizeFirstLetter(item.where);
1877
- productEntry.fields = await removeFromRelationFields(
1878
- productEntry,
1879
- subFamiliesFieldKey,
1880
- item.subfamilyCode,
1881
- true
1882
- );
1883
- }
1884
- if (item?.models) {
1885
- const modelsFieldKey =
1886
- "models" + capitalizeFirstLetter(item.where);
1887
- productEntry.fields = await removeFromRelationFields(
1888
- productEntry,
1889
- modelsFieldKey,
1890
- item.models,
1891
- true
1892
- );
1893
- }
1894
- if (item?.subModels) {
1895
- const subModelsFieldKey =
1896
- "subModels" + capitalizeFirstLetter(item.where);
1897
- productEntry.fields = await removeFromRelationFields(
1898
- productEntry,
1899
- subModelsFieldKey,
1900
- item.subModels,
1901
- true
1902
- );
1903
- }
1904
- // lastPimSyncDate
1905
- productEntry.fields = await addFieldValue(
1906
- productEntry,
1907
- "lastPimSyncDate",
1908
- getLocalISOTime()
1909
- );
1910
- productEntry = await productEntry.update();
1911
- }
2107
+ }else if(audit.what === "REMOVE_SUBFAMILY_RELATIONS"){
2108
+ const result = await subFamilyAudit(audit, subFamilyEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit)
2109
+ current = result.current
2110
+ if(result.continue){
2111
+ continue
1912
2112
  }
1913
- } else {
1914
- log(
1915
- `It has not yet been defined how to process the state ${audit.what}`
1916
- );
1917
2113
  }
1918
2114
 
1919
2115
  if (serverUtils) {
@@ -1924,7 +2120,6 @@ export const audit = async (
1924
2120
  }
1925
2121
 
1926
2122
  const nextOffset = offset + limit;
1927
-
1928
2123
  const tEnd = new Date();
1929
2124
  const secs = secondBetweenTwoDate(timeStart, tEnd);
1930
2125
  log(`Execution time: ${secs} seconds`);
@@ -1935,7 +2130,7 @@ export const audit = async (
1935
2130
  completed: limit === -1 || count >= total,
1936
2131
  s3FilePath,
1937
2132
  total,
1938
- processedEntries: entries.length,
2133
+ processedEntries: productEntries.length + familyEntries.length + subFamilyEntries.length,
1939
2134
  };
1940
2135
  } else {
1941
2136
  log(`Execution completed`);
@@ -2694,3 +2889,45 @@ export const reimportAuditProducts = async (
2694
2889
  processedEntries: 0,
2695
2890
  };
2696
2891
  };
2892
+
2893
+
2894
+ export const populateDestinations = async (offset: number, limit: number) => {
2895
+ const env = await getEnvironment();
2896
+ const defEnvLocaleCode = await getEnvironmentDefaultLocaleCode();
2897
+
2898
+ const opts = {
2899
+ content_type: 'topicProduct',
2900
+ skip: offset,
2901
+ limit,
2902
+ locale: defEnvLocaleCode,
2903
+ // select: 'sys.id,fields',
2904
+ "sys.archivedAt[exists]": false,
2905
+ };
2906
+ const {items, total} = await env.getEntries(opts);
2907
+
2908
+ let count: number = offset;
2909
+ for (let productEntry of items) {
2910
+ log(`${++count} of ${total}`);
2911
+ log(`Product: ${productEntry.sys.id}`);
2912
+ const destinations = productEntry.fields.productFields[defEnvLocaleCode]?.destinations?.map((destination:any) => destination.code) || []
2913
+
2914
+ if(destinations.length){
2915
+ log(`Set destinations: ${destinations.join(', ')}`)
2916
+ productEntry.fields = await addFieldValue(productEntry, "destinations", destinations)
2917
+ productEntry = await productEntry.update();
2918
+ }else{
2919
+ log(`No destinations found.`, "WARN")
2920
+ }
2921
+
2922
+ if (productEntry.isPublished()) {
2923
+ try {
2924
+ productEntry = await productEntry.publish();
2925
+ } catch (err) {
2926
+ log(`Cannot publish entry.`);
2927
+ log(err);
2928
+ }
2929
+ }
2930
+ }
2931
+
2932
+ return {completed: total === count + offset, offset, limit}
2933
+ }
@@ -396,9 +396,19 @@ const getTopicSubFamilyData = async (
396
396
  }
397
397
  }
398
398
 
399
+ // Destinations
400
+ if(subFamily?.destinations?.length){
401
+ log(`set destinations`);
402
+ data.fields = await addFieldValue(data, "destinations", subFamily.destinations);
403
+ }else{
404
+ log(`destinations not found`, "WARN");
405
+ }
406
+
399
407
  // lastPimSyncDate
400
408
  data.fields = await addFieldValue(data, "lastPimSyncDate", getLocalISOTime());
401
409
 
410
+
411
+
402
412
  return data;
403
413
  };
404
414
 
@@ -112,9 +112,18 @@ const getTopicSubModelData = async (
112
112
  }
113
113
  }
114
114
 
115
+ // Destinations
116
+ if(subModel?.destinations?.length){
117
+ log(`set destinations`);
118
+ data.fields = await addFieldValue(data, "destinations", subModel.destinations);
119
+ }else{
120
+ log(`destinations not found`, "WARN");
121
+ }
122
+
115
123
  // lastPimSyncDate
116
124
  data.fields = await addFieldValue(data, "lastPimSyncDate", getLocalISOTime());
117
125
 
126
+
118
127
  return data;
119
128
  };
120
129
 
@@ -5,6 +5,7 @@ type AuditCatalog = {
5
5
  subfamilyCode?: string;
6
6
  models?: string;
7
7
  subModels?: string;
8
+ codes?: string;
8
9
  };
9
10
  export interface Audit {
10
11
  what: string;