@resolveio/server-lib 20.12.30 → 20.12.32

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.
@@ -48,27 +48,59 @@ function loadReportBuilderMethods(methodManager) {
48
48
  check: new simpl_schema_1.default({
49
49
  collection_root: {
50
50
  type: String
51
+ },
52
+ collectionJoins: {
53
+ type: Array,
54
+ optional: true
55
+ },
56
+ 'collectionJoins.$': {
57
+ type: Object
58
+ },
59
+ 'collectionJoins.$.collection': {
60
+ type: String
61
+ },
62
+ 'collectionJoins.$.alias': {
63
+ type: String,
64
+ optional: true
65
+ },
66
+ 'collectionJoins.$.local_key': {
67
+ type: String
68
+ },
69
+ 'collectionJoins.$.foreign_key': {
70
+ type: String
51
71
  }
52
72
  }),
53
- function: function (collection_root) {
54
- var lookupSchemaData = (0, schema_report_builder_1.getReportLookupSchemas)(collection_root).sort(function (a, b) { return a.collection_name.localeCompare(b.collection_name); });
55
- var lookupSchemaTree = lookupSchemaData.find(function (a) { return a.is_root === true; }).tree;
56
- var treeItems = (0, schema_report_builder_1.buildTree)(collection_root, lookupSchemaTree);
57
- Object.keys(lookupSchemaTree).filter(function (a) { return a.endsWith('(Lookup)'); }).forEach(function (lookup) {
58
- var fieldPath = lookup.split('.');
59
- var field = null;
60
- fieldPath.forEach(function (path) {
61
- if (!field) {
62
- field = treeItems.find(function (a) { return a.fieldName === path; });
63
- }
64
- else {
65
- field = field.children.find(function (a) { return a.fieldName === path; });
66
- }
73
+ function: function (collection_root, collectionJoins) {
74
+ if (collectionJoins === void 0) { collectionJoins = []; }
75
+ var baseRootTree = buildCollectionTree(collection_root);
76
+ var collectionTrees = [
77
+ {
78
+ collection: collection_root,
79
+ alias: (0, common_1.toTitleCase)(collection_root.replace(/\_/g, ' ').replace(/-/g, ' ')),
80
+ lookup_as: '',
81
+ tree: (0, common_1.deepCopy)(baseRootTree)
82
+ }
83
+ ];
84
+ var rootTree = baseRootTree;
85
+ (collectionJoins || []).forEach(function (join, joinIndex) {
86
+ var joinAlias = buildJoinAlias(join, joinIndex);
87
+ var joinNode = buildJoinLookupNode(collection_root, join, joinAlias);
88
+ var joinTree = buildCollectionTree(join.collection, joinNode);
89
+ joinNode.children = joinTree.children;
90
+ joinNode.is_join = true;
91
+ rootTree.children.push(joinNode);
92
+ collectionTrees.push({
93
+ collection: join.collection,
94
+ alias: joinAlias,
95
+ lookup_as: joinNode.lookup_as,
96
+ tree: (0, common_1.deepCopy)(joinTree)
67
97
  });
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
98
  });
70
- // console.log('----------- Tree Items -----------', treeItems);
71
- return Promise.resolve({ isLeaf: false, children: treeItems });
99
+ return Promise.resolve({
100
+ isLeaf: false,
101
+ children: rootTree.children,
102
+ trees: collectionTrees
103
+ });
72
104
  }
73
105
  },
74
106
  reportBuilderGetDistinctValue: {
@@ -836,5 +868,74 @@ function loadReportBuilderMethods(methodManager) {
836
868
  }
837
869
  });
838
870
  }
871
+ function buildCollectionTree(collectionName, lookupNode) {
872
+ var _a;
873
+ var lookupSchemaData = (0, schema_report_builder_1.getReportLookupSchemas)(collectionName).sort(function (a, b) { return a.collection_name.localeCompare(b.collection_name); });
874
+ var lookupSchemaTree = ((_a = lookupSchemaData.find(function (a) { return a.is_root === true; })) === null || _a === void 0 ? void 0 : _a.tree) || {};
875
+ var treeItems = (0, schema_report_builder_1.buildTree)(collectionName, lookupSchemaTree, lookupNode);
876
+ attachLookupChildren(treeItems, lookupSchemaData, lookupSchemaTree);
877
+ return { isLeaf: false, children: treeItems };
878
+ }
879
+ function attachLookupChildren(treeItems, lookupSchemaData, lookupSchemaTree) {
880
+ Object.keys(lookupSchemaTree || {}).filter(function (a) { return a.endsWith('(Lookup)'); }).forEach(function (lookup) {
881
+ var field = findFieldByPath(treeItems, lookup);
882
+ if (field) {
883
+ var lookupTree = lookupSchemaData.find(function (a) { return a.collection_name === lookupSchemaTree[lookup].lookup_collection; });
884
+ if (lookupTree) {
885
+ var childTree = (0, schema_report_builder_1.buildTree)(lookupSchemaTree[lookup].lookup_collection, lookupTree.tree, field);
886
+ attachLookupChildren(childTree, lookupSchemaData, lookupTree.tree);
887
+ field.children = childTree;
888
+ }
889
+ }
890
+ });
891
+ }
892
+ function findFieldByPath(treeItems, lookupPath) {
893
+ var fieldPath = lookupPath.split('.');
894
+ var field = null;
895
+ fieldPath.forEach(function (path) {
896
+ if (!field) {
897
+ field = treeItems.find(function (a) { return a.fieldName === path; });
898
+ }
899
+ else if (field.children) {
900
+ field = field.children.find(function (a) { return a.fieldName === path; });
901
+ }
902
+ });
903
+ return field;
904
+ }
905
+ function buildJoinLookupNode(collection_root, join, alias) {
906
+ return {
907
+ collection_name: collection_root,
908
+ columnName: alias,
909
+ fieldName: alias,
910
+ fieldType: 'Lookup',
911
+ fieldTypeName: 'Lookup',
912
+ distinctFieldValues: [],
913
+ fieldPath: alias,
914
+ fieldPathName: alias,
915
+ path: alias,
916
+ lookup_collection: join.collection,
917
+ lookup_local_key: join.local_key,
918
+ lookup_foreign_key: join.foreign_key,
919
+ lookup_as: alias,
920
+ text: alias,
921
+ value: alias,
922
+ isLeaf: false,
923
+ isActive: false,
924
+ isSelected: false,
925
+ is_join: false,
926
+ depth: 0,
927
+ leafValueType: '',
928
+ leafFormatType: '',
929
+ children: []
930
+ };
931
+ }
932
+ function buildJoinAlias(join, joinIndex) {
933
+ if (join.alias && join.alias.trim()) {
934
+ return join.alias.trim();
935
+ }
936
+ var joinName = (0, common_1.toTitleCase)(join.collection.replace(/\_/g, ' ').replace(/-/g, ' '));
937
+ var joinPath = (0, common_1.toTitleCase)(join.local_key.replace(/\.\$\./g, ' -> ').replace(/\./g, ' -> ').replace(/\_/g, ' '));
938
+ return "".concat(joinName, " (").concat(joinPath || 'Join', ") (Lookup ").concat(joinIndex + 1, ")");
939
+ }
839
940
 
840
941
  //# sourceMappingURL=report-builder.js.map