acomm-core-lib 2.6.7 → 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 (132) hide show
  1. package/dist/package.json +4 -1
  2. package/dist/src/core/activity_log/activity_log.schema.d.ts +2 -1
  3. package/dist/src/core/activity_log/activity_log.schema.js +4 -0
  4. package/dist/src/core/activity_log/activity_log.schema.js.map +1 -1
  5. package/dist/src/core/activity_log/activity_log.service.js +5 -0
  6. package/dist/src/core/activity_log/activity_log.service.js.map +1 -1
  7. package/dist/src/core/address/address.schema.d.ts +6 -6
  8. package/dist/src/core/api_key/api_key.layout.js +22 -0
  9. package/dist/src/core/api_key/api_key.layout.js.map +1 -1
  10. package/dist/src/core/api_key/api_key.schema.d.ts +5 -1
  11. package/dist/src/core/api_key/api_key.schema.js +21 -0
  12. package/dist/src/core/api_key/api_key.schema.js.map +1 -1
  13. package/dist/src/core/api_key/api_key.service.js.map +1 -1
  14. package/dist/src/core/attachment/attachment.schema.d.ts +1 -1
  15. package/dist/src/core/audit_log/audit_log.enum.d.ts +2 -0
  16. package/dist/src/core/audit_log/audit_log.enum.js +2 -0
  17. package/dist/src/core/audit_log/audit_log.enum.js.map +1 -1
  18. package/dist/src/core/audit_log/audit_log.schema.d.ts +1 -1
  19. package/dist/src/core/audit_log/audit_log.service.js +6 -4
  20. package/dist/src/core/audit_log/audit_log.service.js.map +1 -1
  21. package/dist/src/core/auth/auth.controller.d.ts +2 -0
  22. package/dist/src/core/auth/auth.controller.js +22 -1
  23. package/dist/src/core/auth/auth.controller.js.map +1 -1
  24. package/dist/src/core/auth/auth.service.d.ts +2 -0
  25. package/dist/src/core/auth/auth.service.js +226 -7
  26. package/dist/src/core/auth/auth.service.js.map +1 -1
  27. package/dist/src/core/auth/constants.d.ts +2 -0
  28. package/dist/src/core/auth/constants.js +3 -1
  29. package/dist/src/core/auth/constants.js.map +1 -1
  30. package/dist/src/core/auth/guards/jwt_auth.guard.js +9 -1
  31. package/dist/src/core/auth/guards/jwt_auth.guard.js.map +1 -1
  32. package/dist/src/core/auth/providers/auth_exchange.provider.d.ts +12 -0
  33. package/dist/src/core/auth/providers/auth_exchange.provider.js +46 -0
  34. package/dist/src/core/auth/providers/auth_exchange.provider.js.map +1 -0
  35. package/dist/src/core/comment_data/comment_data.schema.d.ts +1 -1
  36. package/dist/src/core/common/base/base.service.js +22 -8
  37. package/dist/src/core/common/base/base.service.js.map +1 -1
  38. package/dist/src/core/common/constants.d.ts +3 -1
  39. package/dist/src/core/common/constants.js +4 -1
  40. package/dist/src/core/common/constants.js.map +1 -1
  41. package/dist/src/core/common/interceptors/filter.interceptor.js +5 -4
  42. package/dist/src/core/common/interceptors/filter.interceptor.js.map +1 -1
  43. package/dist/src/core/common/utils/build_search.js +118 -66
  44. package/dist/src/core/common/utils/build_search.js.map +1 -1
  45. package/dist/src/core/common/utils/exchange.d.ts +1 -0
  46. package/dist/src/core/common/utils/exchange.js +21 -0
  47. package/dist/src/core/common/utils/exchange.js.map +1 -0
  48. package/dist/src/core/common/utils/global.d.ts +2 -1
  49. package/dist/src/core/common/utils/global.js +12 -1
  50. package/dist/src/core/common/utils/global.js.map +1 -1
  51. package/dist/src/core/common/utils/hook.js +6 -2
  52. package/dist/src/core/common/utils/hook.js.map +1 -1
  53. package/dist/src/core/common/utils/mongo.js +14 -7
  54. package/dist/src/core/common/utils/mongo.js.map +1 -1
  55. package/dist/src/core/common/utils/schema.js +392 -31
  56. package/dist/src/core/common/utils/schema.js.map +1 -1
  57. package/dist/src/core/common/utils/template.d.ts +1 -1
  58. package/dist/src/core/common/utils/template.js +10 -2
  59. package/dist/src/core/common/utils/template.js.map +1 -1
  60. package/dist/src/core/company/company.schema.d.ts +1 -1
  61. package/dist/src/core/currency/currency.schema.d.ts +1 -1
  62. package/dist/src/core/custom_function/custom_function.schema.d.ts +1 -1
  63. package/dist/src/core/deploy/deploy.schema.d.ts +2 -2
  64. package/dist/src/core/deploy/log/deploy_log.schema.d.ts +1 -1
  65. package/dist/src/core/entities/entities.service.d.ts +1 -1
  66. package/dist/src/core/entities/entities.service.js +133 -17
  67. package/dist/src/core/entities/entities.service.js.map +1 -1
  68. package/dist/src/core/entities/entity.schema.d.ts +3 -1
  69. package/dist/src/core/entities/entity.schema.js +10 -0
  70. package/dist/src/core/entities/entity.schema.js.map +1 -1
  71. package/dist/src/core/execution_log/execution_log.schema.d.ts +1 -1
  72. package/dist/src/core/import_export/import_export.schema.d.ts +1 -1
  73. package/dist/src/core/import_export/log/import_export_log.schema.d.ts +1 -1
  74. package/dist/src/core/language/language.schema.d.ts +1 -1
  75. package/dist/src/core/layout/share_widget/share_widget.schema.d.ts +1 -1
  76. package/dist/src/core/mail/log/mail_log.schema.d.ts +1 -1
  77. package/dist/src/core/mail/mail.schema.d.ts +1 -1
  78. package/dist/src/core/map_location/map_location.schema.d.ts +1 -1
  79. package/dist/src/core/notification/log/log.schema.d.ts +1 -1
  80. package/dist/src/core/notification/message/message.schema.d.ts +1 -1
  81. package/dist/src/core/notification/message_queue/message_queue.schema.d.ts +1 -1
  82. package/dist/src/core/notification/subscription /subscription.schema.d.ts +1 -1
  83. package/dist/src/core/pipeline/job/job.schema.d.ts +1 -1
  84. package/dist/src/core/pipeline/log/log.schema.d.ts +1 -1
  85. package/dist/src/core/pipeline/pipeline.schema.d.ts +1 -1
  86. package/dist/src/core/reason/reason.schema.d.ts +3 -3
  87. package/dist/src/core/schedule_action/log/schedule_log.schema.d.ts +1 -1
  88. package/dist/src/core/schedule_action/schedule_action.schema.d.ts +1 -1
  89. package/dist/src/core/search_text/reindex_search_text/reindex_search_text.schema.d.ts +1 -1
  90. package/dist/src/core/search_text/search_text.controller.d.ts +8 -1
  91. package/dist/src/core/search_text/search_text.controller.js +9 -0
  92. package/dist/src/core/search_text/search_text.controller.js.map +1 -1
  93. package/dist/src/core/search_text/search_text.schema.d.ts +1 -1
  94. package/dist/src/core/search_text/search_text.service.d.ts +9 -1
  95. package/dist/src/core/search_text/search_text.service.js +52 -0
  96. package/dist/src/core/search_text/search_text.service.js.map +1 -1
  97. package/dist/src/core/setting/setting.schema.d.ts +1 -1
  98. package/dist/src/core/setting/setting.service.js +8 -1
  99. package/dist/src/core/setting/setting.service.js.map +1 -1
  100. package/dist/src/core/shortly/url/url.schema.d.ts +1 -1
  101. package/dist/src/core/sms/log/sms_log.schema.d.ts +1 -1
  102. package/dist/src/core/sms/sms.schema.d.ts +1 -1
  103. package/dist/src/core/template/log/print_log.schema.d.ts +1 -1
  104. package/dist/src/core/template/provider/base_template.provider.d.ts +1 -1
  105. package/dist/src/core/template/provider/base_template.provider.js +1 -1
  106. package/dist/src/core/template/provider/base_template.provider.js.map +1 -1
  107. package/dist/src/core/template/provider/template.interface.d.ts +1 -1
  108. package/dist/src/core/template/provider/template_print.provider.d.ts +1 -1
  109. package/dist/src/core/template/provider/template_print.provider.js +6 -3
  110. package/dist/src/core/template/provider/template_print.provider.js.map +1 -1
  111. package/dist/src/core/template/template.controller.d.ts +1 -0
  112. package/dist/src/core/template/template.controller.js +12 -0
  113. package/dist/src/core/template/template.controller.js.map +1 -1
  114. package/dist/src/core/template/template.schema.d.ts +1 -1
  115. package/dist/src/core/template/template.service.d.ts +1 -0
  116. package/dist/src/core/template/template.service.js +28 -6
  117. package/dist/src/core/template/template.service.js.map +1 -1
  118. package/dist/src/core/translate/translate.json +2 -1
  119. package/dist/src/core/translate/translate.schema.d.ts +1 -1
  120. package/dist/src/core/users/user.constant.d.ts +1 -1
  121. package/dist/src/core/users/user.constant.js +1 -1
  122. package/dist/src/core/users/user.constant.js.map +1 -1
  123. package/dist/src/core/users/user.enum.d.ts +2 -1
  124. package/dist/src/core/users/user.enum.js +1 -0
  125. package/dist/src/core/users/user.enum.js.map +1 -1
  126. package/dist/src/core/users/user.schema.d.ts +4 -4
  127. package/dist/src/core/users/user.service.js +11 -0
  128. package/dist/src/core/users/user.service.js.map +1 -1
  129. package/dist/src/core/version_data/version_data.schema.d.ts +1 -1
  130. package/dist/src/core/widget/widget.schema.d.ts +1 -1
  131. package/dist/tsconfig.build.tsbuildinfo +1 -1
  132. package/package.json +5 -2
@@ -688,7 +688,7 @@ const listFieldByType = (data, entity, type, extra) => {
688
688
  }
689
689
  }
690
690
  if (extra) {
691
- const componentImage = _.get(data, `${extra}`, []);
691
+ const componentImage = _.get(data, `${extra}`) || [];
692
692
  if (!_.isEmpty(componentImage)) {
693
693
  for (const component of componentImage) {
694
694
  const name = _.get(component, "key", "");
@@ -749,7 +749,7 @@ const saveRelatedFields = async (entity, reqHeaders, newData, oldData) => {
749
749
  if (_.get(field, "options.related_type") === "one2many" && field.data) {
750
750
  const ids = [];
751
751
  for (const data of field.data || []) {
752
- const isExists = _.get(oldData, field.name, [])
752
+ const isExists = (_.get(oldData, field.name) || [])
753
753
  .filter(j => j)
754
754
  .find(j => `${j._id || j.id}` === `${data._id || data.id}`);
755
755
  let id = data._id || data.id;
@@ -770,7 +770,7 @@ const saveRelatedFields = async (entity, reqHeaders, newData, oldData) => {
770
770
  ids.push(id);
771
771
  }
772
772
  newData[field.name] = ids;
773
- const removedList = _.get(oldData, field.name, [])
773
+ const removedList = (_.get(oldData, field.name) || [])
774
774
  .filter(Boolean)
775
775
  .filter(i => {
776
776
  const isExists = (field.data || [])
@@ -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");
@@ -826,10 +827,21 @@ const saveRelatedFieldsV2 = async (entity, reqHeaders, newData, oldData) => {
826
827
  const updateList = [];
827
828
  const createList = [];
828
829
  for (const data of field.data || []) {
829
- const isExists = _.get(oldData, field.name, [])
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;
@@ -856,7 +868,7 @@ const saveRelatedFieldsV2 = async (entity, reqHeaders, newData, oldData) => {
856
868
  ids = [...ids, ...resIds];
857
869
  }
858
870
  newData[field.name] = ids;
859
- const removedList = _.get(oldData, field.name, [])
871
+ const removedList = (_.get(oldData, field.name) || [])
860
872
  .filter(Boolean)
861
873
  .filter(i => {
862
874
  var _a;
@@ -922,7 +934,7 @@ const saveImageFields = async (fields, reqHeaders, data, entity, system) => {
922
934
  let deleted = [], updated = [];
923
935
  const result = {};
924
936
  for (const field of fields) {
925
- const oldDatas = _.get(data, `${field.name}`, []);
937
+ const oldDatas = _.get(data, `${field.name}`) || [];
926
938
  const oldData = _.cloneDeep(oldDatas || []);
927
939
  if (!_.isArray(field.data))
928
940
  continue;
@@ -954,7 +966,7 @@ const saveImageFields = async (fields, reqHeaders, data, entity, system) => {
954
966
  }, reqHeaders);
955
967
  if (isComponent) {
956
968
  const transformDataImage = await transformImageComponents(res);
957
- let value = _.get(data, `${field.name}`, []);
969
+ let value = _.get(data, `${field.name}`) || [];
958
970
  delete data[`${field.name}`];
959
971
  if (!_.isArray(value))
960
972
  value = [];
@@ -968,7 +980,7 @@ const saveImageFields = async (fields, reqHeaders, data, entity, system) => {
968
980
  else {
969
981
  if (isComponent) {
970
982
  if (!_.isEmpty(img)) {
971
- let value = _.get(data, `${field.name}`, []);
983
+ let value = _.get(data, `${field.name}`) || [];
972
984
  delete data[`${field.name}`];
973
985
  if (!_.isArray(value))
974
986
  value = [];
@@ -1026,7 +1038,7 @@ const saveImageFields = async (fields, reqHeaders, data, entity, system) => {
1026
1038
  }
1027
1039
  }
1028
1040
  if (_.isArray(field.data)) {
1029
- const dataNew = _.get(result, `${field.name}`, []);
1041
+ const dataNew = _.get(result, `${field.name}`) || [];
1030
1042
  const diff = (0, attachment_1.diffData)(oldData || [], dataNew || []);
1031
1043
  deleted = [...deleted, ...diff.deleted];
1032
1044
  updated = [...updated, ...diff.need_update];
@@ -1078,7 +1090,7 @@ const saveFileFields = async (fields, reqHeaders, data, entity) => {
1078
1090
  }, reqHeaders);
1079
1091
  if (isComponent) {
1080
1092
  const dataFile = await transformImageComponents(res);
1081
- let value = _.get(data, `${field.name}`, []);
1093
+ let value = _.get(data, `${field.name}`) || [];
1082
1094
  delete data[`${field.name}`];
1083
1095
  if (!_.isArray(value))
1084
1096
  value = [];
@@ -1092,7 +1104,7 @@ const saveFileFields = async (fields, reqHeaders, data, entity) => {
1092
1104
  else {
1093
1105
  if (isComponent) {
1094
1106
  if (!_.isEmpty(file)) {
1095
- let value = _.get(data, `${field.name}`, []);
1107
+ let value = _.get(data, `${field.name}`) || [];
1096
1108
  delete data[`${field.name}`];
1097
1109
  if (!_.isArray(value))
1098
1110
  value = [];
@@ -1565,6 +1577,8 @@ const saveEncryptFields = async (entity, newData, oldData) => {
1565
1577
  };
1566
1578
  exports.saveEncryptFields = saveEncryptFields;
1567
1579
  const hashEncryptFields = (entity, newData) => {
1580
+ if (_.isEmpty(newData))
1581
+ return newData;
1568
1582
  const fields = listEncryptFields(entity, newData);
1569
1583
  for (const field of fields) {
1570
1584
  newData[field.name] = _.isPlainObject(_.get(newData, `_encrypt.${field.name}`)) ?
@@ -1577,7 +1591,7 @@ const hashEncryptFields = (entity, newData) => {
1577
1591
  };
1578
1592
  exports.hashEncryptFields = hashEncryptFields;
1579
1593
  const addActivityLog = async (entity, request, data, type, action, status, start_process_time, end_process_time, message, description, newData, oldData) => {
1580
- var _a;
1594
+ var _a, _b;
1581
1595
  if ((entity.name === "activity_log" ||
1582
1596
  _.get(request, "headers.ignore_activity_log") === "true") &&
1583
1597
  type !== activity_log_enum_1.ActivityLogType.workflow) {
@@ -1613,7 +1627,12 @@ const addActivityLog = async (entity, request, data, type, action, status, start
1613
1627
  log["diff"] = diff;
1614
1628
  }
1615
1629
  }
1616
- 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);
1617
1636
  return true;
1618
1637
  };
1619
1638
  exports.addActivityLog = addActivityLog;
@@ -1802,6 +1821,35 @@ const getCollectionName = (entity) => {
1802
1821
  return entity;
1803
1822
  }
1804
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
+ };
1805
1853
  const transformMediaPipeline = (fieldPath) => {
1806
1854
  const apiDomain = _.get(constants_1.CONST.MEDIA_SETTING, "api_domain");
1807
1855
  const provideCode = _.get(constants_1.CONST.MEDIA_SETTING, "provider");
@@ -2021,6 +2069,228 @@ const _normalizeField = (fields, currentPath = "", ignorePaths = []) => {
2021
2069
  }
2022
2070
  return normalizedFields;
2023
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
+ };
2024
2294
  const buildNestedLookups = async (pipeline, relateFieldNames, entityName, requestedFieldNames = [], currentPath = "", level = 0, visitedEntities = new Set(), customPopulate = false, originalRequestedFieldNames = [], maxLevel = 1) => {
2025
2295
  if (level > maxLevel)
2026
2296
  return;
@@ -2051,6 +2321,59 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2051
2321
  const collectionName = needMapEntities.includes(field.type)
2052
2322
  ? getCollectionName(mapEntity(field.type))
2053
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
+ });
2054
2377
  pipeline.push({
2055
2378
  $lookup: {
2056
2379
  from: collectionName,
@@ -2059,7 +2382,6 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2059
2382
  as: fieldPath
2060
2383
  }
2061
2384
  });
2062
- const relatedType = field.related_type || _.get(field, "options.related_type");
2063
2385
  if (relatedType === "one2one") {
2064
2386
  pipeline.push({
2065
2387
  $addFields: { [fieldPath]: { $arrayElemAt: [`$${fieldPath}`, 0] } }
@@ -2196,19 +2518,7 @@ const buildNestedLookups = async (pipeline, relateFieldNames, entityName, reques
2196
2518
  }
2197
2519
  });
2198
2520
  }
2199
- const currentFieldPrefix = currentPath
2200
- ? `${currentPath}.${field.name}`
2201
- : field.name;
2202
- const hasMoreNestedRelationships = originalRequestedFieldNames.some(f => {
2203
- if (!f.startsWith(currentFieldPrefix + "."))
2204
- return false;
2205
- if (f === `${currentFieldPrefix}.*`)
2206
- return false;
2207
- const remaining = f.substring(currentFieldPrefix.length + 1);
2208
- const result = remaining.includes(".");
2209
- return result;
2210
- });
2211
- if (hasMoreNestedRelationships) {
2521
+ if (relatedType === "one2one" && hasMoreNestedRelationships) {
2212
2522
  const newVisitedEntities = new Set(visitedEntities);
2213
2523
  newVisitedEntities.add(targetEntityPath);
2214
2524
  const subFieldNames = originalRequestedFieldNames
@@ -2311,12 +2621,60 @@ const buildPopulate = async (doc, entityName, fields) => {
2311
2621
  }
2312
2622
  };
2313
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
+ };
2314
2653
  const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize = 10000, sort = "") => {
2315
2654
  let cloneAggregate = _.cloneDeep(aggregate);
2316
2655
  const customPopulate = _.isArray(fields) && fields.length > 0 && _.isString(fields[0]);
2317
2656
  if (_.isPlainObject(cloneAggregate)) {
2318
2657
  cloneAggregate = [cloneAggregate];
2319
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
+ }
2320
2678
  let normalizedFields = [];
2321
2679
  let includedFields = [];
2322
2680
  let excludedFields = [];
@@ -2485,6 +2843,9 @@ const buildAggregate = async (aggregate, entityName, fields, page = 1, pageSize
2485
2843
  cloneAggregate.push({
2486
2844
  $unset: ["_search_text"]
2487
2845
  });
2846
+ if (!_.isEmpty(nestedStages)) {
2847
+ cloneAggregate.push(...nestedStages);
2848
+ }
2488
2849
  return cloneAggregate;
2489
2850
  };
2490
2851
  exports.buildAggregate = buildAggregate;
@@ -2685,8 +3046,8 @@ const getWorkflowLogMessage = (entity, request, currentState, targetState) => {
2685
3046
  const workflowField = entity.fields.find(f => f.name === entity.workflows.field);
2686
3047
  const lang = _.get(request, "headers.accept-language", "en-US");
2687
3048
  const workFlowTranslate = _.get(entity, `workflows._translate.${lang}.options_values`, []);
2688
- const fromIndex = _.get(workflowField, "values", []).findIndex(val => val === currentState);
2689
- const toIndex = _.get(workflowField, "values", []).findIndex(val => val === targetState);
3049
+ const fromIndex = (_.get(workflowField, "values") || []).findIndex(val => val === currentState);
3050
+ const toIndex = (_.get(workflowField, "values") || []).findIndex(val => val === targetState);
2690
3051
  const from = workFlowTranslate[fromIndex] || currentState;
2691
3052
  const to = workFlowTranslate[toIndex] || targetState;
2692
3053
  const message = `<span>Changed status to <strong>${to}</strong></span>`;