Built motion from commit 1fa89557.|2.6.10
[motion2.git] / server / api / voiceExtension / voiceExtension.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x1b31=['createRouting','bulkCreate','findAndCountAll','cast(priority\x20as\x20unsigned)','finally','lodash','ioredis','../../config/logger','api','../../mysqldb','localhost','redis','./voiceExtension.socket','register','sendStatus','status','end','json','limit','undefined','count','offset','set','Content-Range','destroy','get','InboundRoutes','inbound','OutboundRoutes','InternalRoutes','filter','ignore','map','value','includes','type','find','name','UserProfileResource','then','stack','send','VoiceExtension','rawAttributes','model','query','keys','filters','intersection','attributes','fields','nolimit','sort','pick','where','merge','options','includeAll','include','rows','catch','show','params','create','body','outbound','startsWith','exten','concat','user','internal','error','update','context','description','recordingFormat','cutdigits','mandatoryDisposition','alias','sequelize','Transaction','findOne','VoiceExtensionId'];(function(_0x2a20d1,_0x10d8f2){var _0x1d29af=function(_0x118fa6){while(--_0x118fa6){_0x2a20d1['push'](_0x2a20d1['shift']());}};_0x1d29af(++_0x10d8f2);}(_0x1b31,0x1a4));var _0x11b3=function(_0xf15641,_0x515f15){_0xf15641=_0xf15641-0x0;var _0x47595a=_0x1b31[_0xf15641];return _0x47595a;};'use strict';var _=require(_0x11b3('0x0'));var Redis=require(_0x11b3('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x11b3('0x2'))(_0x11b3('0x3'));var config=require('../../config/environment');var db=require(_0x11b3('0x4'))['db'];config['redis']=_['defaults'](config['redis'],{'host':_0x11b3('0x5'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x11b3('0x6')]));require(_0x11b3('0x7'))[_0x11b3('0x8')](socket);function respondWithStatusCode(_0xaa3975,_0x479530){_0x479530=_0x479530||0xcc;return function(_0x190e22){if(_0x190e22){return _0xaa3975[_0x11b3('0x9')](_0x479530);}return _0xaa3975[_0x11b3('0xa')](_0x479530)[_0x11b3('0xb')]();};}function respondWithResult(_0x961e66,_0x321756){_0x321756=_0x321756||0xc8;return function(_0x5886e6){if(_0x5886e6){return _0x961e66[_0x11b3('0xa')](_0x321756)[_0x11b3('0xc')](_0x5886e6);}};}function respondWithFilteredResult(_0x597a8a,_0x57cabb){return function(_0xce99de){if(_0xce99de){var _0x215885=typeof _0x57cabb['offset']==='undefined'&&typeof _0x57cabb[_0x11b3('0xd')]===_0x11b3('0xe');var _0x5d301c=_0xce99de[_0x11b3('0xf')];var _0x5e4f29=_0x215885?0x0:_0x57cabb['offset'];var _0x22d817=_0x215885?_0xce99de[_0x11b3('0xf')]:_0x57cabb[_0x11b3('0x10')]+_0x57cabb[_0x11b3('0xd')];var _0x54f391;if(_0x22d817>=_0x5d301c){_0x22d817=_0x5d301c;_0x54f391=0xc8;}else{_0x54f391=0xce;}_0x597a8a['status'](_0x54f391);return _0x597a8a[_0x11b3('0x11')](_0x11b3('0x12'),_0x5e4f29+'-'+_0x22d817+'/'+_0x5d301c)[_0x11b3('0xc')](_0xce99de);}return null;};}function removeEntity(_0x18191e){return function(_0x3577ea){if(_0x3577ea){return _0x3577ea[_0x11b3('0x13')]()['then'](function(){var _0x1e1d1a=_0x3577ea[_0x11b3('0x14')]({'plain':!![]});var _0x54bd51=[{'name':_0x11b3('0x15'),'value':_0x11b3('0x16'),'ignore':![]},{'name':_0x11b3('0x17'),'value':'outbound','ignore':![]},{'name':_0x11b3('0x18'),'value':'internal','ignore':![]}];var _0x1c2035=_(_0x54bd51)[_0x11b3('0x19')](_0x11b3('0x1a'))[_0x11b3('0x1b')](_0x11b3('0x1c'))[_0x11b3('0x1c')]();if(_[_0x11b3('0x1d')](_0x1c2035,_0x1e1d1a[_0x11b3('0x1e')])){return;}var _0x277dc4=_[_0x11b3('0x1f')](_0x54bd51,['value',_0x1e1d1a[_0x11b3('0x1e')]])[_0x11b3('0x20')];return db[_0x11b3('0x21')]['destroy']({'where':{'type':_0x277dc4,'resourceId':_0x1e1d1a['id']}})['then'](function(){return _0x3577ea;});})[_0x11b3('0x22')](function(){_0x18191e[_0x11b3('0xa')](0xcc)[_0x11b3('0xb')]();});}};}function handleEntityNotFound(_0xa6f777){return function(_0x164f7a){if(!_0x164f7a){_0xa6f777[_0x11b3('0x9')](0x194);}return _0x164f7a;};}function handleError(_0x31aa8b,_0x24b391){_0x24b391=_0x24b391||0x1f4;return function(_0x137581){logger['error'](_0x137581[_0x11b3('0x23')]);if(_0x137581[_0x11b3('0x20')]){delete _0x137581[_0x11b3('0x20')];}_0x31aa8b['status'](_0x24b391)[_0x11b3('0x24')](_0x137581);};}exports['index']=function(_0x20728c,_0x319bcc){var _0x555d7d={},_0x2469e6={},_0x38dff9={'count':0x0,'rows':[]};var _0x599b5c=_[_0x11b3('0x1b')](db[_0x11b3('0x25')][_0x11b3('0x26')],function(_0x439e39){return{'name':_0x439e39['fieldName'],'type':_0x439e39[_0x11b3('0x1e')]['key']};});_0x2469e6[_0x11b3('0x27')]=_[_0x11b3('0x1b')](_0x599b5c,'name');_0x2469e6[_0x11b3('0x28')]=_[_0x11b3('0x29')](_0x20728c[_0x11b3('0x28')]);_0x2469e6[_0x11b3('0x2a')]=_[_0x11b3('0x2b')](_0x2469e6[_0x11b3('0x27')],_0x2469e6[_0x11b3('0x28')]);_0x555d7d[_0x11b3('0x2c')]=_[_0x11b3('0x2b')](_0x2469e6[_0x11b3('0x27')],qs[_0x11b3('0x2d')](_0x20728c[_0x11b3('0x28')][_0x11b3('0x2d')]));_0x555d7d[_0x11b3('0x2c')]=_0x555d7d['attributes']['length']?_0x555d7d['attributes']:_0x2469e6[_0x11b3('0x27')];if(!_0x20728c[_0x11b3('0x28')]['hasOwnProperty'](_0x11b3('0x2e'))){_0x555d7d[_0x11b3('0xd')]=qs[_0x11b3('0xd')](_0x20728c[_0x11b3('0x28')]['limit']);_0x555d7d[_0x11b3('0x10')]=qs['offset'](_0x20728c['query'][_0x11b3('0x10')]);}_0x555d7d['order']=qs[_0x11b3('0x2f')](_0x20728c[_0x11b3('0x28')]['sort']);_0x555d7d['where']=qs[_0x11b3('0x2a')](_[_0x11b3('0x30')](_0x20728c['query'],_0x2469e6['filters']),_0x599b5c);if(_0x20728c[_0x11b3('0x28')][_0x11b3('0x19')]){_0x555d7d[_0x11b3('0x31')]=_[_0x11b3('0x32')](_0x555d7d[_0x11b3('0x31')],{'$or':_[_0x11b3('0x1b')](_0x599b5c,function(_0x49224e){if(_0x49224e[_0x11b3('0x1e')]!=='VIRTUAL'){var _0x607f16={};_0x607f16[_0x49224e[_0x11b3('0x20')]]={'$like':'%'+_0x20728c[_0x11b3('0x28')][_0x11b3('0x19')]+'%'};return _0x607f16;}})});}_0x555d7d=_[_0x11b3('0x32')]({},_0x555d7d,_0x20728c[_0x11b3('0x33')]);var _0x4ee3b6={'where':_0x555d7d[_0x11b3('0x31')]};return db['VoiceExtension'][_0x11b3('0xf')](_0x4ee3b6)[_0x11b3('0x22')](function(_0x3f6f77){_0x38dff9[_0x11b3('0xf')]=_0x3f6f77;if(_0x20728c[_0x11b3('0x28')][_0x11b3('0x34')]){_0x555d7d[_0x11b3('0x35')]=[{'all':!![]}];}return db[_0x11b3('0x25')]['findAll'](_0x555d7d);})[_0x11b3('0x22')](function(_0x7afc1){_0x38dff9[_0x11b3('0x36')]=_0x7afc1;return _0x38dff9;})['then'](respondWithFilteredResult(_0x319bcc,_0x555d7d))[_0x11b3('0x37')](handleError(_0x319bcc,null));};exports[_0x11b3('0x38')]=function(_0x1aff8e,_0x16b3e0){var _0x4f22e0={'raw':![],'where':{'id':_0x1aff8e['params']['id']}},_0x2012fe={};_0x2012fe[_0x11b3('0x27')]=_[_0x11b3('0x29')](db[_0x11b3('0x25')]['rawAttributes']);_0x2012fe[_0x11b3('0x28')]=_[_0x11b3('0x29')](_0x1aff8e[_0x11b3('0x28')]);_0x2012fe[_0x11b3('0x2a')]=_[_0x11b3('0x2b')](_0x2012fe['model'],_0x2012fe[_0x11b3('0x28')]);_0x4f22e0[_0x11b3('0x2c')]=_[_0x11b3('0x2b')](_0x2012fe[_0x11b3('0x27')],qs['fields'](_0x1aff8e['query'][_0x11b3('0x2d')]));_0x4f22e0[_0x11b3('0x2c')]=_0x4f22e0[_0x11b3('0x2c')]['length']?_0x4f22e0[_0x11b3('0x2c')]:_0x2012fe['model'];if(_0x1aff8e[_0x11b3('0x28')][_0x11b3('0x34')]){_0x4f22e0[_0x11b3('0x35')]=[{'all':!![]}];}_0x4f22e0=_['merge']({},_0x4f22e0,_0x1aff8e['options']);return db['VoiceExtension']['find'](_0x4f22e0)[_0x11b3('0x22')](handleEntityNotFound(_0x16b3e0,null))[_0x11b3('0x22')](respondWithResult(_0x16b3e0,null))[_0x11b3('0x37')](handleError(_0x16b3e0,null));};exports['destroy']=function(_0x25e469,_0x32068b){return db['VoiceExtension'][_0x11b3('0x1f')]({'where':{'id':_0x25e469[_0x11b3('0x39')]['id']}})[_0x11b3('0x22')](handleEntityNotFound(_0x32068b,null))[_0x11b3('0x22')](removeEntity(_0x32068b,null))[_0x11b3('0x37')](handleError(_0x32068b,null));};exports[_0x11b3('0x3a')]=function(_0xfc5294,_0xa8436a){if(_0xfc5294[_0x11b3('0x3b')]['type']===_0x11b3('0x3c')&&!_[_0x11b3('0x3d')](_0xfc5294[_0x11b3('0x3b')][_0x11b3('0x3e')],'_')){_0xfc5294[_0x11b3('0x3b')]['exten']='_'[_0x11b3('0x3f')](_0xfc5294[_0x11b3('0x3b')][_0x11b3('0x3e')]);}return db[_0x11b3('0x25')]['create'](_0xfc5294[_0x11b3('0x3b')],{'raw':![]})[_0x11b3('0x22')](function(_0x4af8a0){var _0x57c4c6=_0xfc5294['user']['get']({'plain':!![]});if(!_0x57c4c6)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x57c4c6['role']===_0x11b3('0x40')){var _0x311671=_0x4af8a0['get']({'plain':!![]});var _0x444c91=[{'name':_0x11b3('0x15'),'value':_0x11b3('0x16')},{'name':_0x11b3('0x17'),'value':_0x11b3('0x3c')},{'name':_0x11b3('0x18'),'value':_0x11b3('0x41')}];var _0xcff3d0=_['find'](_0x444c91,[_0x11b3('0x1c'),_0x311671[_0x11b3('0x1e')]])[_0x11b3('0x20')];return db['UserProfileSection'][_0x11b3('0x1f')]({'where':{'name':_0xcff3d0,'userProfileId':_0x57c4c6['userProfileId']},'raw':!![]})[_0x11b3('0x22')](function(_0x19a17f){if(_0x19a17f&&_0x19a17f['autoAssociation']===0x0){return db[_0x11b3('0x21')][_0x11b3('0x3a')]({'name':_0x311671[_0x11b3('0x20')],'resourceId':_0x311671['id'],'type':_0x19a17f[_0x11b3('0x20')],'sectionId':_0x19a17f['id']},{})[_0x11b3('0x22')](function(){return _0x4af8a0;});}else{return _0x4af8a0;}})[_0x11b3('0x37')](function(_0x1016fd){logger[_0x11b3('0x42')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x1016fd);throw _0x1016fd;});}return _0x4af8a0;})[_0x11b3('0x22')](respondWithResult(_0xa8436a,0xc9))[_0x11b3('0x37')](handleError(_0xa8436a,null));};exports[_0x11b3('0x43')]=function(_0x527590,_0x126274){if(_0x527590['body']['id']){delete _0x527590[_0x11b3('0x3b')]['id'];}return db['VoiceExtension'][_0x11b3('0x1f')]({'where':{'id':_0x527590[_0x11b3('0x39')]['id']}})[_0x11b3('0x22')](handleEntityNotFound(_0x126274,null))[_0x11b3('0x22')](function(_0x3bebf6){if(_0x3bebf6){return db['VoiceExtension'][_0x11b3('0x43')](_[_0x11b3('0x30')](_0x527590['body'],[_0x11b3('0x3e'),_0x11b3('0x44'),_0x11b3('0x45'),_0x11b3('0x46'),_0x11b3('0x47'),_0x11b3('0x48'),_0x11b3('0x49')]),{'where':{'context':_0x3bebf6[_0x11b3('0x44')],'exten':_0x3bebf6[_0x11b3('0x3e')]},'individualHooks':!![]})['then'](function(_0x1fe8c7){return _0x1fe8c7;});}return null;})[_0x11b3('0x22')](respondWithStatusCode(_0x126274,null))[_0x11b3('0x37')](handleError(_0x126274,null));};exports['addApplications']=function(_0x4c67e3,_0x5d4437){var _0x2552c8=_0x4c67e3[_0x11b3('0x39')]['id'];var _0x289307=_0x4c67e3['body'];var _0xd44abc=0xc8;var _0x5526ae=null;return db[_0x11b3('0x4a')]['transaction']({'isolationLevel':db['sequelize'][_0x11b3('0x4b')]['ISOLATION_LEVELS']['READ_COMMITTED']},function(_0x31cdcb){return db['VoiceExtension'][_0x11b3('0x4c')]({'where':{'id':_0x2552c8},'transaction':_0x31cdcb})[_0x11b3('0x22')](function(_0x1ffb32){if(_0x1ffb32){return db[_0x11b3('0x25')][_0x11b3('0x13')]({'where':{'VoiceExtensionId':_0x2552c8},'transaction':_0x31cdcb})[_0x11b3('0x22')](function(){var _0x380311=_[_0x11b3('0x1b')](_0x289307,function(_0x2e03c5){_0x2e03c5[_0x11b3('0x4d')]=_0x2552c8;_0x2e03c5['isApp']=!![];return _0x2e03c5;});return _0x1ffb32[_0x11b3('0x4e')](_0x380311);})['then'](function(_0x42401a){return db[_0x11b3('0x25')][_0x11b3('0x4f')](_0x42401a,{'transaction':_0x31cdcb});});}else{_0xd44abc=0x194;_0x5526ae=[];}});})['then'](function(){if(_0xd44abc!==0x194){return db[_0x11b3('0x25')][_0x11b3('0x50')]({'where':{'isApp':!![],'VoiceExtensionId':_0x2552c8},'order':_0x11b3('0x51')})[_0x11b3('0x22')](function(_0x5e3b4e){_0x5526ae=_0x5e3b4e;});}})['catch'](function(_0x199194){_0xd44abc=0x1f4;logger[_0x11b3('0x42')](_0x199194[_0x11b3('0x23')]);if(_0x199194[_0x11b3('0x20')]){delete _0x199194['name'];}_0x5526ae=_0x199194;})[_0x11b3('0x52')](function(){if(_0x5526ae===null){_0x5d4437[_0x11b3('0x9')](_0xd44abc);}else{if(_0xd44abc===0x1f4){_0x5d4437[_0x11b3('0xa')](_0xd44abc)[_0x11b3('0x24')](_0x5526ae);}else{_0x5d4437[_0x11b3('0xa')](_0xd44abc)[_0x11b3('0xc')](_0x5526ae);}}});};