pim-import 4.33.2 → 4.34.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.
@@ -61,6 +61,7 @@ import {
61
61
  getDefaultWrapperImgixAttributesByPimUrl,
62
62
  getImgixPimUrlByOriginPath,
63
63
  getOriginPathByPimUrl,
64
+ purgeImageCacheByUrl,
64
65
  } from "../../libs/imgix";
65
66
  import { generatePDFByUrl } from "../../libs/pdf";
66
67
  import { reindexProduct } from "../../algolia/products";
@@ -1856,173 +1857,183 @@ const productAudit = async (
1856
1857
  log(err);
1857
1858
  }
1858
1859
  }
1859
- for (const item of audit?.catalogs) {
1860
- if (!item?.where) {
1861
- log(
1862
- `catalogs.where field not exists. Product: ${audit.product} `,
1863
- "WARN"
1864
- );
1865
- } else {
1866
- let edit = false;
1867
- if (item?.catalogCode) {
1868
- productEntry.fields = await removeFromRelationFields(
1869
- productEntry,
1870
- "catalogs",
1871
- item.catalogCode,
1872
- true
1873
- );
1874
- log(`edit catalogs`);
1875
- edit = true;
1876
- }
1877
- if (item?.categoryCode) {
1878
- const categoriesFieldKey =
1879
- "categories" + capitalizeFirstLetter(item.where);
1880
- productEntry.fields = await removeFromRelationFields(
1881
- productEntry,
1882
- categoriesFieldKey,
1883
- item.categoryCode,
1884
- true
1885
- );
1886
- log(`edit ${categoriesFieldKey}`);
1887
- edit = true;
1888
- }
1889
- if (item?.subfamilyCode) {
1890
- const subFamiliesFieldKey =
1891
- "subFamilies" + capitalizeFirstLetter(item.where);
1892
- productEntry.fields = await removeFromRelationFields(
1893
- productEntry,
1894
- subFamiliesFieldKey,
1895
- item.subfamilyCode,
1896
- true
1897
- );
1898
- log(`edit ${subFamiliesFieldKey}`);
1899
- edit = true;
1900
- }
1901
- if (item?.models) {
1902
- const modelsFieldKey = "models" + capitalizeFirstLetter(item.where);
1903
- productEntry.fields = await removeFromRelationFields(
1904
- productEntry,
1905
- modelsFieldKey,
1906
- item.models,
1907
- true
1908
- );
1909
- log(`edit ${modelsFieldKey}`);
1910
- edit = true;
1911
- }
1912
- if (item?.subModels) {
1913
- const subModelsFieldKey =
1914
- "subModels" + capitalizeFirstLetter(item.where);
1915
- productEntry.fields = await removeFromRelationFields(
1916
- productEntry,
1917
- subModelsFieldKey,
1918
- item.subModels,
1919
- true
1860
+ if (audit?.catalogs) {
1861
+ for (const item of audit?.catalogs) {
1862
+ if (!item?.where) {
1863
+ log(
1864
+ `catalogs.where field not exists. Product: ${audit.product} `,
1865
+ "WARN"
1920
1866
  );
1921
- log(`edit ${subModelsFieldKey}`);
1922
- edit = true;
1923
- }
1867
+ } else {
1868
+ let edit = false;
1869
+ if (item?.catalogCode) {
1870
+ productEntry.fields = await removeFromRelationFields(
1871
+ productEntry,
1872
+ "catalogs",
1873
+ item.catalogCode,
1874
+ true
1875
+ );
1876
+ log(`edit catalogs`);
1877
+ edit = true;
1878
+ }
1879
+ if (item?.categoryCode) {
1880
+ const categoriesFieldKey =
1881
+ "categories" + capitalizeFirstLetter(item.where);
1882
+ productEntry.fields = await removeFromRelationFields(
1883
+ productEntry,
1884
+ categoriesFieldKey,
1885
+ item.categoryCode,
1886
+ true
1887
+ );
1888
+ log(`edit ${categoriesFieldKey}`);
1889
+ edit = true;
1890
+ }
1891
+ if (item?.subfamilyCode) {
1892
+ const subFamiliesFieldKey =
1893
+ "subFamilies" + capitalizeFirstLetter(item.where);
1894
+ productEntry.fields = await removeFromRelationFields(
1895
+ productEntry,
1896
+ subFamiliesFieldKey,
1897
+ item.subfamilyCode,
1898
+ true
1899
+ );
1900
+ log(`edit ${subFamiliesFieldKey}`);
1901
+ edit = true;
1902
+ }
1903
+ if (item?.models) {
1904
+ const modelsFieldKey = "models" + capitalizeFirstLetter(item.where);
1905
+ productEntry.fields = await removeFromRelationFields(
1906
+ productEntry,
1907
+ modelsFieldKey,
1908
+ item.models,
1909
+ true
1910
+ );
1911
+ log(`edit ${modelsFieldKey}`);
1912
+ edit = true;
1913
+ }
1914
+ if (item?.subModels) {
1915
+ const subModelsFieldKey =
1916
+ "subModels" + capitalizeFirstLetter(item.where);
1917
+ productEntry.fields = await removeFromRelationFields(
1918
+ productEntry,
1919
+ subModelsFieldKey,
1920
+ item.subModels,
1921
+ true
1922
+ );
1923
+ log(`edit ${subModelsFieldKey}`);
1924
+ edit = true;
1925
+ }
1924
1926
 
1925
- const objectFieldsRelations: any = {
1926
- ACCESSORIES: "accessories",
1927
- SPARE_PARTS: "spareparts",
1928
- BULBS: "bulbs",
1929
- };
1930
-
1931
- for (const objectFieldsRelationKey of Object.keys(
1932
- objectFieldsRelations
1933
- )) {
1934
- if (item.where === objectFieldsRelationKey) {
1935
- log(`Remove ${objectFieldsRelationKey} relations`);
1936
- const codes = item.codes?.split(",") ?? [];
1937
- if (codes.length) {
1938
- const fieldKey = objectFieldsRelations[objectFieldsRelationKey];
1939
- const lengthBefore =
1940
- productEntry.fields?.[fieldKey]?.[defaultEnvironmentLocaleCode]
1941
- .length;
1942
-
1943
- // Remove from the main product field
1944
- productEntry.fields = await removeFromFieldObject(
1945
- productEntry,
1946
- fieldKey,
1947
- codes
1948
- );
1949
- // Remove from the codes of field
1950
- await removeProductFromAllOfRelationFields(
1951
- productEntry.sys.id,
1952
- fieldKey,
1953
- codes
1954
- );
1927
+ const objectFieldsRelations: any = {
1928
+ ACCESSORIES: "accessories",
1929
+ SPARE_PARTS: "spareparts",
1930
+ BULBS: "bulbs",
1931
+ };
1955
1932
 
1956
- const lengthAfter =
1957
- productEntry.fields?.[fieldKey]?.[defaultEnvironmentLocaleCode]
1958
- .length;
1959
- if (lengthBefore !== lengthAfter) {
1960
- log(
1961
- `edit field ${fieldKey} lengthBefore: ${lengthBefore} lengthAfter: ${lengthAfter}`
1933
+ for (const objectFieldsRelationKey of Object.keys(
1934
+ objectFieldsRelations
1935
+ )) {
1936
+ if (item.where === objectFieldsRelationKey) {
1937
+ log(`Remove ${objectFieldsRelationKey} relations`);
1938
+ const codes = item.codes?.split(",") ?? [];
1939
+ if (codes.length) {
1940
+ const fieldKey = objectFieldsRelations[objectFieldsRelationKey];
1941
+ const lengthBefore =
1942
+ productEntry.fields?.[fieldKey]?.[
1943
+ defaultEnvironmentLocaleCode
1944
+ ].length;
1945
+
1946
+ // Remove from the main product field
1947
+ productEntry.fields = await removeFromFieldObject(
1948
+ productEntry,
1949
+ fieldKey,
1950
+ codes
1962
1951
  );
1963
- edit = true;
1964
- } else {
1965
- log(`No valid ${objectFieldsRelationKey} to remove found`);
1952
+ // Remove from the codes of field
1953
+ await removeProductFromAllOfRelationFields(
1954
+ productEntry.sys.id,
1955
+ fieldKey,
1956
+ codes
1957
+ );
1958
+
1959
+ const lengthAfter =
1960
+ productEntry.fields?.[fieldKey]?.[
1961
+ defaultEnvironmentLocaleCode
1962
+ ].length;
1963
+ if (lengthBefore !== lengthAfter) {
1964
+ log(
1965
+ `edit field ${fieldKey} lengthBefore: ${lengthBefore} lengthAfter: ${lengthAfter}`
1966
+ );
1967
+ edit = true;
1968
+ } else {
1969
+ log(`No valid ${objectFieldsRelationKey} to remove found`);
1970
+ }
1966
1971
  }
1967
1972
  }
1968
1973
  }
1969
- }
1970
1974
 
1971
- if (item.where === "DESIGNERS" && item?.codes) {
1972
- const lengthBefore =
1973
- productEntry.fields?.designer?.[defaultEnvironmentLocaleCode]
1974
- .length;
1975
- const designers = item.codes.split(",");
1976
- for (const designer of designers) {
1977
- log(
1978
- `Remove ${designer} designer to ${productEntry.sys.id} product if exists`
1979
- );
1980
- productEntry.fields = await removeFromRelationFields(
1981
- productEntry,
1982
- "designer",
1983
- designer,
1984
- false
1985
- );
1986
- }
1975
+ if (item.where === "DESIGNERS" && item?.codes) {
1976
+ const lengthBefore =
1977
+ productEntry.fields?.designer?.[defaultEnvironmentLocaleCode]
1978
+ .length;
1979
+ const designers = item.codes.split(",");
1980
+ for (const designer of designers) {
1981
+ log(
1982
+ `Remove ${designer} designer to ${productEntry.sys.id} product if exists`
1983
+ );
1984
+ productEntry.fields = await removeFromRelationFields(
1985
+ productEntry,
1986
+ "designer",
1987
+ designer,
1988
+ false
1989
+ );
1990
+ }
1987
1991
 
1988
- if (
1989
- lengthBefore !==
1990
- productEntry.fields?.designer?.[defaultEnvironmentLocaleCode].length
1991
- ) {
1992
- log(`edit designer`);
1993
- edit = true;
1994
- } else {
1995
- log(`No valid designer to remove found`);
1992
+ if (
1993
+ lengthBefore !==
1994
+ productEntry.fields?.designer?.[defaultEnvironmentLocaleCode]
1995
+ .length
1996
+ ) {
1997
+ log(`edit designer`);
1998
+ edit = true;
1999
+ } else {
2000
+ log(`No valid designer to remove found`);
2001
+ }
1996
2002
  }
1997
- }
1998
2003
 
1999
- if (edit) {
2000
- // lastPimSyncDate
2001
- productEntry.fields = await addFieldValue(
2002
- productEntry,
2003
- "lastPimSyncDate",
2004
- getLocalISOTime()
2005
- );
2006
- try {
2007
- log(`update ${productEntry.sys.id}`);
2008
- productEntry = await productEntry.update();
2009
- if (productEntry.isPublished()) {
2010
- try {
2011
- productEntry = await productEntry.publish();
2012
- } catch (err: any) {
2013
- log(`Cannot publish entry ${productEntry.sys.id}.`);
2014
- log(err);
2004
+ if (edit) {
2005
+ // lastPimSyncDate
2006
+ productEntry.fields = await addFieldValue(
2007
+ productEntry,
2008
+ "lastPimSyncDate",
2009
+ getLocalISOTime()
2010
+ );
2011
+ try {
2012
+ log(`update ${productEntry.sys.id}`);
2013
+ productEntry = await productEntry.update();
2014
+ if (productEntry.isPublished()) {
2015
+ try {
2016
+ productEntry = await productEntry.publish();
2017
+ } catch (err: any) {
2018
+ log(`Cannot publish entry ${productEntry.sys.id}.`);
2019
+ log(err);
2020
+ }
2015
2021
  }
2022
+ } catch (err: any) {
2023
+ log(`Cannot update entry ${productEntry.sys.id}.`);
2024
+ log(err);
2016
2025
  }
2017
- } catch (err: any) {
2018
- log(`Cannot update entry ${productEntry.sys.id}.`);
2019
- log(err);
2026
+ } else {
2027
+ log(`No valid editable criteria found`);
2028
+ console.log(productEntry.sys.id, "audit", audit);
2020
2029
  }
2021
- } else {
2022
- log(`No valid editable criteria found`);
2023
- console.log(productEntry.sys.id, "audit", audit);
2024
2030
  }
2025
2031
  }
2032
+ } else {
2033
+ log(
2034
+ `No catalogs field found in the audit ${audit.product} product`,
2035
+ "WARN"
2036
+ );
2026
2037
  }
2027
2038
  } else {
2028
2039
  log(`It has not yet been defined how to process the state ${audit.what}`);
@@ -3242,3 +3253,45 @@ export const populateDestinations = async (offset: number, limit: number) => {
3242
3253
 
3243
3254
  return { completed: !total || total === count + offset, offset, limit };
3244
3255
  };
3256
+
3257
+ export const purgeProductThumbCacheByProductCodes = async (
3258
+ productCodes: string[]
3259
+ ) => {
3260
+ const defEnvLocaleCode = await getEnvironmentDefaultLocaleCode();
3261
+
3262
+ const productEntries = await getAllEntriesByCodes(
3263
+ productCodes,
3264
+ "topicProduct",
3265
+ "sys,fields.thumbnailImgix"
3266
+ );
3267
+
3268
+ let count: number = 0;
3269
+ const total: number = productEntries.length;
3270
+ for (const productEntry of productEntries) {
3271
+ log(`${++count} of ${total}`);
3272
+ const thumbEntryId =
3273
+ productEntry.fields?.thumbnailImgix?.[defEnvLocaleCode]?.sys?.id;
3274
+
3275
+ if (thumbEntryId) {
3276
+ let wrapperImgix = await getEntryByID(
3277
+ thumbEntryId,
3278
+ "wrapperImgix",
3279
+ "sys,fields"
3280
+ );
3281
+ if (wrapperImgix) {
3282
+ const imgixUrl =
3283
+ wrapperImgix.fields?.imgixData?.[defEnvLocaleCode]?.url;
3284
+ if (imgixUrl) {
3285
+ log(`Cleaning cache for ${imgixUrl}`);
3286
+ await purgeImageCacheByUrl(imgixUrl);
3287
+ } else {
3288
+ log(`No imgixUrl found in ${thumbEntryId} id`, "WARN");
3289
+ }
3290
+ } else {
3291
+ log(`No wrapperImgix found with ${thumbEntryId} id`, "WARN");
3292
+ }
3293
+ } else {
3294
+ log(`No thumbEntryId found in ${productEntry.sys.id}`, "WARN");
3295
+ }
3296
+ }
3297
+ };
package/types/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export { importModels } from "./pim/methods/models";
8
8
  export { importSubModels } from "./pim/methods/submodels";
9
9
  export { importSubFamilies } from "./pim/methods/subfamilies";
10
10
  export { importFamilies } from "./pim/methods/families";
11
- export { importProductByCode, setProductsRelationships, setProductRelationships, getAllProductEntriesByCatalog, audit, generateTechSpecPdf, setProductsAutodescription, getProductAutodescription, setProductAutodescriptionByTopicId, removeProductFromColorVariantsByProductLine, removeAllProductModelProductRelations, getProductPageIdByCode, reimportAuditProducts, populateDestinations, } from "./pim/methods/products";
11
+ export { importProductByCode, setProductsRelationships, setProductRelationships, getAllProductEntriesByCatalog, audit, generateTechSpecPdf, setProductsAutodescription, getProductAutodescription, setProductAutodescriptionByTopicId, removeProductFromColorVariantsByProductLine, removeAllProductModelProductRelations, getProductPageIdByCode, reimportAuditProducts, populateDestinations, purgeProductThumbCacheByProductCodes, } from "./pim/methods/products";
12
12
  export { publishAllProductDrafts } from "./pim/methods/bulkPublish";
13
13
  export { getIndex } from "./algolia/config";
14
14
  export { reindexFamilies, reindexFamily, removeFamilyObject, } from "./algolia/families";
@@ -13,3 +13,4 @@ export declare const getDefaultWrapperImgixAttributesByPimUrl: (pimUrl: string)
13
13
  export declare const getWrapperImgixFields: (wrapperImgixId: string) => Promise<{}>;
14
14
  export declare const getWrapperImgixAttributesByAsset: (imgixAttributes: ImgixAttributes) => Promise<ImgixData>;
15
15
  export declare const uploadAssetByUrl: (url: string) => Promise<ImgixData | undefined>;
16
+ export declare const purgeImageCacheByUrl: (url: string) => Promise<void>;
@@ -72,3 +72,4 @@ export declare const populateDestinations: (offset: number, limit: number) => Pr
72
72
  offset: number;
73
73
  limit: number;
74
74
  }>;
75
+ export declare const purgeProductThumbCacheByProductCodes: (productCodes: string[]) => Promise<void>;