@resolveio/server-lib 12.5.52 → 12.5.54
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/.nodemon.json +5 -0
- package/.vscode/settings.json +3 -0
- package/README.md +22 -0
- package/compileDTS.pl +64 -0
- package/gulpfile.js +127 -0
- package/mongodbensurers.js +2 -0
- package/mongostop.js +3 -0
- package/package.json +1 -1
- package/settings.development.json +13 -0
- package/src/client-server-app.ts +7 -0
- package/src/collections/app-status.collection.ts +51 -0
- package/src/collections/counter.collection.ts +54 -0
- package/src/collections/cron-job-history.collection.ts +82 -0
- package/src/collections/cron-job.collection.ts +85 -0
- package/src/collections/email-history.collection.ts +66 -0
- package/src/collections/email-verified.collection.ts +61 -0
- package/src/collections/file.collection.ts +73 -0
- package/src/collections/flag.collection.ts +56 -0
- package/src/collections/log-method-latency.collection.ts +64 -0
- package/src/collections/log-subscription.collection.ts +68 -0
- package/src/collections/log-ts.collection.ts +78 -0
- package/src/collections/log.collection.ts +77 -0
- package/src/collections/logged-in-users.collection.ts +65 -0
- package/src/collections/method-response.collection.ts +66 -0
- package/src/collections/monitor-cpu.collection.ts +63 -0
- package/src/collections/monitor-memory.collection.ts +75 -0
- package/src/collections/monitor-mongo.collection.ts +69 -0
- package/src/collections/notification.collection.ts +51 -0
- package/src/collections/report-builder-dashboard-builder.collection.ts +109 -0
- package/src/collections/report-builder-library.collection.ts +89 -0
- package/src/collections/report-builder-report.collection.ts +149 -0
- package/src/collections/user-group.collection.ts +90 -0
- package/src/collections/user-guide.collection.ts +57 -0
- package/src/collections/user.collection.ts +259 -0
- package/src/cron/cron.ts +188 -0
- package/src/fixtures/cron-jobs.ts +25 -0
- package/src/fixtures/init.ts +10 -0
- package/src/http/auth.ts +732 -0
- package/src/http/health.ts +7 -0
- package/src/http/home.ts +71 -0
- package/src/index.ts +71 -0
- package/src/managers/cron.manager.ts +208 -0
- package/src/managers/method.manager.ts +607 -0
- package/src/managers/mongo.manager.ts +2282 -0
- package/src/managers/monitor.manager.ts +323 -0
- package/src/managers/subscription.manager.ts +1196 -0
- package/{managers/subscription.performance.js → src/managers/subscription.performance.ts} +68 -33
- package/src/methods/accounts.ts +202 -0
- package/src/methods/aws.ts +507 -0
- package/src/methods/collections.ts +512 -0
- package/src/methods/counters.ts +75 -0
- package/src/methods/cron-jobs.ts +1454 -0
- package/src/methods/flags.ts +7 -0
- package/src/methods/logs.ts +327 -0
- package/src/methods/monitor.ts +548 -0
- package/src/methods/pdf.ts +615 -0
- package/src/methods/report-builder.ts +876 -0
- package/src/methods/support.ts +146 -0
- package/{models/app-status.model.d.ts → src/models/app-status.model.ts} +3 -2
- package/{models/billing-logged-in-users.model.d.ts → src/models/billing-logged-in-users.model.ts} +5 -4
- package/src/models/collection-document.model.ts +22 -0
- package/{models/counter.model.d.ts → src/models/counter.model.ts} +4 -3
- package/src/models/cron-job-history.model.ts +13 -0
- package/src/models/cron-job.model.ts +14 -0
- package/src/models/dialog.model.ts +25 -0
- package/{models/email-history.model.d.ts → src/models/email-history.model.ts} +8 -7
- package/{models/email-verified.model.d.ts → src/models/email-verified.model.ts} +6 -5
- package/{models/file.model.d.ts → src/models/file.model.ts} +8 -7
- package/{models/flag.model.d.ts → src/models/flag.model.ts} +4 -3
- package/{models/log-method-latency.model.d.ts → src/models/log-method-latency.model.ts} +5 -4
- package/{models/log-subscription.model.d.ts → src/models/log-subscription.model.ts} +8 -6
- package/{models/log.model.d.ts → src/models/log.model.ts} +12 -10
- package/{models/logged-in-users.model.d.ts → src/models/logged-in-users.model.ts} +6 -5
- package/{models/method-response.model.d.ts → src/models/method-response.model.ts} +7 -6
- package/src/models/method.model.ts +11 -0
- package/{models/monitor-cpu.model.d.ts → src/models/monitor-cpu.model.ts} +9 -7
- package/src/models/monitor-memory.model.ts +17 -0
- package/src/models/monitor-mongo.model.ts +15 -0
- package/{models/notification.model.d.ts → src/models/notification.model.ts} +3 -2
- package/src/models/pagination.model.ts +30 -0
- package/src/models/permission.model.ts +14 -0
- package/src/models/report-builder-dashboard-builder.model.ts +29 -0
- package/src/models/report-builder-library.model.ts +20 -0
- package/src/models/report-builder-report.model.ts +110 -0
- package/src/models/report-builder.model.ts +68 -0
- package/src/models/select-data-label.model.ts +9 -0
- package/src/models/server-response.model.ts +5 -0
- package/src/models/subscription.model.ts +64 -0
- package/src/models/support-ticket.model.ts +81 -0
- package/src/models/user-group.model.ts +23 -0
- package/{models/user-guide.model.d.ts → src/models/user-guide.model.ts} +5 -4
- package/src/models/user.model.ts +79 -0
- package/{public_api.d.ts → src/public_api.ts} +1 -1
- package/src/publications/app-status.ts +13 -0
- package/src/publications/cron-jobs.ts +30 -0
- package/src/publications/files.ts +34 -0
- package/src/publications/flags.ts +20 -0
- package/src/publications/logs.ts +164 -0
- package/src/publications/method-responses.ts +13 -0
- package/src/publications/notifications.ts +13 -0
- package/src/publications/report-builder-dashboard-builders.ts +39 -0
- package/src/publications/report-builder-libraries.ts +41 -0
- package/src/publications/report-builder-reports.ts +48 -0
- package/src/publications/super-admin.ts +13 -0
- package/src/publications/user-groups.ts +12 -0
- package/src/publications/user-guides.ts +12 -0
- package/src/server-app.ts +769 -0
- package/src/util/common.ts +519 -0
- package/src/util/schema-report-builder.ts +413 -0
- package/tsconfig.json +30 -0
- package/tslint.json +143 -0
- package/client-server-app.d.ts +0 -1
- package/client-server-app.js +0 -48
- package/client-server-app.js.map +0 -1
- package/collections/app-status.collection.d.ts +0 -3
- package/collections/app-status.collection.js +0 -51
- package/collections/app-status.collection.js.map +0 -1
- package/collections/counter.collection.d.ts +0 -3
- package/collections/counter.collection.js +0 -54
- package/collections/counter.collection.js.map +0 -1
- package/collections/cron-job-history.collection.d.ts +0 -3
- package/collections/cron-job-history.collection.js +0 -82
- package/collections/cron-job-history.collection.js.map +0 -1
- package/collections/cron-job.collection.d.ts +0 -3
- package/collections/cron-job.collection.js +0 -85
- package/collections/cron-job.collection.js.map +0 -1
- package/collections/email-history.collection.d.ts +0 -3
- package/collections/email-history.collection.js +0 -66
- package/collections/email-history.collection.js.map +0 -1
- package/collections/email-verified.collection.d.ts +0 -3
- package/collections/email-verified.collection.js +0 -61
- package/collections/email-verified.collection.js.map +0 -1
- package/collections/file.collection.d.ts +0 -3
- package/collections/file.collection.js +0 -73
- package/collections/file.collection.js.map +0 -1
- package/collections/flag.collection.d.ts +0 -3
- package/collections/flag.collection.js +0 -56
- package/collections/flag.collection.js.map +0 -1
- package/collections/log-method-latency.collection.d.ts +0 -3
- package/collections/log-method-latency.collection.js +0 -64
- package/collections/log-method-latency.collection.js.map +0 -1
- package/collections/log-subscription.collection.d.ts +0 -3
- package/collections/log-subscription.collection.js +0 -68
- package/collections/log-subscription.collection.js.map +0 -1
- package/collections/log-ts.collection.d.ts +0 -3
- package/collections/log-ts.collection.js +0 -78
- package/collections/log-ts.collection.js.map +0 -1
- package/collections/log.collection.d.ts +0 -3
- package/collections/log.collection.js +0 -77
- package/collections/log.collection.js.map +0 -1
- package/collections/logged-in-users.collection.d.ts +0 -3
- package/collections/logged-in-users.collection.js +0 -65
- package/collections/logged-in-users.collection.js.map +0 -1
- package/collections/method-response.collection.d.ts +0 -3
- package/collections/method-response.collection.js +0 -66
- package/collections/method-response.collection.js.map +0 -1
- package/collections/monitor-cpu.collection.d.ts +0 -3
- package/collections/monitor-cpu.collection.js +0 -63
- package/collections/monitor-cpu.collection.js.map +0 -1
- package/collections/monitor-memory.collection.d.ts +0 -3
- package/collections/monitor-memory.collection.js +0 -75
- package/collections/monitor-memory.collection.js.map +0 -1
- package/collections/monitor-mongo.collection.d.ts +0 -3
- package/collections/monitor-mongo.collection.js +0 -69
- package/collections/monitor-mongo.collection.js.map +0 -1
- package/collections/notification.collection.d.ts +0 -3
- package/collections/notification.collection.js +0 -51
- package/collections/notification.collection.js.map +0 -1
- package/collections/report-builder-dashboard-builder.collection.d.ts +0 -3
- package/collections/report-builder-dashboard-builder.collection.js +0 -109
- package/collections/report-builder-dashboard-builder.collection.js.map +0 -1
- package/collections/report-builder-library.collection.d.ts +0 -3
- package/collections/report-builder-library.collection.js +0 -87
- package/collections/report-builder-library.collection.js.map +0 -1
- package/collections/report-builder-report.collection.d.ts +0 -4
- package/collections/report-builder-report.collection.js +0 -149
- package/collections/report-builder-report.collection.js.map +0 -1
- package/collections/user-group.collection.d.ts +0 -4
- package/collections/user-group.collection.js +0 -90
- package/collections/user-group.collection.js.map +0 -1
- package/collections/user-guide.collection.d.ts +0 -3
- package/collections/user-guide.collection.js +0 -57
- package/collections/user-guide.collection.js.map +0 -1
- package/collections/user.collection.d.ts +0 -4
- package/collections/user.collection.js +0 -259
- package/collections/user.collection.js.map +0 -1
- package/cron/cron.d.ts +0 -5
- package/cron/cron.js +0 -162
- package/cron/cron.js.map +0 -1
- package/fixtures/cron-jobs.d.ts +0 -1
- package/fixtures/cron-jobs.js +0 -73
- package/fixtures/cron-jobs.js.map +0 -1
- package/fixtures/init.d.ts +0 -1
- package/fixtures/init.js +0 -59
- package/fixtures/init.js.map +0 -1
- package/http/auth.d.ts +0 -2
- package/http/auth.js +0 -763
- package/http/auth.js.map +0 -1
- package/http/health.d.ts +0 -1
- package/http/health.js +0 -12
- package/http/health.js.map +0 -1
- package/http/home.d.ts +0 -2
- package/http/home.js +0 -66
- package/http/home.js.map +0 -1
- package/index.d.ts +0 -23
- package/index.js +0 -127
- package/index.js.map +0 -1
- package/managers/cron.manager.d.ts +0 -17
- package/managers/cron.manager.js +0 -242
- package/managers/cron.manager.js.map +0 -1
- package/managers/method.manager.d.ts +0 -38
- package/managers/method.manager.js +0 -595
- package/managers/method.manager.js.map +0 -1
- package/managers/mongo.manager.d.ts +0 -178
- package/managers/mongo.manager.js +0 -2221
- package/managers/mongo.manager.js.map +0 -1
- package/managers/monitor.manager.d.ts +0 -59
- package/managers/monitor.manager.js +0 -351
- package/managers/monitor.manager.js.map +0 -1
- package/managers/subscription.manager.d.ts +0 -50
- package/managers/subscription.manager.js +0 -1091
- package/managers/subscription.manager.js.map +0 -1
- package/managers/subscription.performance.d.ts +0 -1
- package/managers/subscription.performance.js.map +0 -1
- package/methods/accounts.d.ts +0 -2
- package/methods/accounts.js +0 -274
- package/methods/accounts.js.map +0 -1
- package/methods/aws.d.ts +0 -2
- package/methods/aws.js +0 -617
- package/methods/aws.js.map +0 -1
- package/methods/collections.d.ts +0 -2
- package/methods/collections.js +0 -499
- package/methods/collections.js.map +0 -1
- package/methods/counters.d.ts +0 -2
- package/methods/counters.js +0 -122
- package/methods/counters.js.map +0 -1
- package/methods/cron-jobs.d.ts +0 -2
- package/methods/cron-jobs.js +0 -1315
- package/methods/cron-jobs.js.map +0 -1
- package/methods/flags.d.ts +0 -2
- package/methods/flags.js +0 -9
- package/methods/flags.js.map +0 -1
- package/methods/logs.d.ts +0 -2
- package/methods/logs.js +0 -371
- package/methods/logs.js.map +0 -1
- package/methods/monitor.d.ts +0 -2
- package/methods/monitor.js +0 -565
- package/methods/monitor.js.map +0 -1
- package/methods/pdf.d.ts +0 -2
- package/methods/pdf.js +0 -324
- package/methods/pdf.js.map +0 -1
- package/methods/report-builder.d.ts +0 -2
- package/methods/report-builder.js +0 -858
- package/methods/report-builder.js.map +0 -1
- package/methods/support.d.ts +0 -2
- package/methods/support.js +0 -246
- package/methods/support.js.map +0 -1
- package/models/app-status.model.js +0 -4
- package/models/app-status.model.js.map +0 -1
- package/models/billing-logged-in-users.model.js +0 -4
- package/models/billing-logged-in-users.model.js.map +0 -1
- package/models/collection-document.model.d.ts +0 -19
- package/models/collection-document.model.js +0 -4
- package/models/collection-document.model.js.map +0 -1
- package/models/counter.model.js +0 -4
- package/models/counter.model.js.map +0 -1
- package/models/cron-job-history.model.d.ts +0 -12
- package/models/cron-job-history.model.js +0 -4
- package/models/cron-job-history.model.js.map +0 -1
- package/models/cron-job.model.d.ts +0 -13
- package/models/cron-job.model.js +0 -4
- package/models/cron-job.model.js.map +0 -1
- package/models/dialog.model.d.ts +0 -23
- package/models/dialog.model.js +0 -4
- package/models/dialog.model.js.map +0 -1
- package/models/email-history.model.js +0 -4
- package/models/email-history.model.js.map +0 -1
- package/models/email-verified.model.js +0 -4
- package/models/email-verified.model.js.map +0 -1
- package/models/file.model.js +0 -4
- package/models/file.model.js.map +0 -1
- package/models/flag.model.js +0 -4
- package/models/flag.model.js.map +0 -1
- package/models/log-method-latency.model.js +0 -4
- package/models/log-method-latency.model.js.map +0 -1
- package/models/log-subscription.model.js +0 -4
- package/models/log-subscription.model.js.map +0 -1
- package/models/log.model.js +0 -4
- package/models/log.model.js.map +0 -1
- package/models/logged-in-users.model.js +0 -4
- package/models/logged-in-users.model.js.map +0 -1
- package/models/method-response.model.js +0 -4
- package/models/method-response.model.js.map +0 -1
- package/models/method.model.d.ts +0 -9
- package/models/method.model.js +0 -4
- package/models/method.model.js.map +0 -1
- package/models/monitor-cpu.model.js +0 -4
- package/models/monitor-cpu.model.js.map +0 -1
- package/models/monitor-memory.model.d.ts +0 -15
- package/models/monitor-memory.model.js +0 -4
- package/models/monitor-memory.model.js.map +0 -1
- package/models/monitor-mongo.model.d.ts +0 -13
- package/models/monitor-mongo.model.js +0 -4
- package/models/monitor-mongo.model.js.map +0 -1
- package/models/notification.model.js +0 -4
- package/models/notification.model.js.map +0 -1
- package/models/pagination.model.d.ts +0 -10
- package/models/pagination.model.js +0 -24
- package/models/pagination.model.js.map +0 -1
- package/models/permission.model.d.ts +0 -12
- package/models/permission.model.js +0 -4
- package/models/permission.model.js.map +0 -1
- package/models/report-builder-dashboard-builder.model.d.ts +0 -25
- package/models/report-builder-dashboard-builder.model.js +0 -4
- package/models/report-builder-dashboard-builder.model.js.map +0 -1
- package/models/report-builder-library.model.d.ts +0 -17
- package/models/report-builder-library.model.js +0 -4
- package/models/report-builder-library.model.js.map +0 -1
- package/models/report-builder-report.model.d.ts +0 -98
- package/models/report-builder-report.model.js +0 -4
- package/models/report-builder-report.model.js.map +0 -1
- package/models/report-builder.model.d.ts +0 -61
- package/models/report-builder.model.js +0 -4
- package/models/report-builder.model.js.map +0 -1
- package/models/select-data-label.model.d.ts +0 -9
- package/models/select-data-label.model.js +0 -4
- package/models/select-data-label.model.js.map +0 -1
- package/models/server-response.model.d.ts +0 -5
- package/models/server-response.model.js +0 -4
- package/models/server-response.model.js.map +0 -1
- package/models/subscription.model.d.ts +0 -23
- package/models/subscription.model.js +0 -4
- package/models/subscription.model.js.map +0 -1
- package/models/support-ticket.model.d.ts +0 -72
- package/models/support-ticket.model.js +0 -4
- package/models/support-ticket.model.js.map +0 -1
- package/models/user-group.model.d.ts +0 -19
- package/models/user-group.model.js +0 -4
- package/models/user-group.model.js.map +0 -1
- package/models/user-guide.model.js +0 -4
- package/models/user-guide.model.js.map +0 -1
- package/models/user.model.d.ts +0 -70
- package/models/user.model.js +0 -4
- package/models/user.model.js.map +0 -1
- package/public_api.js +0 -75
- package/public_api.js.map +0 -1
- package/publications/app-status.d.ts +0 -2
- package/publications/app-status.js +0 -17
- package/publications/app-status.js.map +0 -1
- package/publications/cron-jobs.d.ts +0 -2
- package/publications/cron-jobs.js +0 -33
- package/publications/cron-jobs.js.map +0 -1
- package/publications/files.d.ts +0 -2
- package/publications/files.js +0 -37
- package/publications/files.js.map +0 -1
- package/publications/flags.d.ts +0 -2
- package/publications/flags.js +0 -23
- package/publications/flags.js.map +0 -1
- package/publications/logs.d.ts +0 -2
- package/publications/logs.js +0 -165
- package/publications/logs.js.map +0 -1
- package/publications/method-responses.d.ts +0 -2
- package/publications/method-responses.js +0 -17
- package/publications/method-responses.js.map +0 -1
- package/publications/notifications.d.ts +0 -2
- package/publications/notifications.js +0 -17
- package/publications/notifications.js.map +0 -1
- package/publications/report-builder-dashboard-builders.d.ts +0 -2
- package/publications/report-builder-dashboard-builders.js +0 -43
- package/publications/report-builder-dashboard-builders.js.map +0 -1
- package/publications/report-builder-libraries.d.ts +0 -2
- package/publications/report-builder-libraries.js +0 -90
- package/publications/report-builder-libraries.js.map +0 -1
- package/publications/report-builder-reports.d.ts +0 -2
- package/publications/report-builder-reports.js +0 -51
- package/publications/report-builder-reports.js.map +0 -1
- package/publications/super-admin.d.ts +0 -2
- package/publications/super-admin.js +0 -17
- package/publications/super-admin.js.map +0 -1
- package/publications/user-groups.d.ts +0 -1
- package/publications/user-groups.js +0 -17
- package/publications/user-groups.js.map +0 -1
- package/publications/user-guides.d.ts +0 -1
- package/publications/user-guides.js +0 -17
- package/publications/user-guides.js.map +0 -1
- package/server-app.d.ts +0 -52
- package/server-app.js +0 -739
- package/server-app.js.map +0 -1
- package/util/common.d.ts +0 -25
- package/util/common.js +0 -524
- package/util/common.js.map +0 -1
- package/util/schema-report-builder.d.ts +0 -6
- package/util/schema-report-builder.js +0 -453
- package/util/schema-report-builder.js.map +0 -1
- /package/{private → src/private}/email-templates/enrollment.html +0 -0
- /package/{private → src/private}/email-templates/forgot-password.html +0 -0
- /package/{private → src/private}/email-templates/support-ticket-deleted.html +0 -0
- /package/{private → src/private}/email-templates/support-ticket-modified.html +0 -0
- /package/{private → src/private}/email-templates/support-ticket.html +0 -0
- /package/{private → src/private}/images/ResolveIO.png +0 -0
|
@@ -0,0 +1,876 @@
|
|
|
1
|
+
import SimpleSchema from 'simpl-schema';
|
|
2
|
+
import { MethodManager } from '../managers/method.manager';
|
|
3
|
+
import { PaginationOptions, PaginationOptionsSchema } from '../models/pagination.model';
|
|
4
|
+
import { ResolveIOServer } from '../index';
|
|
5
|
+
import { getReportLookupSchemas, buildTree, mongoCommand } from '../util/schema-report-builder';
|
|
6
|
+
|
|
7
|
+
export function loadReportBuilderMethods(methodManager: MethodManager) {
|
|
8
|
+
methodManager.methods({
|
|
9
|
+
reportBuilderBuildTree: {
|
|
10
|
+
check: new SimpleSchema({
|
|
11
|
+
collection_root: {
|
|
12
|
+
type: String
|
|
13
|
+
}
|
|
14
|
+
}),
|
|
15
|
+
function: function(collection_root: string) {
|
|
16
|
+
return new Promise(async (resolve, reject) => {
|
|
17
|
+
let lookupSchemaData = getReportLookupSchemas(collection_root).sort((a, b) => a.collection_name.localeCompare(b.collection_name));
|
|
18
|
+
let lookupSchemaTree = lookupSchemaData.filter(a => a.is_root === true)[0].tree;
|
|
19
|
+
|
|
20
|
+
let treeItems = buildTree(collection_root, lookupSchemaTree);
|
|
21
|
+
|
|
22
|
+
Object.keys(lookupSchemaTree).filter(a => a.endsWith('(Lookup)')).forEach(lookup => {
|
|
23
|
+
let fieldPath = lookup.split('.');
|
|
24
|
+
let field = null;
|
|
25
|
+
fieldPath.forEach(path => {
|
|
26
|
+
if (!field) {
|
|
27
|
+
field = treeItems.filter(a => a.fieldName === path)[0];
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
field = field.children.filter(a => a.fieldName === path)[0];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
field.children = buildTree(lookupSchemaTree[lookup].lookup_collection, lookupSchemaData.filter(a => a.collection_name === lookupSchemaTree[lookup].lookup_collection)[0].tree, field);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// console.log('----------- Tree Items -----------', treeItems);
|
|
37
|
+
|
|
38
|
+
resolve({isLeaf: false, children: treeItems});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
reportBuilderGetDistinctValue: {
|
|
43
|
+
check: new SimpleSchema({
|
|
44
|
+
treeLeaf: {
|
|
45
|
+
type: Object,
|
|
46
|
+
blackbox: true
|
|
47
|
+
}
|
|
48
|
+
}),
|
|
49
|
+
function: function(treeLeaf) {
|
|
50
|
+
return new Promise(async (resolve, reject) => {
|
|
51
|
+
if (treeLeaf.fieldType === 'String') {
|
|
52
|
+
if (treeLeaf.fieldPath.includes('(Lookup)')) {
|
|
53
|
+
resolve(await mongoCommand('distinct', treeLeaf.collection_name, treeLeaf.fieldPath.replace(/^.+\(.+\) \(Lookup\)\.\$\./g, '').replace(/\.\$/g, '')));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
resolve(await mongoCommand('distinct', treeLeaf.collection_name, treeLeaf.fieldPath.replace(/\.\$/g, '')));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
resolve([]);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
reportBuilderGetResults: {
|
|
66
|
+
check: new SimpleSchema({
|
|
67
|
+
reportType: {
|
|
68
|
+
type: String
|
|
69
|
+
},
|
|
70
|
+
rootCollectionName: {
|
|
71
|
+
type: String
|
|
72
|
+
},
|
|
73
|
+
options: {
|
|
74
|
+
type: PaginationOptionsSchema
|
|
75
|
+
},
|
|
76
|
+
filters: {
|
|
77
|
+
type: Array,
|
|
78
|
+
optional: true
|
|
79
|
+
},
|
|
80
|
+
'filters.$': {
|
|
81
|
+
type: Object,
|
|
82
|
+
blackbox: true,
|
|
83
|
+
optional: true
|
|
84
|
+
},
|
|
85
|
+
filterArrays: {
|
|
86
|
+
type: Array,
|
|
87
|
+
optional: true
|
|
88
|
+
},
|
|
89
|
+
'filterArrays.$': {
|
|
90
|
+
type: Object,
|
|
91
|
+
blackbox: true,
|
|
92
|
+
optional: true
|
|
93
|
+
},
|
|
94
|
+
filterArrayFields: {
|
|
95
|
+
type: Array,
|
|
96
|
+
optional: true
|
|
97
|
+
},
|
|
98
|
+
'filterArrayFields.$': {
|
|
99
|
+
type: Object,
|
|
100
|
+
blackbox: true,
|
|
101
|
+
optional: true
|
|
102
|
+
},
|
|
103
|
+
selectedFields: {
|
|
104
|
+
type: Array,
|
|
105
|
+
optional: true
|
|
106
|
+
},
|
|
107
|
+
'selectedFields.$': {
|
|
108
|
+
type: Object,
|
|
109
|
+
optional: true,
|
|
110
|
+
blackbox: true
|
|
111
|
+
},
|
|
112
|
+
customFields: {
|
|
113
|
+
type: Array,
|
|
114
|
+
optional: true
|
|
115
|
+
},
|
|
116
|
+
'customFields.$': {
|
|
117
|
+
type: Object,
|
|
118
|
+
optional: true,
|
|
119
|
+
blackbox: true
|
|
120
|
+
},
|
|
121
|
+
groupsRow: {
|
|
122
|
+
type: Array
|
|
123
|
+
},
|
|
124
|
+
'groupsRow.$': {
|
|
125
|
+
type: Object,
|
|
126
|
+
blackbox: true
|
|
127
|
+
},
|
|
128
|
+
fieldsTotal: {
|
|
129
|
+
type: Array
|
|
130
|
+
},
|
|
131
|
+
'fieldsTotal.$': {
|
|
132
|
+
type: Object,
|
|
133
|
+
blackbox: true
|
|
134
|
+
},
|
|
135
|
+
fieldsLink: {
|
|
136
|
+
type: Array
|
|
137
|
+
},
|
|
138
|
+
'fieldsLink.$': {
|
|
139
|
+
type: Object,
|
|
140
|
+
blackbox: true
|
|
141
|
+
},
|
|
142
|
+
date_field: {
|
|
143
|
+
type: String,
|
|
144
|
+
optional: true
|
|
145
|
+
},
|
|
146
|
+
date_interval: {
|
|
147
|
+
type: String,
|
|
148
|
+
optional: true
|
|
149
|
+
},
|
|
150
|
+
displayType: {
|
|
151
|
+
type: String,
|
|
152
|
+
optional: true
|
|
153
|
+
}
|
|
154
|
+
}),
|
|
155
|
+
function: function(reportType: string, rootCollectionName: string, rootOptions: PaginationOptions, filters: any[] = [], filterArrays: any[] = [], filterArrayFields: any[] = [], selectedFields: any[] = [], customFields: any[] = [], groupsRow: any[] = [], fieldsTotal: any[] = [], fieldsLink: any[] = [], date_field = '', date_interval = '', displayType = '') {
|
|
156
|
+
return new Promise(async (resolve, reject) => {
|
|
157
|
+
|
|
158
|
+
let modelCollection = ResolveIOServer.getMongoManager().collection(rootCollectionName);
|
|
159
|
+
|
|
160
|
+
if (!modelCollection) {
|
|
161
|
+
reject('Invalid collection');
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
// Query
|
|
165
|
+
let query = [];
|
|
166
|
+
|
|
167
|
+
// Non-Lookup Match Filters
|
|
168
|
+
let initialQueryMatchCondition = {
|
|
169
|
+
$and: []
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// selectedFields.filter(a => a.collection_name === rootCollectionName).forEach(field => {
|
|
173
|
+
// initialQueryMatchCondition['$and'].push({
|
|
174
|
+
// $and: [
|
|
175
|
+
// {[field.fieldPath.replace(/\.\$/g, '')]: {$exists: 1}},
|
|
176
|
+
// // {[field.fieldPath.replace(/\.\$/g, '')]: {$ne: ''}},
|
|
177
|
+
// {[field.fieldPath.replace(/\.\$/g, '')]: {$ne: null}},
|
|
178
|
+
// ]
|
|
179
|
+
// });
|
|
180
|
+
// });
|
|
181
|
+
|
|
182
|
+
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) {
|
|
183
|
+
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)'))))).forEach(filter => {
|
|
184
|
+
initialQueryMatchCondition['$and'].push(filter);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Add filter to query
|
|
189
|
+
// 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) {
|
|
190
|
+
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) {
|
|
191
|
+
query.push({
|
|
192
|
+
$match: initialQueryMatchCondition
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Add count
|
|
197
|
+
query.push({ $addFields: { count: { $sum: 1} } });
|
|
198
|
+
|
|
199
|
+
let sizes = [];
|
|
200
|
+
|
|
201
|
+
filterArrayFields.filter(a => a.fieldPath).forEach(filterField => {
|
|
202
|
+
let fieldData = filterField.fieldPath.split('.$');
|
|
203
|
+
|
|
204
|
+
let fieldPath = '';
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < fieldData.length - 1; i++) {
|
|
207
|
+
|
|
208
|
+
fieldPath += fieldData[i];
|
|
209
|
+
|
|
210
|
+
if (fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup)')) {
|
|
211
|
+
if (!query.some(a => a.$lookup && a.$lookup.as === filterField.lookup_as.replace(/\.\$/g, ''))) {
|
|
212
|
+
query.push({$lookup: {
|
|
213
|
+
from: filterField.lookup_collection,
|
|
214
|
+
localField: filterField.lookup_local_key,
|
|
215
|
+
foreignField: filterField.lookup_foreign_key,
|
|
216
|
+
as: filterField.lookup_as.replace(/\.\$/g, '')
|
|
217
|
+
}});
|
|
218
|
+
|
|
219
|
+
sizes.push(filterField.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_'));
|
|
220
|
+
query.push({$addFields: {['size_' + filterField.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_')]: {$size: {$ifNull: ['$' + filterField.lookup_as.replace(/\.\$/g, '').replace(/\./g, '_'), []]}}}});
|
|
221
|
+
|
|
222
|
+
query.push({'$unwind': {path: '$' + filterField.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true}});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (!query.some(a => a.$addFields && Object.keys(a.$addFields).includes('size_' + fieldPath.replace(/\./g, '_')))) {
|
|
227
|
+
sizes.push(fieldPath.replace(/\./g, '_'));
|
|
228
|
+
query.push({$addFields: {['size_' + fieldPath.replace(/\./g, '_')]: {$size: {$ifNull: ['$' + fieldPath, []]}}}});
|
|
229
|
+
|
|
230
|
+
query.push({'$unwind': {path: '$' + fieldPath, preserveNullAndEmptyArrays: true}});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Groups - Lookups / Sizes / Unwinds
|
|
237
|
+
groupsRow.forEach(row => {
|
|
238
|
+
if (row.field.includes('$')) {
|
|
239
|
+
let fieldData = row.field.split('.$');
|
|
240
|
+
|
|
241
|
+
let fieldPath = '';
|
|
242
|
+
|
|
243
|
+
for (let i = 0; i < fieldData.length - 1; i++) {
|
|
244
|
+
|
|
245
|
+
fieldPath += fieldData[i];
|
|
246
|
+
|
|
247
|
+
if (fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup)')) {
|
|
248
|
+
if (!query.some(a => a.$lookup && a.$lookup.as === row.treeItem.lookup_as.replace(/\.\$/g, ''))) {
|
|
249
|
+
query.push({$lookup: {
|
|
250
|
+
from: row.treeItem.lookup_collection,
|
|
251
|
+
localField: row.treeItem.lookup_local_key,
|
|
252
|
+
foreignField: row.treeItem.lookup_foreign_key,
|
|
253
|
+
as: row.treeItem.lookup_as.replace(/\.\$/g, '')
|
|
254
|
+
}});
|
|
255
|
+
|
|
256
|
+
sizes.push(row.treeItem.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_'));
|
|
257
|
+
query.push({$addFields: {['size_' + row.treeItem.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_')]: {$size: {$ifNull: ['$' + row.treeItem.lookup_as.replace(/\.\$/g, '').replace(/\./g, '_'), []]}}}});
|
|
258
|
+
|
|
259
|
+
query.push({'$unwind': {path: '$' + row.treeItem.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true}});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
if (!query.some(a => a.$addFields && Object.keys(a.$addFields).includes('size_' + fieldPath.replace(/\./g, '_')))) {
|
|
264
|
+
sizes.push(fieldPath.replace(/\./g, '_'));
|
|
265
|
+
query.push({$addFields: {['size_' + fieldPath.replace(/\./g, '_')]: {$size: {$ifNull: ['$' + fieldPath, []]}}}});
|
|
266
|
+
|
|
267
|
+
query.push({'$unwind': {path: '$' + fieldPath, preserveNullAndEmptyArrays: true}});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// Selected Fields - Lookups / Sizes / Unwinds
|
|
275
|
+
selectedFields.forEach(field => {
|
|
276
|
+
if (field.fieldPath.includes('$')) {
|
|
277
|
+
let fieldData = field.fieldPath.split('.$');
|
|
278
|
+
|
|
279
|
+
let fieldPath = '';
|
|
280
|
+
|
|
281
|
+
for (let i = 0; i < fieldData.length - 1; i++) {
|
|
282
|
+
|
|
283
|
+
fieldPath += fieldData[i];
|
|
284
|
+
|
|
285
|
+
if (fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup)')) {
|
|
286
|
+
if (!query.some(a => a.$lookup && a.$lookup.as === field.lookup_as.replace(/\.\$/g, ''))) {
|
|
287
|
+
query.push({$lookup: {
|
|
288
|
+
from: field.lookup_collection,
|
|
289
|
+
localField: field.lookup_local_key,
|
|
290
|
+
foreignField: field.lookup_foreign_key,
|
|
291
|
+
as: field.lookup_as.replace(/\.\$/g, '')
|
|
292
|
+
}});
|
|
293
|
+
|
|
294
|
+
sizes.push(field.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_'));
|
|
295
|
+
query.push({$addFields: {['size_' + field.lookup_as.replace(/\.\$\./g, '_').replace(/\./g, '_')]: {$size: {$ifNull: ['$' + field.lookup_as.replace(/\.\$/g, '').replace(/\./g, '_'), []]}}}});
|
|
296
|
+
|
|
297
|
+
query.push({'$unwind': {path: '$' + field.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true}});
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
if (!fieldData[i].includes('(Lookup)')) {
|
|
301
|
+
if (!query.some(a => a.$addFields && Object.keys(a.$addFields).includes('size_' + fieldPath.replace(/\./g, '_')))) {
|
|
302
|
+
sizes.push(fieldPath.replace(/\./g, '_'));
|
|
303
|
+
query.push({$addFields: {['size_' + fieldPath.replace(/\./g, '_')]: {$size: {$ifNull: ['$' + fieldPath, []]}}}});
|
|
304
|
+
|
|
305
|
+
query.push({'$unwind': {path: '$' + fieldPath, preserveNullAndEmptyArrays: true}});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
if (!query.some(a => a.$addFields && Object.keys(a.$addFields).includes('size_' + fieldPath.replace(/\./g, '_')))) {
|
|
312
|
+
sizes.push(fieldPath.replace(/\./g, '_'));
|
|
313
|
+
query.push({$addFields: {['size_' + fieldPath.replace(/\./g, '_')]: {$size: {$ifNull: ['$' + fieldPath, []]}}}});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (field.leafValueType !== 'Count' && !query.some(a => a.$unwind && a.$unwind.path === '$' + fieldPath)) {
|
|
317
|
+
query.push({'$unwind': {path: '$' + fieldPath, preserveNullAndEmptyArrays: true}});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// Divide all number fields by sizes
|
|
325
|
+
let divFields = {$addFields: {}};
|
|
326
|
+
|
|
327
|
+
fieldsTotal.filter(a => a.type === 'sum').forEach(total => {
|
|
328
|
+
selectedFields.filter(a => a.fieldType === 'Number' || a.leafValueType === 'Count').forEach(field => {
|
|
329
|
+
let multFields = [];
|
|
330
|
+
|
|
331
|
+
if (!field.fieldPath.includes('.$')) {
|
|
332
|
+
sizes.forEach(size => {
|
|
333
|
+
multFields.push({$cond: { if: { $gt: ['$size_' + size, 0 ] }, then: '$size_' + size, else: 1 }});
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
sizes.filter(a => !field.fieldPath.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a) && !filterArrayFields.filter(a => a.fieldPath).some(b => b.fieldPath.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a)) && !fieldsLink.some(b => b.field_first.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a) || b.field_second.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a))).forEach(size => {
|
|
338
|
+
multFields.push({$cond: { if: { $gt: ['$size_' + size, 0 ] }, then: '$size_' + size, else: 1 }});
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (multFields.length) {
|
|
343
|
+
if (field.leafValueType === 'Count') {
|
|
344
|
+
divFields.$addFields[total.id + '_' + field.id] = {
|
|
345
|
+
$divide: ['$count', {$multiply: multFields}]
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
divFields.$addFields[total.id + '_' + field.id] = {
|
|
350
|
+
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), {$multiply: multFields}]
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
if (field.leafValueType === 'Count') {
|
|
356
|
+
divFields.$addFields[total.id + '_' + field.id] = '$count';
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
divFields.$addFields[total.id + '_' + field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
selectedFields.filter(a => a.fieldType === 'Number' && a.leafValueType === 'Sum').forEach(field => {
|
|
366
|
+
let multFields = [];
|
|
367
|
+
|
|
368
|
+
sizes.filter(a => !field.fieldPath.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a) && !groupsRow.some(b => b.field.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a)) && !filterArrayFields.filter(a => a.fieldPath).some(b => b.fieldPath.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a)) && !fieldsLink.some(b => b.field_first.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a) || b.field_second.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a))).forEach(size => {
|
|
369
|
+
multFields.push({$cond: { if: { $gt: ['$size_' + size, 0 ] }, then: '$size_' + size, else: 1 }});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
if (multFields.length) {
|
|
373
|
+
divFields.$addFields[field.fieldPath.replace(/\.\$/g, '')] = {
|
|
374
|
+
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), {$multiply: multFields}]
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
selectedFields.filter(a => a.leafValueType === 'Count').forEach(field => {
|
|
380
|
+
let multFields = [];
|
|
381
|
+
|
|
382
|
+
sizes.filter(a => !field.fieldPath.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a) && !groupsRow.some(b => b.field.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a)) && !filterArrayFields.filter(a => a.fieldPath).some(b => b.fieldPath.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a)) && !fieldsLink.some(b => b.field_first.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a) || b.field_second.replace(/\.\$\./g, '_').replace(/\./g, '_').includes(a))).forEach(size => {
|
|
383
|
+
multFields.push({$cond: { if: { $gt: ['$size_' + size, 0 ] }, then: '$size_' + size, else: 1 }});
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
if (multFields.length) {
|
|
387
|
+
divFields.$addFields['count_' + field.fieldPath.replace(/\.\$/g, '')] = {
|
|
388
|
+
$divide: ['$count', {$multiply: multFields}]
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
if (reportType !== 'Dated' && sizes.filter(a => field.fieldPath.replace(/\.\$\./g, '_').includes(a)).length) {
|
|
393
|
+
sizes.filter(a => field.fieldPath.replace(/\.\$\./g, '_').includes(a)).forEach(size => {
|
|
394
|
+
divFields.$addFields['count_' + field.fieldPath.replace(/\.\$/g, '')] = '$size_' + size;
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
divFields.$addFields['count_' + field.fieldPath.replace(/\.\$/g, '')] = '$count';
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
fieldsTotal.filter(a => a.type === 'avg').forEach(total => {
|
|
404
|
+
selectedFields.filter(a => a.fieldType === 'Number' || a.leafValueType === 'Count').forEach(field => {
|
|
405
|
+
divFields.$addFields[total.id + '_' + field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
if (Object.keys(divFields.$addFields).length > 0) {
|
|
410
|
+
query.push(divFields);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
414
|
+
query.push({$addFields: {[link.id]: {$cmp: ['$' + link.field_first.replace(/\.\$/g, ''), '$' + link.field_second.replace(/\.\$/g, '')]}}});
|
|
415
|
+
query.push({$match: {[link.id]: 0}});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
customFields.forEach(cust => {
|
|
419
|
+
cust.operations.filter(a => a.operation && !a.fields.some(b => (b.field === null || b.field === '') && b.value === null)).forEach((operation, opIndex) => {
|
|
420
|
+
let custFieldGroup = {};
|
|
421
|
+
|
|
422
|
+
if (operation.operation === 'divide') {
|
|
423
|
+
let condCheck = [];
|
|
424
|
+
operation.fields.filter(a => a.field || a.value !== null).forEach(field => {
|
|
425
|
+
if (field.field) {
|
|
426
|
+
condCheck.push({$eq: ['$' + field.field.replace(/\.\$/g, ''), 0]});
|
|
427
|
+
condCheck.push({$eq: ['$' + field.field.replace(/\.\$/g, ''), null]});
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
condCheck.push({$eq: [field.value, 0]});
|
|
431
|
+
condCheck.push({$eq: [field.value, null]});
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
if (opIndex > 0) {
|
|
436
|
+
condCheck.push({$eq: ['$' + cust.selFieldId, 0]});
|
|
437
|
+
condCheck.push({$eq: ['$' + cust.selFieldId, null]});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
let tmpOperationFields = [];
|
|
441
|
+
|
|
442
|
+
if (opIndex > 0) {
|
|
443
|
+
tmpOperationFields.push('$' + cust.selFieldId);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
operation.fields.filter(a => a.field || a.value !== null).forEach(field => {
|
|
447
|
+
if (field.field) {
|
|
448
|
+
tmpOperationFields.push('$' + field.field.replace(/\.\$/g, ''));
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
tmpOperationFields.push(field.value);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
custFieldGroup[cust.selFieldId] = {
|
|
456
|
+
$cond: { if: { $or: condCheck }, then: 0, else: {['$' + operation.operation]: tmpOperationFields} }
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
custFieldGroup[cust.selFieldId] = {
|
|
461
|
+
['$' + operation.operation]: []
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
if (opIndex > 0) {
|
|
465
|
+
custFieldGroup[cust.selFieldId]['$' + operation.operation].push({$cond: { if: { $eq: ['$' + cust.selFieldId, null] }, then: 0, else: '$' + cust.selFieldId}});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
let condOr = [];
|
|
469
|
+
|
|
470
|
+
operation.fields.filter(a => a.field || a.value !== null).forEach(field => {
|
|
471
|
+
if (field.field) {
|
|
472
|
+
condOr.push({$eq: ['$' + field.field.replace(/\.\$/g, ''), null]});
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
condOr.push({$eq: [field.value, null]});
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
operation.fields.filter(a => a.field || a.value !== null).forEach(field => {
|
|
480
|
+
if (field.field) {
|
|
481
|
+
custFieldGroup[cust.selFieldId]['$' + operation.operation].push({$cond: { if: { $or: condOr }, then: 0, else: '$' + field.field.replace(/\.\$/g, '')}});
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
custFieldGroup[cust.selFieldId]['$' + operation.operation].push({$cond: { if: { $or: condOr }, then: 0, else: field.value}});
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
query.push({$addFields: custFieldGroup});
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
fieldsTotal.forEach(total => {
|
|
494
|
+
customFields.forEach(field => {
|
|
495
|
+
query.push({$addFields:{[total.id + '_' + field.selFieldId]: '$' + field.selFieldId}});
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
// Filters - Lookups / Arrays
|
|
500
|
+
let queryMatchConditionLookup = {
|
|
501
|
+
$and: []
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
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) {
|
|
505
|
+
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)'))))).forEach(filter => {
|
|
506
|
+
queryMatchConditionLookup['$and'].push(filter);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
filterArrays.forEach(filter => {
|
|
511
|
+
queryMatchConditionLookup['$and'].push(filter);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
if (queryMatchConditionLookup.$and.length) {
|
|
515
|
+
query.push({$match: queryMatchConditionLookup});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Group
|
|
519
|
+
let queryGroup = {
|
|
520
|
+
_id: {}
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
if (groupsRow.length || !displayType || displayType === 'grouped') {
|
|
524
|
+
if (!groupsRow.length) {
|
|
525
|
+
queryGroup._id = '$_id';
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
if (rootOptions.sort) {
|
|
529
|
+
let groupedSorts = {};
|
|
530
|
+
Object.keys(rootOptions.sort).forEach(sortField => {
|
|
531
|
+
let selField = selectedFields.filter(a => a.id === sortField)[0];
|
|
532
|
+
|
|
533
|
+
if (selField) {
|
|
534
|
+
groupedSorts[selField.fieldPath.replace(/\.\$/g, '')] = rootOptions.sort[sortField];
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
if (Object.keys(groupedSorts).length > 0) {
|
|
538
|
+
query.push({
|
|
539
|
+
$sort: groupedSorts
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
groupsRow.forEach(row => {
|
|
546
|
+
queryGroup._id[row.id] = '$' + row.field.replace(/\.\$/g, '');
|
|
547
|
+
|
|
548
|
+
if (date_field && date_interval) {
|
|
549
|
+
if (date_interval === 'Seconds') {
|
|
550
|
+
queryGroup._id['second'] = {'$second' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
551
|
+
queryGroup._id['minute'] = {'$minute' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
552
|
+
queryGroup._id['hour'] = {'$hour' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
553
|
+
queryGroup._id['day'] = {'$dayOfMonth' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
554
|
+
queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
555
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
556
|
+
}
|
|
557
|
+
else if (date_interval === 'Minutes') {
|
|
558
|
+
queryGroup._id['minute'] = {'$minute' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
559
|
+
queryGroup._id['hour'] = {'$hour' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
560
|
+
queryGroup._id['day'] = {'$dayOfMonth' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
561
|
+
queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
562
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
563
|
+
}
|
|
564
|
+
else if (date_interval === 'Hours') {
|
|
565
|
+
queryGroup._id['hour'] = {'$hour' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
566
|
+
queryGroup._id['day'] = {'$dayOfMonth' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
567
|
+
queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
568
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
569
|
+
}
|
|
570
|
+
else if (date_interval === 'Daily') {
|
|
571
|
+
queryGroup._id['day'] = {'$dayOfMonth' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
572
|
+
queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
573
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
574
|
+
}
|
|
575
|
+
else if (date_interval === 'Weekly') {
|
|
576
|
+
queryGroup._id['week'] = {'$isoWeek' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
577
|
+
queryGroup._id['year'] = {'$isoWeekYear' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
578
|
+
}
|
|
579
|
+
else if (date_interval === 'Monthly') {
|
|
580
|
+
queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
581
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
582
|
+
}
|
|
583
|
+
else if (date_interval === 'Quarterly') {
|
|
584
|
+
queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
585
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
586
|
+
}
|
|
587
|
+
else if (date_interval === 'Yearly') {
|
|
588
|
+
queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': process.env.TZ}};
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
selectedFields.forEach(field => {
|
|
594
|
+
if (field.leafValueType === 'Average') {
|
|
595
|
+
queryGroup[field.id] = {'$avg': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
596
|
+
}
|
|
597
|
+
else if (field.leafValueType === 'Sum') {
|
|
598
|
+
queryGroup[field.id] = {'$sum': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
599
|
+
}
|
|
600
|
+
else if (field.leafValueType === 'Count') {
|
|
601
|
+
queryGroup[field.id] = { '$sum': '$count_' + field.fieldPath.replace(/\.\$/g, '') };
|
|
602
|
+
}
|
|
603
|
+
else if (field.leafValueType === 'Minimum') {
|
|
604
|
+
queryGroup[field.id] = {'$min': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
605
|
+
}
|
|
606
|
+
else if (field.leafValueType === 'Maximum') {
|
|
607
|
+
queryGroup[field.id] = {'$max': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
608
|
+
}
|
|
609
|
+
else if (field.leafValueType === 'First' || (!groupsRow.length && (!field.fieldPath.includes('.$') || field.fieldPath.includes('(Lookup)')))) {
|
|
610
|
+
queryGroup[field.id] = {'$first': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
611
|
+
}
|
|
612
|
+
else if (field.leafValueType === 'Last') {
|
|
613
|
+
queryGroup[field.id] = {'$last': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
614
|
+
}
|
|
615
|
+
else if (field.leafValueType === 'Unique') {
|
|
616
|
+
queryGroup[field.id] = {'$addToSet': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
queryGroup[field.id] = {'$push': {$ifNull: ['$' + field.fieldPath.replace(/\.\$/g, ''), null]}};
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
customFields.forEach(cust => {
|
|
624
|
+
if (cust.leafValueType === 'Average') {
|
|
625
|
+
queryGroup[cust.selFieldId] = {'$avg': '$' + cust.selFieldId};
|
|
626
|
+
}
|
|
627
|
+
else if (cust.leafValueType === 'Sum') {
|
|
628
|
+
queryGroup[cust.selFieldId] = {'$sum': '$' + cust.selFieldId};
|
|
629
|
+
}
|
|
630
|
+
else if (cust.leafValueType === 'Count') {
|
|
631
|
+
if (reportType === 'Dated') {
|
|
632
|
+
queryGroup[cust.selFieldId] = { '$sum': '$count_' + cust.selFieldId };
|
|
633
|
+
}
|
|
634
|
+
else {
|
|
635
|
+
queryGroup[cust.selFieldId] = { '$push': '$count_' + cust.selFieldId };
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
else if (cust.leafValueType === 'Minimum') {
|
|
639
|
+
queryGroup[cust.selFieldId] = {'$min': '$' + cust.selFieldId};
|
|
640
|
+
}
|
|
641
|
+
else if (cust.leafValueType === 'Maximum') {
|
|
642
|
+
queryGroup[cust.selFieldId] = {'$max': '$' + cust.selFieldId};
|
|
643
|
+
}
|
|
644
|
+
else if (cust.leafValueType === 'First') {
|
|
645
|
+
queryGroup[cust.selFieldId] = {'$first': '$' + cust.selFieldId};
|
|
646
|
+
}
|
|
647
|
+
else if (cust.leafValueType === 'Last') {
|
|
648
|
+
queryGroup[cust.selFieldId] = {'$last': '$' + cust.selFieldId};
|
|
649
|
+
}
|
|
650
|
+
else if (cust.leafValueType === 'Unique') {
|
|
651
|
+
queryGroup[cust.selFieldId] = {'$addToSet': '$' + cust.selFieldId};
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
queryGroup[cust.selFieldId] = {'$push': {$ifNull: ['$' + cust.selFieldId, null]}};
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
// Totals
|
|
659
|
+
if (fieldsTotal.length) {
|
|
660
|
+
fieldsTotal.forEach(total => {
|
|
661
|
+
selectedFields.filter(a => a.fieldType === 'Number' || a.leafValueType === 'Count').forEach(field => {
|
|
662
|
+
queryGroup[total.id + '_' + field.id] = {$sum: '$' + total.id + '_' + field.id};
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
customFields.forEach(field => {
|
|
666
|
+
queryGroup[total.id + '_' + field.selFieldId] = {$sum: '$' + total.id + '_' + field.selFieldId};
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
query.push({$group: queryGroup});
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
//Projection
|
|
675
|
+
let queryProjection = {
|
|
676
|
+
_id: 1
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
selectedFields.forEach(field => {
|
|
680
|
+
if (field.leafValueType === 'Average') {
|
|
681
|
+
queryProjection[field.id] = {$avg: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
682
|
+
}
|
|
683
|
+
else if (field.leafValueType === 'Sum') {
|
|
684
|
+
queryProjection[field.id] = {$sum: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
685
|
+
}
|
|
686
|
+
else if (field.leafValueType === 'Count') {
|
|
687
|
+
queryProjection[field.id] = {$sum: 1};
|
|
688
|
+
}
|
|
689
|
+
else if (field.leafValueType === 'Minimum') {
|
|
690
|
+
queryProjection[field.id] = {$min: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
691
|
+
}
|
|
692
|
+
else if (field.leafValueType === 'Maximum') {
|
|
693
|
+
queryProjection[field.id] = {$max: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
queryProjection[field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
customFields.forEach(cust => {
|
|
701
|
+
if (cust === 'Average') {
|
|
702
|
+
queryProjection[cust.selFieldId] = {$avg: '$' + cust.selFieldId};
|
|
703
|
+
}
|
|
704
|
+
else if (cust === 'Sum') {
|
|
705
|
+
queryProjection[cust.selFieldId] = {$sum: '$' + cust.selFieldId};
|
|
706
|
+
}
|
|
707
|
+
else if (cust === 'Count') {
|
|
708
|
+
queryProjection[cust.selFieldId] = {$sum: 1};
|
|
709
|
+
}
|
|
710
|
+
else if (cust === 'Minimum') {
|
|
711
|
+
queryProjection[cust.selFieldId] = {$min: '$' + cust.selFieldId};
|
|
712
|
+
}
|
|
713
|
+
else if (cust === 'Maximum') {
|
|
714
|
+
queryProjection[cust.selFieldId] = {$max: '$' + cust.selFieldId};
|
|
715
|
+
}
|
|
716
|
+
else {
|
|
717
|
+
queryProjection[cust.selFieldId] = '$' + cust.selFieldId;
|
|
718
|
+
}
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
//Totals
|
|
722
|
+
if (fieldsTotal.length && (reportType === 'List' || reportType === 'Dated')) {
|
|
723
|
+
fieldsTotal.forEach(total => {
|
|
724
|
+
selectedFields.filter(a => a.fieldType === 'Number' || a.leafValueType === 'Count').forEach(field => {
|
|
725
|
+
queryProjection[total.id + '_' + field.id] = {['$' + total.type]: '$' + total.id + '_' + field.id};
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
customFields.forEach(field => {
|
|
729
|
+
queryProjection[total.id + '_' + field.selFieldId] = {['$' + total.type]: '$' + total.id + '_' + field.selFieldId};
|
|
730
|
+
});
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
query.push({$project: queryProjection});
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// SORT
|
|
738
|
+
if (rootOptions.sort) {
|
|
739
|
+
query.push({
|
|
740
|
+
$sort: rootOptions.sort
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
if (reportType === 'Group' && groupsRow.length > 1) {
|
|
745
|
+
for (let i = groupsRow.length - 1; i >= 1; i--) {
|
|
746
|
+
let projQuery = {_id: 1};
|
|
747
|
+
|
|
748
|
+
projQuery[groupsRow[i].id] = {
|
|
749
|
+
_id: {[groupsRow[i].id]: '$_id.' + groupsRow[i].id}
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
if (i === groupsRow.length - 1) {
|
|
753
|
+
selectedFields.forEach(field => {
|
|
754
|
+
projQuery[groupsRow[i].id][field.id] = '$' + field.id;
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
customFields.forEach(field => {
|
|
758
|
+
projQuery[groupsRow[i].id][field.selFieldId] = '$' + field.selFieldId;
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
else {
|
|
762
|
+
projQuery[groupsRow[i].id][groupsRow[i + 1].id] = '$' + groupsRow[i + 1].id;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
fieldsTotal.forEach(totField => {
|
|
766
|
+
selectedFields.filter(b => b.fieldType === 'Number' || b.leafValueType === 'Count').forEach(selField => {
|
|
767
|
+
projQuery[totField.id + '_' + selField.id] = 1;
|
|
768
|
+
projQuery[groupsRow[i].id][totField.id + '_' + selField.id] = '$' + totField.id + '_' + selField.id;
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
customFields.forEach(field => {
|
|
772
|
+
projQuery[groupsRow[i].id][totField.id + '_' + field.selFieldId] = '$' + totField.id + '_' + field.selFieldId;
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
query.push({$project: projQuery});
|
|
777
|
+
|
|
778
|
+
let grQuery = {
|
|
779
|
+
_id: {}
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
for (let j = 0; j < i; j++) {
|
|
783
|
+
grQuery._id[groupsRow[j].id] = '$_id.' + groupsRow[j].id;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
grQuery[groupsRow[i].id] = {
|
|
787
|
+
$push: '$' + groupsRow[i].id
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
fieldsTotal.forEach(totField => {
|
|
791
|
+
selectedFields.filter(b => b.fieldType === 'Number' || b.leafValueType === 'Count').forEach(selField => {
|
|
792
|
+
grQuery[totField.id + '_' + selField.id] = {['$' + totField.type]: '$' + totField.id + '_' + selField.id};
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
customFields.forEach(field => {
|
|
796
|
+
grQuery[totField.id + '_' + field.selFieldId] = {['$' + totField.type]: '$' + totField.id + '_' + field.selFieldId};
|
|
797
|
+
});
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
query.push({$group: grQuery});
|
|
801
|
+
|
|
802
|
+
if (rootOptions.sort) {
|
|
803
|
+
let sortKeys = Object.keys(rootOptions.sort);
|
|
804
|
+
let sort = {};
|
|
805
|
+
|
|
806
|
+
let groupString = '';
|
|
807
|
+
groupsRow.forEach((row, groupIndex) => {
|
|
808
|
+
if (groupIndex > 0) {
|
|
809
|
+
groupString += 'gr_' + (groupIndex + 1) + '.';
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
if (sortKeys.includes('_id.gr_' + (groupIndex + 1))) {
|
|
813
|
+
sort[groupString + '_id.gr_' + (groupIndex + 1)] = rootOptions.sort['_id.gr_' + (groupIndex + 1)];
|
|
814
|
+
}
|
|
815
|
+
else {
|
|
816
|
+
sort[groupString + '_id.gr_' + (groupIndex + 1)] = 1;
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
sortKeys.filter(a => !a.startsWith('_id.gr_')).forEach(key => {
|
|
821
|
+
sort[groupString + key] = rootOptions.sort[key];
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
query.push({$sort: sort});
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
// console.log('_____________ query __________'); console.log(query); console.log(JSON.stringify(query, null, 2));
|
|
830
|
+
|
|
831
|
+
let res = null;
|
|
832
|
+
|
|
833
|
+
try {
|
|
834
|
+
res = await modelCollection.aggregate(query, {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);
|
|
835
|
+
}
|
|
836
|
+
catch (err) {
|
|
837
|
+
console.log(err);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
let tmpTotals = {};
|
|
841
|
+
|
|
842
|
+
if (res) {
|
|
843
|
+
fieldsTotal.forEach(totField => {
|
|
844
|
+
selectedFields.filter(b => b.fieldType === 'Number' || b.leafValueType === 'Count').forEach(selField => {
|
|
845
|
+
if (totField.type === 'sum') {
|
|
846
|
+
tmpTotals[totField.id + '_' + selField.id] = res.map(a => a[totField.id + '_' + selField.id] || 0).reduce((a, b) => a + b, 0);
|
|
847
|
+
}
|
|
848
|
+
else if (totField.type === 'avg') {
|
|
849
|
+
tmpTotals[totField.id + '_' + selField.id] = Math.round(res.map(a => a[totField.id + '_' + selField.id] || 0).reduce((a, b) => a + b, 0) / res.length);
|
|
850
|
+
}
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
customFields.forEach(custField => {
|
|
854
|
+
if (totField.type === 'sum') {
|
|
855
|
+
tmpTotals[totField.id + '_' + custField.selFieldId] = res.map(a => a[totField.id + '_' + custField.selFieldId] || 0).reduce((a, b) => a + b, 0);
|
|
856
|
+
}
|
|
857
|
+
else if (totField.type === 'avg') {
|
|
858
|
+
tmpTotals[totField.id + '_' + custField.selFieldId] = Math.round(res.map(a => a[totField.id + '_' + custField.selFieldId] || 0).reduce((a, b) => a + b, 0) / res.length);
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
let tmpRes = [{
|
|
865
|
+
count: res ? res.length : 0,
|
|
866
|
+
results: res ? (rootOptions.limit ? res.splice(0, rootOptions.limit) : res) : [],
|
|
867
|
+
totals: tmpTotals
|
|
868
|
+
}];
|
|
869
|
+
|
|
870
|
+
resolve(tmpRes);
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
},
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
}
|