@resolveio/server-lib 22.2.9 → 22.2.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.
@@ -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; })) {
@@ -584,7 +594,7 @@ function loadReportBuilderMethods(methodManager) {
584
594
  }
585
595
  else {
586
596
  divFields_1.$addFields[total.id + '_' + field.id] = {
587
- $divide: ['$' + fieldPath.replace(/\.\$/g, ''), { $multiply: multFields }]
597
+ $divide: [buildArrayAwareNumericFieldExpr(fieldPath), { $multiply: multFields }]
588
598
  };
589
599
  }
590
600
  }
@@ -593,7 +603,7 @@ function loadReportBuilderMethods(methodManager) {
593
603
  divFields_1.$addFields[total.id + '_' + field.id] = '$count';
594
604
  }
595
605
  else {
596
- divFields_1.$addFields[total.id + '_' + field.id] = '$' + fieldPath.replace(/\.\$/g, '');
606
+ divFields_1.$addFields[total.id + '_' + field.id] = buildArrayAwareNumericFieldExpr(fieldPath);
597
607
  }
598
608
  }
599
609
  });
@@ -609,7 +619,7 @@ function loadReportBuilderMethods(methodManager) {
609
619
  });
610
620
  if (multFields.length) {
611
621
  divFields_1.$addFields[fieldPath.replace(/\.\$/g, '')] = {
612
- $divide: ['$' + fieldPath.replace(/\.\$/g, ''), { $multiply: multFields }]
622
+ $divide: [buildArrayAwareNumericFieldExpr(fieldPath), { $multiply: multFields }]
613
623
  };
614
624
  }
615
625
  });
@@ -644,7 +654,7 @@ function loadReportBuilderMethods(methodManager) {
644
654
  if (!fieldPath && field.leafValueType !== 'Count') {
645
655
  return;
646
656
  }
647
- divFields_1.$addFields[total.id + '_' + field.id] = '$' + fieldPath.replace(/\.\$/g, '');
657
+ divFields_1.$addFields[total.id + '_' + field.id] = buildArrayAwareNumericFieldExpr(fieldPath);
648
658
  });
649
659
  });
650
660
  if (Object.keys(divFields_1.$addFields).length > 0) {
@@ -843,7 +853,7 @@ function loadReportBuilderMethods(methodManager) {
843
853
  queryGroup_1[field.id] = { '$avg': '$' + fieldPath.replace(/\.\$/g, '') };
844
854
  }
845
855
  else if (field.leafValueType === 'Sum') {
846
- queryGroup_1[field.id] = { '$sum': '$' + fieldPath.replace(/\.\$/g, '') };
856
+ queryGroup_1[field.id] = { '$sum': buildArrayAwareNumericFieldExpr(fieldPath) };
847
857
  }
848
858
  else if (field.leafValueType === 'Count') {
849
859
  queryGroup_1[field.id] = { '$sum': '$count_' + fieldPath.replace(/\.\$/g, '') };
@@ -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;
@@ -1177,6 +1196,40 @@ function buildJoinAlias(join, joinIndex) {
1177
1196
  var joinPath = (0, common_1.toTitleCase)(join.local_key.replace(/\.\$\./g, ' -> ').replace(/\./g, ' -> ').replace(/\_/g, ' '));
1178
1197
  return "".concat(joinName, " (").concat(joinPath || 'Join', ") (Lookup ").concat(joinIndex + 1, ")");
1179
1198
  }
1199
+ function buildArrayAwareNumericFieldExpr(fieldPath) {
1200
+ var normalizedFieldPath = (fieldPath || '').replace(/\.\$/g, '');
1201
+ if (!normalizedFieldPath) {
1202
+ return 0;
1203
+ }
1204
+ var fieldExpr = '$' + normalizedFieldPath;
1205
+ return {
1206
+ $cond: [
1207
+ { $isArray: fieldExpr },
1208
+ { $sum: { $ifNull: [fieldExpr, []] } },
1209
+ { $ifNull: [fieldExpr, 0] }
1210
+ ]
1211
+ };
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
+ }
1180
1233
  function appendSafeSort(query, sortSpec, selectedFields, customFields) {
1181
1234
  if (selectedFields === void 0) { selectedFields = []; }
1182
1235
  if (customFields === void 0) { customFields = []; }
@@ -1439,10 +1492,10 @@ function buildCollectionPipeline(collectionName, alias, selectedFields, filters,
1439
1492
  .forEach(function (f) {
1440
1493
  var mappedPath = stripAliasFromPath(f.fieldPath || '', targetAlias || f.lookup_as || '');
1441
1494
  if (f.leafValueType === 'Average') {
1442
- projection[f.id] = { $avg: '$' + mappedPath };
1495
+ projection[f.id] = buildArrayAwareAverageFieldExpr(mappedPath);
1443
1496
  }
1444
1497
  else if (f.leafValueType === 'Sum') {
1445
- projection[f.id] = { $sum: '$' + mappedPath };
1498
+ projection[f.id] = buildArrayAwareNumericFieldExpr(mappedPath);
1446
1499
  }
1447
1500
  else if (f.leafValueType === 'Count') {
1448
1501
  projection[f.id] = { $sum: 1 };