Built motion from commit 503e72f.|0.0.143
[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   }, {
122     tableName: 'voice_extensions',
123     associate: function(models) {
124       VoiceExtension.hasMany(models.VoiceExtension, {
125         as: 'Applications',
126         onDelete: 'cascade',
127         hooks: true
128       });
129       // SCOPES MANAGEMENT
130       VoiceExtension.addScope('application', {
131         include: [{
132           model: models.VoiceExtension,
133           as: 'Applications',
134           required: false,
135           where: {
136             isApp: true
137           },
138           attributes: ['app', 'interval', 'IntervalId', 'callerID', 'appdata', 'type', 'appType']
139         }]
140       });
141
142       VoiceExtension.addScope('routes', {
143         where: {
144           VoiceExtensionId: null
145         }
146       });
147     }
148   });
149
150   return VoiceExtension;
151 };