Built motion from commit fa8c70b.|0.0.144
[motion.git] / server / models / voice_extension.js
1 /* jshint indent: 2 */
2 var util = require('util');
3 var reverseMustache = require('reverse-mustache');
4
5 module.exports = function(sequelize, DataTypes) {
6   var VoiceExtension = sequelize.define('VoiceExtension', {
7     context: {
8       type: DataTypes.STRING,
9       allowNull: false,
10       unique: 'compositeIndex',
11       validate: {
12         notEmpty: true,
13         is: /[A-Za-z0-9._\+*!\-\[\]]+$/i
14       },
15     },
16     exten: {
17       type: DataTypes.STRING,
18       allowNull: false,
19       unique: 'compositeIndex'
20     },
21     priority: {
22       type: DataTypes.STRING,
23       allowNull: false,
24       defaultValue: '1',
25       unique: 'compositeIndex'
26     },
27     tag: {
28       type: DataTypes.STRING,
29       allowNull: false,
30       defaultValue: '--'
31     },
32     app: {
33       type: DataTypes.STRING,
34       allowNull: false,
35       defaultValue: 'NoOp'
36     },
37     appdata: {
38       type: DataTypes.STRING(4096),
39       allowNull: true,
40       defaultValue: '',
41       get: function() {
42         // 'this' allows you to access attributes of the instance
43         if (!this.getDataValue('id')) {
44           var appdata;
45           if (this.getDataValue('app') === 'Dial' && this.getDataValue('appType') === 'outboundDial') {
46             appdata = reverseMustache({
47               template: 'SIP/{{trunk}}/{{prefix}}${EXTEN:{{cutdigits}}},{{timeout}},{{options}},{{url}}',
48               content: this.getDataValue('appdata')
49             });
50             if (appdata) {
51               return [appdata.trunk, this.getDataValue('callerID') || '', appdata.prefix, appdata.timeout, appdata.options, appdata.url];
52             } else {
53               return ['', '', '', '', '', '', ''];
54             }
55           } else if (this.getDataValue('app') === 'Dial' && this.getDataValue('appType') === 'externalDial') {
56             appdata = reverseMustache({
57               template: 'SIP/{{trunk}}/{{phone}},{{timeout}},{{options}},{{url}}',
58               content: this.getDataValue('appdata')
59             });
60             if (appdata) {
61               return [appdata.trunk, appdata.phone, appdata.timeout, appdata.options, appdata.url];
62             } else {
63               return ['', '', '', '', ''];
64             }
65           } else if (this.getDataValue('app') === 'Dial' && this.getDataValue('appType') === 'internalDial') {
66             appdata = reverseMustache({
67               template: 'SIP/{{internal}},{{timeout}},{{options}},{{url}}',
68               content: this.getDataValue('appdata')
69             });
70             if (appdata) {
71               return [appdata.internal, appdata.timeout, appdata.options, appdata.url];
72             } else {
73               return ['', '', '', ''];
74             }
75           } else if (this.getDataValue('app') === 'Set' || this.getDataValue('appType') === 'custom') {
76             return [this.getDataValue('appdata')];
77           } else {
78             return this.getDataValue('appdata').split(',');
79           }
80         } else {
81           return this.getDataValue('appdata');
82         }
83       },
84     },
85     type: {
86       type: DataTypes.ENUM('inbound', 'outbound', 'internal', 'inbound-fax', 'outbound-fax', 'system', 'any'),
87       allowNull: false,
88     },
89     description: {
90       type: DataTypes.STRING,
91     },
92     interval: {
93       type: DataTypes.STRING,
94     },
95     IntervalId: {
96       type: DataTypes.INTEGER,
97     },
98     isApp: {
99       type: DataTypes.BOOLEAN,
100       defaultValue: false
101     },
102     appType: {
103       type: DataTypes.STRING,
104       defaultValue: null
105     },
106     callerID: {
107       type: DataTypes.STRING,
108       allowNull: true
109     },
110     record: {
111       type: DataTypes.BOOLEAN,
112       defaultValue: false
113     },
114     cutdigits: {
115       type: DataTypes.INTEGER
116     },
117     recordingFormat: {
118       type: DataTypes.STRING,
119       defaultValue: 'wav'
120     },
121     answer: {
122       type: DataTypes.BOOLEAN,
123       defaultValue: true
124     }
125   }, {
126     tableName: 'voice_extensions',
127     associate: function(models) {
128       VoiceExtension.hasMany(models.VoiceExtension, {
129         as: 'Applications',
130         onDelete: 'cascade',
131         hooks: true
132       });
133       // SCOPES MANAGEMENT
134       VoiceExtension.addScope('application', {
135         include: [{
136           model: models.VoiceExtension,
137           as: 'Applications',
138           required: false,
139           where: {
140             isApp: true
141           },
142           attributes: ['app', 'interval', 'IntervalId', 'callerID', 'appdata', 'type', 'appType', 'answer']
143         }]
144       });
145
146       VoiceExtension.addScope('routes', {
147         where: {
148           VoiceExtensionId: null
149         }
150       });
151     }
152   });
153
154   return VoiceExtension;
155 };