pim-import 4.11.0 → 4.12.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.
Files changed (37) hide show
  1. package/dist/index.js +3 -2
  2. package/dist/index.js.map +1 -1
  3. package/dist/libs/contentful.js +1 -1
  4. package/dist/libs/contentful.js.map +1 -1
  5. package/dist/libs/logs.js +3 -0
  6. package/dist/libs/logs.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 +2 -2
  18. package/src/index.ts +1 -0
  19. package/src/libs/contentful.ts +1 -1
  20. package/src/libs/logs.ts +4 -0
  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/index.d.ts +1 -1
  32. package/types/pim/methods/products.d.ts +5 -0
  33. package/types/resources/Audit.d.ts +1 -0
  34. package/types/resources/CollectionModels.d.ts +1 -0
  35. package/types/resources/CollectionSubFamilies.d.ts +1 -0
  36. package/types/resources/CollectionSubModels.d.ts +1 -0
  37. package/types/resources/FamilyDetails.d.ts +1 -0
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.reimportAuditProducts = exports.removeAllProductModelProductRelations = exports.removeProductFromColorVariantsByProductLine = exports.getProductAutodescription = exports.setProductAutodescriptionByTopicId = exports.setProductsAutodescription = exports.generateTechSpecPdf = exports.importProductByCode = exports.audit = exports.getAllProductEntriesByCatalog = exports.setProductsRelationships = exports.setProductRelationships = exports.importProduct = exports.getProductPageIdByCode = exports.excludeCertificationFieldsKeys = exports.certificationFields = void 0;
6
+ exports.populateDestinations = exports.reimportAuditProducts = exports.removeAllProductModelProductRelations = exports.removeProductFromColorVariantsByProductLine = exports.getProductAutodescription = exports.setProductAutodescriptionByTopicId = exports.setProductsAutodescription = exports.generateTechSpecPdf = exports.importProductByCode = exports.audit = exports.getAllProductEntriesByCatalog = exports.setProductsRelationships = exports.setProductRelationships = exports.importProduct = exports.getProductPageIdByCode = exports.excludeCertificationFieldsKeys = exports.certificationFields = void 0;
7
7
  const logs_1 = require("../../libs/logs");
8
8
  const contentful_1 = require("../../libs/contentful");
9
9
  const utils_1 = require("../../utils");
@@ -622,6 +622,13 @@ const getProductData = async (productEntry, productDetails) => {
622
622
  }
623
623
  }
624
624
  data.fields.autoDescription = await getTopicProductAutodescription(data.fields);
625
+ if (productDetails?.destinations?.length) {
626
+ logs_1.log(`set destinations`);
627
+ data.fields = await contentful_1.addFieldValue(data, "destinations", productDetails.destinations.map(item => item.code));
628
+ }
629
+ else {
630
+ logs_1.log(`destinations not found`, "WARN");
631
+ }
625
632
  const productEntryId = getTopicProductIdByCode(productDetails.code);
626
633
  const { path, fileName } = getProductPayloadS3Details(productEntryId);
627
634
  logs_1.log(`Uploading "${fileName}" to S3 path "${path}"`);
@@ -925,6 +932,202 @@ const getAllProductEntriesByCatalog = async (catalog, limit = 100, select = "")
925
932
  return allItems;
926
933
  };
927
934
  exports.getAllProductEntriesByCatalog = getAllProductEntriesByCatalog;
935
+ const productAudit = async (audit, productEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit, productPageEntries) => {
936
+ logs_1.log(`Search product entry with id ${audit.product}...`);
937
+ let productEntry = productEntries.find((currentEntry) => currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
938
+ audit.product);
939
+ if (!productEntry) {
940
+ let logMessage = "";
941
+ if (catalog) {
942
+ logMessage = `The ${audit.product} product was not found in the CMS with catalog ${catalog}`;
943
+ }
944
+ else {
945
+ logMessage = `The ${audit.product} product was not found in the CMS`;
946
+ }
947
+ logs_1.log(logMessage, "WARN");
948
+ if (logs_1.serverUtils) {
949
+ logs_1.serverUtils.log(logMessage);
950
+ const progress = Math.floor((++current / allAudit.length) * 100);
951
+ logs_1.serverUtils.updateProgress(progress);
952
+ }
953
+ return { current, continue: true };
954
+ }
955
+ logs_1.log(`Founded product with id ${productEntry.sys.id}`);
956
+ const pageEntryFromId = exports.getProductPageIdByCode(audit.product);
957
+ logs_1.log(`Search product page entry with id ${pageEntryFromId}...`);
958
+ let pageEntryFrom = productPageEntries.find((currentEntry) => currentEntry.sys.id === pageEntryFromId);
959
+ if (pageEntryFrom) {
960
+ logs_1.log(`Founded product page with id ${pageEntryFrom.sys.id}`);
961
+ }
962
+ else {
963
+ logs_1.log(`Product page with id ${pageEntryFromId} not found`, "WARN");
964
+ }
965
+ logs_1.log(`Get product catalogs...`);
966
+ const productCatalogs = productEntry?.fields?.catalogs?.[defaultEnvironmentLocaleCode].map((entryCatalog) => {
967
+ return entryCatalog.sys.id;
968
+ });
969
+ if (catalog && !productCatalogs.includes(catalog)) {
970
+ logs_1.log(`Product ${audit.product} does not belong to the ${catalog} catalog`);
971
+ }
972
+ else if (["PRODUCT_UNPUBLISH", "PRODUCT_WEBOFF"].includes(audit.what)) {
973
+ logs_1.log(`Set the product status as archive...`);
974
+ productEntry = await contentful_1.archiveEntry(productEntry, true);
975
+ if (!pageEntryFrom) {
976
+ logs_1.log(`${pageEntryFromId} page from not found`);
977
+ }
978
+ else if (pageEntryFrom.isArchived()) {
979
+ logs_1.log(`Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryFrom.sys.id} is archived.`, "WARN");
980
+ }
981
+ else if (pageEntryFrom?.fields) {
982
+ if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
983
+ logs_1.log(`Creating redirect from ${audit.product} to ${audit.upgradeProduct}`);
984
+ const pageEntryToId = exports.getProductPageIdByCode(audit.upgradeProduct);
985
+ const pageEntryTo = await contentful_1.getEntryByID(pageEntryToId, "page", "sys.id");
986
+ if (pageEntryTo) {
987
+ pageEntryFrom.fields = await contentful_1.addToRelationFields(pageEntryFrom, "redirectTo", pageEntryTo.sys.id);
988
+ pageEntryFrom = await pageEntryFrom.update();
989
+ }
990
+ else {
991
+ logs_1.log(`Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryToId} not found.`, "WARN");
992
+ }
993
+ }
994
+ pageEntryFrom = await contentful_1.archiveEntry(pageEntryFrom);
995
+ }
996
+ }
997
+ else if (audit.what === "REMOVE_PRODUCT_RELATIONS" &&
998
+ !productEntry.isArchived()) {
999
+ for (const item of audit?.catalogs) {
1000
+ if (!item?.where) {
1001
+ logs_1.log(`catalogs.where field not exists. Product: ${audit.product} `, "WARN");
1002
+ }
1003
+ else {
1004
+ if (item?.catalogCode) {
1005
+ productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, "catalogs", item.catalogCode, true);
1006
+ }
1007
+ if (item?.categoryCode) {
1008
+ const categoriesFieldKey = "categories" + utils_1.capitalizeFirstLetter(item.where);
1009
+ productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, categoriesFieldKey, item.categoryCode, true);
1010
+ }
1011
+ if (item?.subfamilyCode) {
1012
+ const subFamiliesFieldKey = "subFamilies" + utils_1.capitalizeFirstLetter(item.where);
1013
+ productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, subFamiliesFieldKey, item.subfamilyCode, true);
1014
+ }
1015
+ if (item?.models) {
1016
+ const modelsFieldKey = "models" + utils_1.capitalizeFirstLetter(item.where);
1017
+ productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, modelsFieldKey, item.models, true);
1018
+ }
1019
+ if (item?.subModels) {
1020
+ const subModelsFieldKey = "subModels" + utils_1.capitalizeFirstLetter(item.where);
1021
+ productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, subModelsFieldKey, item.subModels, true);
1022
+ }
1023
+ productEntry.fields = await contentful_1.addFieldValue(productEntry, "lastPimSyncDate", utils_1.getLocalISOTime());
1024
+ productEntry = await productEntry.update();
1025
+ }
1026
+ }
1027
+ }
1028
+ else {
1029
+ logs_1.log(`It has not yet been defined how to process the state ${audit.what}`);
1030
+ }
1031
+ return { current, continue: false };
1032
+ };
1033
+ const familyAudit = async (audit, familyEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit) => {
1034
+ logs_1.log(`Search family entry with id ${audit.product}...`);
1035
+ let familyEntry = familyEntries.find((currentEntry) => currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1036
+ audit.product);
1037
+ if (!familyEntry) {
1038
+ let logMessage = "";
1039
+ if (catalog) {
1040
+ logMessage = `The ${audit.product} family was not found in the CMS with catalog ${catalog}`;
1041
+ }
1042
+ else {
1043
+ logMessage = `The ${audit.product} family was not found in the CMS`;
1044
+ }
1045
+ logs_1.log(logMessage, "WARN");
1046
+ if (logs_1.serverUtils) {
1047
+ logs_1.serverUtils.log(logMessage);
1048
+ const progress = Math.floor((++current / allAudit.length) * 100);
1049
+ logs_1.serverUtils.updateProgress(progress);
1050
+ }
1051
+ return { current, continue: true };
1052
+ }
1053
+ logs_1.log(`Founded family with id ${familyEntry.sys.id}`);
1054
+ logs_1.log(`Get family catalogs...`);
1055
+ const familyCatalogs = familyEntry?.fields?.catalogs?.[defaultEnvironmentLocaleCode].map((entryCatalog) => {
1056
+ return entryCatalog.sys.id;
1057
+ });
1058
+ if (catalog && !familyCatalogs.includes(catalog)) {
1059
+ logs_1.log(`Family ${audit.product} does not belong to the ${catalog} catalog`);
1060
+ }
1061
+ else if (audit.what === "REMOVE_FAMILY_RELATIONS") {
1062
+ for (const item of audit?.catalogs) {
1063
+ if (!item?.where) {
1064
+ logs_1.log(`catalogs.where field not exists. Family: ${audit.product} `, "WARN");
1065
+ }
1066
+ else {
1067
+ if (item.where === "DESIGNERS" && item?.codes) {
1068
+ const designers = item.codes.split(',');
1069
+ for (const designer of designers) {
1070
+ logs_1.log(`Remove ${designer} designer to ${familyEntry.sys.id} family if exists`);
1071
+ familyEntry.fields = await contentful_1.removeFromRelationFields(familyEntry, "designer", designer, false);
1072
+ }
1073
+ familyEntry = await familyEntry.update();
1074
+ }
1075
+ }
1076
+ }
1077
+ }
1078
+ else {
1079
+ logs_1.log(`It has not yet been defined how to process the state ${audit.what}`);
1080
+ }
1081
+ return { current, continue: false };
1082
+ };
1083
+ const subFamilyAudit = async (audit, subfamilyEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit) => {
1084
+ logs_1.log(`Search subFamily entry with id ${audit.product}...`);
1085
+ let subFamilyEntry = subfamilyEntries.find((currentEntry) => currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1086
+ audit.product);
1087
+ if (!subFamilyEntry) {
1088
+ let logMessage = "";
1089
+ if (catalog) {
1090
+ logMessage = `The ${audit.product} subFamily was not found in the CMS with catalog ${catalog}`;
1091
+ }
1092
+ else {
1093
+ logMessage = `The ${audit.product} subFamily was not found in the CMS`;
1094
+ }
1095
+ logs_1.log(logMessage, "WARN");
1096
+ if (logs_1.serverUtils) {
1097
+ logs_1.serverUtils.log(logMessage);
1098
+ const progress = Math.floor((++current / allAudit.length) * 100);
1099
+ logs_1.serverUtils.updateProgress(progress);
1100
+ }
1101
+ return { current, continue: true };
1102
+ }
1103
+ logs_1.log(`Founded subFamily with id ${subFamilyEntry.sys.id}`);
1104
+ logs_1.log(`Get subFamily catalogs...`);
1105
+ const subFamilyCatalog = subFamilyEntry?.fields?.catalog?.[defaultEnvironmentLocaleCode].sys.id;
1106
+ if (catalog && subFamilyCatalog !== catalog) {
1107
+ logs_1.log(`SubFamily ${audit.product} does not belong to the ${catalog} catalog`);
1108
+ }
1109
+ else if (audit.what === "REMOVE_SUBFAMILY_RELATIONS") {
1110
+ for (const item of audit?.catalogs) {
1111
+ if (!item?.where) {
1112
+ logs_1.log(`catalogs.where field not exists. SubFamily: ${audit.product} `, "WARN");
1113
+ }
1114
+ else {
1115
+ if (item.where === "DESIGNERS" && item?.codes) {
1116
+ const designers = item.codes.split(',');
1117
+ for (const designer of designers) {
1118
+ logs_1.log(`Remove ${designer} designer to ${subFamilyEntry.sys.id} subFamily if exists`);
1119
+ subFamilyEntry.fields = await contentful_1.removeFromRelationFields(subFamilyEntry, "designer", designer, false);
1120
+ }
1121
+ subFamilyEntry = await subFamilyEntry.update();
1122
+ }
1123
+ }
1124
+ }
1125
+ }
1126
+ else {
1127
+ logs_1.log(`It has not yet been defined how to process the state ${audit.what}`);
1128
+ }
1129
+ return { current, continue: false };
1130
+ };
928
1131
  const audit = async (lastModified, catalog, offset = 0, limit = 150, s3FilePath = "") => {
929
1132
  offset = Number(offset);
930
1133
  limit = Number(limit);
@@ -967,16 +1170,29 @@ const audit = async (lastModified, catalog, offset = 0, limit = 150, s3FilePath
967
1170
  logs_1.log(`Founded ${allAudit.length} items`);
968
1171
  if (allAudit) {
969
1172
  const defaultEnvironmentLocaleCode = await contentful_1.getEnvironmentDefaultLocaleCode();
970
- const productCodes = allAudit.map((audit) => audit.product);
1173
+ const productAuditWhat = ["PRODUCT_UNPUBLISH", "PRODUCT_WEBOFF", "REMOVE_PRODUCT_RELATIONS"];
1174
+ const productCodes = allAudit.filter(item => productAuditWhat.includes(item.what)).map((audit) => audit.product);
1175
+ const familyCodes = allAudit.filter(item => item.what === "REMOVE_FAMILY_RELATIONS").map((audit) => audit.product);
1176
+ const subFamilyCodes = allAudit.filter(item => item.what === "REMOVE_SUBFAMILY_RELATIONS").map((audit) => audit.product);
971
1177
  const otherFilters = [];
972
1178
  if (catalog) {
973
1179
  otherFilters.push({ key: "fields.catalogs.sys.id[in]", value: catalog });
974
1180
  }
975
1181
  logs_1.log(`Get ${productCodes.length} product entry from Contentful`);
976
- const entries = await contentful_1.getAllEntriesByCodes(productCodes, "topicProduct", "sys,fields", "fields.code", otherFilters);
977
- logs_1.log(`Founded ${entries.length} topicProduct`);
978
- if (!entries.length) {
979
- logs_1.log(`No products found between offset: ${offset} and limit: ${limit}. Total: ${total}`);
1182
+ const productEntries = await contentful_1.getAllEntriesByCodes(productCodes, "topicProduct", "sys,fields", "fields.code", otherFilters);
1183
+ logs_1.log(`Founded ${productEntries.length} topicProduct`);
1184
+ let familyEntries = [];
1185
+ if (familyCodes.length) {
1186
+ familyEntries = await contentful_1.getAllEntriesByCodes(familyCodes, "topicFamily", "sys,fields", "fields.code", otherFilters);
1187
+ }
1188
+ logs_1.log(`Founded ${familyEntries.length} topicFamily`);
1189
+ let subFamilyEntries = [];
1190
+ if (subFamilyCodes.length) {
1191
+ subFamilyEntries = await contentful_1.getAllEntriesByCodes(subFamilyCodes, "topicSubFamily", "sys,fields", "fields.code", otherFilters);
1192
+ }
1193
+ logs_1.log(`Founded ${subFamilyEntries.length} topicSubFamily`);
1194
+ if (!productEntries.length && !familyEntries.length && !subFamilyEntries.length) {
1195
+ logs_1.log(`No items found between offset: ${offset} and limit: ${limit}. Total: ${total}`);
980
1196
  const nextOffset = offset + limit;
981
1197
  const completed = limit === -1 || offset >= total;
982
1198
  if (completed) {
@@ -998,110 +1214,36 @@ const audit = async (lastModified, catalog, offset = 0, limit = 150, s3FilePath
998
1214
  processedEntries: 0,
999
1215
  };
1000
1216
  }
1001
- const pageCodes = entries.map((entry) => exports.getProductPageIdByCode(entry.fields.code[defaultEnvironmentLocaleCode]));
1217
+ const pageCodes = productEntries.map((entry) => exports.getProductPageIdByCode(entry.fields.code[defaultEnvironmentLocaleCode]));
1002
1218
  logs_1.log(`Get ${pageCodes.length} product page entry from Contentful`);
1003
- const entriesPage = await contentful_1.getAllEntriesByCodes(pageCodes, "page", "sys,fields", "sys.id");
1004
- logs_1.log(`Founded ${entriesPage.length} topicProduct pages`);
1219
+ const productPageEntries = await contentful_1.getAllEntriesByCodes(pageCodes, "page", "sys,fields", "sys.id");
1220
+ logs_1.log(`Founded ${productPageEntries.length} topicProduct pages`);
1005
1221
  let count = offset;
1006
1222
  let current = 0;
1007
1223
  for (const audit of allAudit) {
1008
1224
  logs_1.log(`${++count} of ${total}`);
1009
- logs_1.log(`I process the product ${audit.product} with status ${audit.what}`);
1010
- logs_1.log(`Search product entry with id ${audit.product}...`);
1011
- let productEntry = entries.find((currentEntry) => currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
1012
- audit.product);
1013
- if (!productEntry) {
1014
- let logMessage = "";
1015
- if (catalog) {
1016
- logMessage = `The ${audit.product} product was not found in the CMS with catalog ${catalog}`;
1017
- }
1018
- else {
1019
- logMessage = `The ${audit.product} product was not found in the CMS`;
1020
- }
1021
- logs_1.log(logMessage, "WARN");
1022
- if (logs_1.serverUtils) {
1023
- logs_1.serverUtils.log(logMessage);
1024
- const progress = Math.floor((++current / allAudit.length) * 100);
1025
- logs_1.serverUtils.updateProgress(progress);
1225
+ logs_1.log(`I process the item ${audit.product} with status ${audit.what}`);
1226
+ if (productAuditWhat.includes(audit.what)) {
1227
+ const result = await productAudit(audit, productEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit, productPageEntries);
1228
+ current = result.current;
1229
+ if (result.continue) {
1230
+ continue;
1026
1231
  }
1027
- continue;
1028
- }
1029
- logs_1.log(`Founded product with id ${productEntry.sys.id}`);
1030
- const pageEntryFromId = exports.getProductPageIdByCode(audit.product);
1031
- logs_1.log(`Search product page entry with id ${pageEntryFromId}...`);
1032
- let pageEntryFrom = entriesPage.find((currentEntry) => currentEntry.sys.id === pageEntryFromId);
1033
- if (pageEntryFrom) {
1034
- logs_1.log(`Founded product page with id ${pageEntryFrom.sys.id}`);
1035
- }
1036
- else {
1037
- logs_1.log(`Product page with id ${pageEntryFromId} not found`, "WARN");
1038
- }
1039
- logs_1.log(`Get product catalogs...`);
1040
- const productCatalogs = productEntry?.fields?.catalogs?.[defaultEnvironmentLocaleCode].map((entryCatalog) => {
1041
- return entryCatalog.sys.id;
1042
- });
1043
- if (catalog && !productCatalogs.includes(catalog)) {
1044
- logs_1.log(`Product ${audit.product} does not belong to the ${catalog} catalog`);
1045
1232
  }
1046
- else if (["PRODUCT_UNPUBLISH", "PRODUCT_WEBOFF"].includes(audit.what)) {
1047
- logs_1.log(`Set the product status as archive...`);
1048
- productEntry = await contentful_1.archiveEntry(productEntry, true);
1049
- if (!pageEntryFrom) {
1050
- logs_1.log(`${pageEntryFromId} page from not found`);
1051
- }
1052
- else if (pageEntryFrom.isArchived()) {
1053
- logs_1.log(`Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryFrom.sys.id} is archived.`, "WARN");
1054
- }
1055
- else if (pageEntryFrom?.fields) {
1056
- if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
1057
- logs_1.log(`Creating redirect from ${audit.product} to ${audit.upgradeProduct}`);
1058
- const pageEntryToId = exports.getProductPageIdByCode(audit.upgradeProduct);
1059
- const pageEntryTo = await contentful_1.getEntryByID(pageEntryToId, "page", "sys.id");
1060
- if (pageEntryTo) {
1061
- pageEntryFrom.fields = await contentful_1.addToRelationFields(pageEntryFrom, "redirectTo", pageEntryTo.sys.id);
1062
- pageEntryFrom = await pageEntryFrom.update();
1063
- }
1064
- else {
1065
- logs_1.log(`Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryToId} not found.`, "WARN");
1066
- }
1067
- }
1068
- pageEntryFrom = await contentful_1.archiveEntry(pageEntryFrom);
1233
+ else if (audit.what === "REMOVE_FAMILY_RELATIONS") {
1234
+ const result = await familyAudit(audit, familyEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit);
1235
+ current = result.current;
1236
+ if (result.continue) {
1237
+ continue;
1069
1238
  }
1070
1239
  }
1071
- else if (audit.what === "REMOVE_PRODUCT_RELATIONS" &&
1072
- !productEntry.isArchived()) {
1073
- for (const item of audit?.catalogs) {
1074
- if (!item?.where) {
1075
- logs_1.log(`catalogs.where field not exists. Product: ${audit.product} `, "WARN");
1076
- }
1077
- else {
1078
- if (item?.catalogCode) {
1079
- productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, "catalogs", item.catalogCode, true);
1080
- }
1081
- if (item?.categoryCode) {
1082
- const categoriesFieldKey = "categories" + utils_1.capitalizeFirstLetter(item.where);
1083
- productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, categoriesFieldKey, item.categoryCode, true);
1084
- }
1085
- if (item?.subfamilyCode) {
1086
- const subFamiliesFieldKey = "subFamilies" + utils_1.capitalizeFirstLetter(item.where);
1087
- productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, subFamiliesFieldKey, item.subfamilyCode, true);
1088
- }
1089
- if (item?.models) {
1090
- const modelsFieldKey = "models" + utils_1.capitalizeFirstLetter(item.where);
1091
- productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, modelsFieldKey, item.models, true);
1092
- }
1093
- if (item?.subModels) {
1094
- const subModelsFieldKey = "subModels" + utils_1.capitalizeFirstLetter(item.where);
1095
- productEntry.fields = await contentful_1.removeFromRelationFields(productEntry, subModelsFieldKey, item.subModels, true);
1096
- }
1097
- productEntry.fields = await contentful_1.addFieldValue(productEntry, "lastPimSyncDate", utils_1.getLocalISOTime());
1098
- productEntry = await productEntry.update();
1099
- }
1240
+ else if (audit.what === "REMOVE_SUBFAMILY_RELATIONS") {
1241
+ const result = await subFamilyAudit(audit, subFamilyEntries, catalog, defaultEnvironmentLocaleCode, current, allAudit);
1242
+ current = result.current;
1243
+ if (result.continue) {
1244
+ continue;
1100
1245
  }
1101
1246
  }
1102
- else {
1103
- logs_1.log(`It has not yet been defined how to process the state ${audit.what}`);
1104
- }
1105
1247
  if (logs_1.serverUtils) {
1106
1248
  logs_1.serverUtils.log(audit.product);
1107
1249
  const progress = Math.floor((++current / allAudit.length) * 100);
@@ -1118,7 +1260,7 @@ const audit = async (lastModified, catalog, offset = 0, limit = 150, s3FilePath
1118
1260
  completed: limit === -1 || count >= total,
1119
1261
  s3FilePath,
1120
1262
  total,
1121
- processedEntries: entries.length,
1263
+ processedEntries: productEntries.length + familyEntries.length + subFamilyEntries.length,
1122
1264
  };
1123
1265
  }
1124
1266
  else {
@@ -1649,4 +1791,41 @@ const reimportAuditProducts = async (lastModified, catalog, offset = 0, limit =
1649
1791
  };
1650
1792
  };
1651
1793
  exports.reimportAuditProducts = reimportAuditProducts;
1794
+ const populateDestinations = async (offset, limit) => {
1795
+ const env = await contentful_1.getEnvironment();
1796
+ const defEnvLocaleCode = await contentful_1.getEnvironmentDefaultLocaleCode();
1797
+ const opts = {
1798
+ content_type: 'topicProduct',
1799
+ skip: offset,
1800
+ limit,
1801
+ locale: defEnvLocaleCode,
1802
+ "sys.archivedAt[exists]": false,
1803
+ };
1804
+ const { items, total } = await env.getEntries(opts);
1805
+ let count = offset;
1806
+ for (let productEntry of items) {
1807
+ logs_1.log(`${++count} of ${total}`);
1808
+ logs_1.log(`Product: ${productEntry.sys.id}`);
1809
+ const destinations = productEntry.fields.productFields[defEnvLocaleCode]?.destinations?.map((destination) => destination.code) || [];
1810
+ if (destinations.length) {
1811
+ logs_1.log(`Set destinations: ${destinations.join(', ')}`);
1812
+ productEntry.fields = await contentful_1.addFieldValue(productEntry, "destinations", destinations);
1813
+ productEntry = await productEntry.update();
1814
+ }
1815
+ else {
1816
+ logs_1.log(`No destinations found.`, "WARN");
1817
+ }
1818
+ if (productEntry.isPublished()) {
1819
+ try {
1820
+ productEntry = await productEntry.publish();
1821
+ }
1822
+ catch (err) {
1823
+ logs_1.log(`Cannot publish entry.`);
1824
+ logs_1.log(err);
1825
+ }
1826
+ }
1827
+ }
1828
+ return { completed: total === count + offset, offset, limit };
1829
+ };
1830
+ exports.populateDestinations = populateDestinations;
1652
1831
  //# sourceMappingURL=products.js.map