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