Built motion from commit f7831a8.|0.0.80
[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           if (this.getDataValue('app') === 'Dial' && this.getDataValue('type') === 'outbound') {
45             var appdata = reverseMustache({
46               template: 'SIP/{{trunk}}/{{prefix}}${EXTEN:{{cutdigits}}}',
47               content: this.getDataValue('appdata')
48             });
49             return [appdata.trunk, this.getDataValue('callerID') || '', appdata.cutdigits, appdata.prefix];
50           } else {
51             return this.getDataValue('appdata').split(',');
52           }
53         } else {
54           return this.getDataValue('appdata');
55         }
56       },
57     },
58     type: {
59       type: DataTypes.ENUM('inbound', 'outbound', 'internal', 'inbound-fax', 'outbound-fax', 'system', 'any'),
60       allowNull: false,
61     },
62     description: {
63       type: DataTypes.STRING,
64     },
65     interval: {
66       type: DataTypes.STRING,
67     },
68     IntervalId: {
69       type: DataTypes.INTEGER,
70     },
71     isApp: {
72       type: DataTypes.BOOLEAN,
73       defaultValue: false
74     },
75     callerID: {
76       type: DataTypes.STRING,
77       allowNull: true
78     },
79     record: {
80       type: DataTypes.BOOLEAN,
81       defaultValue: false
82     }
83   }, {
84     tableName: 'voice_extensions',
85     associate: function(models) {
86       VoiceExtension.hasMany(models.VoiceExtension, {
87         as: 'Applications',
88         onDelete: 'cascade',
89         hooks: true
90       });
91       // SCOPES MANAGEMENT
92       VoiceExtension.addScope('application', {
93         include: [{
94           model: models.VoiceExtension,
95           as: 'Applications',
96           required: false,
97           where: {
98             isApp: true
99           },
100           attributes: ['app', 'interval', 'IntervalId', 'callerID', 'appdata', 'type']
101         }]
102       });
103
104       VoiceExtension.addScope('routes', {
105         where: {
106           VoiceExtensionId: null
107         }
108       });
109     }
110   });
111
112   return VoiceExtension;
113 };