pim-import 2.47.4 → 2.49.2

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.
@@ -26,11 +26,14 @@ import {
26
26
  addFieldValue,
27
27
  addToRelationFields,
28
28
  createWrapperImgix,
29
+ getDictionaryLocaleValue,
30
+ getDictionaryJson,
29
31
  } from "../../libs/contentful";
30
32
  import type {
31
33
  Entry,
32
34
  CreateEntryProps,
33
35
  } from "contentful-management/dist/typings/entities/entry";
36
+ import { KeyValueMap } from "contentful-management/dist/typings/common-types";
34
37
  import {
35
38
  stringToSlug,
36
39
  secondBetweenTwoDate,
@@ -901,15 +904,6 @@ const getProductData = async (
901
904
  // lastPimSyncDate
902
905
  data.fields = await addFieldValue(data, "lastPimSyncDate", getLocalISOTime());
903
906
 
904
- // endpoint payload - used into setProductRelationships
905
-
906
- const productEntryId = getTopicProductIdByCode(productDetails.code);
907
- const { path, fileName } = getProductPayloadS3Details(productEntryId);
908
- log(`Uploading "${fileName}" to S3 path "${path}"`);
909
- const { Location } = await saveJsonToS3(pimDetails, fileName, path);
910
- data.fields = await addFieldValue(data, "endpointPayload", Location);
911
- log(`"${fileName}" uploaded to ${Location}`);
912
-
913
907
  // designers
914
908
  if (productDetails.designers) {
915
909
  for (const designer of productDetails.designers) {
@@ -942,6 +936,19 @@ const getProductData = async (
942
936
  }
943
937
  }
944
938
 
939
+ // autodescription
940
+ data.fields.autoDescription = await getTopicProductAutodescription(
941
+ data.fields
942
+ );
943
+
944
+ // endpoint payload - used into setProductRelationships
945
+ const productEntryId = getTopicProductIdByCode(productDetails.code);
946
+ const { path, fileName } = getProductPayloadS3Details(productEntryId);
947
+ log(`Uploading "${fileName}" to S3 path "${path}"`);
948
+ const { Location } = await saveJsonToS3(pimDetails, fileName, path);
949
+ data.fields = await addFieldValue(data, "endpointPayload", Location);
950
+ log(`"${fileName}" uploaded to ${Location}`);
951
+
945
952
  return data;
946
953
  };
947
954
 
@@ -1757,3 +1764,292 @@ export const generateTechSpecPdf = async (topicProductId: string) => {
1757
1764
 
1758
1765
  return s3Url;
1759
1766
  };
1767
+
1768
+ const getLightModuleAutoDescriptionByProductFields = async (
1769
+ productFileds: any
1770
+ ) => {
1771
+ const autoDescription: any = {};
1772
+ const dictionaryJson = await getDictionaryJson();
1773
+ for (const locale of cfLocales) {
1774
+ if (!autoDescription?.[locale]) {
1775
+ autoDescription[locale] = "";
1776
+ }
1777
+ let addSeparator = false;
1778
+ if (productFileds?.electrical?.lampCategories) {
1779
+ const lampCategoriesLocalizedValues = [];
1780
+ for (const lampCategory of productFileds.electrical.lampCategories) {
1781
+ const lampCategoryLocalizedValue = await getDictionaryLocaleValue(
1782
+ locale,
1783
+ "lampCategories",
1784
+ lampCategory.code,
1785
+ "electrical",
1786
+ dictionaryJson
1787
+ );
1788
+ if (lampCategoryLocalizedValue) {
1789
+ lampCategoriesLocalizedValues.push(lampCategoryLocalizedValue);
1790
+ }
1791
+ }
1792
+
1793
+ if (lampCategoriesLocalizedValues.length) {
1794
+ if (addSeparator) {
1795
+ autoDescription[locale] += " - ";
1796
+ } else {
1797
+ addSeparator = true;
1798
+ }
1799
+ autoDescription[locale] += lampCategoriesLocalizedValues.join(", ");
1800
+ if (
1801
+ productFileds.electrical.lampCategories.find(
1802
+ (item: any) => item.code === "LAMPCAT4"
1803
+ ) &&
1804
+ productFileds?.photometric?.ledTypes?.length
1805
+ ) {
1806
+ const ledTypeLocalizedValues = [];
1807
+ for (const ledType of productFileds?.photometric?.ledTypes) {
1808
+ const ledTypeLocalizedValue = await getDictionaryLocaleValue(
1809
+ locale,
1810
+ "ledTypes",
1811
+ ledType.code,
1812
+ "photometric",
1813
+ dictionaryJson
1814
+ );
1815
+ if (ledTypeLocalizedValue) {
1816
+ ledTypeLocalizedValues.push(ledTypeLocalizedValue);
1817
+ }
1818
+ }
1819
+
1820
+ if (ledTypeLocalizedValues.length) {
1821
+ if (addSeparator) {
1822
+ autoDescription[locale] += " - ";
1823
+ } else {
1824
+ addSeparator = true;
1825
+ }
1826
+ autoDescription[locale] += ledTypeLocalizedValues.join(", ");
1827
+ }
1828
+ }
1829
+ }
1830
+ }
1831
+
1832
+ if (productFileds?.electrical?.wPower) {
1833
+ if (addSeparator) {
1834
+ autoDescription[locale] += " - ";
1835
+ } else {
1836
+ addSeparator = true;
1837
+ }
1838
+ if (productFileds?.optical?.numberOfHeads >= 2) {
1839
+ autoDescription[locale] += `${productFileds.optical.numberOfHeads} x `;
1840
+ }
1841
+ autoDescription[locale] += `${productFileds.electrical.wPower}W`;
1842
+ }
1843
+ if (productFileds?.photometric?.realNetFlow) {
1844
+ if (addSeparator) {
1845
+ autoDescription[locale] += " - ";
1846
+ } else {
1847
+ addSeparator = true;
1848
+ }
1849
+ if (productFileds?.optical?.numberOfHeads >= 2) {
1850
+ autoDescription[locale] += `${productFileds.optical.numberOfHeads} x `;
1851
+ }
1852
+ autoDescription[locale] += `${productFileds.photometric.realNetFlow}lm`;
1853
+ }
1854
+
1855
+ if (productFileds?.photometric?.temperatureColor?.code) {
1856
+ const temperatireColorLocalizedValue = await getDictionaryLocaleValue(
1857
+ locale,
1858
+ "temperatureColor",
1859
+ productFileds.photometric.temperatureColor.code,
1860
+ "photometric",
1861
+ dictionaryJson
1862
+ );
1863
+ if (temperatireColorLocalizedValue) {
1864
+ if (addSeparator) {
1865
+ autoDescription[locale] += " - ";
1866
+ } else {
1867
+ addSeparator = true;
1868
+ }
1869
+ autoDescription[locale] += temperatireColorLocalizedValue;
1870
+ }
1871
+ }
1872
+
1873
+ if (productFileds?.photometric?.cri?.code) {
1874
+ const CriLocalizedValue = await getDictionaryLocaleValue(
1875
+ locale,
1876
+ "cri",
1877
+ productFileds.photometric.cri.code,
1878
+ "photometric",
1879
+ dictionaryJson
1880
+ );
1881
+ if (CriLocalizedValue) {
1882
+ if (addSeparator) {
1883
+ autoDescription[locale] += " - ";
1884
+ } else {
1885
+ addSeparator = true;
1886
+ }
1887
+ autoDescription[locale] += "CRI> " + CriLocalizedValue;
1888
+ }
1889
+ }
1890
+
1891
+ if (productFileds?.photometric?.beam0_180) {
1892
+ if (addSeparator) {
1893
+ autoDescription[locale] += " - ";
1894
+ } else {
1895
+ addSeparator = true;
1896
+ }
1897
+ autoDescription[locale] += "BEAM° " + productFileds.photometric.beam0_180;
1898
+ }
1899
+ }
1900
+
1901
+ return autoDescription;
1902
+ };
1903
+
1904
+ const getOtherAutoDescriptionByProductFields = async (productFileds: any) => {
1905
+ const autoDescription: any = {};
1906
+ const dictionaryJson = await getDictionaryJson();
1907
+ for (const locale of cfLocales) {
1908
+ if (!autoDescription?.[locale]) {
1909
+ autoDescription[locale] = "";
1910
+ }
1911
+ let addSeparator = false;
1912
+ // Profile and others
1913
+ if (productFileds?.physical?.length) {
1914
+ autoDescription[locale] += "Length: " + productFileds.physical.length;
1915
+ addSeparator = true;
1916
+ }
1917
+
1918
+ if (productFileds?.mountings) {
1919
+ const mountingsLocalizedValues = [];
1920
+ for (const mounting of productFileds.mountings) {
1921
+ const mountingLocalizedValue = await getDictionaryLocaleValue(
1922
+ locale,
1923
+ "mountings",
1924
+ mounting.code,
1925
+ "",
1926
+ dictionaryJson
1927
+ );
1928
+ if (mountingLocalizedValue) {
1929
+ mountingsLocalizedValues.push(mountingLocalizedValue);
1930
+ }
1931
+ }
1932
+
1933
+ if (addSeparator) {
1934
+ autoDescription[locale] += " - ";
1935
+ } else {
1936
+ addSeparator = true;
1937
+ }
1938
+ autoDescription[locale] += mountingsLocalizedValues.join(", ");
1939
+ }
1940
+ }
1941
+
1942
+ return autoDescription;
1943
+ };
1944
+
1945
+ /**
1946
+ * Get topicProduct autoDescription
1947
+ *
1948
+ * @param topicProduct
1949
+ * @returns
1950
+ */
1951
+ const getTopicProductAutodescription = async (
1952
+ topicProductFields: KeyValueMap
1953
+ ) => {
1954
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
1955
+ const productLineCode =
1956
+ topicProductFields?.productLine?.[defaultEnvironmentLocaleCode]?.sys?.id;
1957
+ const productFileds =
1958
+ topicProductFields?.productFields?.[defaultEnvironmentLocaleCode];
1959
+
1960
+ let autoDescription: any = {};
1961
+ switch (productLineCode) {
1962
+ case "PLINE6": // Soft Plate
1963
+ case "PLINE5": // Light Bulb
1964
+ case "PLINE3": // Driver
1965
+ case "PLINE2": // Accessory
1966
+ // post_content
1967
+ autoDescription = topicProductFields?.description;
1968
+ break;
1969
+ case "PLINE1": // Light Module
1970
+ autoDescription = await getLightModuleAutoDescriptionByProductFields(
1971
+ productFileds
1972
+ );
1973
+
1974
+ break;
1975
+ default:
1976
+ autoDescription = await getOtherAutoDescriptionByProductFields(
1977
+ productFileds
1978
+ );
1979
+
1980
+ break;
1981
+ }
1982
+
1983
+ return autoDescription;
1984
+ };
1985
+
1986
+ const setProductAutodescription = async (topicProduct: Entry) => {
1987
+ log(`Set audtodescription of the topic ${topicProduct.sys.id}`);
1988
+ const autoDescription = await getTopicProductAutodescription(
1989
+ topicProduct.fields
1990
+ );
1991
+ topicProduct.fields.autoDescription = autoDescription;
1992
+ if (!topicProduct.isArchived()) {
1993
+ topicProduct = await topicProduct.update();
1994
+ if (topicProduct.isPublished()) {
1995
+ topicProduct = await topicProduct.publish();
1996
+ }
1997
+ } else {
1998
+ log(
1999
+ `Product ${topicProduct.sys.id} is archived, it is not possible to update the autodescription`,
2000
+ "WARN"
2001
+ );
2002
+ }
2003
+
2004
+ return autoDescription;
2005
+ };
2006
+
2007
+ export const setProductsAutodescription = async (
2008
+ catalog: AvailableCatalogs,
2009
+ offset: number = 0,
2010
+ limit: number = 100
2011
+ ) => {
2012
+ log(
2013
+ `setProductAutodescription - catalog: ${catalog}, offset: ${offset}, limit: ${limit}`,
2014
+ "INFO"
2015
+ );
2016
+ const env = await getEnvironment();
2017
+ const defEnvLocaleCode = await getEnvironmentDefaultLocaleCode();
2018
+
2019
+ const { items, total } = await env.getEntries({
2020
+ content_type: "topicProduct",
2021
+ "fields.catalogs.sys.id": catalog,
2022
+ limit,
2023
+ skip: offset,
2024
+ locale: defEnvLocaleCode,
2025
+ include: 0,
2026
+ select: "sys,fields",
2027
+ "sys.archivedAt[exists]": false,
2028
+ });
2029
+
2030
+ for (let item of items) {
2031
+ setProductAutodescription(item);
2032
+ }
2033
+
2034
+ return {
2035
+ catalog,
2036
+ offset: Number(offset) + Number(limit),
2037
+ limit: Number(limit),
2038
+ completed: Number(offset) + Number(limit) >= total,
2039
+ total,
2040
+ };
2041
+ };
2042
+
2043
+ export const setProductAutodescriptionByTopicId = async (
2044
+ topicProductId: string
2045
+ ) => {
2046
+ const topicProduct = await getEntryByID(topicProductId, "topicProduct");
2047
+
2048
+ return await setProductAutodescription(topicProduct);
2049
+ };
2050
+
2051
+ export const getProductAutodescription = async (topicProductId: string) => {
2052
+ const topicProduct = await getEntryByID(topicProductId, "topicProduct");
2053
+
2054
+ return await getTopicProductAutodescription(topicProduct.fields);
2055
+ };
package/src/types.ts CHANGED
@@ -40,7 +40,7 @@ export type PimLocale =
40
40
  | "no"
41
41
  | "da"
42
42
  | "ru";
43
- export type ContentfulLocale = "en-GB" | "en-US" | "it" | "es" | "de" | "fr";
43
+ export type ContentfulLocale = "en" | "en-US" | "it" | "es" | "de" | "fr";
44
44
 
45
45
  export type PimValues = {
46
46
  value_en: string;
package/src/utils.ts CHANGED
@@ -95,7 +95,7 @@ export const stringToSlug = (str: string, skipReplaceDots: boolean = false) => {
95
95
  };
96
96
 
97
97
  export const pimLocaleMap: Partial<Record<PimLocale, ContentfulLocale>> = {
98
- en: "en-GB",
98
+ en: "en",
99
99
  en_US: "en-US",
100
100
  it: "it",
101
101
  es: "es",
@@ -210,7 +210,7 @@ export const capitalizeFirstLetter = (text: string) => {
210
210
  * Convert the keys of the object to lowercase
211
211
  *
212
212
  * @example
213
- * keysToLowerCase({'en-GB' : "Foo"}) -> {'en-gb': 'Foo'}
213
+ * keysToLowerCase({'en' : "Foo"}) -> {'en': 'Foo'}
214
214
  *
215
215
  * @param obj
216
216
  * @param includeNestedKeys if true all keys of all levels nested in the object will be converted