Built motion from commit c50d73b2.|2.0.46
[motion2.git] / server / config / seedDB / voiceExtensions.js
1 'use strict';
2
3 var _ = require('lodash');
4
5 var db = require('../../mysqldb').db;
6 var logger = require('../logger')('app');
7 var DBPromise = require('bluebird');
8
9 var applications = {
10     '600': [{
11         app: 'Set',
12         appdata: 'CDR(type)=internal',
13     }, {
14         app: 'Answer'
15     }, {
16         app: 'Playback',
17         appdata: 'demo-echotest',
18         isApp: true
19     }, {
20         app: 'Echo',
21         isApp: true
22     }, {
23         app: 'Playback',
24         appdata: 'demo-echodone',
25         isApp: true
26     }, {
27         app: 'Hangup'
28     }],
29     '6000': [{
30         app: 'Set',
31         appdata: 'CDR(type)=internal'
32     }, {
33         app: 'Answer'
34     }, {
35         app: 'Queue',
36         appdata: 'Support,xX,,,300,,,,,',
37         appType: 'queue',
38         isApp: true
39     }, {
40         app: 'Hangup'
41     }],
42     '6001': [{
43         app: 'Set',
44         appdata: 'CDR(type)=internal'
45     }, {
46         app: 'Answer'
47     }, {
48         app: 'Queue',
49         appdata: 'Sales,xX,,,300,,,,,',
50         appType: 'queue',
51         isApp: true
52     }, {
53         app: 'Hangup'
54     }],
55     '6666': [{
56         app: 'Set',
57         appdata: 'CDR(type)=internal'
58     }, {
59         app: 'AGI',
60         appdata: 'agi://127.0.0.1/square,AutoAttendant',
61         appType: 'agi',
62         isApp: true
63     }, {
64         app: 'Hangup'
65     }],
66     '6667': [{
67         app: 'Set',
68         appdata: 'CDR(type)=internal'
69     }, {
70         app: 'AGI',
71         appdata: 'agi://127.0.0.1/square,Orders',
72         appType: 'agi',
73         isApp: true
74     }, {
75         app: 'Hangup'
76     }],
77     '6668': [{
78         app: 'Set',
79         appdata: 'CDR(type)=internal'
80     }, {
81         app: 'AGI',
82         appdata: 'agi://127.0.0.1/square,CSI',
83         appType: 'agi',
84         isApp: true
85     }, {
86         app: 'Hangup'
87     }],
88     '6669': [{
89         app: 'Set',
90         appdata: 'CDR(type)=internal'
91     }, {
92         app: 'AGI',
93         appdata: 'agi://127.0.0.1/square,Survey',
94         appType: 'agi',
95         isApp: true
96     }, {
97         app: 'Hangup'
98     }],
99     'xcally-motion-dialer': [{
100         app: 'Set',
101         appdata: 'CDR(type)=dialer'
102     }, {
103         app: 'Set',
104         appdata: 'CALLERID(all)=${XMD-CALLERID}'
105     }, {
106         app: 'GotoIf',
107         appdata: '$["${XMD-AMD}" != "NONE" ]?5:9'
108     }, {
109         app: 'AMD',
110         appdata: '${XMD-AMDINITIALSILENCE},${XMD-AMDGREETING},${XMD-AMDAFTERGREETINGSILENCE},${XMD-AMDTOTALANALYSISTIME},${XMD-AMDMINWORDLENGTH},${XMD-AMDBETWEENWORDSSILENCE},${XMD-AMDMAXIMUMNUMBEROFWORDS},${XMD-AMDSILENCETHRESHOLD},${XMD-AMDMAXIMUMWORDLENGTH}'
111     }, {
112         app: 'GotoIf',
113         appdata: '$["${AMDSTATUS}" == "HUMAN" ]?9:7'
114     }, {
115         app: 'GotoIf',
116         appdata: '$["${AMDSTATUS}" == "NOTSURE" ]?9:8'
117     }, {
118         app: 'GotoIf',
119         appdata: '$["${AMDSTATUS}" == "MACHINE" ]?11:9'
120     }, {
121         app: 'Execif',
122         appdata: '$["${XMD-AGI}" != "NONE" ]?AGI(${XMD-AGI})'
123     }, {
124         app: 'Execif',
125         appdata: '$["${XMD-QUEUE}" != "NONE" ]?QUEUE(${XMD-QUEUE})'
126     }, {
127         app: 'Hangup'
128     }],
129     'xcally-motion-fax': [{
130         app: 'Set',
131         appdata: 'CDR(type)=outbound-fax'
132     }, {
133         app: 'Wait',
134         appdata: '6'
135     }, {
136         app: 'Set',
137         appdata: 'FAXOPT(ecm)=${XMF-ECM}'
138     }, {
139         app: 'Set',
140         appdata: 'FAXOPT(headerinfo)=${XMF-HEADERINFO}'
141     }, {
142         app: 'Set',
143         appdata: 'FAXOPT(localstationid)=${XMF-LOCALSTATIONID}'
144     }, {
145         app: 'Set',
146         appdata: 'FAXOPT(maxrate)=${XMF-MAXRATE}'
147     }, {
148         app: 'Set',
149         appdata: 'FAXOPT(minrate)=${XMF-MINRATE}'
150     }, {
151         app: 'Set',
152         appdata: 'FAXOPT(modem)=${XMF-MODEM}'
153     }, {
154         app: 'Set',
155         appdata: 'FAXOPT(t38timeout)=${XMF-T38TIMEOUT}'
156     }, {
157         app: 'SendFax',
158         appdata: '${XMF-PATH},d'
159     }],
160     'xcally-motion-preview': [{
161         app: 'Set',
162         appdata: 'CDR(type)=dialer'
163     }, {
164         app: 'Set',
165         appdata: 'CALLERID(all)=${XMD-CALLERID}'
166     }, {
167         app: 'Dial',
168         appdata: '${XMD-DIALCHANNEL},${XMD-DIALTIMEOUT},${XMD-DIALOPTIONS},${XMD-DIALURL}'
169     }, {
170         app: 'Hangup'
171     }],
172     '_.': [{
173         app: 'Set',
174         appdata: 'CDR(type)=outbound'
175     }, {
176         app: 'Set',
177         outboundrouteid: true
178     }, {
179         app: 'Playback',
180         appdata: 'demo-congrats',
181         appType: 'custom',
182         isApp: true
183     }, {
184         app: 'Hangup'
185     }],
186     '_X.': [{
187         app: 'Set',
188         appdata: 'CDR(type)=inbound'
189     }, {
190         app: 'Answer'
191     }, {
192         app: 'Playback',
193         appdata: 'demo-congrats',
194         isApp: true
195     }, {
196         app: 'Hangup'
197     }],
198     'h': [{
199         app: 'Hangup'
200     }],
201     'failed': [{
202         app: 'Hangup'
203     }],
204 };
205
206 var voiceExtensions = [{
207     type: 'internal',
208     context: 'from-sip',
209     exten: '600',
210     description: 'echo test demo auto generated'
211 }, {
212     type: 'internal',
213     context: 'from-sip',
214     exten: '6000',
215     description: 'forward to Support queue auto generated'
216 }, {
217     type: 'internal',
218     context: 'from-sip',
219     exten: '6001',
220     description: 'forward to Sales queue auto generated'
221 }, {
222     type: 'internal',
223     context: 'from-sip',
224     exten: '6666',
225     description: 'forward to Auto Attendant IVR auto generated'
226 }, {
227     type: 'internal',
228     context: 'from-sip',
229     exten: '6667',
230     description: 'forward to Orders IVR auto generated'
231 }, {
232     type: 'internal',
233     context: 'from-sip',
234     exten: '6668',
235     description: 'forward to CSI IVR auto generated'
236 }, {
237     type: 'internal',
238     context: 'from-sip',
239     exten: '6669',
240     description: 'forward to Survey IVR auto generated'
241 }, {
242     type: 'system',
243     context: 'from-sip',
244     exten: 'xcally-motion-dialer',
245     description: 'dialer extensions auto generated'
246 }, {
247     type: 'system',
248     context: 'from-sip',
249     exten: 'xcally-motion-fax',
250     description: 'fax extensions auto generated'
251 }, {
252     type: 'system',
253     context: 'from-sip',
254     exten: 'xcally-motion-preview',
255     description: 'dialer extensions auto generated'
256 }, {
257     type: 'outbound',
258     context: 'from-sip',
259     exten: '_.',
260     description: 'demo outbound calls auto generated'
261 }, {
262     type: 'inbound',
263     context: 'from-voip-provider',
264     exten: '_X.',
265     description: 'catch all inbound calls auto generated'
266 }, {
267     type: 'system',
268     context: 'from-sip',
269     exten: 'h',
270     description: 'catch hangup event auto generated'
271 }, {
272     type: 'system',
273     context: 'from-sip',
274     exten: 'failed',
275     description: 'catch failed event auto generated'
276 }];
277
278 var applicationsAlways = {
279     's': [{
280         app: 'MixMonitor',
281         appdata: '${XCALLY_MIXMONITOR_UNIQUEID}.${XCALLY_MIXMONITOR_FORMAT},ab'
282     }, {
283         app: 'Return'
284     }],
285     '_[x][c][a][l][l][y][Q][u][e][u][e][E][x][t][e][n][s][i][o][n].': [{
286         app: 'Progress'
287     }, {
288         app: 'Queue',
289         appdata: '${EXTEN:20},xX'
290     }]
291 };
292
293 var voiceExtensionsAlways = [{
294     type: 'system',
295     context: 'xcally-mixmonitor-context',
296     exten: 's'
297 }, {
298     type: 'system',
299     context: 'from-sip',
300     exten: '_[x][c][a][l][l][y][Q][u][e][u][e][E][x][t][e][n][s][i][o][n].'
301 }];
302
303 function createExtensions(voiceExtensions, applications) {
304     return db.VoiceExtension
305         .bulkCreate(voiceExtensions, {
306             ignoreDuplicates: true,
307             individualHooks: true
308         })
309         .then(function(entities) {
310             for (var i = 0; i < entities.length; i += 1) {
311                 if (applications[entities[i].exten]) {
312                     for (var j = 0; j < applications[entities[i].exten].length; j += 1) {
313
314                         // NOTE: only for outbound route with outboundrouteid
315                         if (applications[entities[i].exten][j].outboundrouteid) {
316                             applications[entities[i].exten][j].appdata = 'outboundrouteid=' + entities[i].id;
317                             delete applications[entities[i].exten][j].outboundrouteid;
318                         }
319
320                         applications[entities[i].exten][j] = _.defaults(applications[entities[i].exten][j], {
321                             isApp: false,
322                             appType: 'custom',
323                             type: entities[i].type,
324                             context: entities[i].context,
325                             exten: entities[i].exten,
326                             priority: parseInt(entities[i].priority, 10) + 1 + j,
327                             description: entities[i].description,
328                             VoiceExtensionId: entities[i].id
329                         });
330
331                         db.VoiceExtension.create(applications[entities[i].exten][j]);
332                     }
333                 }
334
335             }
336         })
337         .then(function() {
338             logger.info('Voice Extensions have been created');
339         })
340         .catch(function(err) {
341             logger.error('Can\'t create Voice Extensions', err);
342         });
343 }
344
345 exports.create = function() {
346     createExtensions(voiceExtensions, applications);
347 };
348
349 exports.createAlways = function() {
350     return db.VoiceExtension
351         .destroy({
352             where: {
353                 $or: voiceExtensionsAlways
354             }
355         })
356         .then(function() {
357             createExtensions(voiceExtensionsAlways, applicationsAlways);
358         })
359         .catch(function(err) {
360             logger.error('Can\'t create Voice Extensions', err);
361         });
362 };