acomm-core-lib 2.6.9 → 2.6.11

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 (185) hide show
  1. package/dist/package.json +11 -9
  2. package/dist/src/core/activity_log/activity_log.layout.d.ts +1 -1
  3. package/dist/src/core/activity_log/activity_log.schema.d.ts +3 -3
  4. package/dist/src/core/activity_log/activity_log.service.d.ts +1 -1
  5. package/dist/src/core/address/address.layout.d.ts +1 -1
  6. package/dist/src/core/address/address.schema.d.ts +1 -1
  7. package/dist/src/core/address/address.service.d.ts +2 -2
  8. package/dist/src/core/api_key/api_key.layout.d.ts +1 -1
  9. package/dist/src/core/api_key/api_key.schema.d.ts +2 -1
  10. package/dist/src/core/api_key/api_key.schema.js +4 -0
  11. package/dist/src/core/api_key/api_key.schema.js.map +1 -1
  12. package/dist/src/core/api_key/api_key.service.d.ts +2 -2
  13. package/dist/src/core/attachment/attachment.service.d.ts +1 -1
  14. package/dist/src/core/attachment/attachment.service.js +31 -2
  15. package/dist/src/core/attachment/attachment.service.js.map +1 -1
  16. package/dist/src/core/audit_log/audit_log.layout.d.ts +1 -1
  17. package/dist/src/core/audit_log/audit_log.schema.d.ts +2 -2
  18. package/dist/src/core/audit_log/audit_log.service.d.ts +1 -1
  19. package/dist/src/core/auth/auth.service.js +11 -2
  20. package/dist/src/core/auth/auth.service.js.map +1 -1
  21. package/dist/src/core/auth/providers/auth_local.provider.d.ts +1 -1
  22. package/dist/src/core/auth/providers/auth_otp.provider.d.ts +1 -1
  23. package/dist/src/core/auth/providers/auth_otp.provider.js +3 -0
  24. package/dist/src/core/auth/providers/auth_otp.provider.js.map +1 -1
  25. package/dist/src/core/auth/providers/base_auth.provider.d.ts +2 -2
  26. package/dist/src/core/comment_data/comment_data.layout.d.ts +1 -1
  27. package/dist/src/core/comment_data/comment_data.schema.d.ts +2 -2
  28. package/dist/src/core/comment_data/comment_data.service.d.ts +1 -1
  29. package/dist/src/core/common/base/base.service.js +2 -1
  30. package/dist/src/core/common/base/base.service.js.map +1 -1
  31. package/dist/src/core/common/base/base_module.service.d.ts +1 -1
  32. package/dist/src/core/common/interceptors/filter.interceptor.js +5 -2
  33. package/dist/src/core/common/interceptors/filter.interceptor.js.map +1 -1
  34. package/dist/src/core/common/utils/build_search.d.ts +2 -1
  35. package/dist/src/core/common/utils/build_search.js +100 -33
  36. package/dist/src/core/common/utils/build_search.js.map +1 -1
  37. package/dist/src/core/common/utils/entity.d.ts +1 -1
  38. package/dist/src/core/common/utils/executions.d.ts +1 -1
  39. package/dist/src/core/common/utils/executions.js +2 -0
  40. package/dist/src/core/common/utils/executions.js.map +1 -1
  41. package/dist/src/core/common/utils/file.js.map +1 -1
  42. package/dist/src/core/common/utils/func.d.ts +1 -1
  43. package/dist/src/core/common/utils/func.js.map +1 -1
  44. package/dist/src/core/common/utils/hook.d.ts +2 -2
  45. package/dist/src/core/common/utils/hook.js +3 -1
  46. package/dist/src/core/common/utils/hook.js.map +1 -1
  47. package/dist/src/core/common/utils/sanitize.d.ts +4 -0
  48. package/dist/src/core/common/utils/sanitize.js +45 -0
  49. package/dist/src/core/common/utils/sanitize.js.map +1 -0
  50. package/dist/src/core/common/utils/schema.d.ts +1 -1
  51. package/dist/src/core/common/utils/schema.js +108 -72
  52. package/dist/src/core/common/utils/schema.js.map +1 -1
  53. package/dist/src/core/common/utils/text_search.d.ts +1 -1
  54. package/dist/src/core/common/utils/text_search.js +23 -2
  55. package/dist/src/core/common/utils/text_search.js.map +1 -1
  56. package/dist/src/core/company/company.layout.d.ts +1 -1
  57. package/dist/src/core/company/company.schema.d.ts +3 -3
  58. package/dist/src/core/company/company.service.d.ts +2 -2
  59. package/dist/src/core/currency/currency.layout.d.ts +1 -1
  60. package/dist/src/core/currency/currency.schema.d.ts +1 -1
  61. package/dist/src/core/currency/currency.service.d.ts +2 -2
  62. package/dist/src/core/custom_function/custom_function.schema.d.ts +1 -1
  63. package/dist/src/core/deploy/deploy.layout.d.ts +1 -1
  64. package/dist/src/core/deploy/deploy.schema.d.ts +1 -1
  65. package/dist/src/core/deploy/deploy.service.d.ts +1 -1
  66. package/dist/src/core/deploy/log/deploy_log.schema.d.ts +1 -1
  67. package/dist/src/core/deploy/log/deploy_log.service.d.ts +1 -1
  68. package/dist/src/core/entities/entities.service.d.ts +1 -1
  69. package/dist/src/core/entities/entities.service.js +25 -1
  70. package/dist/src/core/entities/entities.service.js.map +1 -1
  71. package/dist/src/core/entities/entity.schema.d.ts +2 -1
  72. package/dist/src/core/entities/entity.schema.js +4 -0
  73. package/dist/src/core/entities/entity.schema.js.map +1 -1
  74. package/dist/src/core/execution_log/execution_log.layout.d.ts +1 -1
  75. package/dist/src/core/import_export/import_export.layout.d.ts +1 -1
  76. package/dist/src/core/import_export/import_export.schema.d.ts +1 -1
  77. package/dist/src/core/import_export/import_export.service.d.ts +1 -1
  78. package/dist/src/core/import_export/import_export.service.js +1 -1
  79. package/dist/src/core/import_export/import_export.service.js.map +1 -1
  80. package/dist/src/core/import_export/log/import_export_log.layout.d.ts +1 -1
  81. package/dist/src/core/import_export/log/import_export_log.schema.d.ts +2 -2
  82. package/dist/src/core/import_export/log/import_export_log.service.d.ts +2 -2
  83. package/dist/src/core/import_export/provider/export.provider.d.ts +2 -2
  84. package/dist/src/core/import_export/provider/import.provider.d.ts +2 -2
  85. package/dist/src/core/index.d.ts +1 -0
  86. package/dist/src/core/index.js +4 -1
  87. package/dist/src/core/index.js.map +1 -1
  88. package/dist/src/core/language/language.layout.d.ts +1 -1
  89. package/dist/src/core/language/language.schema.d.ts +1 -1
  90. package/dist/src/core/language/language.service.d.ts +1 -1
  91. package/dist/src/core/layout/share_widget/share_widget.layout.d.ts +1 -1
  92. package/dist/src/core/layout/share_widget/share_widget.schema.d.ts +1 -1
  93. package/dist/src/core/layout/share_widget/share_widget.service.d.ts +3 -3
  94. package/dist/src/core/mail/log/mail_log.layout.d.ts +1 -1
  95. package/dist/src/core/mail/log/mail_log.schema.d.ts +1 -1
  96. package/dist/src/core/mail/log/mail_log.service.d.ts +2 -2
  97. package/dist/src/core/mail/mail.enum.d.ts +2 -1
  98. package/dist/src/core/mail/mail.enum.js +1 -0
  99. package/dist/src/core/mail/mail.enum.js.map +1 -1
  100. package/dist/src/core/mail/mail.layout.d.ts +1 -1
  101. package/dist/src/core/mail/mail.schema.d.ts +1 -1
  102. package/dist/src/core/mail/mail.service.d.ts +1 -1
  103. package/dist/src/core/map_location/map_location.layout.d.ts +1 -1
  104. package/dist/src/core/map_location/map_location.schema.d.ts +1 -1
  105. package/dist/src/core/map_location/map_location.service.d.ts +2 -2
  106. package/dist/src/core/notification/log/log.layout.d.ts +1 -1
  107. package/dist/src/core/notification/log/log.schema.d.ts +1 -1
  108. package/dist/src/core/notification/log/log.service.d.ts +2 -2
  109. package/dist/src/core/notification/message/message.layout.d.ts +1 -1
  110. package/dist/src/core/notification/message/message.schema.d.ts +1 -1
  111. package/dist/src/core/notification/message/message.service.d.ts +8 -2
  112. package/dist/src/core/notification/message/message.service.js +23 -0
  113. package/dist/src/core/notification/message/message.service.js.map +1 -1
  114. package/dist/src/core/notification/message/message.workflow.d.ts +1 -1
  115. package/dist/src/core/notification/message_queue/message_queue.layout.d.ts +1 -1
  116. package/dist/src/core/notification/message_queue/message_queue.schema.d.ts +1 -1
  117. package/dist/src/core/notification/message_queue/message_queue.service.d.ts +2 -2
  118. package/dist/src/core/notification/subscription /subscription.layout.d.ts +1 -1
  119. package/dist/src/core/notification/subscription /subscription.schema.d.ts +1 -1
  120. package/dist/src/core/notification/subscription /subscription.service.d.ts +8 -2
  121. package/dist/src/core/notification/subscription /subscription.service.js +23 -0
  122. package/dist/src/core/notification/subscription /subscription.service.js.map +1 -1
  123. package/dist/src/core/pipeline/job/job.layout.d.ts +1 -1
  124. package/dist/src/core/pipeline/job/job.schema.d.ts +1 -1
  125. package/dist/src/core/pipeline/job/job.service.d.ts +1 -1
  126. package/dist/src/core/pipeline/log/log.layout.d.ts +1 -1
  127. package/dist/src/core/pipeline/log/log.schema.d.ts +1 -1
  128. package/dist/src/core/pipeline/log/log.service.d.ts +2 -2
  129. package/dist/src/core/pipeline/pipeline.layout.d.ts +1 -1
  130. package/dist/src/core/pipeline/pipeline.schema.d.ts +1 -1
  131. package/dist/src/core/pipeline/pipeline.service.d.ts +1 -1
  132. package/dist/src/core/reason/reason.layout.d.ts +1 -1
  133. package/dist/src/core/reason/reason.schema.d.ts +2 -2
  134. package/dist/src/core/reason/reason.service.d.ts +1 -1
  135. package/dist/src/core/schedule_action/log/schedule_log.layout.d.ts +1 -1
  136. package/dist/src/core/schedule_action/log/schedule_log.schema.d.ts +1 -1
  137. package/dist/src/core/schedule_action/log/schedule_log.service.d.ts +2 -2
  138. package/dist/src/core/schedule_action/schedule_action.layout.d.ts +1 -1
  139. package/dist/src/core/schedule_action/schedule_action.schema.d.ts +2 -2
  140. package/dist/src/core/schedule_action/schedule_action.service.d.ts +2 -2
  141. package/dist/src/core/search_text/search_text.service.js +45 -17
  142. package/dist/src/core/search_text/search_text.service.js.map +1 -1
  143. package/dist/src/core/setting/dto/menu_setting.dto.d.ts +1 -1
  144. package/dist/src/core/setting/setting.controller.d.ts +1 -1
  145. package/dist/src/core/setting/setting.schema.d.ts +1 -1
  146. package/dist/src/core/setting/setting.service.js +5 -2
  147. package/dist/src/core/setting/setting.service.js.map +1 -1
  148. package/dist/src/core/shortly/url/url.layout.d.ts +1 -1
  149. package/dist/src/core/shortly/url/url.schema.d.ts +1 -1
  150. package/dist/src/core/shortly/url/url.service.d.ts +2 -2
  151. package/dist/src/core/sms/log/sms_log.layout.d.ts +1 -1
  152. package/dist/src/core/sms/log/sms_log.service.d.ts +1 -1
  153. package/dist/src/core/sms/sms.layout.d.ts +1 -1
  154. package/dist/src/core/template/log/print_log.layout.d.ts +1 -1
  155. package/dist/src/core/template/log/print_log.schema.d.ts +2 -2
  156. package/dist/src/core/template/log/print_log.service.d.ts +2 -2
  157. package/dist/src/core/template/provider/email.provider.d.ts +2 -2
  158. package/dist/src/core/template/provider/sms.provider.d.ts +2 -2
  159. package/dist/src/core/template/template.layout.d.ts +1 -1
  160. package/dist/src/core/template/template.schema.d.ts +2 -2
  161. package/dist/src/core/template/template.service.d.ts +2 -2
  162. package/dist/src/core/translate/translate.json +7 -1
  163. package/dist/src/core/translate/translate.layout.d.ts +1 -1
  164. package/dist/src/core/translate/translate.schema.d.ts +3 -3
  165. package/dist/src/core/translate/translate.service.d.ts +1 -1
  166. package/dist/src/core/users/user.controller.d.ts +1 -0
  167. package/dist/src/core/users/user.controller.js +10 -0
  168. package/dist/src/core/users/user.controller.js.map +1 -1
  169. package/dist/src/core/users/user.hook.d.ts +2 -0
  170. package/dist/src/core/users/user.hook.js +2 -1
  171. package/dist/src/core/users/user.hook.js.map +1 -1
  172. package/dist/src/core/users/user.layout.d.ts +1 -1
  173. package/dist/src/core/users/user.permission.d.ts +1 -1
  174. package/dist/src/core/users/user.schema.d.ts +4 -4
  175. package/dist/src/core/users/user.service.d.ts +5 -2
  176. package/dist/src/core/users/user.service.js +4 -0
  177. package/dist/src/core/users/user.service.js.map +1 -1
  178. package/dist/src/core/version_data/version_data.schema.d.ts +2 -2
  179. package/dist/src/core/version_data/version_data.service.d.ts +1 -1
  180. package/dist/src/core/widget/widget.schema.d.ts +2 -2
  181. package/dist/src/core/widget/widget.service.d.ts +1 -1
  182. package/dist/src/main.js +16 -0
  183. package/dist/src/main.js.map +1 -1
  184. package/dist/tsconfig.build.tsbuildinfo +1 -1
  185. package/package.json +12 -10
@@ -2292,7 +2292,8 @@ const _buildOne2ManySubPipeline = async (entityName, requestedFieldNames, origin
2292
2292
  return subPipeline;
2293
2293
  };
2294
2294
  const buildNestedLookups = async (pipeline, relateFieldNames, entityName, requestedFieldNames = [], currentPath = "", level = 0, visitedEntities = new Set(), customPopulate = false, originalRequestedFieldNames = [], maxLevel = 1) => {
2295
- if (level > maxLevel)
2295
+ const finalizedMaxLevel = (requestedFieldNames.length > 0 || !customPopulate) ? maxLevel : 1;
2296
+ if (level >= finalizedMaxLevel)
2296
2297
  return;
2297
2298
  const entity = await _loadEntity(entityName);
2298
2299
  if (!entity || !entity.fields)
@@ -2369,6 +2370,9 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2369
2370
  });
2370
2371
  continue;
2371
2372
  }
2373
+ const lookupAlias = fieldPath.includes(".")
2374
+ ? `__tmp_${fieldPath.replace(/\./g, "__")}`
2375
+ : fieldPath;
2372
2376
  pipeline.push({
2373
2377
  $addFields: {
2374
2378
  [fieldPath]: _getObjectIdCastingExpression(`$${fieldPath}`),
@@ -2379,9 +2383,17 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2379
2383
  from: collectionName,
2380
2384
  localField: fieldPath,
2381
2385
  foreignField: "_id",
2382
- as: fieldPath
2386
+ as: lookupAlias
2383
2387
  }
2384
2388
  });
2389
+ if (lookupAlias !== fieldPath) {
2390
+ pipeline.push({
2391
+ $addFields: { [fieldPath]: `$${lookupAlias}` }
2392
+ });
2393
+ pipeline.push({
2394
+ $unset: lookupAlias
2395
+ });
2396
+ }
2385
2397
  if (relatedType === "one2one") {
2386
2398
  pipeline.push({
2387
2399
  $addFields: { [fieldPath]: { $arrayElemAt: [`$${fieldPath}`, 0] } }
@@ -2392,58 +2404,43 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2392
2404
  pipeline.push(...transformStage);
2393
2405
  continue;
2394
2406
  }
2395
- const nestedFieldsForThisField = requestedFieldNames.filter(f => f.startsWith(`${fieldPath}.`) && f !== `${fieldPath}.*`);
2396
- const hasNestedProjection = nestedFieldsForThisField.length > 0;
2397
- const nextLevelFields = hasNestedProjection
2398
- ? nestedFieldsForThisField
2399
- .map(f => f.substring(fieldPath.length + 1))
2400
- .map(f => f.split(".")[0])
2401
- .filter((f, i, arr) => arr.indexOf(f) === i)
2402
- : [];
2403
- const directFields = requestedFieldNames
2404
- .filter(f => !f.includes(".") || f.split(".").length === 1)
2405
- .filter(f => f !== "*");
2406
- const nestedFields = [];
2407
- for (const field of requestedFieldNames) {
2408
- if (field.includes(".") && !field.endsWith(".*")) {
2409
- const parts = field.split(".");
2410
- if (hasNestedProjection) {
2411
- for (const part of parts) {
2412
- if (part !== "*") {
2413
- nestedFields.push(part);
2414
- }
2415
- }
2416
- }
2417
- else {
2418
- const lastPart = parts[parts.length - 1];
2419
- if (lastPart !== "*") {
2420
- nestedFields.push(lastPart);
2421
- }
2422
- }
2423
- }
2407
+ const isWildcardActive = requestedFieldNames.includes("*") || (fieldPath && requestedFieldNames.includes(`${fieldPath}.*`));
2408
+ const subRequestedFields = requestedFieldNames
2409
+ .filter(f => f === field.name || f.startsWith(`${field.name}.`))
2410
+ .map(f => f === field.name ? "*" : f.substring(field.name.length + 1));
2411
+ if (isWildcardActive && !subRequestedFields.includes("*")) {
2412
+ subRequestedFields.push("*");
2424
2413
  }
2425
- const levelSpecificFields = [...directFields, ...nestedFields].filter((f, i, arr) => arr.indexOf(f) === i);
2426
- const hasWildcard = requestedFieldNames.includes("*") ||
2427
- requestedFieldNames.some(f => f.endsWith(".*"));
2414
+ const nestedAbsolute = requestedFieldNames.filter(f => f.startsWith(`${fieldPath}.`) && f !== `${fieldPath}.*`);
2415
+ const hasWildcard = subRequestedFields.includes("*") || isWildcardActive;
2428
2416
  let allowedFields = null;
2429
2417
  if (hasWildcard) {
2430
2418
  allowedFields = null;
2431
2419
  }
2432
- else if (hasNestedProjection || levelSpecificFields.length > 0) {
2433
- if (hasNestedProjection) {
2434
- allowedFields = [...nextLevelFields, ...levelSpecificFields];
2435
- }
2436
- else {
2437
- allowedFields = [...levelSpecificFields];
2420
+ else {
2421
+ const nextLevelFromRelative = subRequestedFields
2422
+ .filter(f => f.includes("."))
2423
+ .map(f => f.split(".")[0]);
2424
+ const nextLevelFromAbsolute = nestedAbsolute
2425
+ .map(f => f.substring(fieldPath.length + 1).split(".")[0]);
2426
+ const directFromRelative = subRequestedFields.filter(f => !f.includes(".") && f !== "*");
2427
+ const combinedFields = [...nextLevelFromRelative, ...nextLevelFromAbsolute, ...directFromRelative];
2428
+ const uniqueFields = combinedFields.filter((f, i, arr) => f && arr.indexOf(f) === i).sort();
2429
+ if (uniqueFields.length > 0) {
2430
+ allowedFields = ["_id", ...uniqueFields];
2438
2431
  }
2439
- allowedFields = ["_id", ...allowedFields];
2440
2432
  }
2441
2433
  if (relatedType === "one2one") {
2442
2434
  pipeline.push({
2443
2435
  $addFields: {
2444
2436
  [fieldPath]: {
2445
2437
  $cond: [
2446
- { $ne: [{ $ifNull: [`$${fieldPath}._id`, null] }, null] },
2438
+ {
2439
+ $and: [
2440
+ { $ne: [{ $type: `$${fieldPath}` }, "missing"] },
2441
+ { $ne: [`$${fieldPath}`, null] }
2442
+ ]
2443
+ },
2447
2444
  allowedFields !== null
2448
2445
  ? {
2449
2446
  $mergeObjects: [
@@ -2656,14 +2653,23 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2656
2653
  if (_.isPlainObject(cloneAggregate)) {
2657
2654
  cloneAggregate = [cloneAggregate];
2658
2655
  }
2656
+ const entity = await _loadEntity(entityName);
2659
2657
  const nestedStages = [];
2658
+ const nestedFilterFields = new Set();
2660
2659
  if (Array.isArray(cloneAggregate)) {
2661
2660
  for (let i = 0; i < cloneAggregate.length; i++) {
2662
2661
  const stage = cloneAggregate[i];
2663
2662
  if (stage === null || stage === void 0 ? void 0 : stage.$match) {
2663
+ if (entity && entity.fields) {
2664
+ stage.$match = (0, build_search_1.convertQueryValues)(stage.$match, entity.fields);
2665
+ }
2664
2666
  const { local, nested } = _splitFilter(stage.$match);
2665
2667
  if (!_.isEmpty(nested)) {
2666
2668
  nestedStages.push({ $match: nested });
2669
+ Object.keys(nested).forEach(k => {
2670
+ const root = k.split('.')[0];
2671
+ nestedFilterFields.add(root);
2672
+ });
2667
2673
  if (_.isEmpty(local)) {
2668
2674
  cloneAggregate.splice(i, 1);
2669
2675
  i--;
@@ -2678,41 +2684,35 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2678
2684
  let normalizedFields = [];
2679
2685
  let includedFields = [];
2680
2686
  let excludedFields = [];
2687
+ let maxLevel = 1;
2681
2688
  if (typeof fields === "string") {
2682
- const fieldList = fields.split(",").map(name => name.trim());
2689
+ const fieldList = (fields || "").split(",").map(name => name.trim()).filter(f => !_.isEmpty(f));
2683
2690
  normalizedFields = fieldList.map(name => ({ name }));
2684
2691
  includedFields = fieldList.filter(f => !f.startsWith("-"));
2685
- excludedFields = fieldList
2686
- .filter(f => f.startsWith("-"))
2687
- .map(f => f.substring(1));
2692
+ excludedFields = fieldList.filter(f => f.startsWith("-")).map(f => f.substring(1));
2688
2693
  }
2689
2694
  else if (Array.isArray(fields)) {
2690
2695
  if (fields.length > 0 && typeof fields[0] === "string") {
2691
- const trimmedFields = fields.map(name => name.trim());
2692
- normalizedFields = trimmedFields.map(name => ({ name }));
2693
- includedFields = trimmedFields.filter(f => !f.startsWith("-"));
2694
- excludedFields = trimmedFields
2695
- .filter(f => f.startsWith("-"))
2696
- .map(f => f.substring(1));
2696
+ const fieldList = fields.map(name => name.trim()).filter(f => !_.isEmpty(f));
2697
+ normalizedFields = fieldList.map(name => ({ name }));
2698
+ includedFields = fieldList.filter(f => !f.startsWith("-"));
2699
+ excludedFields = fieldList.filter(f => f.startsWith("-")).map(f => f.substring(1));
2697
2700
  }
2698
2701
  else {
2699
2702
  normalizedFields = fields;
2703
+ includedFields = fields.map(f => f.name).filter(f => !f.startsWith("-"));
2704
+ excludedFields = fields.map(f => f.name).filter(f => f.startsWith("-")).map(f => f.substring(1));
2700
2705
  }
2701
2706
  }
2702
- if (sort) {
2703
- cloneAggregate.push({
2704
- $sort: (0, build_search_1.buildSort)(sort)
2705
- });
2706
- }
2707
- if (page > 0) {
2708
- cloneAggregate.push({
2709
- $skip: page
2710
- });
2711
- }
2712
- if (pageSize > 0) {
2713
- cloneAggregate.push({
2714
- $limit: pageSize
2707
+ if (includedFields.length > 0) {
2708
+ let dynamicMaxLevel = 1;
2709
+ includedFields.forEach(f => {
2710
+ const depth = (f.match(/\./g) || []).length + 1;
2711
+ if (depth > dynamicMaxLevel) {
2712
+ dynamicMaxLevel = depth;
2713
+ }
2715
2714
  });
2715
+ maxLevel = dynamicMaxLevel;
2716
2716
  }
2717
2717
  let project = {};
2718
2718
  if (!_.isEmpty(normalizedFields)) {
@@ -2730,7 +2730,7 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2730
2730
  else if (includedFields.length > 0) {
2731
2731
  fieldsToProject = allAvailableFields.filter(field => includedFields.some(pattern => pattern === field ||
2732
2732
  (pattern.endsWith(".*") && field === pattern.slice(0, -2)) ||
2733
- field.startsWith(pattern.replace(".*", "."))));
2733
+ pattern.startsWith(field + ".")));
2734
2734
  }
2735
2735
  else {
2736
2736
  fieldsToProject = allAvailableFields;
@@ -2764,10 +2764,7 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2764
2764
  project = projectSelected;
2765
2765
  }
2766
2766
  }
2767
- const entity = await _loadEntity(entityName);
2768
2767
  if (entity && entity.fields) {
2769
- const entityNormalizedFields = _normalizeField(entity.fields, "");
2770
- cloneAggregate.push(...entityNormalizedFields);
2771
2768
  const initialVisitedEntities = new Set();
2772
2769
  initialVisitedEntities.add(entityName);
2773
2770
  const relateFieldNames = [];
@@ -2785,9 +2782,38 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2785
2782
  else {
2786
2783
  requestedFieldNames = [];
2787
2784
  }
2785
+ nestedFilterFields.forEach(root => {
2786
+ const rootExists = requestedFieldNames.some(f => {
2787
+ const fRoot = f.split('.')[0].replace(/^_keepPopulate\((.*)\)$/, '$1');
2788
+ return fRoot === root;
2789
+ });
2790
+ if (!rootExists) {
2791
+ requestedFieldNames.push(root);
2792
+ }
2793
+ });
2788
2794
  const maxLevel = _.max(requestedFieldNames.map(f => f.split(".").length - 1)) || 1;
2789
2795
  const originalRequestedFieldNames = _.cloneDeep(requestedFieldNames);
2796
+ const canDoEarlyPagination = _.isEmpty(nestedStages);
2797
+ if (canDoEarlyPagination) {
2798
+ if (sort)
2799
+ cloneAggregate.push({ $sort: (0, build_search_1.buildSort)(sort) });
2800
+ if (page > 0)
2801
+ cloneAggregate.push({ $skip: page });
2802
+ if (pageSize > 0)
2803
+ cloneAggregate.push({ $limit: pageSize });
2804
+ }
2790
2805
  await buildNestedLookups(cloneAggregate, relateFieldNames, entityName, requestedFieldNames, "", 0, initialVisitedEntities, customPopulate, originalRequestedFieldNames, maxLevel);
2806
+ if (!_.isEmpty(nestedStages)) {
2807
+ cloneAggregate.push(...nestedStages);
2808
+ }
2809
+ if (!canDoEarlyPagination) {
2810
+ if (sort)
2811
+ cloneAggregate.push({ $sort: (0, build_search_1.buildSort)(sort) });
2812
+ if (page > 0)
2813
+ cloneAggregate.push({ $skip: page });
2814
+ if (pageSize > 0)
2815
+ cloneAggregate.push({ $limit: pageSize });
2816
+ }
2791
2817
  if (project && typeof project === "object" && "$project" in project) {
2792
2818
  const projectStage = project;
2793
2819
  const filteredRelateFieldNames = relateFieldNames.filter(fieldPath => {
@@ -2806,6 +2832,19 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2806
2832
  projectStage.$project[fieldPath] = 1;
2807
2833
  }
2808
2834
  });
2835
+ includedFields.forEach(f => {
2836
+ if (f.includes(".") && !f.endsWith(".*")) {
2837
+ const rootField = f.split(".")[0];
2838
+ const hasParentProjected = Object.keys(projectStage.$project).some(existingPath => {
2839
+ return f.startsWith(existingPath + ".");
2840
+ });
2841
+ if (!hasParentProjected && projectStage.$project[rootField] === undefined) {
2842
+ projectStage.$project[f] = 1;
2843
+ projectStage.$project[`${rootField}._id`] = 1;
2844
+ projectStage.$project[`${rootField}.id`] = 1;
2845
+ }
2846
+ }
2847
+ });
2809
2848
  }
2810
2849
  }
2811
2850
  if (entityName === "attachment") {
@@ -2843,9 +2882,6 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2843
2882
  cloneAggregate.push({
2844
2883
  $unset: ["_search_text"]
2845
2884
  });
2846
- if (!_.isEmpty(nestedStages)) {
2847
- cloneAggregate.push(...nestedStages);
2848
- }
2849
2885
  return cloneAggregate;
2850
2886
  };
2851
2887
  exports.buildAggregate = buildAggregate;