Built motion from commit 48095ee.|0.0.102
[motion.git] / server / models / report_jscripty_question.js
1 'use strict';
2
3 module.exports = function(sequelize, DataTypes) {
4   return sequelize.define('ReportJscriptyQuestion', {
5     question_id: DataTypes.INTEGER,
6     question_type: DataTypes.STRING,
7     question_label: DataTypes.STRING,
8     question_text: DataTypes.STRING,
9     question_answer: DataTypes.STRING,
10     question_other_label: DataTypes.STRING,
11     question_other_answer: DataTypes.STRING,
12     question_options: DataTypes.STRING,
13     index: DataTypes.INTEGER
14   }, {
15     tableName: 'report_jscripty_questions',
16     associate: function(models) {
17       models.ReportJscriptyQuestion.hasMany(models.ReportJscriptyInput, {
18         onDelete: 'cascade',
19         foreignKey: 'questionId'
20
21       });
22       models.ReportJscriptyQuestion.addScope('otherOption', function(projectId, questionId) {
23         return {
24           where: {
25             ProjectId: projectId,
26             question_id: questionId,
27             question_type: 'question',
28             question_other_answer: {
29               $ne: null
30             }
31           },
32           attributes: ['question_type', 'question_id', 'question_label', 'question_text', 'question_other_label', 'question_other_answer'],
33         }
34
35       });
36       models.ReportJscriptyQuestion.addScope('summary', function(projectId) {
37         return {
38           where: {
39             projectId: projectId,
40             question_type: {
41               $and: [{
42                 $ne: 'end'
43               }, {
44                 $ne: 'start'
45               }]
46
47             }
48           },
49           attributes: ['question_id', 'question_type', 'question_label', 'question_text', [sequelize.fn('count', sequelize.col('question_id')), 'count']],
50           order: [
51             ['updatedAt', 'ASC']
52           ],
53           group: ['question_id']
54         }
55
56       });
57     }
58   });
59 };