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