@resolveio/server-lib 22.2.10 → 22.2.12

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.
@@ -471,7 +471,12 @@ function loadReportBuilderMethods(methodManager) {
471
471
  var _a, _b;
472
472
  fieldPath += fieldData[i];
473
473
  if (filterField.lookup_collection || fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup')) {
474
- (0, report_builder_unwinds_1.ensureLookupLocalKeyUnwinds)({ query: query_1, sizes: sizes_1, lookupLocalKey: filterField.lookup_local_key || '' });
474
+ (0, report_builder_unwinds_1.ensureLookupLocalKeyUnwinds)({
475
+ query: query_1,
476
+ sizes: sizes_1,
477
+ lookupLocalKey: filterField.lookup_local_key || '',
478
+ lookupAs: filterField.lookup_as || ''
479
+ });
475
480
  var lookupAs_1 = filterField.lookup_as.replace(/\.\$/g, '');
476
481
  if (!query_1.some(function (a) { return a.$lookup && a.$lookup.as === lookupAs_1; })) {
477
482
  query_1.push({ $lookup: {
@@ -515,7 +520,12 @@ function loadReportBuilderMethods(methodManager) {
515
520
  var lastSegment = fieldPath_1.split('.$')[fieldPath_1.split('.$').length - 1];
516
521
  var hasLookup = !!((_a = row.treeItem) === null || _a === void 0 ? void 0 : _a.lookup_collection) || (lastSegment || '').includes('(Lookup');
517
522
  if (hasLookup && ((_b = row.treeItem) === null || _b === void 0 ? void 0 : _b.lookup_collection) && ((_c = row.treeItem) === null || _c === void 0 ? void 0 : _c.lookup_as)) {
518
- (0, report_builder_unwinds_1.ensureLookupLocalKeyUnwinds)({ query: query_1, sizes: sizes_1, lookupLocalKey: row.treeItem.lookup_local_key || '' });
523
+ (0, report_builder_unwinds_1.ensureLookupLocalKeyUnwinds)({
524
+ query: query_1,
525
+ sizes: sizes_1,
526
+ lookupLocalKey: row.treeItem.lookup_local_key || '',
527
+ lookupAs: row.treeItem.lookup_as || ''
528
+ });
519
529
  var lookupAsRaw = row.treeItem.lookup_as || '';
520
530
  var lookupAs_2 = lookupAsRaw.replace(/\.\$/g, '');
521
531
  if (!query_1.some(function (a) { return a.$lookup && a.$lookup.as === lookupAs_2; })) {
@@ -926,10 +936,10 @@ function loadReportBuilderMethods(methodManager) {
926
936
  return;
927
937
  }
928
938
  if (field.leafValueType === 'Average') {
929
- queryProjection_1[field.id] = { $avg: '$' + fieldPath.replace(/\.\$/g, '') };
939
+ queryProjection_1[field.id] = buildArrayAwareAverageFieldExpr(fieldPath);
930
940
  }
931
941
  else if (field.leafValueType === 'Sum') {
932
- queryProjection_1[field.id] = { $sum: '$' + fieldPath.replace(/\.\$/g, '') };
942
+ queryProjection_1[field.id] = buildArrayAwareNumericFieldExpr(fieldPath);
933
943
  }
934
944
  else if (field.leafValueType === 'Count') {
935
945
  queryProjection_1[field.id] = { $sum: 1 };
@@ -969,7 +979,16 @@ function loadReportBuilderMethods(methodManager) {
969
979
  fieldsTotal.forEach(function (total) {
970
980
  selectedFields.filter(function (a) { return a.fieldType === 'Number' || a.leafValueType === 'Count'; }).forEach(function (field) {
971
981
  var _a;
972
- queryProjection_1[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + total.id + '_' + field.id, _a);
982
+ var totalFieldPath = total.id + '_' + field.id;
983
+ if (total.type === 'sum') {
984
+ queryProjection_1[totalFieldPath] = buildArrayAwareNumericFieldExpr(totalFieldPath);
985
+ }
986
+ else if (total.type === 'avg') {
987
+ queryProjection_1[totalFieldPath] = buildArrayAwareAverageFieldExpr(totalFieldPath);
988
+ }
989
+ else {
990
+ queryProjection_1[totalFieldPath] = (_a = {}, _a['$' + total.type] = '$' + totalFieldPath, _a);
991
+ }
973
992
  });
974
993
  customFields.forEach(function (field) {
975
994
  var _a;
@@ -1191,6 +1210,26 @@ function buildArrayAwareNumericFieldExpr(fieldPath) {
1191
1210
  ]
1192
1211
  };
1193
1212
  }
1213
+ function buildArrayAwareAverageFieldExpr(fieldPath) {
1214
+ var normalizedFieldPath = (fieldPath || '').replace(/\.\$/g, '');
1215
+ if (!normalizedFieldPath) {
1216
+ return 0;
1217
+ }
1218
+ var fieldExpr = '$' + normalizedFieldPath;
1219
+ return {
1220
+ $cond: [
1221
+ { $isArray: fieldExpr },
1222
+ {
1223
+ $cond: [
1224
+ { $gt: [{ $size: { $ifNull: [fieldExpr, []] } }, 0] },
1225
+ { $avg: { $ifNull: [fieldExpr, []] } },
1226
+ 0
1227
+ ]
1228
+ },
1229
+ { $ifNull: [fieldExpr, 0] }
1230
+ ]
1231
+ };
1232
+ }
1194
1233
  function appendSafeSort(query, sortSpec, selectedFields, customFields) {
1195
1234
  if (selectedFields === void 0) { selectedFields = []; }
1196
1235
  if (customFields === void 0) { customFields = []; }
@@ -1453,10 +1492,10 @@ function buildCollectionPipeline(collectionName, alias, selectedFields, filters,
1453
1492
  .forEach(function (f) {
1454
1493
  var mappedPath = stripAliasFromPath(f.fieldPath || '', targetAlias || f.lookup_as || '');
1455
1494
  if (f.leafValueType === 'Average') {
1456
- projection[f.id] = { $avg: '$' + mappedPath };
1495
+ projection[f.id] = buildArrayAwareAverageFieldExpr(mappedPath);
1457
1496
  }
1458
1497
  else if (f.leafValueType === 'Sum') {
1459
- projection[f.id] = { $sum: '$' + mappedPath };
1498
+ projection[f.id] = buildArrayAwareNumericFieldExpr(mappedPath);
1460
1499
  }
1461
1500
  else if (f.leafValueType === 'Count') {
1462
1501
  projection[f.id] = { $sum: 1 };