@resolveio/server-lib 22.2.33 → 22.2.35

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.
@@ -314,7 +314,7 @@ function loadReportBuilderMethods(methodManager) {
314
314
  }),
315
315
  function: function (reportType_1, rootCollectionName_1, rootOptions_1) {
316
316
  return __awaiter(this, arguments, void 0, function (reportType, rootCollectionName, rootOptions, filters, filterArrays, filterArrayFields, selectedFields, customFields, groupsRow, fieldsTotal, fieldsLink, date_field, date_interval, displayType) {
317
- var modelCollection, datePaths, reportBuilderTimezone, lookupAliases, _a, rootFilters_1, lookupFilters_1, additionalCollectionsMap_1, additionalCollections, unionQuery_1, _b, sortToUse, layoutSortAdd, remappedFilters, remappedArrayFilters, postMatchAnd, res_1, err_1, tmpTotals_1, resultsOut, tmpRes_1, query_1, initialQueryMatchCondition_1, sizes_1, divFields_1, queryMatchConditionLookup_1, queryGroup_1, groupedSorts_1, dateFieldPath, dateFieldExpr, utcHourExpr, utcMinuteExpr, utcSecondExpr, utcDayExpr, tzDayExpr, useUtcForDateOnlyExpr, groupingTimezoneExpr, queryProjection_1, _loop_1, i, res_2, err_2, tmpTotals_2, tmpRes;
317
+ var modelCollection, datePaths, reportBuilderTimezone, lookupAliases, _a, rootFilters_1, lookupFilters_1, additionalCollectionsMap_1, additionalCollections, unionQuery_1, _b, sortToUse, layoutSortAdd, remappedFilters, remappedArrayFilters, postMatchAnd, readPreference_1, queryResult_1, err_1, tmpRes_1, query_1, initialQueryMatchCondition_1, sizes_1, divFields_1, queryMatchConditionLookup_1, queryGroup_1, groupedSorts_1, dateFieldPath, dateFieldExpr, utcHourExpr, utcMinuteExpr, utcSecondExpr, utcDayExpr, tzDayExpr, useUtcForDateOnlyExpr, groupingTimezoneExpr, queryProjection_1, _loop_1, i, readPreference, queryResult, err_2, tmpRes;
318
318
  if (filters === void 0) { filters = []; }
319
319
  if (filterArrays === void 0) { filterArrays = []; }
320
320
  if (filterArrayFields === void 0) { filterArrayFields = []; }
@@ -387,54 +387,28 @@ function loadReportBuilderMethods(methodManager) {
387
387
  if (postMatchAnd.length) {
388
388
  unionQuery_1.push({ $match: { $and: postMatchAnd } });
389
389
  }
390
- res_1 = null;
390
+ readPreference_1 = this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary';
391
+ queryResult_1 = {
392
+ count: 0,
393
+ results: [],
394
+ totals: {}
395
+ };
391
396
  _c.label = 2;
392
397
  case 2:
393
398
  _c.trys.push([2, 4, , 5]);
394
- return [4 /*yield*/, modelCollection.aggregate(unionQuery_1, { allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary' }, true)];
399
+ return [4 /*yield*/, executeReportBuilderQuery(modelCollection, unionQuery_1, rootOptions, fieldsTotal, selectedFields, customFields, readPreference_1)];
395
400
  case 3:
396
- res_1 = _c.sent();
401
+ queryResult_1 = _c.sent();
397
402
  return [3 /*break*/, 5];
398
403
  case 4:
399
404
  err_1 = _c.sent();
400
405
  console.log(err_1);
401
406
  return [3 /*break*/, 5];
402
407
  case 5:
403
- tmpTotals_1 = {};
404
- if (res_1) {
405
- fieldsTotal.forEach(function (totField) {
406
- selectedFields.filter(function (b) { return b.fieldType === 'Number' || b.leafValueType === 'Count'; }).forEach(function (selField) {
407
- var sum = res_1.map(function (a) {
408
- var raw = a ? a[selField.id] : null;
409
- if (Array.isArray(raw)) {
410
- return raw
411
- .map(function (v) { return (typeof v === 'number' ? v : Number(v)); })
412
- .filter(function (v) { return !isNaN(v); })
413
- .reduce(function (x, y) { return x + y; }, 0);
414
- }
415
- var num = typeof raw === 'number' ? raw : (raw !== null && raw !== undefined ? Number(raw) : 0);
416
- return isNaN(num) ? 0 : num;
417
- }).reduce(function (a, b) { return a + b; }, 0);
418
- if (totField.type === 'sum') {
419
- tmpTotals_1[totField.id + '_' + selField.id] = sum;
420
- }
421
- else if (totField.type === 'avg') {
422
- tmpTotals_1[totField.id + '_' + selField.id] = res_1.length ? (0, common_1.round)(sum / res_1.length) : 0;
423
- }
424
- });
425
- });
426
- }
427
- resultsOut = res_1 || [];
428
- if (rootOptions.skip) {
429
- resultsOut = resultsOut.slice(rootOptions.skip);
430
- }
431
- if (rootOptions.limit) {
432
- resultsOut = resultsOut.slice(0, rootOptions.limit);
433
- }
434
408
  tmpRes_1 = [{
435
- count: res_1 ? res_1.length : 0,
436
- results: resultsOut,
437
- totals: tmpTotals_1
409
+ count: queryResult_1.count,
410
+ results: queryResult_1.results,
411
+ totals: queryResult_1.totals
438
412
  }];
439
413
  return [2 /*return*/, tmpRes_1];
440
414
  case 6:
@@ -1079,44 +1053,28 @@ function loadReportBuilderMethods(methodManager) {
1079
1053
  console.log('_____________ query __________');
1080
1054
  console.log(JSON.stringify(query_1, null, 2));
1081
1055
  }
1082
- res_2 = null;
1056
+ readPreference = this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary';
1057
+ queryResult = {
1058
+ count: 0,
1059
+ results: [],
1060
+ totals: {}
1061
+ };
1083
1062
  _c.label = 7;
1084
1063
  case 7:
1085
1064
  _c.trys.push([7, 9, , 10]);
1086
- return [4 /*yield*/, modelCollection.aggregate(query_1, { allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary' }, true)];
1065
+ return [4 /*yield*/, executeReportBuilderQuery(modelCollection, query_1, rootOptions, fieldsTotal, selectedFields, customFields, readPreference)];
1087
1066
  case 8:
1088
- res_2 = _c.sent();
1067
+ queryResult = _c.sent();
1089
1068
  return [3 /*break*/, 10];
1090
1069
  case 9:
1091
1070
  err_2 = _c.sent();
1092
1071
  console.log(err_2);
1093
1072
  return [3 /*break*/, 10];
1094
1073
  case 10:
1095
- tmpTotals_2 = {};
1096
- if (res_2) {
1097
- fieldsTotal.forEach(function (totField) {
1098
- selectedFields.filter(function (b) { return b.fieldType === 'Number' || b.leafValueType === 'Count'; }).forEach(function (selField) {
1099
- if (totField.type === 'sum') {
1100
- tmpTotals_2[totField.id + '_' + selField.id] = res_2.map(function (a) { return a[totField.id + '_' + selField.id] || 0; }).reduce(function (a, b) { return a + b; }, 0);
1101
- }
1102
- else if (totField.type === 'avg') {
1103
- tmpTotals_2[totField.id + '_' + selField.id] = (0, common_1.round)(res_2.map(function (a) { return a[totField.id + '_' + selField.id] || 0; }).reduce(function (a, b) { return a + b; }, 0) / res_2.length);
1104
- }
1105
- });
1106
- customFields.forEach(function (custField) {
1107
- if (totField.type === 'sum') {
1108
- tmpTotals_2[totField.id + '_' + custField.selFieldId] = res_2.map(function (a) { return a[totField.id + '_' + custField.selFieldId] || 0; }).reduce(function (a, b) { return a + b; }, 0);
1109
- }
1110
- else if (totField.type === 'avg') {
1111
- tmpTotals_2[totField.id + '_' + custField.selFieldId] = (0, common_1.round)(res_2.map(function (a) { return a[totField.id + '_' + custField.selFieldId] || 0; }).reduce(function (a, b) { return a + b; }, 0) / res_2.length);
1112
- }
1113
- });
1114
- });
1115
- }
1116
1074
  tmpRes = [{
1117
- count: res_2 ? res_2.length : 0,
1118
- results: res_2 ? (rootOptions.limit ? res_2.splice(0, rootOptions.limit) : res_2) : [],
1119
- totals: tmpTotals_2
1075
+ count: queryResult.count,
1076
+ results: queryResult.results,
1077
+ totals: queryResult.totals
1120
1078
  }];
1121
1079
  return [2 /*return*/, tmpRes];
1122
1080
  }
@@ -1127,6 +1085,187 @@ function loadReportBuilderMethods(methodManager) {
1127
1085
  }
1128
1086
  });
1129
1087
  }
1088
+ function coerceReportBuilderNumeric(value) {
1089
+ if (Array.isArray(value)) {
1090
+ return value.map(function (v) { return coerceReportBuilderNumeric(v); }).reduce(function (a, b) { return a + b; }, 0);
1091
+ }
1092
+ var num = typeof value === 'number' ? value : (value !== null && value !== undefined ? Number(value) : 0);
1093
+ return isNaN(num) ? 0 : num;
1094
+ }
1095
+ function buildReportBuilderTotalsFieldKeySets(fieldsTotal, selectedFields, customFields) {
1096
+ if (fieldsTotal === void 0) { fieldsTotal = []; }
1097
+ if (selectedFields === void 0) { selectedFields = []; }
1098
+ if (customFields === void 0) { customFields = []; }
1099
+ var numericSelectedFields = selectedFields.filter(function (field) { return (field === null || field === void 0 ? void 0 : field.fieldType) === 'Number' || (field === null || field === void 0 ? void 0 : field.leafValueType) === 'Count'; });
1100
+ var sumKeys = new Set();
1101
+ var avgKeys = new Set();
1102
+ fieldsTotal.forEach(function (totalField) {
1103
+ numericSelectedFields.forEach(function (selectedField) {
1104
+ var key = "".concat(totalField.id, "_").concat(selectedField.id);
1105
+ if (!key) {
1106
+ return;
1107
+ }
1108
+ if (totalField.type === 'sum') {
1109
+ sumKeys.add(key);
1110
+ }
1111
+ else if (totalField.type === 'avg') {
1112
+ avgKeys.add(key);
1113
+ }
1114
+ });
1115
+ customFields.forEach(function (customField) {
1116
+ var customId = customField === null || customField === void 0 ? void 0 : customField.selFieldId;
1117
+ if (!customId) {
1118
+ return;
1119
+ }
1120
+ var key = "".concat(totalField.id, "_").concat(customId);
1121
+ if (totalField.type === 'sum') {
1122
+ sumKeys.add(key);
1123
+ }
1124
+ else if (totalField.type === 'avg') {
1125
+ avgKeys.add(key);
1126
+ }
1127
+ });
1128
+ });
1129
+ return { sumKeys: sumKeys, avgKeys: avgKeys };
1130
+ }
1131
+ function buildReportBuilderTotalsFromSums(fieldsTotal, selectedFields, customFields, sumValues, count) {
1132
+ if (fieldsTotal === void 0) { fieldsTotal = []; }
1133
+ if (selectedFields === void 0) { selectedFields = []; }
1134
+ if (customFields === void 0) { customFields = []; }
1135
+ if (sumValues === void 0) { sumValues = {}; }
1136
+ if (count === void 0) { count = 0; }
1137
+ var numericSelectedFields = selectedFields.filter(function (field) { return (field === null || field === void 0 ? void 0 : field.fieldType) === 'Number' || (field === null || field === void 0 ? void 0 : field.leafValueType) === 'Count'; });
1138
+ var totals = {};
1139
+ fieldsTotal.forEach(function (totalField) {
1140
+ numericSelectedFields.forEach(function (selectedField) {
1141
+ var key = "".concat(totalField.id, "_").concat(selectedField.id);
1142
+ var sum = coerceReportBuilderNumeric(sumValues[key]);
1143
+ if (totalField.type === 'sum') {
1144
+ totals[key] = sum;
1145
+ }
1146
+ else if (totalField.type === 'avg') {
1147
+ totals[key] = count ? (0, common_1.round)(sum / count) : 0;
1148
+ }
1149
+ });
1150
+ customFields.forEach(function (customField) {
1151
+ var customId = customField === null || customField === void 0 ? void 0 : customField.selFieldId;
1152
+ if (!customId) {
1153
+ return;
1154
+ }
1155
+ var key = "".concat(totalField.id, "_").concat(customId);
1156
+ var sum = coerceReportBuilderNumeric(sumValues[key]);
1157
+ if (totalField.type === 'sum') {
1158
+ totals[key] = sum;
1159
+ }
1160
+ else if (totalField.type === 'avg') {
1161
+ totals[key] = count ? (0, common_1.round)(sum / count) : 0;
1162
+ }
1163
+ });
1164
+ });
1165
+ return totals;
1166
+ }
1167
+ function buildReportBuilderNumericSumExpr(fieldKey) {
1168
+ var fieldExpr = '$' + fieldKey;
1169
+ return {
1170
+ $cond: [
1171
+ { $isArray: fieldExpr },
1172
+ {
1173
+ $reduce: {
1174
+ input: fieldExpr,
1175
+ initialValue: 0,
1176
+ in: {
1177
+ $add: [
1178
+ '$$value',
1179
+ { $convert: { input: '$$this', to: 'double', onError: 0, onNull: 0 } }
1180
+ ]
1181
+ }
1182
+ }
1183
+ },
1184
+ { $convert: { input: fieldExpr, to: 'double', onError: 0, onNull: 0 } }
1185
+ ]
1186
+ };
1187
+ }
1188
+ function executeReportBuilderQuery(modelCollection_1, query_2, rootOptions_1) {
1189
+ return __awaiter(this, arguments, void 0, function (modelCollection, query, rootOptions, fieldsTotal, selectedFields, customFields, readPreference) {
1190
+ var aggregateOptions, _a, sumKeys, avgKeys, allTotalKeys, resultsFacet, facet, totalsGroup_1, facetedQuery, facetedResult, facetDoc, count, results_1, totalsDoc_1, sumValues_1, totals_1, fullResult, rows, sumValues, results, totals;
1191
+ var _b, _c, _d;
1192
+ if (fieldsTotal === void 0) { fieldsTotal = []; }
1193
+ if (selectedFields === void 0) { selectedFields = []; }
1194
+ if (customFields === void 0) { customFields = []; }
1195
+ if (readPreference === void 0) { readPreference = 'primary'; }
1196
+ return __generator(this, function (_e) {
1197
+ switch (_e.label) {
1198
+ case 0:
1199
+ aggregateOptions = {
1200
+ allowDiskUse: true,
1201
+ readPreference: readPreference
1202
+ };
1203
+ _a = buildReportBuilderTotalsFieldKeySets(fieldsTotal, selectedFields, customFields), sumKeys = _a.sumKeys, avgKeys = _a.avgKeys;
1204
+ allTotalKeys = new Set(__spreadArray(__spreadArray([], __read(sumKeys), false), __read(avgKeys), false));
1205
+ if (!((rootOptions === null || rootOptions === void 0 ? void 0 : rootOptions.limit) && rootOptions.limit > 0)) return [3 /*break*/, 2];
1206
+ resultsFacet = [];
1207
+ if (rootOptions.skip && rootOptions.skip > 0) {
1208
+ resultsFacet.push({ $skip: rootOptions.skip });
1209
+ }
1210
+ resultsFacet.push({ $limit: rootOptions.limit });
1211
+ facet = {
1212
+ results: resultsFacet,
1213
+ count: [{ $count: 'count' }],
1214
+ totals: []
1215
+ };
1216
+ if (allTotalKeys.size) {
1217
+ totalsGroup_1 = {
1218
+ _id: null,
1219
+ __count: { $sum: 1 }
1220
+ };
1221
+ Array.from(allTotalKeys).forEach(function (key) {
1222
+ totalsGroup_1[key] = { $sum: buildReportBuilderNumericSumExpr(key) };
1223
+ });
1224
+ facet.totals = [
1225
+ { $group: totalsGroup_1 }
1226
+ ];
1227
+ }
1228
+ facetedQuery = query.concat([
1229
+ { $facet: facet }
1230
+ ]);
1231
+ return [4 /*yield*/, modelCollection.aggregate(facetedQuery, aggregateOptions, true)];
1232
+ case 1:
1233
+ facetedResult = _e.sent();
1234
+ facetDoc = Array.isArray(facetedResult) && facetedResult.length ? facetedResult[0] : {};
1235
+ count = ((_c = (_b = facetDoc === null || facetDoc === void 0 ? void 0 : facetDoc.count) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.count) || 0;
1236
+ results_1 = Array.isArray(facetDoc === null || facetDoc === void 0 ? void 0 : facetDoc.results) ? facetDoc.results : [];
1237
+ totalsDoc_1 = ((_d = facetDoc === null || facetDoc === void 0 ? void 0 : facetDoc.totals) === null || _d === void 0 ? void 0 : _d[0]) || {};
1238
+ sumValues_1 = {};
1239
+ Array.from(allTotalKeys).forEach(function (key) {
1240
+ sumValues_1[key] = coerceReportBuilderNumeric(totalsDoc_1[key]);
1241
+ });
1242
+ totals_1 = buildReportBuilderTotalsFromSums(fieldsTotal, selectedFields, customFields, sumValues_1, count);
1243
+ return [2 /*return*/, { count: count, results: results_1, totals: totals_1 }];
1244
+ case 2: return [4 /*yield*/, modelCollection.aggregate(query, aggregateOptions, true)];
1245
+ case 3:
1246
+ fullResult = _e.sent();
1247
+ rows = Array.isArray(fullResult) ? fullResult : [];
1248
+ sumValues = {};
1249
+ Array.from(allTotalKeys).forEach(function (key) {
1250
+ sumValues[key] = rows.map(function (row) { return coerceReportBuilderNumeric(row ? row[key] : 0); }).reduce(function (a, b) { return a + b; }, 0);
1251
+ });
1252
+ results = rows;
1253
+ if ((rootOptions === null || rootOptions === void 0 ? void 0 : rootOptions.skip) && rootOptions.skip > 0) {
1254
+ results = results.slice(rootOptions.skip);
1255
+ }
1256
+ if ((rootOptions === null || rootOptions === void 0 ? void 0 : rootOptions.limit) && rootOptions.limit > 0) {
1257
+ results = results.slice(0, rootOptions.limit);
1258
+ }
1259
+ totals = buildReportBuilderTotalsFromSums(fieldsTotal, selectedFields, customFields, sumValues, rows.length);
1260
+ return [2 /*return*/, {
1261
+ count: rows.length,
1262
+ results: results,
1263
+ totals: totals
1264
+ }];
1265
+ }
1266
+ });
1267
+ });
1268
+ }
1130
1269
  function buildCollectionTree(collectionName, lookupNode) {
1131
1270
  var _a;
1132
1271
  var lookupSchemaData = (0, schema_report_builder_1.getReportLookupSchemas)(collectionName).sort(function (a, b) { return a.collection_name.localeCompare(b.collection_name); });