Built motion from commit c2984ba.|0.0.114
[motion.git] / server / models / analytics_custom_report.js
1 'use strict';
2
3 module.exports = function(sequelize, DataTypes) {
4
5   var CustomReport = sequelize.define('CustomReport', {
6     name: DataTypes.STRING,
7     description: DataTypes.STRING,
8     parent: DataTypes.STRING,
9     table: DataTypes.STRING,
10     conditions: DataTypes.TEXT
11   }, {
12     paranoid: true,
13     tableName: 'analytics_custom_reports',
14     associate: function(models) {
15       CustomReport.hasMany(models.ReportField, {
16         as: 'Fields',
17         onDelete: 'cascade',
18         hooks: true
19       });
20       CustomReport.addScope('fields', {
21         include: [{
22           model: models.ReportField,
23           as: 'Fields',
24           required: false,
25           attributes: ['field', 'alias', 'function', 'groupBy', 'orderBy', 'MetricId', 'format', 'custom'],
26         }]
27       });
28
29       CustomReport.addScope('exportFields', function(query) {
30         return {
31           include: [{
32             model: models.ReportField,
33             as: 'Fields',
34             required: false,
35             attributes: ['field', 'alias', 'function', 'groupBy', 'orderBy', 'MetricId', 'format', 'custom'],
36             include: [{
37               model: models.Metric,
38               attributes: ['description', 'metric', 'name', 'table'],
39               required: false,
40               include: [{
41                 model: models.ReportField,
42                 as: 'ReportFields',
43                 attributes: ['field', 'alias', 'function', 'groupBy', 'orderBy', 'format'],
44                 required: false,
45                 where: query
46               }],
47             }]
48           }]
49         };
50       });
51     }
52   });
53
54   return CustomReport;
55 };