@resolveio/server-lib 22.2.29 → 22.2.30

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.
@@ -11030,7 +11030,9 @@ function resolveLookupDisplayFieldCandidates(baseToken, probeDocs, schemaFields)
11030
11030
  }
11031
11031
  return !/(^|\.)(id|code|number|count|qty|quantity|total)(\.|_|$)/.test(normalized);
11032
11032
  });
11033
- return normalizeAggregateFieldList(__spreadArray(__spreadArray(__spreadArray([], __read(seededCandidates), false), __read(schemaCandidates), false), __read(probeCandidates), false)).slice(0, 24);
11033
+ var normalizedCandidates = normalizeAggregateFieldList(__spreadArray(__spreadArray(__spreadArray([], __read(seededCandidates), false), __read(schemaCandidates), false), __read(probeCandidates), false).map(function (field) { return normalizeLookupPath(field) || normalizeOptionalString(field); }));
11034
+ var safeCandidates = normalizedCandidates.filter(function (field) { return isAssistantProjectionKeyAllowed(field); });
11035
+ return safeCandidates.slice(0, 24);
11034
11036
  }
11035
11037
  function scoreLookupDisplayFieldFromDocs(field, lookupDocs) {
11036
11038
  var docs = Array.isArray(lookupDocs) ? lookupDocs : [];
@@ -11154,7 +11156,7 @@ function applyIdLookupDisplayEnrichment(params) {
11154
11156
  return [2 /*return*/, "continue"];
11155
11157
  }
11156
11158
  _loop_8 = function (candidate) {
11157
- var probeDocs, candidateHasClientScope, _e, schemaFields, nameFieldCandidates, mapping, lookupKey, idsForQuery, query, candidateHasClientScope, _f, projection, lookupDocs, bestLookupField, nameField, nameMap, displayField;
11159
+ var probeDocs, candidateHasClientScope, _e, schemaFields, nameFieldCandidates, mapping, lookupKey, idsForQuery, normalizedNameFieldCandidates, query, candidateHasClientScope, _f, rawProjection, projection, lookupDocs, bestLookupField, nameField, nameMap, displayField;
11158
11160
  var _g, _h;
11159
11161
  return __generator(this, function (_j) {
11160
11162
  switch (_j.label) {
@@ -11192,11 +11194,19 @@ function applyIdLookupDisplayEnrichment(params) {
11192
11194
  return [2 /*return*/, "continue"];
11193
11195
  }
11194
11196
  mapping = mappingMatches.find(function (entry) { return entry.lookupCollection === candidate; });
11195
- lookupKey = normalizeOptionalString(mapping === null || mapping === void 0 ? void 0 : mapping.lookupKey) || '_id';
11197
+ lookupKey = normalizeLookupPath((mapping === null || mapping === void 0 ? void 0 : mapping.lookupKey) || '') || '_id';
11196
11198
  idsForQuery = normalizeIdsForTargetField(values, probeDocs, lookupKey);
11197
11199
  if (!idsForQuery.length) {
11198
11200
  return [2 /*return*/, "continue"];
11199
11201
  }
11202
+ normalizedNameFieldCandidates = normalizeAggregateFieldList(nameFieldCandidates
11203
+ .map(function (field) { return normalizeLookupPath(field) || normalizeOptionalString(field); })
11204
+ .filter(Boolean))
11205
+ .filter(function (field) { return field !== lookupKey; })
11206
+ .filter(function (field) { return isAssistantProjectionKeyAllowed(field); });
11207
+ if (!normalizedNameFieldCandidates.length) {
11208
+ return [2 /*return*/, "continue"];
11209
+ }
11200
11210
  query = (_g = {}, _g[lookupKey] = { $in: idsForQuery }, _g);
11201
11211
  if (!!isSuperAdmin) return [3 /*break*/, 9];
11202
11212
  if (!idClient) return [3 /*break*/, 7];
@@ -11215,10 +11225,14 @@ function applyIdLookupDisplayEnrichment(params) {
11215
11225
  query = applyCustomerScopeFilter(query, candidate, idCustomer, isSuperAdmin);
11216
11226
  _j.label = 9;
11217
11227
  case 9:
11218
- projection = (_h = {}, _h[lookupKey] = 1, _h);
11219
- nameFieldCandidates.forEach(function (field) {
11220
- projection[field] = 1;
11228
+ rawProjection = (_h = {}, _h[lookupKey] = 1, _h);
11229
+ normalizedNameFieldCandidates.forEach(function (field) {
11230
+ rawProjection[field] = 1;
11221
11231
  });
11232
+ projection = sanitizeAssistantProjection(rawProjection);
11233
+ if (!projection || !Object.keys(projection).length) {
11234
+ return [2 /*return*/, "continue"];
11235
+ }
11222
11236
  return [4 /*yield*/, db.collection(candidate).find(query, {
11223
11237
  projection: projection,
11224
11238
  limit: AI_ASSISTANT_ID_DISPLAY_LOOKUP_MAX_IDS,
@@ -11229,7 +11243,7 @@ function applyIdLookupDisplayEnrichment(params) {
11229
11243
  if (!lookupDocs.length) {
11230
11244
  return [2 /*return*/, "continue"];
11231
11245
  }
11232
- bestLookupField = resolveBestLookupDisplayFieldFromDocs(nameFieldCandidates, lookupDocs);
11246
+ bestLookupField = resolveBestLookupDisplayFieldFromDocs(normalizedNameFieldCandidates, lookupDocs);
11233
11247
  if (!(bestLookupField === null || bestLookupField === void 0 ? void 0 : bestLookupField.field)) {
11234
11248
  return [2 /*return*/, "continue"];
11235
11249
  }
@@ -11278,7 +11292,7 @@ function applyIdLookupDisplayEnrichment(params) {
11278
11292
  nameField: nameField,
11279
11293
  nameFieldFillRate: (0, common_1.round)(bestLookupField.fillRate * 100, 2),
11280
11294
  nameFieldUniqueValues: bestLookupField.uniqueCount,
11281
- candidateFieldsTested: nameFieldCandidates.length,
11295
+ candidateFieldsTested: normalizedNameFieldCandidates.length,
11282
11296
  idCount: values.length,
11283
11297
  matched: nameMap.size,
11284
11298
  strategy: mapping ? 'schema' : 'token'
@@ -16195,12 +16209,14 @@ function buildNameLookupFields(baseToken, docs) {
16195
16209
  }
16196
16210
  function lookupIdsForNameMatch(params) {
16197
16211
  return __awaiter(this, void 0, void 0, function () {
16198
- var db, dbName, collection, baseToken, regex, idClient, idCustomer, isSuperAdmin, probeDocs, fields, query, hasClient, normalizedCustomer, hasCustomer, projection, docs, ids;
16212
+ var db, dbName, collection, baseToken, regex, idClient, idCustomer, isSuperAdmin, probeDocs, fields, query, hasClient, normalizedCustomer, hasCustomer, rawProjection, projection, docs, ids;
16199
16213
  return __generator(this, function (_a) {
16200
16214
  switch (_a.label) {
16201
16215
  case 0:
16202
16216
  db = params.db, dbName = params.dbName, collection = params.collection, baseToken = params.baseToken, regex = params.regex, idClient = params.idClient, idCustomer = params.idCustomer, isSuperAdmin = params.isSuperAdmin, probeDocs = params.probeDocs;
16203
- fields = buildNameLookupFields(baseToken, probeDocs);
16217
+ fields = normalizeAggregateFieldList(buildNameLookupFields(baseToken, probeDocs)
16218
+ .map(function (field) { return normalizeLookupPath(field) || normalizeOptionalString(field); })
16219
+ .filter(Boolean)).filter(function (field) { return isAssistantProjectionKeyAllowed(field); });
16204
16220
  if (!fields.length) {
16205
16221
  return [2 /*return*/, null];
16206
16222
  }
@@ -16225,10 +16241,14 @@ function lookupIdsForNameMatch(params) {
16225
16241
  }
16226
16242
  _a.label = 3;
16227
16243
  case 3:
16228
- projection = { _id: 1 };
16244
+ rawProjection = { _id: 1 };
16229
16245
  fields.forEach(function (field) {
16230
- projection[field] = 1;
16246
+ rawProjection[field] = 1;
16231
16247
  });
16248
+ projection = sanitizeAssistantProjection(rawProjection);
16249
+ if (!projection || !Object.keys(projection).length) {
16250
+ return [2 /*return*/, null];
16251
+ }
16232
16252
  return [4 /*yield*/, db.collection(collection)
16233
16253
  .find(query, {
16234
16254
  projection: projection,