Built motion from commit 95b01fa.|0.0.70
[motion.git] / server / models / report_jscripty_session.js
1 'use strict';
2 var moment = require('moment');
3
4 module.exports = function(sequelize, DataTypes) {
5   return sequelize.define('ReportJscriptySession', {
6     session_id: {
7       type: DataTypes.INTEGER,
8       primaryKey: true,
9       autoIncrement: true
10     },
11     status: {
12       type: DataTypes.ENUM('started', 'completed', 'incomplete')
13     },
14     last_question_id: DataTypes.INTEGER,
15     completedAt: {
16       type: DataTypes.DATE,
17       get: function() {
18         // 'this' allows you to access attributes of the instance
19         return this.getDataValue('completedAt') ? moment(this.getDataValue('completedAt')).format("MM-DD-YYYY HH:mm") : '';
20       }
21     },
22     createdAt: {
23       type: DataTypes.DATE,
24       get: function() {
25         // 'this' allows you to access attributes of the instance
26         return moment(this.getDataValue('createdAt')).format("MM-DD-YYYY HH:mm");
27       }
28     }
29   }, {
30     tableName: 'report_jscripty_sessions',
31     associate: function(models) {
32       models.ReportJscriptySession.hasMany(models.ReportJscriptyQuestion, {
33         onDelete: 'cascade',
34         foreignKey: 'sessionId'
35       });
36       models.ReportJscriptySession.addScope('questionsAndInput', function(projectId) {
37         return {
38           where: {
39             ProjectId: projectId
40           },
41           include: [{
42             model: models.ReportJscriptyQuestion,
43             include: [{
44               model: models.ReportJscriptyInput
45             }]
46           }]
47         }
48       });
49     }
50   });
51 };