sr-npm 1.7.534 → 1.7.537

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.
@@ -5,6 +5,8 @@ const COLLECTIONS = {
5
5
  TEMPLATE_TYPE: 'templateType',
6
6
  SECRET_MANAGER_MIRROR: 'SecretManagerMirror',
7
7
  BRANDS: 'Brands',
8
+ CUSTOM_VALUES:'CustomValues',
9
+ CUSTOM_FIELDS:'CustomFields',
8
10
  }
9
11
  const JOBS_COLLECTION_FIELDS = {
10
12
  LOCATION: 'location',
package/backend/data.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const { items: wixData } = require('@wix/data');
2
2
  const { fetchPositionsFromSRAPI, fetchJobDescription } = require('./fetchPositionsFromSRAPI');
3
3
  const { createCollectionIfMissing } = require('@hisense-staging/velo-npm/backend');
4
- const { COLLECTIONS, COLLECTIONS_FIELDS,JOBS_COLLECTION_FIELDS,TEMPLATE_TYPE,TOKEN_NAME } = require('./collectionConsts');
4
+ const { COLLECTIONS, COLLECTIONS_FIELDS,JOBS_COLLECTION_FIELDS,TEMPLATE_TYPE,TOKEN_NAME,CUSTOM_VALUES_COLLECTION_FIELDS,CUSTOM_FIELDS_COLLECTION_FIELDS } = require('./collectionConsts');
5
5
  const { chunkedBulkOperation, countJobsPerGivenField, fillCityLocationAndLocationAddress ,prepareToSaveArray,normalizeString} = require('./utils');
6
6
  const { getAllPositions } = require('./queries');
7
7
  const { retrieveSecretVal, getTokenFromCMS } = require('./secretsData');
@@ -52,21 +52,29 @@ function validateSingleDesiredBrand(desiredBrand) {
52
52
  throw new Error("Desired brand must be a single brand");
53
53
  }
54
54
  }
55
- function getCustomFieldsAndValuesFromPosition(position,customFields) {
55
+ function getCustomFieldsAndValuesFromPosition(position,customFieldsLabels,customFieldsValues) {
56
56
  const customFieldsArray = Array.isArray(position?.customField) ? position.customField : [];
57
57
  for (const field of customFieldsArray) {
58
58
  if(field.fieldLabel==="Country" || field.fieldLabel==="Department") continue; //country and department are not custom fields, they are already in the job object
59
59
  const label = field.fieldLabel==="Brands" ? "brand" : field.fieldLabel
60
- const key = normalizeString(label);
61
- const value = field.valueLabel
62
- customFields[key] ? customFields[key].add(value) : customFields[key]=new Set([value])
60
+ const fieldId=field.fieldId
61
+ const fieldLabel = normalizeString(label);
62
+ const valueId=field.valueId
63
+ const valueLabel = field.valueLabel
64
+ customFieldsLabels[fieldId] = fieldLabel
65
+ // Build nested dictionary: fieldId -> { valueId: valueLabel }
66
+ if (!customFieldsValues[fieldId]) {
67
+ customFieldsValues[fieldId] = {};
68
+ }
69
+ customFieldsValues[fieldId][valueId] = valueLabel;
63
70
  }
64
71
 
65
72
  }
66
73
  async function saveJobsDataToCMS() {
67
74
  const positions = await fetchPositionsFromSRAPI();
68
75
  const sourcePositions = await filterBasedOnBrand(positions);
69
- const customFields = {}
76
+ const customFieldsLabels = {}
77
+ const customFieldsValues = {}
70
78
  // bulk insert to jobs collection without descriptions first
71
79
  const jobsData = sourcePositions.map(position => {
72
80
  const basicJob = {
@@ -92,10 +100,13 @@ async function saveJobsDataToCMS() {
92
100
  brand: getBrand(position.customField),
93
101
  jobDescription: null, // Will be filled later
94
102
  };
95
- getCustomFieldsAndValuesFromPosition(position,customFields);
103
+ getCustomFieldsAndValuesFromPosition(position,customFieldsLabels,customFieldsValues);
96
104
  return basicJob;
97
105
  });
98
- console.log("customFields: ", customFields);
106
+ console.log("customFieldsLabels: ", customFieldsLabels);
107
+ console.log("customFieldsValues: ", customFieldsValues);
108
+ populateCustomFieldsCollection(customFieldsLabels);
109
+ populateCustomValuesCollection(customFieldsValues);
99
110
  // Sort jobs by title (ascending, case-insensitive, numeric-aware)
100
111
  jobsData.sort((a, b) => {
101
112
  const titleA = a.title || '';
@@ -134,6 +145,26 @@ async function saveJobsDataToCMS() {
134
145
  console.log(`✓ All chunks processed. Total jobs saved: ${totalSaved}/${jobsData.length}`);
135
146
  }
136
147
 
148
+ function populateCustomFieldsCollection(customFields) {
149
+ for(const ID of Object.keys(customFields)){
150
+ wixData.save(COLLECTIONS.CUSTOM_FIELDS, {
151
+ title: customFields[ID],
152
+ _id: ID,
153
+ });
154
+ }
155
+ }
156
+ function populateCustomValuesCollection(customFieldsValues) {
157
+ for (const fieldId of Object.keys(customFieldsValues)) {
158
+ const valuesMap = customFieldsValues[fieldId] || {};
159
+ for (const valueId of Object.keys(valuesMap)) {
160
+ wixData.save(COLLECTIONS.CUSTOM_VALUES, {
161
+ _id: valueId,
162
+ title: valuesMap[valueId],
163
+ customField: fieldId, // reference to CustomFields collection (displays the label)
164
+ });
165
+ }
166
+ }
167
+ }
137
168
  async function saveJobsDescriptionsAndLocationApplyUrlToCMS() {
138
169
  console.log('🚀 Starting job descriptions update process for ALL jobs');
139
170
 
@@ -291,6 +322,7 @@ async function referenceJobsToField({ referenceField, sourceCollection, jobField
291
322
  return rest;
292
323
  });
293
324
 
325
+
294
326
  // Bulk update in chunks of 1000
295
327
  const chunkSize = 1000;
296
328
  await chunkedBulkOperation({
@@ -336,13 +368,13 @@ function fetchJobLocation(jobDetails) {
336
368
  async function createCollections() {
337
369
  console.log("Creating collections");
338
370
  await Promise.all(
339
- [createCollectionIfMissing(COLLECTIONS.JOBS, JOBS_COLLECTION_FIELDS.JOBS,{ insert: 'ADMIN', update: 'ADMIN', remove: 'ADMIN', read: 'ANYONE' }),
371
+ [createCollectionIfMissing(COLLECTIONS.JOBS, COLLECTIONS_FIELDS.JOBS,{ insert: 'ADMIN', update: 'ADMIN', remove: 'ADMIN', read: 'ANYONE' }),
340
372
  createCollectionIfMissing(COLLECTIONS.CITIES, COLLECTIONS_FIELDS.CITIES),
341
373
  createCollectionIfMissing(COLLECTIONS.AMOUNT_OF_JOBS_PER_DEPARTMENT, COLLECTIONS_FIELDS.AMOUNT_OF_JOBS_PER_DEPARTMENT),
342
374
  createCollectionIfMissing(COLLECTIONS.SECRET_MANAGER_MIRROR, COLLECTIONS_FIELDS.SECRET_MANAGER_MIRROR),
343
375
  createCollectionIfMissing(COLLECTIONS.BRANDS, COLLECTIONS_FIELDS.BRANDS),
344
- createCollectionIfMissing(COLLECTIONS.CUSTOM_VALUES, CUSTOM_VALUES_COLLECTION_FIELDS.CUSTOM_VALUES),
345
- createCollectionIfMissing(COLLECTIONS.CUSTOM_FIELDS, CUSTOM_FIELDS_COLLECTION_FIELDS.CUSTOM_FIELDS)
376
+ createCollectionIfMissing(COLLECTIONS.CUSTOM_VALUES, COLLECTIONS_FIELDS.CUSTOM_VALUES),
377
+ createCollectionIfMissing(COLLECTIONS.CUSTOM_FIELDS, COLLECTIONS_FIELDS.CUSTOM_FIELDS)
346
378
  ]);
347
379
  console.log("finished creating Collections");
348
380
  }
@@ -362,13 +394,14 @@ async function referenceJobs() {
362
394
  await referenceJobsToField({ referenceField: JOBS_COLLECTION_FIELDS.DEPARTMENT_REF, sourceCollection: COLLECTIONS.AMOUNT_OF_JOBS_PER_DEPARTMENT, jobField: JOBS_COLLECTION_FIELDS.DEPARTMENT });
363
395
  await referenceJobsToField({ referenceField: JOBS_COLLECTION_FIELDS.CITY, sourceCollection: COLLECTIONS.CITIES, jobField: JOBS_COLLECTION_FIELDS.CITY_TEXT });
364
396
  await referenceJobsToField({ referenceField: JOBS_COLLECTION_FIELDS.BRAND_REF, sourceCollection: COLLECTIONS.BRANDS, jobField: JOBS_COLLECTION_FIELDS.BRAND });
397
+ await referenceJobsToField({referenceField:JOBS_COLLECTION_FIELDS.CUSTOM_VALUES,sourceCollection:COLLECTIONS.CUSTOM_VALUES,jobField:JOBS_COLLECTION_FIELDS.CUSTOM_VALUES} )
365
398
  console.log("finished referencing jobs");
366
399
  }
367
400
 
368
401
  async function syncJobsFast() {
369
402
  console.log("Syncing jobs fast");
370
403
  await createCollections();
371
- // await clearCollections();
404
+ await clearCollections();
372
405
  // await fillSecretManagerMirror();
373
406
  console.log("saving jobs data to CMS");
374
407
  await saveJobsDataToCMS();
@@ -387,7 +420,9 @@ async function clearCollections() {
387
420
  wixData.truncate(COLLECTIONS.CITIES),
388
421
  wixData.truncate(COLLECTIONS.AMOUNT_OF_JOBS_PER_DEPARTMENT),
389
422
  wixData.truncate(COLLECTIONS.JOBS),
390
- wixData.truncate(COLLECTIONS.BRANDS)
423
+ wixData.truncate(COLLECTIONS.BRANDS),
424
+ wixData.truncate(COLLECTIONS.CUSTOM_VALUES),
425
+ wixData.truncate(COLLECTIONS.CUSTOM_FIELDS),
391
426
  ]);
392
427
  console.log("cleared collections successfully");
393
428
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sr-npm",
3
- "version": "1.7.534",
3
+ "version": "1.7.537",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {