@resolveio/server-lib 20.12.37 → 20.12.38
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.
- package/collections/report-builder-report.collection.js +20 -0
- package/collections/report-builder-report.collection.js.map +1 -1
- package/collections/user.collection.js +2 -88
- package/collections/user.collection.js.map +1 -1
- package/methods/cron-jobs.js +1 -1
- package/methods/cron-jobs.js.map +1 -1
- package/methods/report-builder.js +300 -52
- package/methods/report-builder.js.map +1 -1
- package/methods.ts +2 -2
- package/models/report-builder-report.model.d.ts +7 -0
- package/models/report-builder-report.model.js.map +1 -1
- package/models/user.model.d.ts +1 -0
- package/models/user.model.js.map +1 -1
- package/package.json +1 -1
- package/util/schema-report-builder.js +1 -1
- package/util/schema-report-builder.js.map +1 -1
|
@@ -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) {
|
|
@@ -48,27 +59,63 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
48
59
|
check: new simpl_schema_1.default({
|
|
49
60
|
collection_root: {
|
|
50
61
|
type: String
|
|
62
|
+
},
|
|
63
|
+
collectionJoins: {
|
|
64
|
+
type: Array,
|
|
65
|
+
optional: true
|
|
66
|
+
},
|
|
67
|
+
'collectionJoins.$': {
|
|
68
|
+
type: Object
|
|
69
|
+
},
|
|
70
|
+
'collectionJoins.$.collection': {
|
|
71
|
+
type: String
|
|
72
|
+
},
|
|
73
|
+
'collectionJoins.$.alias': {
|
|
74
|
+
type: String,
|
|
75
|
+
optional: true
|
|
76
|
+
},
|
|
77
|
+
'collectionJoins.$.local_key': {
|
|
78
|
+
type: String,
|
|
79
|
+
optional: true
|
|
80
|
+
},
|
|
81
|
+
'collectionJoins.$.foreign_key': {
|
|
82
|
+
type: String,
|
|
83
|
+
optional: true
|
|
51
84
|
}
|
|
52
85
|
}),
|
|
53
|
-
function: function (collection_root) {
|
|
54
|
-
|
|
55
|
-
var
|
|
56
|
-
var
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
86
|
+
function: function (collection_root, collectionJoins) {
|
|
87
|
+
if (collectionJoins === void 0) { collectionJoins = []; }
|
|
88
|
+
var baseRootTree = buildCollectionTree(collection_root);
|
|
89
|
+
var collectionTrees = [
|
|
90
|
+
{
|
|
91
|
+
collection: collection_root,
|
|
92
|
+
alias: (0, common_1.toTitleCase)(collection_root.replace(/\_/g, ' ').replace(/-/g, ' ')),
|
|
93
|
+
lookup_as: '',
|
|
94
|
+
tree: (0, common_1.deepCopy)(baseRootTree)
|
|
95
|
+
}
|
|
96
|
+
];
|
|
97
|
+
var rootTree = baseRootTree;
|
|
98
|
+
(collectionJoins || []).forEach(function (join, joinIndex) {
|
|
99
|
+
var joinLocalKey = join.local_key && join.local_key.trim().length ? join.local_key : '_id';
|
|
100
|
+
var joinForeignKey = join.foreign_key && join.foreign_key.trim().length ? join.foreign_key : '_id';
|
|
101
|
+
var joinAlias = buildJoinAlias(join, joinIndex);
|
|
102
|
+
var joinNode = buildJoinLookupNode(collection_root, __assign(__assign({}, join), { local_key: joinLocalKey, foreign_key: joinForeignKey }), joinAlias);
|
|
103
|
+
var joinTree = buildCollectionTree(join.collection, joinNode);
|
|
104
|
+
joinNode.children = joinTree.children;
|
|
105
|
+
joinNode.is_join = true;
|
|
106
|
+
rootTree.children.push(joinNode);
|
|
107
|
+
collectionTrees.push({
|
|
108
|
+
collection: join.collection,
|
|
109
|
+
alias: joinAlias,
|
|
110
|
+
lookup_as: joinNode.lookup_as,
|
|
111
|
+
tree: (0, common_1.deepCopy)(joinTree)
|
|
67
112
|
});
|
|
68
|
-
field.children = (0, schema_report_builder_1.buildTree)(lookupSchemaTree[lookup].lookup_collection, lookupSchemaData.find(function (a) { return a.collection_name === lookupSchemaTree[lookup].lookup_collection; }).tree, field);
|
|
69
113
|
});
|
|
70
|
-
|
|
71
|
-
|
|
114
|
+
return Promise.resolve({
|
|
115
|
+
isLeaf: false,
|
|
116
|
+
children: rootTree.children,
|
|
117
|
+
trees: collectionTrees
|
|
118
|
+
});
|
|
72
119
|
}
|
|
73
120
|
},
|
|
74
121
|
reportBuilderGetDistinctValue: {
|
|
@@ -82,8 +129,8 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
82
129
|
return __awaiter(this, void 0, void 0, function () {
|
|
83
130
|
return __generator(this, function (_a) {
|
|
84
131
|
if (treeLeaf.fieldType === 'String') {
|
|
85
|
-
if (treeLeaf.fieldPath.includes('(Lookup
|
|
86
|
-
return [2 /*return*/, (0, schema_report_builder_1.mongoCommand)('distinct', treeLeaf.collection_name, treeLeaf.fieldPath.replace(/^.+\(.+\) \(Lookup
|
|
132
|
+
if (treeLeaf.fieldPath.includes('(Lookup') || treeLeaf.lookup_collection) {
|
|
133
|
+
return [2 /*return*/, (0, schema_report_builder_1.mongoCommand)('distinct', treeLeaf.collection_name, treeLeaf.fieldPath.replace(/^.+\(.+\) \(Lookup.*\)\.\$\./g, '').replace(/\.\$/g, ''))];
|
|
87
134
|
}
|
|
88
135
|
else {
|
|
89
136
|
return [2 /*return*/, (0, schema_report_builder_1.mongoCommand)('distinct', treeLeaf.collection_name, treeLeaf.fieldPath.replace(/\.\$/g, ''))];
|
|
@@ -189,7 +236,7 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
189
236
|
}),
|
|
190
237
|
function: function (reportType_1, rootCollectionName_1, rootOptions_1) {
|
|
191
238
|
return __awaiter(this, arguments, void 0, function (reportType, rootCollectionName, rootOptions, filters, filterArrays, filterArrayFields, selectedFields, customFields, groupsRow, fieldsTotal, fieldsLink, date_field, date_interval, displayType) {
|
|
192
|
-
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;
|
|
239
|
+
var modelCollection, lookupAliases, _a, rootFilters, lookupFilters, 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;
|
|
193
240
|
if (filters === void 0) { filters = []; }
|
|
194
241
|
if (filterArrays === void 0) { filterArrays = []; }
|
|
195
242
|
if (filterArrayFields === void 0) { filterArrayFields = []; }
|
|
@@ -201,13 +248,15 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
201
248
|
if (date_field === void 0) { date_field = ''; }
|
|
202
249
|
if (date_interval === void 0) { date_interval = ''; }
|
|
203
250
|
if (displayType === void 0) { displayType = ''; }
|
|
204
|
-
return __generator(this, function (
|
|
205
|
-
switch (
|
|
251
|
+
return __generator(this, function (_b) {
|
|
252
|
+
switch (_b.label) {
|
|
206
253
|
case 0:
|
|
207
254
|
modelCollection = resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(rootCollectionName);
|
|
208
255
|
if (!!modelCollection) return [3 /*break*/, 1];
|
|
209
256
|
throw new Error('Error in Report builder get results: Invalid collection');
|
|
210
257
|
case 1:
|
|
258
|
+
lookupAliases = collectLookupAliases(selectedFields, filterArrayFields, groupsRow);
|
|
259
|
+
_a = splitFiltersByLookup(filters, lookupAliases), rootFilters = _a.rootFilters, lookupFilters = _a.lookupFilters;
|
|
211
260
|
query_1 = [];
|
|
212
261
|
initialQueryMatchCondition_1 = {
|
|
213
262
|
$and: []
|
|
@@ -221,14 +270,12 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
221
270
|
// ]
|
|
222
271
|
// });
|
|
223
272
|
// });
|
|
224
|
-
if (
|
|
225
|
-
|
|
226
|
-
initialQueryMatchCondition_1['$and'].push(filter);
|
|
227
|
-
});
|
|
273
|
+
if (rootFilters.length) {
|
|
274
|
+
rootFilters.forEach(function (filter) { return initialQueryMatchCondition_1['$and'].push(filter); });
|
|
228
275
|
}
|
|
229
276
|
// Add filter to query
|
|
230
|
-
// if (filters.filter(a => (!a['$or'] && !Object.keys(a).some(b => b.includes('(Lookup
|
|
231
|
-
if (
|
|
277
|
+
// if (filters.filter(a => (!a['$or'] && !Object.keys(a).some(b => b.includes('(Lookup'))) || (a['$or'] && !a['$or'].some(b => Object.keys(b).some(c => c.includes('(Lookup)'))))).length || selectedFields.filter(a => !a.fieldPath.includes('(Lookup')).length) {
|
|
278
|
+
if (initialQueryMatchCondition_1['$and'].length) {
|
|
232
279
|
query_1.push({
|
|
233
280
|
$match: initialQueryMatchCondition_1
|
|
234
281
|
});
|
|
@@ -242,12 +289,12 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
242
289
|
var fieldPath = '';
|
|
243
290
|
for (var i = 0; i < fieldData.length - 1; i++) {
|
|
244
291
|
fieldPath += fieldData[i];
|
|
245
|
-
if (fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup
|
|
292
|
+
if (filterField.lookup_collection || fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup')) {
|
|
246
293
|
if (!query_1.some(function (a) { return a.$lookup && a.$lookup.as === filterField.lookup_as.replace(/\.\$/g, ''); })) {
|
|
247
294
|
query_1.push({ $lookup: {
|
|
248
295
|
from: filterField.lookup_collection,
|
|
249
|
-
localField: filterField.lookup_local_key,
|
|
250
|
-
foreignField: filterField.lookup_foreign_key,
|
|
296
|
+
localField: normalizeLookupField(filterField.lookup_local_key),
|
|
297
|
+
foreignField: normalizeLookupField(filterField.lookup_foreign_key),
|
|
251
298
|
as: filterField.lookup_as.replace(/\.\$/g, '')
|
|
252
299
|
} });
|
|
253
300
|
sizes_1.push(filterField.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_'));
|
|
@@ -267,17 +314,18 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
267
314
|
// Groups - Lookups / Sizes / Unwinds
|
|
268
315
|
groupsRow.forEach(function (row) {
|
|
269
316
|
var _a, _b;
|
|
317
|
+
var _c;
|
|
270
318
|
if (row.field.includes('$')) {
|
|
271
319
|
var fieldData = row.field.split('.$');
|
|
272
320
|
var fieldPath_1 = '';
|
|
273
321
|
for (var i = 0; i < fieldData.length - 1; i++) {
|
|
274
322
|
fieldPath_1 += fieldData[i];
|
|
275
|
-
if (fieldPath_1.split('.$')[fieldPath_1.split('.$').length - 1].includes('(Lookup
|
|
323
|
+
if (((_c = row.treeItem) === null || _c === void 0 ? void 0 : _c.lookup_collection) || fieldPath_1.split('.$')[fieldPath_1.split('.$').length - 1].includes('(Lookup')) {
|
|
276
324
|
if (!query_1.some(function (a) { return a.$lookup && a.$lookup.as === row.treeItem.lookup_as.replace(/\.\$/g, ''); })) {
|
|
277
325
|
query_1.push({ $lookup: {
|
|
278
326
|
from: row.treeItem.lookup_collection,
|
|
279
|
-
localField: row.treeItem.lookup_local_key,
|
|
280
|
-
foreignField: row.treeItem.lookup_foreign_key,
|
|
327
|
+
localField: normalizeLookupField(row.treeItem.lookup_local_key),
|
|
328
|
+
foreignField: normalizeLookupField(row.treeItem.lookup_foreign_key),
|
|
281
329
|
as: row.treeItem.lookup_as.replace(/\.\$/g, '')
|
|
282
330
|
} });
|
|
283
331
|
sizes_1.push(row.treeItem.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_'));
|
|
@@ -304,12 +352,12 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
304
352
|
var fieldPath_2 = '';
|
|
305
353
|
for (var i = 0; i < fieldData.length - 1; i++) {
|
|
306
354
|
fieldPath_2 += fieldData[i];
|
|
307
|
-
if (fieldPath_2.split('.$')[fieldPath_2.split('.$').length - 1].includes('(Lookup
|
|
355
|
+
if (field.lookup_collection || fieldPath_2.split('.$')[fieldPath_2.split('.$').length - 1].includes('(Lookup')) {
|
|
308
356
|
if (!query_1.some(function (a) { return a.$lookup && a.$lookup.as === field.lookup_as.replace(/\.\$/g, ''); })) {
|
|
309
357
|
query_1.push({ $lookup: {
|
|
310
358
|
from: field.lookup_collection,
|
|
311
|
-
localField: field.lookup_local_key,
|
|
312
|
-
foreignField: field.lookup_foreign_key,
|
|
359
|
+
localField: normalizeLookupField(field.lookup_local_key),
|
|
360
|
+
foreignField: normalizeLookupField(field.lookup_foreign_key),
|
|
313
361
|
as: field.lookup_as.replace(/\.\$/g, '')
|
|
314
362
|
} });
|
|
315
363
|
sizes_1.push(field.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_'));
|
|
@@ -317,7 +365,7 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
317
365
|
query_1.push({ '$unwind': { path: '$' + field.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true } });
|
|
318
366
|
}
|
|
319
367
|
else {
|
|
320
|
-
if (!fieldData[i].includes('(Lookup
|
|
368
|
+
if (!fieldData[i].includes('(Lookup')) {
|
|
321
369
|
if (!query_1.some(function (a) { return a.$addFields && Object.keys(a.$addFields).includes('size_' + fieldPath_2.replace(/\./g, '_')); })) {
|
|
322
370
|
sizes_1.push(fieldPath_2.replace(/\./g, '_'));
|
|
323
371
|
query_1.push({ $addFields: (_b = {}, _b['size_' + fieldPath_2.replace(/\./g, '_')] = { $size: { $ifNull: ['$' + fieldPath_2, []] } }, _b) });
|
|
@@ -492,10 +540,8 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
492
540
|
queryMatchConditionLookup_1 = {
|
|
493
541
|
$and: []
|
|
494
542
|
};
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
queryMatchConditionLookup_1['$and'].push(filter);
|
|
498
|
-
});
|
|
543
|
+
if (lookupFilters.length) {
|
|
544
|
+
lookupFilters.forEach(function (filter) { return queryMatchConditionLookup_1['$and'].push(filter); });
|
|
499
545
|
}
|
|
500
546
|
filterArrays.forEach(function (filter) {
|
|
501
547
|
queryMatchConditionLookup_1['$and'].push(filter);
|
|
@@ -711,16 +757,14 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
711
757
|
}
|
|
712
758
|
// SORT
|
|
713
759
|
if (rootOptions.sort) {
|
|
714
|
-
query_1.
|
|
715
|
-
$sort: rootOptions.sort
|
|
716
|
-
});
|
|
760
|
+
appendSafeSort(query_1, rootOptions.sort, selectedFields, customFields);
|
|
717
761
|
}
|
|
718
762
|
if (reportType === 'Group' && groupsRow.length > 1) {
|
|
719
763
|
_loop_1 = function (i) {
|
|
720
|
-
var
|
|
764
|
+
var _c;
|
|
721
765
|
var projQuery = { _id: 1 };
|
|
722
766
|
projQuery[groupsRow[i].id] = {
|
|
723
|
-
_id: (
|
|
767
|
+
_id: (_c = {}, _c[groupsRow[i].id] = '$_id.' + groupsRow[i].id, _c)
|
|
724
768
|
};
|
|
725
769
|
if (i === groupsRow.length - 1) {
|
|
726
770
|
selectedFields.forEach(function (field) {
|
|
@@ -781,7 +825,7 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
781
825
|
sortKeys_1.filter(function (a) { return !a.startsWith('_id.gr_'); }).forEach(function (key) {
|
|
782
826
|
sort_1[groupString_1 + key] = rootOptions.sort[key];
|
|
783
827
|
});
|
|
784
|
-
query_1
|
|
828
|
+
appendSafeSort(query_1, sort_1, selectedFields, customFields);
|
|
785
829
|
}
|
|
786
830
|
};
|
|
787
831
|
for (i = groupsRow.length - 1; i >= 1; i--) {
|
|
@@ -789,15 +833,15 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
789
833
|
}
|
|
790
834
|
}
|
|
791
835
|
res_1 = null;
|
|
792
|
-
|
|
836
|
+
_b.label = 2;
|
|
793
837
|
case 2:
|
|
794
|
-
|
|
838
|
+
_b.trys.push([2, 4, , 5]);
|
|
795
839
|
return [4 /*yield*/, modelCollection.aggregate(query_1, { allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary' }, true)];
|
|
796
840
|
case 3:
|
|
797
|
-
res_1 =
|
|
841
|
+
res_1 = _b.sent();
|
|
798
842
|
return [3 /*break*/, 5];
|
|
799
843
|
case 4:
|
|
800
|
-
err_1 =
|
|
844
|
+
err_1 = _b.sent();
|
|
801
845
|
console.log(err_1);
|
|
802
846
|
return [3 /*break*/, 5];
|
|
803
847
|
case 5:
|
|
@@ -836,5 +880,209 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
836
880
|
}
|
|
837
881
|
});
|
|
838
882
|
}
|
|
883
|
+
function buildCollectionTree(collectionName, lookupNode) {
|
|
884
|
+
var _a;
|
|
885
|
+
var lookupSchemaData = (0, schema_report_builder_1.getReportLookupSchemas)(collectionName).sort(function (a, b) { return a.collection_name.localeCompare(b.collection_name); });
|
|
886
|
+
var lookupSchemaTree = ((_a = lookupSchemaData.find(function (a) { return a.is_root === true; })) === null || _a === void 0 ? void 0 : _a.tree) || {};
|
|
887
|
+
var treeItems = (0, schema_report_builder_1.buildTree)(collectionName, lookupSchemaTree, lookupNode);
|
|
888
|
+
attachLookupChildren(treeItems, lookupSchemaData, lookupSchemaTree);
|
|
889
|
+
return { isLeaf: false, children: treeItems };
|
|
890
|
+
}
|
|
891
|
+
function attachLookupChildren(treeItems, lookupSchemaData, lookupSchemaTree) {
|
|
892
|
+
Object.keys(lookupSchemaTree || {}).filter(function (a) { return a.endsWith('(Lookup)'); }).forEach(function (lookup) {
|
|
893
|
+
var field = findFieldByPath(treeItems, lookup);
|
|
894
|
+
if (field) {
|
|
895
|
+
var lookupTree = lookupSchemaData.find(function (a) { return a.collection_name === lookupSchemaTree[lookup].lookup_collection; });
|
|
896
|
+
if (lookupTree) {
|
|
897
|
+
var childTree = (0, schema_report_builder_1.buildTree)(lookupSchemaTree[lookup].lookup_collection, lookupTree.tree, field);
|
|
898
|
+
attachLookupChildren(childTree, lookupSchemaData, lookupTree.tree);
|
|
899
|
+
field.children = childTree;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
function findFieldByPath(treeItems, lookupPath) {
|
|
905
|
+
var fieldPath = lookupPath.split('.');
|
|
906
|
+
var field = null;
|
|
907
|
+
fieldPath.forEach(function (path) {
|
|
908
|
+
if (!field) {
|
|
909
|
+
field = treeItems.find(function (a) { return a.fieldName === path; });
|
|
910
|
+
}
|
|
911
|
+
else if (field.children) {
|
|
912
|
+
field = field.children.find(function (a) { return a.fieldName === path; });
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
return field;
|
|
916
|
+
}
|
|
917
|
+
function buildJoinLookupNode(collection_root, join, alias) {
|
|
918
|
+
return {
|
|
919
|
+
collection_name: collection_root,
|
|
920
|
+
columnName: alias,
|
|
921
|
+
fieldName: alias,
|
|
922
|
+
fieldType: 'Lookup',
|
|
923
|
+
fieldTypeName: 'Lookup',
|
|
924
|
+
distinctFieldValues: [],
|
|
925
|
+
fieldPath: alias,
|
|
926
|
+
fieldPathName: alias,
|
|
927
|
+
path: alias,
|
|
928
|
+
lookup_collection: join.collection,
|
|
929
|
+
lookup_local_key: join.local_key,
|
|
930
|
+
lookup_foreign_key: join.foreign_key,
|
|
931
|
+
lookup_as: alias,
|
|
932
|
+
text: alias,
|
|
933
|
+
value: alias,
|
|
934
|
+
isLeaf: false,
|
|
935
|
+
isActive: false,
|
|
936
|
+
isSelected: false,
|
|
937
|
+
is_join: false,
|
|
938
|
+
depth: 0,
|
|
939
|
+
leafValueType: '',
|
|
940
|
+
leafFormatType: '',
|
|
941
|
+
children: []
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
function buildJoinAlias(join, joinIndex) {
|
|
945
|
+
if (join.alias && join.alias.trim()) {
|
|
946
|
+
return join.alias.trim();
|
|
947
|
+
}
|
|
948
|
+
var joinName = (0, common_1.toTitleCase)(join.collection.replace(/\_/g, ' ').replace(/-/g, ' '));
|
|
949
|
+
var joinPath = (0, common_1.toTitleCase)(join.local_key.replace(/\.\$\./g, ' -> ').replace(/\./g, ' -> ').replace(/\_/g, ' '));
|
|
950
|
+
return "".concat(joinName, " (").concat(joinPath || 'Join', ") (Lookup ").concat(joinIndex + 1, ")");
|
|
951
|
+
}
|
|
952
|
+
function normalizeLookupField(path) {
|
|
953
|
+
if (!path) {
|
|
954
|
+
return path;
|
|
955
|
+
}
|
|
956
|
+
// Strip any lookup alias prefixes and ".$" tokens
|
|
957
|
+
var cleaned = path.replace(/\.\$/g, '');
|
|
958
|
+
// Remove leading alias like "Alias (Lookup 1)." or "Alias (Lookup 1).$."
|
|
959
|
+
cleaned = cleaned.replace(/^[^\.]*\(.*?Lookup.*?\)\.?/, '');
|
|
960
|
+
return cleaned;
|
|
961
|
+
}
|
|
962
|
+
function appendSafeSort(query, sortSpec, selectedFields, customFields) {
|
|
963
|
+
if (selectedFields === void 0) { selectedFields = []; }
|
|
964
|
+
if (customFields === void 0) { customFields = []; }
|
|
965
|
+
if (!sortSpec || !Object.keys(sortSpec).length) {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
var safeAddFields = {};
|
|
969
|
+
var safeSort = {};
|
|
970
|
+
Object.keys(sortSpec).forEach(function (key) {
|
|
971
|
+
if (key.startsWith('_id.')) {
|
|
972
|
+
safeSort[key] = sortSpec[key];
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
if (isArrayLikeField(key, selectedFields, customFields)) {
|
|
976
|
+
var safeKey = 'sort_' + key.replace(/\./g, '_');
|
|
977
|
+
safeAddFields[safeKey] = {
|
|
978
|
+
$cond: [
|
|
979
|
+
{ $isArray: '$' + key },
|
|
980
|
+
{ $size: { $ifNull: ['$' + key, []] } },
|
|
981
|
+
'$' + key
|
|
982
|
+
]
|
|
983
|
+
};
|
|
984
|
+
safeSort[safeKey] = sortSpec[key];
|
|
985
|
+
}
|
|
986
|
+
else {
|
|
987
|
+
safeSort[key] = sortSpec[key];
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
if (Object.keys(safeAddFields).length) {
|
|
991
|
+
query.push({ $addFields: safeAddFields });
|
|
992
|
+
}
|
|
993
|
+
if (Object.keys(safeSort).length) {
|
|
994
|
+
query.push({ $sort: safeSort });
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
function isArrayLikeField(sortKey, selectedFields, customFields) {
|
|
998
|
+
if (selectedFields === void 0) { selectedFields = []; }
|
|
999
|
+
if (customFields === void 0) { customFields = []; }
|
|
1000
|
+
var field = selectedFields.find(function (a) { return a.id === sortKey; });
|
|
1001
|
+
var custom = customFields.find(function (a) { return a.selFieldId === sortKey; });
|
|
1002
|
+
var leafValueType = field ? field.leafValueType : (custom ? custom.leafValueType : '');
|
|
1003
|
+
return leafValueType !== 'Average' &&
|
|
1004
|
+
leafValueType !== 'Sum' &&
|
|
1005
|
+
leafValueType !== 'Count' &&
|
|
1006
|
+
leafValueType !== 'Minimum' &&
|
|
1007
|
+
leafValueType !== 'Maximum' &&
|
|
1008
|
+
leafValueType !== 'First' &&
|
|
1009
|
+
leafValueType !== 'Last' &&
|
|
1010
|
+
leafValueType !== 'Unique';
|
|
1011
|
+
}
|
|
1012
|
+
function collectLookupAliases(selectedFields, filterArrayFields, groupsRow) {
|
|
1013
|
+
if (selectedFields === void 0) { selectedFields = []; }
|
|
1014
|
+
if (filterArrayFields === void 0) { filterArrayFields = []; }
|
|
1015
|
+
if (groupsRow === void 0) { groupsRow = []; }
|
|
1016
|
+
var aliases = new Set();
|
|
1017
|
+
var addAlias = function (alias) {
|
|
1018
|
+
var normalized = normalizeLookupAlias(alias);
|
|
1019
|
+
if (normalized) {
|
|
1020
|
+
aliases.add(normalized);
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
(selectedFields || []).forEach(function (field) {
|
|
1024
|
+
if (field && (field.lookup_collection || (field.fieldPath && field.fieldPath.includes('(Lookup')))) {
|
|
1025
|
+
addAlias(field.lookup_as || extractLookupAlias(field.fieldPath));
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
(filterArrayFields || []).forEach(function (field) {
|
|
1029
|
+
if (field && (field.lookup_collection || (field.fieldPath && field.fieldPath.includes('(Lookup')))) {
|
|
1030
|
+
addAlias(field.lookup_as || extractLookupAlias(field.fieldPath));
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
(groupsRow || []).forEach(function (group) {
|
|
1034
|
+
if ((group === null || group === void 0 ? void 0 : group.treeItem) && (group.treeItem.lookup_collection || (group.treeItem.fieldPath && group.treeItem.fieldPath.includes('(Lookup')))) {
|
|
1035
|
+
addAlias(group.treeItem.lookup_as || extractLookupAlias(group.treeItem.fieldPath));
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
return aliases;
|
|
1039
|
+
}
|
|
1040
|
+
function splitFiltersByLookup(filters, lookupAliases) {
|
|
1041
|
+
if (filters === void 0) { filters = []; }
|
|
1042
|
+
if (lookupAliases === void 0) { lookupAliases = new Set(); }
|
|
1043
|
+
var rootFilters = [];
|
|
1044
|
+
var lookupFilters = [];
|
|
1045
|
+
(filters || []).forEach(function (filter) {
|
|
1046
|
+
if (filterContainsLookup(filter, lookupAliases)) {
|
|
1047
|
+
lookupFilters.push(filter);
|
|
1048
|
+
}
|
|
1049
|
+
else {
|
|
1050
|
+
rootFilters.push(filter);
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
return { rootFilters: rootFilters, lookupFilters: lookupFilters };
|
|
1054
|
+
}
|
|
1055
|
+
function filterContainsLookup(filter, lookupAliases) {
|
|
1056
|
+
if (!filter || typeof filter !== 'object') {
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
return Object.keys(filter).some(function (key) {
|
|
1060
|
+
if (key === '$or' || key === '$and') {
|
|
1061
|
+
var branch = filter[key];
|
|
1062
|
+
return Array.isArray(branch) && branch.some(function (child) { return filterContainsLookup(child, lookupAliases); });
|
|
1063
|
+
}
|
|
1064
|
+
return isLookupPath(key, lookupAliases);
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
function isLookupPath(path, lookupAliases) {
|
|
1068
|
+
if (!path) {
|
|
1069
|
+
return false;
|
|
1070
|
+
}
|
|
1071
|
+
var normalizedPath = path.replace(/\.\$/g, '');
|
|
1072
|
+
if (normalizedPath.includes('(Lookup')) {
|
|
1073
|
+
return true;
|
|
1074
|
+
}
|
|
1075
|
+
var rootKey = normalizedPath.split('.')[0];
|
|
1076
|
+
return lookupAliases.has(rootKey);
|
|
1077
|
+
}
|
|
1078
|
+
function extractLookupAlias(path) {
|
|
1079
|
+
if (!path || !path.includes('(Lookup')) {
|
|
1080
|
+
return '';
|
|
1081
|
+
}
|
|
1082
|
+
return path.replace(/\.\$/g, '').split('.')[0];
|
|
1083
|
+
}
|
|
1084
|
+
function normalizeLookupAlias(alias) {
|
|
1085
|
+
return (alias || '').replace(/\.\$/g, '').trim();
|
|
1086
|
+
}
|
|
839
1087
|
|
|
840
1088
|
//# sourceMappingURL=report-builder.js.map
|