acomm-core-lib 2.6.8 → 2.6.9

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 (82) hide show
  1. package/dist/src/core/activity_log/activity_log.schema.d.ts +1 -0
  2. package/dist/src/core/activity_log/activity_log.schema.js +4 -0
  3. package/dist/src/core/activity_log/activity_log.schema.js.map +1 -1
  4. package/dist/src/core/activity_log/activity_log.service.js +5 -0
  5. package/dist/src/core/activity_log/activity_log.service.js.map +1 -1
  6. package/dist/src/core/api_key/api_key.layout.js +22 -0
  7. package/dist/src/core/api_key/api_key.layout.js.map +1 -1
  8. package/dist/src/core/api_key/api_key.schema.d.ts +3 -0
  9. package/dist/src/core/api_key/api_key.schema.js +17 -0
  10. package/dist/src/core/api_key/api_key.schema.js.map +1 -1
  11. package/dist/src/core/api_key/api_key.service.js.map +1 -1
  12. package/dist/src/core/audit_log/audit_log.enum.d.ts +2 -0
  13. package/dist/src/core/audit_log/audit_log.enum.js +2 -0
  14. package/dist/src/core/audit_log/audit_log.enum.js.map +1 -1
  15. package/dist/src/core/audit_log/audit_log.service.js +6 -4
  16. package/dist/src/core/audit_log/audit_log.service.js.map +1 -1
  17. package/dist/src/core/auth/auth.controller.d.ts +2 -0
  18. package/dist/src/core/auth/auth.controller.js +22 -1
  19. package/dist/src/core/auth/auth.controller.js.map +1 -1
  20. package/dist/src/core/auth/auth.service.d.ts +2 -0
  21. package/dist/src/core/auth/auth.service.js +188 -2
  22. package/dist/src/core/auth/auth.service.js.map +1 -1
  23. package/dist/src/core/auth/constants.d.ts +2 -0
  24. package/dist/src/core/auth/constants.js +3 -1
  25. package/dist/src/core/auth/constants.js.map +1 -1
  26. package/dist/src/core/auth/guards/jwt_auth.guard.js +9 -1
  27. package/dist/src/core/auth/guards/jwt_auth.guard.js.map +1 -1
  28. package/dist/src/core/auth/providers/auth_exchange.provider.d.ts +12 -0
  29. package/dist/src/core/auth/providers/auth_exchange.provider.js +46 -0
  30. package/dist/src/core/auth/providers/auth_exchange.provider.js.map +1 -0
  31. package/dist/src/core/common/base/base.service.js +22 -8
  32. package/dist/src/core/common/base/base.service.js.map +1 -1
  33. package/dist/src/core/common/constants.d.ts +1 -0
  34. package/dist/src/core/common/constants.js +1 -0
  35. package/dist/src/core/common/constants.js.map +1 -1
  36. package/dist/src/core/common/interceptors/filter.interceptor.js +5 -4
  37. package/dist/src/core/common/interceptors/filter.interceptor.js.map +1 -1
  38. package/dist/src/core/common/utils/build_search.js +48 -33
  39. package/dist/src/core/common/utils/build_search.js.map +1 -1
  40. package/dist/src/core/common/utils/exchange.d.ts +1 -0
  41. package/dist/src/core/common/utils/exchange.js +21 -0
  42. package/dist/src/core/common/utils/exchange.js.map +1 -0
  43. package/dist/src/core/common/utils/global.d.ts +2 -1
  44. package/dist/src/core/common/utils/global.js +12 -1
  45. package/dist/src/core/common/utils/global.js.map +1 -1
  46. package/dist/src/core/common/utils/hook.js +6 -2
  47. package/dist/src/core/common/utils/hook.js.map +1 -1
  48. package/dist/src/core/common/utils/mongo.js +14 -7
  49. package/dist/src/core/common/utils/mongo.js.map +1 -1
  50. package/dist/src/core/common/utils/schema.js +377 -18
  51. package/dist/src/core/common/utils/schema.js.map +1 -1
  52. package/dist/src/core/common/utils/template.d.ts +1 -1
  53. package/dist/src/core/common/utils/template.js +10 -2
  54. package/dist/src/core/common/utils/template.js.map +1 -1
  55. package/dist/src/core/entities/entities.service.js +24 -8
  56. package/dist/src/core/entities/entities.service.js.map +1 -1
  57. package/dist/src/core/setting/setting.service.js +8 -1
  58. package/dist/src/core/setting/setting.service.js.map +1 -1
  59. package/dist/src/core/template/provider/base_template.provider.d.ts +1 -1
  60. package/dist/src/core/template/provider/base_template.provider.js +1 -1
  61. package/dist/src/core/template/provider/base_template.provider.js.map +1 -1
  62. package/dist/src/core/template/provider/template.interface.d.ts +1 -1
  63. package/dist/src/core/template/provider/template_print.provider.d.ts +1 -1
  64. package/dist/src/core/template/provider/template_print.provider.js +6 -3
  65. package/dist/src/core/template/provider/template_print.provider.js.map +1 -1
  66. package/dist/src/core/template/template.controller.d.ts +1 -0
  67. package/dist/src/core/template/template.controller.js +12 -0
  68. package/dist/src/core/template/template.controller.js.map +1 -1
  69. package/dist/src/core/template/template.service.d.ts +1 -0
  70. package/dist/src/core/template/template.service.js +28 -6
  71. package/dist/src/core/template/template.service.js.map +1 -1
  72. package/dist/src/core/translate/translate.json +2 -1
  73. package/dist/src/core/users/user.constant.d.ts +1 -1
  74. package/dist/src/core/users/user.constant.js +1 -1
  75. package/dist/src/core/users/user.constant.js.map +1 -1
  76. package/dist/src/core/users/user.enum.d.ts +2 -1
  77. package/dist/src/core/users/user.enum.js +1 -0
  78. package/dist/src/core/users/user.enum.js.map +1 -1
  79. package/dist/src/core/users/user.service.js +11 -0
  80. package/dist/src/core/users/user.service.js.map +1 -1
  81. package/dist/tsconfig.build.tsbuildinfo +1 -1
  82. package/package.json +1 -1
@@ -799,6 +799,7 @@ const saveRelatedFields = async (entity, reqHeaders, newData, oldData) => {
799
799
  };
800
800
  exports.saveRelatedFields = saveRelatedFields;
801
801
  const saveRelatedFieldsV2 = async (entity, reqHeaders, newData, oldData) => {
802
+ var _a;
802
803
  const relatedFields = listFieldByType(newData, entity, "related");
803
804
  for (const field of relatedFields) {
804
805
  const ignoreSaveRelated = _.get(reqHeaders, "ignore_save_related");
@@ -828,8 +829,19 @@ const saveRelatedFieldsV2 = async (entity, reqHeaders, newData, oldData) => {
828
829
  for (const data of field.data || []) {
829
830
  const isExists = (_.get(oldData, field.name) || [])
830
831
  .filter(j => j)
831
- .find(j => `${j._id || j.id}` === `${data._id || data.id}`);
832
- let id = data._id || data.id;
832
+ .find(j => {
833
+ var _a, _b;
834
+ let jId = j;
835
+ if (_.isPlainObject(j)) {
836
+ jId = j.id || ((_a = j._id) === null || _a === void 0 ? void 0 : _a.toString());
837
+ }
838
+ let dataId = data;
839
+ if (_.isPlainObject(data)) {
840
+ dataId = data.id || ((_b = data._id) === null || _b === void 0 ? void 0 : _b.toString());
841
+ }
842
+ return `${jId}` === `${dataId}`;
843
+ });
844
+ let id = data.id || ((_a = data._id) === null || _a === void 0 ? void 0 : _a.toString());
833
845
  if (id && !isExists) {
834
846
  ids.push(id);
835
847
  continue;
@@ -1579,7 +1591,7 @@ const hashEncryptFields = (entity, newData) => {
1579
1591
  };
1580
1592
  exports.hashEncryptFields = hashEncryptFields;
1581
1593
  const addActivityLog = async (entity, request, data, type, action, status, start_process_time, end_process_time, message, description, newData, oldData) => {
1582
- var _a;
1594
+ var _a, _b;
1583
1595
  if ((entity.name === "activity_log" ||
1584
1596
  _.get(request, "headers.ignore_activity_log") === "true") &&
1585
1597
  type !== activity_log_enum_1.ActivityLogType.workflow) {
@@ -1615,7 +1627,12 @@ const addActivityLog = async (entity, request, data, type, action, status, start
1615
1627
  log["diff"] = diff;
1616
1628
  }
1617
1629
  }
1618
- await callCreateInternalService("activity_log", log, _.get(request, "headers"));
1630
+ if (action === activity_log_enum_1.ActivityLogAction.deleted) {
1631
+ const uniqueFields = (_b = entity === null || entity === void 0 ? void 0 : entity.fields) === null || _b === void 0 ? void 0 : _b.filter(field => field.unique).map(field => field.name);
1632
+ const uniqueData = _.pick(data, uniqueFields);
1633
+ log['raw_data'] = JSON.stringify(uniqueData);
1634
+ }
1635
+ await callCreateInternalService("activity_log", log);
1619
1636
  return true;
1620
1637
  };
1621
1638
  exports.addActivityLog = addActivityLog;
@@ -1804,6 +1821,35 @@ const getCollectionName = (entity) => {
1804
1821
  return entity;
1805
1822
  }
1806
1823
  };
1824
+ const _getObjectIdCastingExpression = (field) => {
1825
+ return {
1826
+ $cond: [
1827
+ { $isArray: field },
1828
+ {
1829
+ $map: {
1830
+ input: field,
1831
+ as: "it",
1832
+ in: {
1833
+ $convert: {
1834
+ input: "$$it",
1835
+ to: "objectId",
1836
+ onError: "$$it",
1837
+ onNull: "$$it",
1838
+ },
1839
+ },
1840
+ },
1841
+ },
1842
+ {
1843
+ $convert: {
1844
+ input: field,
1845
+ to: "objectId",
1846
+ onError: field,
1847
+ onNull: field,
1848
+ },
1849
+ },
1850
+ ],
1851
+ };
1852
+ };
1807
1853
  const transformMediaPipeline = (fieldPath) => {
1808
1854
  const apiDomain = _.get(constants_1.CONST.MEDIA_SETTING, "api_domain");
1809
1855
  const provideCode = _.get(constants_1.CONST.MEDIA_SETTING, "provider");
@@ -2023,6 +2069,228 @@ const _normalizeField = (fields, currentPath = "", ignorePaths = []) => {
2023
2069
  }
2024
2070
  return normalizedFields;
2025
2071
  };
2072
+ const _buildOne2ManySubPipeline = async (entityName, requestedFieldNames, originalRequestedFieldNames, currentPath, level, visitedEntities, customPopulate, maxLevel) => {
2073
+ if (level > maxLevel)
2074
+ return [];
2075
+ const entity = await _loadEntity(entityName);
2076
+ if (!entity || !entity.fields)
2077
+ return [];
2078
+ const relatedFields = ["related", "image", "map", "file"];
2079
+ const needMapEntities = ["image", "map", "file"];
2080
+ const subPipeline = [];
2081
+ subPipeline.push({ $addFields: { id: { $toString: "$_id" } } });
2082
+ subPipeline.push({ $unset: ["_search_text"] });
2083
+ const availableFieldNames = entity.fields
2084
+ .filter(f => relatedFields.includes(f.type))
2085
+ .map(f => f.name);
2086
+ const fieldsToPopulate = customPopulate && requestedFieldNames.length > 0
2087
+ ? getFieldsToPopulate(availableFieldNames, requestedFieldNames)
2088
+ : availableFieldNames;
2089
+ const relateFields = entity.fields.filter(f => relatedFields.includes(f.type) &&
2090
+ (requestedFieldNames.length === 0 || fieldsToPopulate.includes(f.name)));
2091
+ for (const field of relateFields) {
2092
+ const fieldName = field.name;
2093
+ const targetEntityPath = `${currentPath}.${fieldName}.${field.related_ref}`;
2094
+ if (!needMapEntities.includes(field.type) &&
2095
+ visitedEntities.has(targetEntityPath)) {
2096
+ continue;
2097
+ }
2098
+ try {
2099
+ const collectionName = needMapEntities.includes(field.type)
2100
+ ? getCollectionName(mapEntity(field.type))
2101
+ : getCollectionName(field.related_ref);
2102
+ const relatedType = field.related_type || _.get(field, "options.related_type");
2103
+ const isRequestedSpecifically = !customPopulate ||
2104
+ requestedFieldNames.some(f => f === fieldName || f === `${fieldName}.*` || f.startsWith(`${fieldName}.`));
2105
+ if (!isRequestedSpecifically) {
2106
+ continue;
2107
+ }
2108
+ const hasMoreNestedRelationships = originalRequestedFieldNames.some(f => {
2109
+ const prefix = `${currentPath}.${fieldName}`;
2110
+ if (!f.startsWith(prefix + "."))
2111
+ return false;
2112
+ if (f === `${prefix}.*`)
2113
+ return false;
2114
+ const remaining = f.substring(prefix.length + 1);
2115
+ return remaining.includes(".");
2116
+ });
2117
+ if (relatedType === "one2many" && hasMoreNestedRelationships) {
2118
+ const newVisitedEntities = new Set(visitedEntities);
2119
+ newVisitedEntities.add(targetEntityPath);
2120
+ const subFieldNames = originalRequestedFieldNames
2121
+ .filter(f => f.startsWith(`${currentPath}.${fieldName}.`))
2122
+ .map(f => f.substring(`${currentPath}.${fieldName}.`.length));
2123
+ const nestedSubPipeline = await _buildOne2ManySubPipeline(field.related_ref, subFieldNames, originalRequestedFieldNames, `${currentPath}.${fieldName}`, level + 1, newVisitedEntities, customPopulate, maxLevel);
2124
+ subPipeline.push({
2125
+ $lookup: {
2126
+ from: collectionName,
2127
+ let: { local_ids: _getObjectIdCastingExpression(`$${fieldName}`) },
2128
+ pipeline: [
2129
+ {
2130
+ $match: {
2131
+ $expr: {
2132
+ $in: [
2133
+ "$_id",
2134
+ {
2135
+ $cond: [
2136
+ { $isArray: "$$local_ids" },
2137
+ "$$local_ids",
2138
+ { $cond: [{ $ne: ["$$local_ids", null] }, ["$$local_ids"], []] },
2139
+ ],
2140
+ },
2141
+ ],
2142
+ },
2143
+ },
2144
+ },
2145
+ ...nestedSubPipeline,
2146
+ ],
2147
+ as: fieldName,
2148
+ },
2149
+ });
2150
+ }
2151
+ else if (relatedType === "one2one") {
2152
+ subPipeline.push({
2153
+ $addFields: {
2154
+ [fieldName]: _getObjectIdCastingExpression(`$${fieldName}`),
2155
+ },
2156
+ });
2157
+ subPipeline.push({
2158
+ $lookup: {
2159
+ from: collectionName,
2160
+ localField: fieldName,
2161
+ foreignField: "_id",
2162
+ as: fieldName
2163
+ }
2164
+ });
2165
+ subPipeline.push({
2166
+ $addFields: { [fieldName]: { $arrayElemAt: [`$${fieldName}`, 0] } }
2167
+ });
2168
+ subPipeline.push({
2169
+ $addFields: {
2170
+ [fieldName]: {
2171
+ $cond: [
2172
+ { $ne: [{ $ifNull: [`$${fieldName}._id`, null] }, null] },
2173
+ { $mergeObjects: [`$${fieldName}`, { id: { $toString: `$${fieldName}._id` } }] },
2174
+ null
2175
+ ]
2176
+ }
2177
+ }
2178
+ });
2179
+ subPipeline.push({
2180
+ $addFields: {
2181
+ [fieldName]: {
2182
+ $cond: [
2183
+ { $ne: [`$${fieldName}`, null] },
2184
+ { $unsetField: { field: "_search_text", input: `$${fieldName}` } },
2185
+ null
2186
+ ]
2187
+ }
2188
+ }
2189
+ });
2190
+ }
2191
+ else {
2192
+ const fieldSubRequests = requestedFieldNames
2193
+ .filter(f => f.startsWith(`${fieldName}.`) && !f.endsWith(".*"))
2194
+ .map(f => f.substring(`${fieldName}.`.length));
2195
+ const hasEntityWildcard = requestedFieldNames.some(f => f === `${fieldName}.*`);
2196
+ const hasSpecificSubFields = fieldSubRequests.length > 0 && !hasEntityWildcard;
2197
+ if (hasSpecificSubFields) {
2198
+ const projectFields = { _id: 1 };
2199
+ for (const f of fieldSubRequests) {
2200
+ projectFields[f] = 1;
2201
+ }
2202
+ subPipeline.push({
2203
+ $lookup: {
2204
+ from: collectionName,
2205
+ let: { local_ids: _getObjectIdCastingExpression(`$${fieldName}`) },
2206
+ pipeline: [
2207
+ {
2208
+ $match: {
2209
+ $expr: {
2210
+ $in: [
2211
+ "$_id",
2212
+ {
2213
+ $cond: [
2214
+ { $isArray: "$$local_ids" },
2215
+ "$$local_ids",
2216
+ { $cond: [{ $ne: ["$$local_ids", null] }, ["$$local_ids"], []] },
2217
+ ],
2218
+ },
2219
+ ],
2220
+ },
2221
+ },
2222
+ },
2223
+ { $addFields: { id: { $toString: "$_id" } } },
2224
+ { $unset: ["_search_text"] },
2225
+ { $project: Object.assign(Object.assign({}, projectFields), { id: 1 }) },
2226
+ ],
2227
+ as: fieldName,
2228
+ },
2229
+ });
2230
+ }
2231
+ else {
2232
+ subPipeline.push({
2233
+ $addFields: {
2234
+ [fieldName]: _getObjectIdCastingExpression(`$${fieldName}`),
2235
+ },
2236
+ });
2237
+ subPipeline.push({
2238
+ $lookup: {
2239
+ from: collectionName,
2240
+ localField: fieldName,
2241
+ foreignField: "_id",
2242
+ as: fieldName
2243
+ }
2244
+ });
2245
+ subPipeline.push({
2246
+ $addFields: {
2247
+ [fieldName]: {
2248
+ $map: {
2249
+ input: `$${fieldName}`,
2250
+ as: "item",
2251
+ in: { $mergeObjects: ["$$item", { id: { $toString: "$$item._id" } }] }
2252
+ }
2253
+ }
2254
+ }
2255
+ });
2256
+ subPipeline.push({
2257
+ $addFields: {
2258
+ [fieldName]: {
2259
+ $map: {
2260
+ input: `$${fieldName}`,
2261
+ as: "item",
2262
+ in: { $unsetField: { field: "_search_text", input: "$$item" } }
2263
+ }
2264
+ }
2265
+ }
2266
+ });
2267
+ }
2268
+ }
2269
+ }
2270
+ catch (error) {
2271
+ console.error(`Could not load related entity (sub-pipeline): ${field.related_ref} at level ${level}`);
2272
+ }
2273
+ }
2274
+ const hasWildcardAtThisLevel = requestedFieldNames.includes("*") ||
2275
+ requestedFieldNames.some(f => f === "*" || f.startsWith("*."));
2276
+ if (!hasWildcardAtThisLevel && requestedFieldNames.length > 0) {
2277
+ const topLevelFields = [
2278
+ ...new Set(requestedFieldNames
2279
+ .map(f => f.split(".")[0])
2280
+ .filter(f => f && f !== "*"))
2281
+ ];
2282
+ if (topLevelFields.length > 0) {
2283
+ const projectStage = {
2284
+ $project: { _id: 1, id: 1 }
2285
+ };
2286
+ for (const f of topLevelFields) {
2287
+ projectStage.$project[f] = 1;
2288
+ }
2289
+ subPipeline.push(projectStage);
2290
+ }
2291
+ }
2292
+ return subPipeline;
2293
+ };
2026
2294
  const buildNestedLookups = async (pipeline, relateFieldNames, entityName, requestedFieldNames = [], currentPath = "", level = 0, visitedEntities = new Set(), customPopulate = false, originalRequestedFieldNames = [], maxLevel = 1) => {
2027
2295
  if (level > maxLevel)
2028
2296
  return;
@@ -2053,6 +2321,59 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2053
2321
  const collectionName = needMapEntities.includes(field.type)
2054
2322
  ? getCollectionName(mapEntity(field.type))
2055
2323
  : getCollectionName(field.related_ref);
2324
+ const relatedType = field.related_type || _.get(field, "options.related_type");
2325
+ const currentFieldPrefix = currentPath
2326
+ ? `${currentPath}.${field.name}`
2327
+ : field.name;
2328
+ const hasMoreNestedRelationships = originalRequestedFieldNames.some(f => {
2329
+ if (!f.startsWith(currentFieldPrefix + "."))
2330
+ return false;
2331
+ if (f === `${currentFieldPrefix}.*`)
2332
+ return false;
2333
+ const remaining = f.substring(currentFieldPrefix.length + 1);
2334
+ return remaining.includes(".");
2335
+ });
2336
+ if (relatedType === "one2many" && hasMoreNestedRelationships) {
2337
+ const newVisitedEntities = new Set(visitedEntities);
2338
+ newVisitedEntities.add(targetEntityPath);
2339
+ const subFieldNames = originalRequestedFieldNames
2340
+ .filter(f => f.startsWith(currentFieldPrefix + "."))
2341
+ .map(f => f.substring(currentFieldPrefix.length + 1));
2342
+ const clonedOriginalRequestedFieldNames = _.cloneDeep(originalRequestedFieldNames);
2343
+ const subPipeline = await _buildOne2ManySubPipeline(field.related_ref, subFieldNames, clonedOriginalRequestedFieldNames, currentFieldPrefix, level + 1, newVisitedEntities, customPopulate, maxLevel);
2344
+ pipeline.push({
2345
+ $lookup: {
2346
+ from: collectionName,
2347
+ let: { local_ids: _getObjectIdCastingExpression(`$${fieldPath}`) },
2348
+ pipeline: [
2349
+ {
2350
+ $match: {
2351
+ $expr: {
2352
+ $in: [
2353
+ "$_id",
2354
+ {
2355
+ $cond: [
2356
+ { $isArray: "$$local_ids" },
2357
+ "$$local_ids",
2358
+ { $cond: [{ $ne: ["$$local_ids", null] }, ["$$local_ids"], []] },
2359
+ ],
2360
+ },
2361
+ ],
2362
+ },
2363
+ },
2364
+ },
2365
+ ...subPipeline,
2366
+ ],
2367
+ as: fieldPath,
2368
+ },
2369
+ });
2370
+ continue;
2371
+ }
2372
+ pipeline.push({
2373
+ $addFields: {
2374
+ [fieldPath]: _getObjectIdCastingExpression(`$${fieldPath}`),
2375
+ },
2376
+ });
2056
2377
  pipeline.push({
2057
2378
  $lookup: {
2058
2379
  from: collectionName,
@@ -2061,7 +2382,6 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2061
2382
  as: fieldPath
2062
2383
  }
2063
2384
  });
2064
- const relatedType = field.related_type || _.get(field, "options.related_type");
2065
2385
  if (relatedType === "one2one") {
2066
2386
  pipeline.push({
2067
2387
  $addFields: { [fieldPath]: { $arrayElemAt: [`$${fieldPath}`, 0] } }
@@ -2198,19 +2518,7 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2198
2518
  }
2199
2519
  });
2200
2520
  }
2201
- const currentFieldPrefix = currentPath
2202
- ? `${currentPath}.${field.name}`
2203
- : field.name;
2204
- const hasMoreNestedRelationships = originalRequestedFieldNames.some(f => {
2205
- if (!f.startsWith(currentFieldPrefix + "."))
2206
- return false;
2207
- if (f === `${currentFieldPrefix}.*`)
2208
- return false;
2209
- const remaining = f.substring(currentFieldPrefix.length + 1);
2210
- const result = remaining.includes(".");
2211
- return result;
2212
- });
2213
- if (hasMoreNestedRelationships) {
2521
+ if (relatedType === "one2one" && hasMoreNestedRelationships) {
2214
2522
  const newVisitedEntities = new Set(visitedEntities);
2215
2523
  newVisitedEntities.add(targetEntityPath);
2216
2524
  const subFieldNames = originalRequestedFieldNames
@@ -2313,12 +2621,60 @@ const buildPopulate = async (doc, entityName, fields) => {
2313
2621
  }
2314
2622
  };
2315
2623
  exports.buildPopulate = buildPopulate;
2624
+ const _splitFilter = (query) => {
2625
+ const local = {};
2626
+ const nested = {};
2627
+ if (!_.isObject(query) || _.isArray(query))
2628
+ return { local: query, nested: {} };
2629
+ for (const key of Object.keys(query)) {
2630
+ if (key.startsWith("$") && ["$and", "$or", "$nor"].includes(key) && Array.isArray(query[key])) {
2631
+ const localParts = [], nestedParts = [];
2632
+ for (const part of query[key]) {
2633
+ const { local: lp, nested: np } = _splitFilter(part);
2634
+ if (!_.isEmpty(lp))
2635
+ localParts.push(lp);
2636
+ if (!_.isEmpty(np))
2637
+ nestedParts.push(np);
2638
+ }
2639
+ if (localParts.length > 0)
2640
+ local[key] = localParts;
2641
+ if (nestedParts.length > 0)
2642
+ nested[key] = nestedParts;
2643
+ }
2644
+ else if (key.includes(".")) {
2645
+ nested[key] = query[key];
2646
+ }
2647
+ else {
2648
+ local[key] = query[key];
2649
+ }
2650
+ }
2651
+ return { local, nested };
2652
+ };
2316
2653
  const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize = 10000, sort = "") => {
2317
2654
  let cloneAggregate = _.cloneDeep(aggregate);
2318
2655
  const customPopulate = _.isArray(fields) && fields.length > 0 && _.isString(fields[0]);
2319
2656
  if (_.isPlainObject(cloneAggregate)) {
2320
2657
  cloneAggregate = [cloneAggregate];
2321
2658
  }
2659
+ const nestedStages = [];
2660
+ if (Array.isArray(cloneAggregate)) {
2661
+ for (let i = 0; i < cloneAggregate.length; i++) {
2662
+ const stage = cloneAggregate[i];
2663
+ if (stage === null || stage === void 0 ? void 0 : stage.$match) {
2664
+ const { local, nested } = _splitFilter(stage.$match);
2665
+ if (!_.isEmpty(nested)) {
2666
+ nestedStages.push({ $match: nested });
2667
+ if (_.isEmpty(local)) {
2668
+ cloneAggregate.splice(i, 1);
2669
+ i--;
2670
+ }
2671
+ else {
2672
+ cloneAggregate[i] = { $match: local };
2673
+ }
2674
+ }
2675
+ }
2676
+ }
2677
+ }
2322
2678
  let normalizedFields = [];
2323
2679
  let includedFields = [];
2324
2680
  let excludedFields = [];
@@ -2487,6 +2843,9 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2487
2843
  cloneAggregate.push({
2488
2844
  $unset: ["_search_text"]
2489
2845
  });
2846
+ if (!_.isEmpty(nestedStages)) {
2847
+ cloneAggregate.push(...nestedStages);
2848
+ }
2490
2849
  return cloneAggregate;
2491
2850
  };
2492
2851
  exports.buildAggregate = buildAggregate;