pim-import 2.60.0 → 2.62.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 (40) hide show
  1. package/dist/algolia/config.js +19 -1
  2. package/dist/algolia/config.js.map +1 -1
  3. package/dist/algolia/downloads.js +6 -3
  4. package/dist/algolia/downloads.js.map +1 -1
  5. package/dist/algolia/families.js +8 -3
  6. package/dist/algolia/families.js.map +1 -1
  7. package/dist/algolia/models.js +8 -6
  8. package/dist/algolia/models.js.map +1 -1
  9. package/dist/algolia/news.js +142 -0
  10. package/dist/algolia/news.js.map +1 -0
  11. package/dist/algolia/pressRelease.js +6 -4
  12. package/dist/algolia/pressRelease.js.map +1 -1
  13. package/dist/algolia/pressReview.js +6 -4
  14. package/dist/algolia/pressReview.js.map +1 -1
  15. package/dist/algolia/products.js +8 -3
  16. package/dist/algolia/products.js.map +1 -1
  17. package/dist/algolia/projects.js +6 -4
  18. package/dist/algolia/projects.js.map +1 -1
  19. package/dist/algolia/stories.js +6 -4
  20. package/dist/algolia/stories.js.map +1 -1
  21. package/dist/algolia/subFamilies.js +8 -3
  22. package/dist/algolia/subFamilies.js.map +1 -1
  23. package/dist/algolia/subModels.js +3 -3
  24. package/dist/algolia/subModels.js.map +1 -1
  25. package/dist/index.js +5 -1
  26. package/dist/index.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/algolia/config.ts +21 -2
  29. package/src/algolia/downloads.ts +9 -3
  30. package/src/algolia/families.ts +14 -3
  31. package/src/algolia/models.ts +12 -7
  32. package/src/algolia/news.ts +233 -0
  33. package/src/algolia/pressRelease.ts +10 -4
  34. package/src/algolia/pressReview.ts +10 -4
  35. package/src/algolia/products.ts +14 -3
  36. package/src/algolia/projects.ts +10 -4
  37. package/src/algolia/stories.ts +10 -4
  38. package/src/algolia/subFamilies.ts +14 -3
  39. package/src/algolia/subModels.ts +4 -4
  40. package/src/index.ts +1 -0
@@ -0,0 +1,233 @@
1
+ import { log } from "../libs/logs";
2
+ import {
3
+ getClient,
4
+ getEnvironmentDefaultLocaleCode,
5
+ getEntryByID,
6
+ getTopicPage,
7
+ } from "../libs/contentful-cda";
8
+ import { getIndex, AvailableIndicesKey, removeIndexObject } from "./config";
9
+ import { getLocalISOTime, secondBetweenTwoDate } from "../utils";
10
+ import type { Entry } from "contentful-management/dist/typings/entities/entry";
11
+ import {
12
+ CfLocalizedEntryField,
13
+ AlgoliaPaginateRecords,
14
+ WrapperImageFields,
15
+ } from "../types";
16
+ import { getWrapperImgixFields } from "../libs/imgix";
17
+
18
+ const indexKey: AvailableIndicesKey = "news";
19
+
20
+ export type AlgoliaPostRecord = {
21
+ objectID: string;
22
+ name?: CfLocalizedEntryField;
23
+ slugs?: {};
24
+ previewExcerpt?: string;
25
+ thumbnailImageImgix?: WrapperImageFields | {};
26
+ fullScreenImageImgix?: WrapperImageFields | {};
27
+ visualizationDate?: string;
28
+ highlighted?: boolean;
29
+ lastSyncDate?: string;
30
+ };
31
+
32
+ const isObjectToDelete = (
33
+ object: any,
34
+ defaultEnvironmentLocaleCode: string
35
+ ) => {
36
+ return !object?.slugs?.[defaultEnvironmentLocaleCode];
37
+ };
38
+
39
+ const getObject = async (topicNews: Entry): Promise<AlgoliaPostRecord> => {
40
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
41
+ log(`Sync the ${topicNews.sys.id} topicNews...`);
42
+ let topicNewsWithFields: Entry = topicNews;
43
+
44
+ if (!topicNewsWithFields?.fields) {
45
+ log(`Get the ${topicNews.sys.id} topicNews details...`);
46
+ topicNewsWithFields = await getEntryByID(topicNews.sys.id, "topicNews");
47
+ }
48
+
49
+ if (!topicNewsWithFields) {
50
+ log(`The topicNews ${topicNews.sys.id} not found`, "WARN");
51
+
52
+ const recordFail: AlgoliaPostRecord = {
53
+ objectID: topicNews?.sys?.id,
54
+ };
55
+
56
+ return recordFail; // return objectID to delete the record if it exists
57
+ }
58
+
59
+ log(`Get page details...`);
60
+ const page = await getTopicPage(topicNews.sys.id, "fields.slug");
61
+ const slugs = page?.fields.slug || {};
62
+
63
+ log(`Get thumbnail imgix details...`);
64
+ let thumbnailImageImgix = {};
65
+ const thumbnailImgixWrapperImgixID =
66
+ topicNewsWithFields?.fields?.thumbnailImageImgix?.[
67
+ defaultEnvironmentLocaleCode
68
+ ]?.sys.id;
69
+ if (thumbnailImgixWrapperImgixID) {
70
+ thumbnailImageImgix = await getWrapperImgixFields(
71
+ thumbnailImgixWrapperImgixID
72
+ );
73
+ } else {
74
+ log(`No thumbnail imgix found`, "WARN");
75
+ }
76
+
77
+ log(`Get fullScreenImageImgix imgix details...`);
78
+ let fullScreenImageImgix = {};
79
+ const fullScreenImageImgixWrapperImgixID =
80
+ topicNewsWithFields?.fields?.fullScreenImageImgix?.[
81
+ defaultEnvironmentLocaleCode
82
+ ]?.sys.id;
83
+ if (fullScreenImageImgixWrapperImgixID) {
84
+ fullScreenImageImgix = await getWrapperImgixFields(
85
+ fullScreenImageImgixWrapperImgixID
86
+ );
87
+ } else {
88
+ log(`No fullScreenImageImgix found`, "WARN");
89
+ }
90
+
91
+ // Single record
92
+ const record: AlgoliaPostRecord = {
93
+ objectID: topicNewsWithFields.sys.id,
94
+ name:
95
+ topicNewsWithFields?.fields?.title?.[defaultEnvironmentLocaleCode] || "",
96
+ slugs,
97
+ thumbnailImageImgix,
98
+ fullScreenImageImgix,
99
+ highlighted:
100
+ !!topicNewsWithFields?.fields?.highlighted?.[
101
+ defaultEnvironmentLocaleCode
102
+ ],
103
+ visualizationDate:
104
+ topicNewsWithFields?.fields?.visualizationDate?.[
105
+ defaultEnvironmentLocaleCode
106
+ ] || "",
107
+ lastSyncDate: getLocalISOTime(),
108
+ };
109
+
110
+ return record;
111
+ };
112
+
113
+ export const reindexPost = async (topicNewsId: string) => {
114
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
115
+ const timeStart = new Date();
116
+
117
+ log(`reindexPost - entryId: ${topicNewsId} `);
118
+
119
+ const topicNews = await getEntryByID(topicNewsId, "topicNews");
120
+
121
+ if (!topicNews) {
122
+ log(`No topicNews found with id ${topicNewsId}`, "WARN");
123
+ return false;
124
+ }
125
+
126
+ const object = await getObject(topicNews);
127
+
128
+ // Save record to Algolia
129
+ const index = getIndex(indexKey);
130
+ let record = null;
131
+ if (!isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
132
+ log(`Save object`);
133
+ record = await index.saveObject(object);
134
+ } else {
135
+ log(`Delete object`);
136
+ record = await index.deleteObject(object.objectID);
137
+ }
138
+ const timeEnd = new Date();
139
+ const seconds = secondBetweenTwoDate(timeStart, timeEnd);
140
+ log(`Execution time: ${seconds} seconds`);
141
+
142
+ return { ...record, ...object };
143
+ };
144
+
145
+ /**
146
+ * Get Objects
147
+ *
148
+ * @param offset
149
+ * @param limit
150
+ * @param filterKey
151
+ * @param filterValue
152
+ * @returns
153
+ */
154
+ const getObjects = async (
155
+ offset: number,
156
+ limit: number,
157
+ filterKey?: string,
158
+ filterValue?: string
159
+ ): Promise<AlgoliaPaginateRecords> => {
160
+ const client = await getClient();
161
+
162
+ const opts: any = {
163
+ content_type: "topicNews",
164
+ limit,
165
+ skip: offset,
166
+ locale: "*",
167
+ // select: "",
168
+ };
169
+ if (filterKey && filterValue) {
170
+ opts[filterKey] = filterValue;
171
+ }
172
+ const { items, total } = await client.getEntries(opts);
173
+
174
+ const objects: AlgoliaPostRecord[] = [];
175
+ let count: number = Number(offset);
176
+ for (const topicNews of items) {
177
+ log(`${++count} of ${total}`, "INFO");
178
+ const timeStart = new Date();
179
+ const record = await getObject(topicNews);
180
+ const timeEnd = new Date();
181
+ const seconds = secondBetweenTwoDate(timeStart, timeEnd);
182
+ log(`Execution time: ${seconds} seconds`);
183
+ objects.push(record);
184
+ }
185
+
186
+ return {
187
+ objects,
188
+ offset: Number(offset) + Number(limit),
189
+ limit: Number(limit),
190
+ completed: count >= total, // if is true the import is completed
191
+ total,
192
+ };
193
+ };
194
+
195
+ export const reindexPosts = async (
196
+ offset: number = 0,
197
+ limit: number = 50,
198
+ filterKey: string = "",
199
+ filterValue: string = ""
200
+ ) => {
201
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
202
+ const timeStart = new Date();
203
+ log(
204
+ `reindexPosts - filterKey: ${filterKey} filterValue: ${filterValue} offset: ${offset} limit: ${limit} `
205
+ );
206
+
207
+ const records = await getObjects(offset, limit, filterKey, filterValue);
208
+ const objectsToSave = records.objects.filter(
209
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
210
+ );
211
+ const objectIdsToDelete = records.objects
212
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
213
+ .map((object) => object.objectID);
214
+
215
+ // Save records to Algolia
216
+ const index = getIndex(indexKey);
217
+ log(`Save ${objectsToSave.length} objects to ${index.indexName} index`);
218
+ const savedObjectIDs = await index.saveObjects(objectsToSave);
219
+ log(
220
+ `Delete ${objectIdsToDelete.length} objects from ${index.indexName} index`
221
+ );
222
+ const deletedObjectIDs = await index.deleteObjects(objectIdsToDelete);
223
+
224
+ const timeEnd = new Date();
225
+ const seconds = secondBetweenTwoDate(timeStart, timeEnd);
226
+ log(`Execution time: ${seconds} seconds`);
227
+
228
+ return { ...records, ...savedObjectIDs, ...deletedObjectIDs };
229
+ };
230
+
231
+ export const removePostObject = async (objectId: string) => {
232
+ return removeIndexObject(objectId, indexKey);
233
+ };
@@ -31,6 +31,13 @@ export type AlgoliaPressReleaseRecord = {
31
31
  visualizationDate?: string;
32
32
  };
33
33
 
34
+ const isObjectToDelete = (
35
+ object: any,
36
+ defaultEnvironmentLocaleCode: string
37
+ ) => {
38
+ return !object?.name?.[defaultEnvironmentLocaleCode];
39
+ };
40
+
34
41
  const getObject = async (
35
42
  topicPressRelease: Entry
36
43
  ): Promise<AlgoliaPressReleaseRecord> => {
@@ -129,8 +136,7 @@ export const reindexPressRelease = async (topicPressReleaseId: string) => {
129
136
  // Save record to Algolia
130
137
  const index = getIndex(indexKey);
131
138
  let record = null;
132
- const names: any = object?.name;
133
- if (names?.[defaultEnvironmentLocaleCode]) {
139
+ if (!isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
134
140
  log(`Save object`);
135
141
  record = await index.saveObject(object);
136
142
  } else {
@@ -208,10 +214,10 @@ export const reindexPressReleases = async (
208
214
 
209
215
  const records = await getObjects(offset, limit, filterKey, filterValue);
210
216
  const objectsToSave = records.objects.filter(
211
- (object) => object?.name?.[defaultEnvironmentLocaleCode]
217
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
212
218
  );
213
219
  const objectIdsToDelete = records.objects
214
- .filter((object) => !object?.name?.[defaultEnvironmentLocaleCode])
220
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
215
221
  .map((object) => object.objectID);
216
222
 
217
223
  // Save records to Algolia
@@ -28,6 +28,13 @@ export type AlgoliaPressReviewRecord = {
28
28
  lastSyncDate?: string;
29
29
  };
30
30
 
31
+ const isObjectToDelete = (
32
+ object: any,
33
+ defaultEnvironmentLocaleCode: string
34
+ ) => {
35
+ return !object?.name?.[defaultEnvironmentLocaleCode];
36
+ };
37
+
31
38
  const getObject = async (
32
39
  topicPressReview: Entry
33
40
  ): Promise<AlgoliaPressReviewRecord> => {
@@ -122,8 +129,7 @@ export const reindexPressReview = async (topicPressReviewId: string) => {
122
129
  // Save record to Algolia
123
130
  const index = getIndex(indexKey);
124
131
  let record = null;
125
- const names: any = object?.name;
126
- if (names?.[defaultEnvironmentLocaleCode]) {
132
+ if (!isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
127
133
  log(`Save object`);
128
134
  record = await index.saveObject(object);
129
135
  } else {
@@ -201,10 +207,10 @@ export const reindexPressReviews = async (
201
207
 
202
208
  const records = await getObjects(offset, limit, filterKey, filterValue);
203
209
  const objectsToSave = records.objects.filter(
204
- (object) => object?.name?.[defaultEnvironmentLocaleCode]
210
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
205
211
  );
206
212
  const objectIdsToDelete = records.objects
207
- .filter((object) => !object?.name?.[defaultEnvironmentLocaleCode])
213
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
208
214
  .map((object) => object.objectID);
209
215
 
210
216
  // Save records to Algolia
@@ -74,6 +74,13 @@ const pipedreamPDFGenerator = async (topicProductId: string) => {
74
74
  }
75
75
  };
76
76
 
77
+ const isObjectToDelete = (
78
+ object: any,
79
+ defaultEnvironmentLocaleCode: string
80
+ ) => {
81
+ return !object?.code || !object?.slugs?.[defaultEnvironmentLocaleCode];
82
+ };
83
+
77
84
  const getObject = async (
78
85
  topicProduct: Entry
79
86
  ): Promise<AlgoliaProductRecord> => {
@@ -341,6 +348,7 @@ export const reindexProduct = async (
341
348
 
342
349
  log(`reindexProduct - entryId: ${topicProductId} `);
343
350
 
351
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
344
352
  const topicProduct = await getEntryByID(
345
353
  topicProductId,
346
354
  "topicProduct",
@@ -356,7 +364,7 @@ export const reindexProduct = async (
356
364
  // Save record to Algolia
357
365
  const index = getIndex(indexKey);
358
366
  let record = null;
359
- if (object?.code) {
367
+ if (!isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
360
368
  log(`Save object`);
361
369
  record = await index.saveObject(object);
362
370
  if (generatePdf && record?.objectID) {
@@ -447,9 +455,12 @@ export const reindexProducts = async (
447
455
  limit,
448
456
  lastPimSyncDateGte
449
457
  );
450
- const objectsToSave = records.objects.filter((object) => object?.code);
458
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
459
+ const objectsToSave = records.objects.filter(
460
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
461
+ );
451
462
  const objectIdsToDelete = records.objects
452
- .filter((object) => !object?.code)
463
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
453
464
  .map((object) => object.objectID);
454
465
 
455
466
  // Save records to Algolia
@@ -33,6 +33,13 @@ export type AlgoliaProjectRecord = {
33
33
  lastSyncDate?: string;
34
34
  };
35
35
 
36
+ const isObjectToDelete = (
37
+ object: any,
38
+ defaultEnvironmentLocaleCode: string
39
+ ) => {
40
+ return !object?.slugs?.[defaultEnvironmentLocaleCode];
41
+ };
42
+
36
43
  const getObject = async (
37
44
  topicProject: Entry
38
45
  ): Promise<AlgoliaProjectRecord> => {
@@ -166,8 +173,7 @@ export const reindexProject = async (topicProjectId: string) => {
166
173
  // Save record to Algolia
167
174
  const index = getIndex(indexKey);
168
175
  let record = null;
169
- const slugs: any = object?.slugs;
170
- if (slugs?.[defaultEnvironmentLocaleCode]) {
176
+ if (!isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
171
177
  log(`Save object`);
172
178
  record = await index.saveObject(object);
173
179
  } else {
@@ -245,10 +251,10 @@ export const reindexProjects = async (
245
251
 
246
252
  const records = await getObjects(offset, limit, filterKey, filterValue);
247
253
  const objectsToSave = records.objects.filter(
248
- (object) => object?.slugs?.[defaultEnvironmentLocaleCode]
254
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
249
255
  );
250
256
  const objectIdsToDelete = records.objects
251
- .filter((object) => !object?.slugs?.[defaultEnvironmentLocaleCode])
257
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
252
258
  .map((object) => object.objectID);
253
259
 
254
260
  // Save records to Algolia
@@ -34,6 +34,13 @@ export type AlgoliaStoryRecord = {
34
34
  lastSyncDate?: string;
35
35
  };
36
36
 
37
+ const isObjectToDelete = (
38
+ object: any,
39
+ defaultEnvironmentLocaleCode: string
40
+ ) => {
41
+ return !object?.slugs?.[defaultEnvironmentLocaleCode];
42
+ };
43
+
37
44
  const getObject = async (topicStory: Entry): Promise<AlgoliaStoryRecord> => {
38
45
  const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
39
46
  log(`Sync the ${topicStory.sys.id} topicStory...`);
@@ -194,8 +201,7 @@ export const reindexStory = async (topicStoryId: string) => {
194
201
  // Save record to Algolia
195
202
  const index = getIndex(indexKey);
196
203
  let record = null;
197
- const slugs: any = object?.slugs;
198
- if (slugs?.[defaultEnvironmentLocaleCode]) {
204
+ if (isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
199
205
  log(`Save object to ${index.indexName} index`);
200
206
  record = await index.saveObject(object);
201
207
  } else {
@@ -273,10 +279,10 @@ export const reindexStories = async (
273
279
 
274
280
  const records = await getObjects(offset, limit, filterKey, filterValue);
275
281
  const objectsToSave = records.objects.filter(
276
- (object) => object?.slugs?.[defaultEnvironmentLocaleCode]
282
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
277
283
  );
278
284
  const objectIdsToDelete = records.objects
279
- .filter((object) => !object?.slugs?.[defaultEnvironmentLocaleCode])
285
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
280
286
  .map((object) => object.objectID);
281
287
 
282
288
  // Save records to Algolia
@@ -231,6 +231,15 @@ const getProductFields = async (topicSubFamily: Entry): Promise<any> => {
231
231
  return productFields;
232
232
  };
233
233
 
234
+ const isObjectToDelete = (
235
+ object: any,
236
+ defaultEnvironmentLocaleCode: string
237
+ ) => {
238
+ return (
239
+ !object?.productFields || !object?.slugs?.[defaultEnvironmentLocaleCode]
240
+ );
241
+ };
242
+
234
243
  const getObject = async (
235
244
  topicSubFamily: Entry
236
245
  ): Promise<AlgoliaFamilyRecord> => {
@@ -375,12 +384,13 @@ export const reindexSubFamilies = async (
375
384
  `reindexSubFamilies - offset: ${offset} limit: ${limit} catalog: ${catalogCode}`
376
385
  );
377
386
 
387
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
378
388
  const records = await getObjects(offset, limit, catalogCode);
379
389
  const objectsToSave = records.objects.filter(
380
- (object) => object.productFields
390
+ (object) => !isObjectToDelete(object, defaultEnvironmentLocaleCode)
381
391
  );
382
392
  const objectIdsToDelete = records.objects
383
- .filter((object) => !object?.productFields)
393
+ .filter((object) => isObjectToDelete(object, defaultEnvironmentLocaleCode))
384
394
  .map((object) => object.objectID);
385
395
 
386
396
  // Save records to Algolia
@@ -420,7 +430,8 @@ export const reindexSubFamily = async (topicSubFamilyEntryId: string) => {
420
430
  // Save record to Algolia
421
431
  const index = getIndex(indexKey);
422
432
  let record = null;
423
- if (object.productFields) {
433
+ const defaultEnvironmentLocaleCode = await getEnvironmentDefaultLocaleCode();
434
+ if (!isObjectToDelete(object, defaultEnvironmentLocaleCode)) {
424
435
  log(`Save object`);
425
436
  record = await index.saveObject(object);
426
437
  } else {
@@ -125,6 +125,10 @@ const getProductFields = async (
125
125
  return productFields;
126
126
  };
127
127
 
128
+ const isObjectToDelete = (object: any) => {
129
+ return Object.entries(object).length === 1;
130
+ };
131
+
128
132
  const getObject = async (
129
133
  topicSubModel: string | Entry
130
134
  ): Promise<AlgoliaSubModelRecord> => {
@@ -238,10 +242,6 @@ const getObject = async (
238
242
  return record;
239
243
  };
240
244
 
241
- const isObjectToDelete = (object: any) => {
242
- return Object.entries(object).length === 1;
243
- };
244
-
245
245
  export const reindexSubModel = async (topicSubModelId: string) => {
246
246
  const timeStart = new Date();
247
247
 
package/src/index.ts CHANGED
@@ -106,6 +106,7 @@ export {
106
106
  reindexPressReleases,
107
107
  removePressReleaseObject,
108
108
  } from "./algolia/pressRelease";
109
+ export { reindexPost, reindexPosts, removePostObject } from "./algolia/news";
109
110
  export { importDownloads } from "./downloads/import";
110
111
  export { getLocalISOTime } from "./utils";
111
112
  export { getStaticDailyProducts } from "./pim/endpoints";