Built motion from commit 1333b3551.|1.0.38
[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 if (this.getDataValue('app') === 'Queue' && this.getDataValue('appType') !== 'custom') {
78             appdata = reverseMustache({
79               template: '{{queue}},{{options}},{{url}},{{announceoverride}},{{timeout}},{{agi}},{{macro}},{{gosub}},{{rule}},{{position}}',
80               content: this.getDataValue('appdata')
81             });
82             if (appdata) {
83               return [appdata.queue, appdata.options, appdata.url, appdata.announceoverride, appdata.timeout, appdata.agi, appdata.macro, appdata.gosub, appdata.rule, appdata.position];
84             } else {
85               appdata = reverseMustache({ // for retrocompatibility
86                 template: '{{queue}},{{options}},{{url}},{{announceoverride}},{{timeout}}',
87                 content: this.getDataValue('appdata')
88               });
89               if (appdata) {
90                 return [appdata.queue, appdata.options, appdata.url, appdata.announceoverride, appdata.timeout, '', '', '', '', ''];
91               } else {
92                 return ['', '', '', '', '', '', '', '', '', ''];
93               }
94             }
95           } else {
96             return this.getDataValue('appdata').split(',');
97           }
98         } else {
99           return this.getDataValue('appdata');
100         }
101       },
102     },
103     type: {
104       type: DataTypes.ENUM('inbound', 'outbound', 'internal', 'inbound-fax', 'outbound-fax', 'system', 'any'),
105       allowNull: false,
106     },
107     description: {
108       type: DataTypes.STRING,
109     },
110     interval: {
111       type: DataTypes.STRING,
112     },
113     IntervalId: {
114       type: DataTypes.INTEGER,
115     },
116     isApp: {
117       type: DataTypes.BOOLEAN,
118       defaultValue: false
119     },
120     appType: {
121       type: DataTypes.STRING,
122       defaultValue: null
123     },
124     callerID: {
125       type: DataTypes.STRING,
126       allowNull: true
127     },
128     record: {
129       type: DataTypes.BOOLEAN,
130       defaultValue: false
131     },
132     cutdigits: {
133       type: DataTypes.INTEGER
134     },
135     recordingFormat: {
136       type: DataTypes.STRING,
137       defaultValue: 'wav'
138     },
139     answer: {
140       type: DataTypes.BOOLEAN,
141       defaultValue: true
142     }
143   }, {
144     tableName: 'voice_extensions',
145     associate: function(models) {
146       VoiceExtension.hasMany(models.VoiceExtension, {
147         as: 'Applications',
148         onDelete: 'cascade',
149         hooks: true
150       });
151       // SCOPES MANAGEMENT
152       VoiceExtension.addScope('application', {
153         include: [{
154           model: models.VoiceExtension,
155           as: 'Applications',
156           required: false,
157           where: {
158             isApp: true
159           },
160           attributes: ['app', 'interval', 'IntervalId', 'callerID', 'appdata', 'type', 'appType', 'answer']
161         }]
162       });
163
164       VoiceExtension.addScope('routes', {
165         where: {
166           VoiceExtensionId: null
167         }
168       });
169     }
170   });
171
172   return VoiceExtension;
173 };