pim-import 2.47.3 → 2.49.1

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.
@@ -12,6 +12,7 @@ import {
12
12
  AvailableCatalogs,
13
13
  CfLocalizedEntryField,
14
14
  WrapperImageFields,
15
+ CfSys,
15
16
  } from "../../types";
16
17
  import { log } from "../../libs/logs";
17
18
  import {
@@ -26,11 +27,14 @@ import {
26
27
  addFieldValue,
27
28
  addToRelationFields,
28
29
  createWrapperImgix,
30
+ getDictionaryLocaleValue,
31
+ getDictionaryJson,
29
32
  } from "../../libs/contentful";
30
33
  import type {
31
34
  Entry,
32
35
  CreateEntryProps,
33
36
  } from "contentful-management/dist/typings/entities/entry";
37
+ import { KeyValueMap } from "contentful-management/dist/typings/common-types";
34
38
  import {
35
39
  stringToSlug,
36
40
  secondBetweenTwoDate,
@@ -384,7 +388,10 @@ const getProductFields = (pimDetails: any) => {
384
388
  const productFields: any = {};
385
389
  productFieldsRequiredData.forEach((fieldData) => {
386
390
  if (fieldData.parent) {
387
- if (pimDetails?.[fieldData.parent]?.[fieldData.key]) {
391
+ if (
392
+ pimDetails?.[fieldData.parent]?.[fieldData.key] ||
393
+ pimDetails?.[fieldData.parent]?.[fieldData.key] === false
394
+ ) {
388
395
  if (Array.isArray(pimDetails[fieldData.parent][fieldData.key])) {
389
396
  pimDetails[fieldData.parent][fieldData.key].forEach((item: any) => {
390
397
  if (!productFields?.[fieldData.parent]?.[fieldData.key]) {
@@ -413,7 +420,8 @@ const getProductFields = (pimDetails: any) => {
413
420
  };
414
421
  } else if (
415
422
  typeof pimDetails[fieldData.parent][fieldData.key] !== "object" &&
416
- sanitizeValue(pimDetails[fieldData.parent][fieldData.key])
423
+ (sanitizeValue(pimDetails[fieldData.parent][fieldData.key]) ||
424
+ pimDetails[fieldData.parent][fieldData.key] === false)
417
425
  ) {
418
426
  value = pimDetails[fieldData.parent][fieldData.key];
419
427
  }
@@ -456,7 +464,8 @@ const getProductFields = (pimDetails: any) => {
456
464
  };
457
465
  } else if (
458
466
  typeof pimDetails[fieldData.key] !== "object" &&
459
- sanitizeValue(pimDetails[fieldData.key])
467
+ (sanitizeValue(pimDetails[fieldData.key]) ||
468
+ pimDetails[fieldData.key] === false)
460
469
  ) {
461
470
  value = pimDetails[fieldData.key];
462
471
  }
@@ -896,15 +905,6 @@ const getProductData = async (
896
905
  // lastPimSyncDate
897
906
  data.fields = await addFieldValue(data, "lastPimSyncDate", getLocalISOTime());
898
907
 
899
- // endpoint payload - used into setProductRelationships
900
-
901
- const productEntryId = getTopicProductIdByCode(productDetails.code);
902
- const { path, fileName } = getProductPayloadS3Details(productEntryId);
903
- log(`Uploading "${fileName}" to S3 path "${path}"`);
904
- const { Location } = await saveJsonToS3(pimDetails, fileName, path);
905
- data.fields = await addFieldValue(data, "endpointPayload", Location);
906
- log(`"${fileName}" uploaded to ${Location}`);
907
-
908
908
  // designers
909
909
  if (productDetails.designers) {
910
910
  for (const designer of productDetails.designers) {
@@ -937,6 +937,19 @@ const getProductData = async (
937
937
  }
938
938
  }
939
939
 
940
+ // autodescription
941
+ data.fields.autoDescription = await getTopicProductAutodescription(
942
+ data.fields
943
+ );
944
+
945
+ // endpoint payload - used into setProductRelationships
946
+ const productEntryId = getTopicProductIdByCode(productDetails.code);
947
+ const { path, fileName } = getProductPayloadS3Details(productEntryId);
948
+ log(`Uploading "${fileName}" to S3 path "${path}"`);
949
+ const { Location } = await saveJsonToS3(pimDetails, fileName, path);
950
+ data.fields = await addFieldValue(data, "endpointPayload", Location);
951
+ log(`"${fileName}" uploaded to ${Location}`);
952
+
940
953
  return data;
941
954
  };
942
955
 
@@ -1752,3 +1765,288 @@ export const generateTechSpecPdf = async (topicProductId: string) => {
1752
1765
 
1753
1766
  return s3Url;
1754
1767
  };
1768
+
1769
+ const getLightModuleAutoDescriptionByProductFields = async (
1770
+ productFileds: any,
1771
+ catalogs: AvailableCatalogs[]
1772
+ ) => {
1773
+ const autoDescription: any = {};
1774
+ const dictionaryJson = await getDictionaryJson();
1775
+ for (const locale of cfLocales) {
1776
+ if (!autoDescription?.[locale]) {
1777
+ autoDescription[locale] = "";
1778
+ }
1779
+ let addSeparator = false;
1780
+ if (productFileds?.electrical?.lampCategories) {
1781
+ const lampCategoriesLocalizedValues = [];
1782
+ for (const lampCategory of productFileds.electrical.lampCategories) {
1783
+ const lampCategoryLocalizedValue = await getDictionaryLocaleValue(
1784
+ locale,
1785
+ "lampCategories",
1786
+ lampCategory.code,
1787
+ "electrical",
1788
+ dictionaryJson
1789
+ );
1790
+ if (lampCategoryLocalizedValue) {
1791
+ lampCategoriesLocalizedValues.push(lampCategoryLocalizedValue);
1792
+ }
1793
+ }
1794
+
1795
+ if (lampCategoriesLocalizedValues.length) {
1796
+ if (addSeparator) {
1797
+ autoDescription[locale] += " - ";
1798
+ } else {
1799
+ addSeparator = true;
1800
+ }
1801
+ autoDescription[locale] += lampCategoriesLocalizedValues.join(", ");
1802
+ if (
1803
+ productFileds.electrical.lampCategories.find(
1804
+ (item: any) => item.code === "LAMPCAT4"
1805
+ ) &&
1806
+ productFileds?.photometric?.ledTypes?.length
1807
+ ) {
1808
+ const ledTypeLocalizedValues = [];
1809
+ for (const ledType of productFileds?.photometric?.ledTypes) {
1810
+ const ledTypeLocalizedValue = await getDictionaryLocaleValue(
1811
+ locale,
1812
+ "ledTypes",
1813
+ ledType.code,
1814
+ "photometric",
1815
+ dictionaryJson
1816
+ );
1817
+ if (ledTypeLocalizedValue) {
1818
+ ledTypeLocalizedValues.push(ledTypeLocalizedValue);
1819
+ }
1820
+ }
1821
+
1822
+ if (ledTypeLocalizedValues.length) {
1823
+ if (addSeparator) {
1824
+ autoDescription[locale] += " - ";
1825
+ } else {
1826
+ addSeparator = true;
1827
+ }
1828
+ autoDescription[locale] += ledTypeLocalizedValues.join(", ");
1829
+ }
1830
+ }
1831
+ }
1832
+ }
1833
+
1834
+ if (
1835
+ productFileds?.optical?.numberOfHeads &&
1836
+ productFileds?.optical?.numberOfHeads >= 2
1837
+ ) {
1838
+ if (productFileds?.electrical?.wPower) {
1839
+ if (addSeparator) {
1840
+ autoDescription[locale] += " - ";
1841
+ } else {
1842
+ addSeparator = true;
1843
+ }
1844
+ autoDescription[
1845
+ locale
1846
+ ] += `${productFileds.optical.numberOfHeads} x ${productFileds.electrical.wPower}`;
1847
+ }
1848
+ if (productFileds?.photometric?.realNetFlow) {
1849
+ if (addSeparator) {
1850
+ autoDescription[locale] += " - ";
1851
+ } else {
1852
+ addSeparator = true;
1853
+ }
1854
+ autoDescription[
1855
+ locale
1856
+ ] += `${productFileds.optical.numberOfHeads} x ${productFileds.photometric.realNetFlow}lm`;
1857
+ }
1858
+ }
1859
+
1860
+ if (productFileds?.photometric?.temperatureColor?.code) {
1861
+ const temperatireColorLocalizedValue = await getDictionaryLocaleValue(
1862
+ locale,
1863
+ "temperatureColor",
1864
+ productFileds.photometric.temperatureColor.code,
1865
+ "photometric",
1866
+ dictionaryJson
1867
+ );
1868
+ if (temperatireColorLocalizedValue) {
1869
+ if (addSeparator) {
1870
+ autoDescription[locale] += " - ";
1871
+ } else {
1872
+ addSeparator = true;
1873
+ }
1874
+ autoDescription[locale] += temperatireColorLocalizedValue;
1875
+ }
1876
+ }
1877
+
1878
+ if (productFileds?.photometric?.cri?.code) {
1879
+ const CriLocalizedValue = await getDictionaryLocaleValue(
1880
+ locale,
1881
+ "cri",
1882
+ productFileds.photometric.cri.code,
1883
+ "photometric",
1884
+ dictionaryJson
1885
+ );
1886
+ if (CriLocalizedValue) {
1887
+ if (addSeparator) {
1888
+ autoDescription[locale] += " - ";
1889
+ } else {
1890
+ addSeparator = true;
1891
+ }
1892
+ autoDescription[locale] += "CRI " + CriLocalizedValue;
1893
+ }
1894
+ }
1895
+
1896
+ if (
1897
+ productFileds?.photometric?.beam0_180 &&
1898
+ catalogs.indexOf("OUTDOOR") !== -1
1899
+ ) {
1900
+ if (addSeparator) {
1901
+ autoDescription[locale] += " - ";
1902
+ } else {
1903
+ addSeparator = true;
1904
+ }
1905
+ autoDescription[locale] += "BEAM " + productFileds.photometric.beam0_180;
1906
+ }
1907
+ }
1908
+
1909
+ return autoDescription;
1910
+ };
1911
+
1912
+ const getOtherAutoDescriptionByProductFields = async (productFileds: any) => {
1913
+ const autoDescription: any = {};
1914
+ const dictionaryJson = await getDictionaryJson();
1915
+ for (const locale of cfLocales) {
1916
+ if (!autoDescription?.[locale]) {
1917
+ autoDescription[locale] = "";
1918
+ }
1919
+ let addSeparator = false;
1920
+ // Profile and others
1921
+ if (productFileds?.physical?.length) {
1922
+ autoDescription[locale] += "Length: " + productFileds.physical.length;
1923
+ addSeparator = true;
1924
+ }
1925
+
1926
+ if (productFileds?.mountings) {
1927
+ const mountingsLocalizedValues = [];
1928
+ for (const mounting of productFileds.mountings) {
1929
+ const mountingLocalizedValue = await getDictionaryLocaleValue(
1930
+ locale,
1931
+ "mountings",
1932
+ mounting.code,
1933
+ "",
1934
+ dictionaryJson
1935
+ );
1936
+ if (mountingLocalizedValue) {
1937
+ mountingsLocalizedValues.push(mountingLocalizedValue);
1938
+ }
1939
+ }
1940
+
1941
+ if (addSeparator) {
1942
+ autoDescription[locale] += " - ";
1943
+ } else {
1944
+ addSeparator = true;
1945
+ }
1946
+ autoDescription[locale] += mountingsLocalizedValues.join(", ");
1947
+ }
1948
+ }
1949
+
1950
+ return autoDescription;
1951
+ };
1952
+
1953
+ /**
1954
+ * Get topicProduct autoDescription
1955
+ *
1956
+ * @param topicProduct
1957
+ * @returns
1958
+ */
1959
+ const getTopicProductAutodescription = async (
1960
+ topicProductFields: KeyValueMap
1961
+ ) => {
1962
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
1963
+ let catalogs = topicProductFields?.catalogs?.[defaultEnvironmentLocaleCode];
1964
+ if (catalogs) {
1965
+ catalogs = catalogs.map((catalog: CfSys) => catalog.sys.id);
1966
+ }
1967
+ const productLineCode =
1968
+ topicProductFields?.productLine?.[defaultEnvironmentLocaleCode]?.sys?.id;
1969
+ const productFileds =
1970
+ topicProductFields?.productFields?.[defaultEnvironmentLocaleCode];
1971
+
1972
+ let autoDescription: any = {};
1973
+ switch (productLineCode) {
1974
+ case "PLINE6": // Soft Plate
1975
+ case "PLINE5": // Light Bulb
1976
+ case "PLINE3": // Driver
1977
+ case "PLINE2": // Accessory
1978
+ // post_content
1979
+ autoDescription = topicProductFields?.description;
1980
+ break;
1981
+ case "PLINE1": // Light Module
1982
+ autoDescription = await getLightModuleAutoDescriptionByProductFields(
1983
+ productFileds,
1984
+ catalogs
1985
+ );
1986
+
1987
+ break;
1988
+ default:
1989
+ autoDescription = await getOtherAutoDescriptionByProductFields(
1990
+ productFileds
1991
+ );
1992
+
1993
+ break;
1994
+ }
1995
+
1996
+ return autoDescription;
1997
+ };
1998
+
1999
+ export const setProductAutodescription = async (
2000
+ catalog: AvailableCatalogs,
2001
+ offset: number = 0,
2002
+ limit: number = 100
2003
+ ) => {
2004
+ log(
2005
+ `setProductAutodescription - catalog: ${catalog}, offset: ${offset}, limit: ${limit}`,
2006
+ "INFO"
2007
+ );
2008
+ const env = await getEnvironment();
2009
+ const defEnvLocaleCode = await getEnvironmentDefaultLocaleCode();
2010
+
2011
+ const { items, total } = await env.getEntries({
2012
+ content_type: "topicProduct",
2013
+ "fields.catalogs.sys.id": catalog,
2014
+ limit,
2015
+ skip: offset,
2016
+ locale: defEnvLocaleCode,
2017
+ include: 0,
2018
+ select: "sys,fields",
2019
+ "sys.archivedAt[exists]": false,
2020
+ });
2021
+
2022
+ for (let item of items) {
2023
+ log(`Set audtodescription of the topic ${item.sys.id}`);
2024
+ const autoDescription = await getTopicProductAutodescription(item.fields);
2025
+ item.fields.autoDescription = autoDescription;
2026
+ if (!item.isArchived()) {
2027
+ item = await item.update();
2028
+ if (item.isPublished()) {
2029
+ item = await item.publish();
2030
+ }
2031
+ } else {
2032
+ log(
2033
+ `Product ${item.sys.id} is archived, it is not possible to update the autodescription`,
2034
+ "WARN"
2035
+ );
2036
+ }
2037
+ }
2038
+
2039
+ return {
2040
+ catalog,
2041
+ offset: Number(offset),
2042
+ limit: Number(limit),
2043
+ completed: false,
2044
+ total,
2045
+ };
2046
+ };
2047
+
2048
+ export const getProductAutodescription = async (topicProductId: string) => {
2049
+ const topicProduct = await getEntryByID(topicProductId, "topicProduct");
2050
+
2051
+ return await getTopicProductAutodescription(topicProduct.fields);
2052
+ };
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