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