@resolveio/server-lib 22.2.9 → 22.2.10

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.
@@ -584,7 +584,7 @@ function loadReportBuilderMethods(methodManager) {
584
584
  }
585
585
  else {
586
586
  divFields_1.$addFields[total.id + '_' + field.id] = {
587
- $divide: ['$' + fieldPath.replace(/\.\$/g, ''), { $multiply: multFields }]
587
+ $divide: [buildArrayAwareNumericFieldExpr(fieldPath), { $multiply: multFields }]
588
588
  };
589
589
  }
590
590
  }
@@ -593,7 +593,7 @@ function loadReportBuilderMethods(methodManager) {
593
593
  divFields_1.$addFields[total.id + '_' + field.id] = '$count';
594
594
  }
595
595
  else {
596
- divFields_1.$addFields[total.id + '_' + field.id] = '$' + fieldPath.replace(/\.\$/g, '');
596
+ divFields_1.$addFields[total.id + '_' + field.id] = buildArrayAwareNumericFieldExpr(fieldPath);
597
597
  }
598
598
  }
599
599
  });
@@ -609,7 +609,7 @@ function loadReportBuilderMethods(methodManager) {
609
609
  });
610
610
  if (multFields.length) {
611
611
  divFields_1.$addFields[fieldPath.replace(/\.\$/g, '')] = {
612
- $divide: ['$' + fieldPath.replace(/\.\$/g, ''), { $multiply: multFields }]
612
+ $divide: [buildArrayAwareNumericFieldExpr(fieldPath), { $multiply: multFields }]
613
613
  };
614
614
  }
615
615
  });
@@ -644,7 +644,7 @@ function loadReportBuilderMethods(methodManager) {
644
644
  if (!fieldPath && field.leafValueType !== 'Count') {
645
645
  return;
646
646
  }
647
- divFields_1.$addFields[total.id + '_' + field.id] = '$' + fieldPath.replace(/\.\$/g, '');
647
+ divFields_1.$addFields[total.id + '_' + field.id] = buildArrayAwareNumericFieldExpr(fieldPath);
648
648
  });
649
649
  });
650
650
  if (Object.keys(divFields_1.$addFields).length > 0) {
@@ -843,7 +843,7 @@ function loadReportBuilderMethods(methodManager) {
843
843
  queryGroup_1[field.id] = { '$avg': '$' + fieldPath.replace(/\.\$/g, '') };
844
844
  }
845
845
  else if (field.leafValueType === 'Sum') {
846
- queryGroup_1[field.id] = { '$sum': '$' + fieldPath.replace(/\.\$/g, '') };
846
+ queryGroup_1[field.id] = { '$sum': buildArrayAwareNumericFieldExpr(fieldPath) };
847
847
  }
848
848
  else if (field.leafValueType === 'Count') {
849
849
  queryGroup_1[field.id] = { '$sum': '$count_' + fieldPath.replace(/\.\$/g, '') };
@@ -1177,6 +1177,20 @@ function buildJoinAlias(join, joinIndex) {
1177
1177
  var joinPath = (0, common_1.toTitleCase)(join.local_key.replace(/\.\$\./g, ' -> ').replace(/\./g, ' -> ').replace(/\_/g, ' '));
1178
1178
  return "".concat(joinName, " (").concat(joinPath || 'Join', ") (Lookup ").concat(joinIndex + 1, ")");
1179
1179
  }
1180
+ function buildArrayAwareNumericFieldExpr(fieldPath) {
1181
+ var normalizedFieldPath = (fieldPath || '').replace(/\.\$/g, '');
1182
+ if (!normalizedFieldPath) {
1183
+ return 0;
1184
+ }
1185
+ var fieldExpr = '$' + normalizedFieldPath;
1186
+ return {
1187
+ $cond: [
1188
+ { $isArray: fieldExpr },
1189
+ { $sum: { $ifNull: [fieldExpr, []] } },
1190
+ { $ifNull: [fieldExpr, 0] }
1191
+ ]
1192
+ };
1193
+ }
1180
1194
  function appendSafeSort(query, sortSpec, selectedFields, customFields) {
1181
1195
  if (selectedFields === void 0) { selectedFields = []; }
1182
1196
  if (customFields === void 0) { customFields = []; }