@resolveio/server-lib 20.10.2 → 20.10.4

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.
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -42,18 +53,73 @@ var pagination_model_1 = require("../models/pagination.model");
42
53
  var resolveio_server_app_1 = require("../resolveio-server-app");
43
54
  var common_1 = require("../util/common");
44
55
  var schema_report_builder_1 = require("../util/schema-report-builder");
56
+ var ReportBuilderJoinAggregationSchema = new simpl_schema_1.default({
57
+ fieldPath: {
58
+ type: String,
59
+ optional: true
60
+ },
61
+ operator: {
62
+ type: String,
63
+ allowedValues: ['sum', 'avg', 'min', 'max', 'count']
64
+ },
65
+ alias: {
66
+ type: String
67
+ }
68
+ });
69
+ var ReportBuilderCollectionJoinSchema = new simpl_schema_1.default({
70
+ collection: {
71
+ type: String
72
+ },
73
+ alias: {
74
+ type: String
75
+ },
76
+ rootFieldPath: {
77
+ type: String
78
+ },
79
+ foreignField: {
80
+ type: String
81
+ },
82
+ aggregations: {
83
+ type: Array
84
+ },
85
+ 'aggregations.$': {
86
+ type: ReportBuilderJoinAggregationSchema
87
+ },
88
+ matchFilters: {
89
+ type: Array,
90
+ optional: true
91
+ },
92
+ 'matchFilters.$': {
93
+ type: Object,
94
+ blackbox: true
95
+ }
96
+ });
97
+ var numberLeafValueTypesTemplate = (0, schema_report_builder_1.getLeafValueTypes)({ rbType: 'Number' }, '');
98
+ var numberLeafFormatTypesTemplate = (0, schema_report_builder_1.getLeafFormatTypes)({ rbType: 'Number' }, '');
45
99
  function loadReportBuilderMethods(methodManager) {
46
100
  methodManager.methods({
47
101
  reportBuilderBuildTree: {
48
102
  check: new simpl_schema_1.default({
49
103
  collection_root: {
50
104
  type: String
105
+ },
106
+ collectionJoins: {
107
+ type: Array,
108
+ optional: true
109
+ },
110
+ 'collectionJoins.$': {
111
+ type: ReportBuilderCollectionJoinSchema
51
112
  }
52
113
  }),
53
- function: function (collection_root) {
114
+ function: function (collection_root, collectionJoins) {
115
+ if (collectionJoins === void 0) { collectionJoins = []; }
54
116
  var lookupSchemaData = (0, schema_report_builder_1.getReportLookupSchemas)(collection_root).sort(function (a, b) { return a.collection_name.localeCompare(b.collection_name); });
55
117
  var lookupSchemaTree = lookupSchemaData.find(function (a) { return a.is_root === true; }).tree;
56
118
  var treeItems = (0, schema_report_builder_1.buildTree)(collection_root, lookupSchemaTree);
119
+ var joinTreeItems = buildCollectionJoinTree(collectionJoins);
120
+ if (joinTreeItems.length) {
121
+ treeItems = treeItems.concat(joinTreeItems).sort(function (a, b) { return a.fieldName.localeCompare(b.fieldName); });
122
+ }
57
123
  Object.keys(lookupSchemaTree).filter(function (a) { return a.endsWith('(Lookup)'); }).forEach(function (lookup) {
58
124
  var fieldPath = lookup.split('.');
59
125
  var field = null;
@@ -185,9 +251,16 @@ function loadReportBuilderMethods(methodManager) {
185
251
  displayType: {
186
252
  type: String,
187
253
  optional: true
254
+ },
255
+ collectionJoins: {
256
+ type: Array,
257
+ optional: true
258
+ },
259
+ 'collectionJoins.$': {
260
+ type: ReportBuilderCollectionJoinSchema
188
261
  }
189
262
  }),
190
- function: function (reportType, rootCollectionName, rootOptions, filters, filterArrays, filterArrayFields, selectedFields, customFields, groupsRow, fieldsTotal, fieldsLink, date_field, date_interval, displayType) {
263
+ function: function (reportType, rootCollectionName, rootOptions, filters, filterArrays, filterArrayFields, selectedFields, customFields, groupsRow, fieldsTotal, fieldsLink, date_field, date_interval, displayType, collectionJoins) {
191
264
  if (filters === void 0) { filters = []; }
192
265
  if (filterArrays === void 0) { filterArrays = []; }
193
266
  if (filterArrayFields === void 0) { filterArrayFields = []; }
@@ -199,6 +272,7 @@ function loadReportBuilderMethods(methodManager) {
199
272
  if (date_field === void 0) { date_field = ''; }
200
273
  if (date_interval === void 0) { date_interval = ''; }
201
274
  if (displayType === void 0) { displayType = ''; }
275
+ if (collectionJoins === void 0) { collectionJoins = []; }
202
276
  return __awaiter(this, void 0, void 0, function () {
203
277
  var modelCollection, query_1, initialQueryMatchCondition_1, sizes_1, divFields_1, queryMatchConditionLookup_1, queryGroup_1, groupedSorts_1, queryProjection_1, _loop_1, i, res_1, err_1, tmpTotals_1, tmpRes;
204
278
  return __generator(this, function (_a) {
@@ -235,6 +309,7 @@ function loadReportBuilderMethods(methodManager) {
235
309
  }
236
310
  // Add count
237
311
  query_1.push({ $addFields: { count: { $sum: 1 } } });
312
+ appendCollectionJoinStages(query_1, collectionJoins);
238
313
  sizes_1 = [];
239
314
  filterArrayFields.filter(function (a) { return a.fieldPath; }).forEach(function (filterField) {
240
315
  var _a, _b;
@@ -837,5 +912,162 @@ function loadReportBuilderMethods(methodManager) {
837
912
  });
838
913
  }
839
914
  exports.loadReportBuilderMethods = loadReportBuilderMethods;
915
+ function appendCollectionJoinStages(query, collectionJoins) {
916
+ if (!Array.isArray(collectionJoins) || !collectionJoins.length) {
917
+ return;
918
+ }
919
+ collectionJoins.filter(function (join) { return join && Array.isArray(join.aggregations) && join.aggregations.length; }).forEach(function (join, joinIndex) {
920
+ if (!isValidJoinAlias(join.alias)) {
921
+ throw new Error('Report builder join alias must not contain "." or "$" characters.');
922
+ }
923
+ var normalizedRootField = normalizeFieldPath(join.rootFieldPath);
924
+ var normalizedForeignField = normalizeFieldPath(join.foreignField);
925
+ if (!normalizedRootField || !normalizedForeignField) {
926
+ return;
927
+ }
928
+ var validAggregations = join.aggregations.filter(function (agg) { return agg && !!agg.alias && !!agg.operator; });
929
+ if (!validAggregations.length) {
930
+ return;
931
+ }
932
+ var bindVariable = 'rbJoin_' + joinIndex;
933
+ var lookupPipeline = [];
934
+ lookupPipeline.push({
935
+ $match: {
936
+ $expr: {
937
+ $eq: ['$' + normalizedForeignField, '$$' + bindVariable]
938
+ }
939
+ }
940
+ });
941
+ if (Array.isArray(join.matchFilters) && join.matchFilters.length) {
942
+ join.matchFilters.forEach(function (filter) {
943
+ lookupPipeline.push({ $match: filter });
944
+ });
945
+ }
946
+ var groupStage = {
947
+ _id: null
948
+ };
949
+ var projectStage = {
950
+ _id: 0
951
+ };
952
+ validAggregations.forEach(function (agg) {
953
+ var _a;
954
+ var operator = (agg.operator || 'sum').toLowerCase();
955
+ if (operator !== 'count' && !agg.fieldPath) {
956
+ throw new Error('Report builder join aggregation requires fieldPath for operator ' + operator);
957
+ }
958
+ if (operator === 'count') {
959
+ groupStage[agg.alias] = { $sum: 1 };
960
+ }
961
+ else {
962
+ var normalizedAggField = normalizeFieldPath(agg.fieldPath);
963
+ groupStage[agg.alias] = (_a = {}, _a["$".concat(operator)] = '$' + normalizedAggField, _a);
964
+ }
965
+ projectStage[agg.alias] = { $ifNull: ['$' + agg.alias, 0] };
966
+ });
967
+ lookupPipeline.push({ $group: groupStage });
968
+ lookupPipeline.push({ $project: projectStage });
969
+ var lookupStage = {
970
+ $lookup: {
971
+ from: join.collection,
972
+ let: {},
973
+ pipeline: lookupPipeline,
974
+ as: join.alias
975
+ }
976
+ };
977
+ lookupStage.$lookup.let[bindVariable] = '$' + normalizedRootField;
978
+ query.push(lookupStage);
979
+ var defaults = buildJoinAggregationDefaults(validAggregations);
980
+ var addFieldsStage = {
981
+ $addFields: {}
982
+ };
983
+ addFieldsStage.$addFields[join.alias] = {
984
+ $ifNull: [{ $first: '$' + join.alias }, defaults]
985
+ };
986
+ query.push(addFieldsStage);
987
+ });
988
+ }
989
+ function buildJoinAggregationDefaults(aggregations) {
990
+ return aggregations.reduce(function (defaults, agg) {
991
+ defaults[agg.alias] = 0;
992
+ return defaults;
993
+ }, {});
994
+ }
995
+ function buildCollectionJoinTree(collectionJoins) {
996
+ var joinNodes = [];
997
+ if (!Array.isArray(collectionJoins) || !collectionJoins.length) {
998
+ return joinNodes;
999
+ }
1000
+ collectionJoins.filter(function (join) { return join && Array.isArray(join.aggregations) && join.aggregations.length; }).forEach(function (join) {
1001
+ var childNodes = join.aggregations.filter(function (agg) { return agg && agg.alias; }).map(function (agg) { return ({
1002
+ collection_name: join.collection,
1003
+ columnName: (0, common_1.toTitleCase)(agg.alias.replace(/\_/g, ' ')),
1004
+ fieldName: agg.alias,
1005
+ fieldType: 'Number',
1006
+ fieldTypeName: 'Number',
1007
+ distinctFieldValues: [],
1008
+ fieldPath: join.alias + '.' + agg.alias,
1009
+ fieldPathName: formatFieldPathName(join.alias + '.' + agg.alias),
1010
+ lookup_collection: '',
1011
+ lookup_local_key: '',
1012
+ lookup_foreign_key: '',
1013
+ lookup_as: '',
1014
+ text: (0, common_1.toTitleCase)(agg.alias.replace(/\_/g, ' ')),
1015
+ value: agg.alias,
1016
+ isLeaf: true,
1017
+ isActive: false,
1018
+ isSelected: false,
1019
+ depth: 0,
1020
+ leafValueType: '',
1021
+ leafValueTypes: cloneNumberLeafValueTypes(),
1022
+ leafFormatType: '',
1023
+ leafFormatTypes: cloneNumberLeafFormatTypes()
1024
+ }); }).sort(function (a, b) { return a.fieldName.localeCompare(b.fieldName); });
1025
+ if (!childNodes.length) {
1026
+ return;
1027
+ }
1028
+ joinNodes.push({
1029
+ collection_name: join.collection,
1030
+ columnName: (0, common_1.toTitleCase)(join.alias.replace(/\_/g, ' ')),
1031
+ fieldName: join.alias,
1032
+ fieldType: 'Group',
1033
+ fieldTypeName: 'Group',
1034
+ distinctFieldValues: [],
1035
+ fieldPath: join.alias,
1036
+ fieldPathName: formatFieldPathName(join.alias),
1037
+ lookup_collection: '',
1038
+ lookup_local_key: '',
1039
+ lookup_foreign_key: '',
1040
+ lookup_as: '',
1041
+ text: (0, common_1.toTitleCase)(join.alias.replace(/\_/g, ' ')),
1042
+ value: join.alias,
1043
+ isLeaf: false,
1044
+ isActive: false,
1045
+ isSelected: false,
1046
+ depth: 0,
1047
+ leafValueType: '',
1048
+ leafFormatType: '',
1049
+ children: childNodes
1050
+ });
1051
+ });
1052
+ return joinNodes.sort(function (a, b) { return a.fieldName.localeCompare(b.fieldName); });
1053
+ }
1054
+ function cloneNumberLeafValueTypes() {
1055
+ return numberLeafValueTypesTemplate.map(function (type) { return (__assign({}, type)); });
1056
+ }
1057
+ function cloneNumberLeafFormatTypes() {
1058
+ return numberLeafFormatTypesTemplate.map(function (type) { return (__assign({}, type)); });
1059
+ }
1060
+ function normalizeFieldPath(fieldPath) {
1061
+ return fieldPath ? fieldPath.replace(/\.\$/g, '') : fieldPath;
1062
+ }
1063
+ function formatFieldPathName(path) {
1064
+ if (!path) {
1065
+ return '';
1066
+ }
1067
+ return (0, common_1.toTitleCase)(path.replace(/\.\$\./g, ' (List) -> ').replace(/\./g, ' (Group) -> ').replace(/\_/g, ' '));
1068
+ }
1069
+ function isValidJoinAlias(alias) {
1070
+ return !!alias && !alias.includes('.') && !alias.includes('$');
1071
+ }
840
1072
 
841
1073
  //# sourceMappingURL=report-builder.js.map