@resolveio/server-lib 4.5.5 → 4.5.7

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.
@@ -294,49 +294,59 @@ function loadReportBuilderMethods(methodManager) {
294
294
  selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field) {
295
295
  initialProjection[field.fieldPath.split('.$')[0]] = 1;
296
296
  });
297
- initialProjection['rb_arrays_0'] = {
298
- $map: {
299
- input: {
300
- $zip: {
301
- inputs: [
302
- // '$chemicals',
303
- // '$drivers',
304
- // '$invoices'
305
- ],
306
- useLongestLength: true
297
+ if (selectedFields.some(function (a) { return a.fieldPath.includes('$'); }) || groupsRow.some(function (a) { return a.field.includes('$') || a.field.includes('(Lookup)'); })) {
298
+ initialProjection['rb_arrays_0'] = {
299
+ $map: {
300
+ input: {
301
+ $zip: {
302
+ inputs: [
303
+ // '$chemicals',
304
+ // '$drivers',
305
+ // '$invoices'
306
+ ],
307
+ useLongestLength: true
308
+ }
309
+ },
310
+ as: 'zipped',
311
+ in: {
312
+ // chemicals: {
313
+ // $arrayElemAt: [
314
+ // '$$zipped',
315
+ // 0
316
+ // ]
317
+ // },
318
+ // drivers: {
319
+ // $arrayElemAt: [
320
+ // '$$zipped',
321
+ // 1
322
+ // ]
323
+ // },
324
+ // invoices: {
325
+ // $arrayElemAt: [
326
+ // '$$zipped',
327
+ // 2
328
+ // ]
329
+ // }
307
330
  }
308
- },
309
- as: 'zipped',
310
- in: {
311
- // chemicals: {
312
- // $arrayElemAt: [
313
- // '$$zipped',
314
- // 0
315
- // ]
316
- // },
317
- // drivers: {
318
- // $arrayElemAt: [
319
- // '$$zipped',
320
- // 1
321
- // ]
322
- // },
323
- // invoices: {
324
- // $arrayElemAt: [
325
- // '$$zipped',
326
- // 2
327
- // ]
328
- // }
329
331
  }
330
- }
331
- };
332
- selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field, index) {
333
- if (!initialProjection['rb_arrays_0'].$map.input.$zip.inputs.some(function (a) { return a === '$' + field.fieldPath.split('.$')[0]; })) {
334
- initialProjection['rb_arrays_0'].$map.input.$zip.inputs.push('$' + field.fieldPath.split('.$')[0]);
335
- initialProjection['rb_arrays_0'].$map.in[field.fieldPath.split('.$')[0]] = {
336
- $arrayElemAt: ['$$zipped', index]
337
- };
338
- }
339
- });
332
+ };
333
+ groupsRow.filter(function (a) { return a.field.includes('$'); }).forEach(function (field, index) {
334
+ if (!initialProjection['rb_arrays_0'].$map.input.$zip.inputs.some(function (a) { return a === '$' + field.field.split('.$')[0]; })) {
335
+ initialProjection['rb_arrays_0'].$map.input.$zip.inputs.push('$' + field.field.split('.$')[0]);
336
+ initialProjection['rb_arrays_0'].$map.in[field.field.split('.$')[0]] = {
337
+ $arrayElemAt: ['$$zipped', index]
338
+ };
339
+ }
340
+ });
341
+ selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field, index) {
342
+ if (!initialProjection['rb_arrays_0'].$map.input.$zip.inputs.some(function (a) { return a === '$' + field.fieldPath.split('.$')[0]; })) {
343
+ initialProjection['rb_arrays_0'].$map.input.$zip.inputs.push('$' + field.fieldPath.split('.$')[0]);
344
+ initialProjection['rb_arrays_0'].$map.in[field.fieldPath.split('.$')[0]] = {
345
+ $arrayElemAt: ['$$zipped', index]
346
+ };
347
+ }
348
+ });
349
+ }
340
350
  queryGroups = [];
341
351
  queryGroupUnwind = [];
342
352
  initialGroup = null;
@@ -346,7 +356,7 @@ function loadReportBuilderMethods(methodManager) {
346
356
  initialGroup = { _id: {} };
347
357
  groupsRow.forEach(function (row) {
348
358
  initialGroup._id[row.id] = '$' + row.field.replace(/\.\$/g, '');
349
- initialGroup.rb_arrays_0 = { '$push': '$rb_arrays_0' };
359
+ initialGroup.rb_arrays_0 = { '$first': '$rb_arrays_0' };
350
360
  initialGroup.count = { '$sum': '$count' };
351
361
  selectedFields.filter(function (a) { return !a.fieldPath.includes('$') && a.collection_name === rootCollectionName; }).forEach(function (field) {
352
362
  if (field.leafValueType === 'Average') {
@@ -388,8 +398,10 @@ function loadReportBuilderMethods(methodManager) {
388
398
  // project next layers
389
399
  secondInitialGroup = { _id: {} };
390
400
  groupsRow.forEach(function (row) {
391
- secondInitialGroup._id[row.id] = '$' + row.field.replace(/\.\$/g, '');
392
- selectedFields.filter(function (a) { return !a.fieldPath.includes('$') && a.collection_name !== rootCollectionName; }).forEach(function (field) {
401
+ secondInitialGroup._id[row.id] = '$rb_arrays_0.' + row.field.replace(/\.\$/g, '');
402
+ secondInitialGroup.rb_arrays_0 = { '$push': '$rb_arrays_0' };
403
+ secondInitialGroup.count = { '$sum': '$count' };
404
+ selectedFields.filter(function (a) { return !a.fieldPath.includes('$') && a.collection_name === rootCollectionName; }).forEach(function (field) {
393
405
  if (field.leafValueType === 'Average') {
394
406
  secondInitialGroup[field.id] = { '$avg': '$' + (field.collection_name === rootCollectionName ? field.fieldPath.replace(/\.\$/g, '') : field.fieldPath.replace(/\.\$/g, '')) };
395
407
  }
@@ -428,7 +440,7 @@ function loadReportBuilderMethods(methodManager) {
428
440
  initialNonSumFilterProj = null;
429
441
  initialNonSumElemAtProj = null;
430
442
  initialNonSumValueProj = null;
431
- if (selectedFields.some(function (a) { return a.fieldPath.split('.$').length === 2 && (a.leafValueType !== 'Average' && a.leafValueType !== 'Sum'); })) {
443
+ if (selectedFields.some(function (a) { return a.fieldPath.split('.$').length === 2; })) {
432
444
  initialNonSumFilterProj = { $addFields: {} };
433
445
  selectedFields.filter(function (a) { return a.fieldPath.split('.$').length === 2; }).forEach(function (field) {
434
446
  var _a;
@@ -444,9 +456,9 @@ function loadReportBuilderMethods(methodManager) {
444
456
  }
445
457
  }
446
458
  };
447
- if (filterArrayPaths.some(function (a) { return a === field.fieldPath; })) {
459
+ if (filterArrayPaths.some(function (a) { return a.replace(new RegExp(/\.\w+$/g), '') === field.fieldPath.replace(new RegExp(/\.\w+$/g), ''); })) {
448
460
  var tmpOr_1 = [];
449
- var filter = common_1.deepCopy(filterArrays[filterArrayPaths.indexOf(field.fieldPath)]);
461
+ var filter = common_1.deepCopy(filterArrays[filterArrayPaths.findIndex(function (a) { return a.replace(new RegExp(/\.\w+$/), '') === field.fieldPath.replace(new RegExp(/\.\w+$/), ''); })]);
450
462
  if (filter['$or']) {
451
463
  filter['$or'].forEach(function (orVal) {
452
464
  var _a;
@@ -534,24 +546,37 @@ function loadReportBuilderMethods(methodManager) {
534
546
  }
535
547
  }
536
548
  };
537
- var path = '$$this';
538
- for (var k = 0; k <= cnt; k++) {
539
- path += '.' + field.fieldPath.split('.$.')[k];
540
- proj_1.$addFields['rb_arrays_' + cnt + '_' + field.id].$filter.cond.$and.push({ $ne: [path, null] });
541
- }
542
- if (filterArrayPaths.some(function (a) { return a === field.fieldPath; })) {
549
+ var path = '$$this' + '.' + field.fieldPath.split('.$.')[cnt];
550
+ proj_1.$addFields['rb_arrays_' + cnt + '_' + field.id].$filter.cond.$and.push({ $ne: [path, null] });
551
+ if (filterArrayPaths.some(function (a) { return a.replace(new RegExp(/\.\w+$/), '') === field.fieldPath.replace(new RegExp(/\.\w+$/), ''); })) {
543
552
  var tmpOr_2 = [];
544
- var filter = common_1.deepCopy(filterArrays[filterArrayPaths.indexOf(field.fieldPath)]);
553
+ var filter = common_1.deepCopy(filterArrays[filterArrayPaths.findIndex(function (a) { return a.replace(new RegExp(/\.\w+$/), '') === field.fieldPath.replace(new RegExp(/\.\w+$/), ''); })]);
545
554
  if (filter['$or']) {
546
555
  filter['$or'].forEach(function (orVal) {
547
556
  var _a;
557
+ var tmpFilterPath = '';
558
+ for (var k = cnt; k < (Object.keys(orVal)[0]).split('.').length; k++) {
559
+ if (tmpFilterPath) {
560
+ tmpFilterPath += '.';
561
+ }
562
+ tmpFilterPath += (Object.keys(orVal)[0]).split('.')[k];
563
+ }
564
+ var filterPath = '$$this.' + tmpFilterPath;
548
565
  tmpOr_2.push((_a = {},
549
- _a[Object.keys(orVal[Object.keys(orVal)[0]])[0]] = [path, orVal[Object.keys(orVal)[0]][Object.keys(orVal[Object.keys(orVal)[0]])[0]]],
566
+ _a[Object.keys(orVal[Object.keys(orVal)[0]])[0]] = [filterPath, orVal[Object.keys(orVal)[0]][Object.keys(orVal[Object.keys(orVal)[0]])[0]]],
550
567
  _a));
551
568
  });
552
569
  }
553
570
  else {
554
- proj_1.$addFields['rb_arrays_' + cnt + '_' + field.id].$filter.cond.$and.push((_a = {}, _a[Object.keys(filter[Object.keys(filter)[0]])[0]] = [path, filter[Object.keys(filter)[0]][Object.keys(filter[Object.keys(filter)[0]])[0]]], _a));
571
+ var tmpFilterPath = '';
572
+ for (var k = cnt; k < (Object.keys(filter)[0]).split('.').length; k++) {
573
+ if (tmpFilterPath) {
574
+ tmpFilterPath += '.';
575
+ }
576
+ tmpFilterPath += (Object.keys(filter)[0]).split('.')[k];
577
+ }
578
+ var filterPath = '$$this.' + tmpFilterPath;
579
+ proj_1.$addFields['rb_arrays_' + cnt + '_' + field.id].$filter.cond.$and.push((_a = {}, _a[Object.keys(filter[Object.keys(filter)[0]])[0]] = [filterPath, filter[Object.keys(filter)[0]][Object.keys(filter[Object.keys(filter)[0]])[0]]], _a));
555
580
  }
556
581
  if (tmpOr_2.length) {
557
582
  proj_1.$addFields['rb_arrays_' + cnt + '_' + field.id].$filter.cond.$and.push({ $or: tmpOr_2 });
@@ -835,16 +860,16 @@ function loadReportBuilderMethods(methodManager) {
835
860
  });
836
861
  }
837
862
  if (secondInitialGroup) {
863
+ query.push({
864
+ $unwind: {
865
+ "path": "$rb_arrays_0",
866
+ "preserveNullAndEmptyArrays": true
867
+ }
868
+ });
838
869
  query.push({
839
870
  $group: secondInitialGroup
840
871
  });
841
872
  }
842
- query.push({
843
- $unwind: {
844
- "path": "$rb_arrays_0",
845
- "preserveNullAndEmptyArrays": true
846
- }
847
- });
848
873
  if (initialNonSumFilterProj) {
849
874
  query.push(initialNonSumFilterProj);
850
875
  }
@@ -974,11 +999,11 @@ function loadReportBuilderMethods(methodManager) {
974
999
  // }
975
1000
  // }
976
1001
  // // SORT
977
- // if (rootOptions.sort && reportType !== 'Tabular Group') {
978
- // query.push({
979
- // $sort: rootOptions.sort
980
- // });
981
- // }
1002
+ if (rootOptions.sort && reportType !== 'Tabular Group') {
1003
+ query.push({
1004
+ $sort: rootOptions.sort
1005
+ });
1006
+ }
982
1007
  // Dated Grouping
983
1008
  // if (date_field && date_interval) {
984
1009
  // if (date_interval === 'Daily') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "4.5.5",
3
+ "version": "4.5.7",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {