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