Built motion from commit 616d3211.|2.6.4
[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 _0x8848=['outbound','InternalRoutes','internal','filter','ignore','map','value','includes','type','name','UserProfileResource','error','stack','send','VoiceExtension','rawAttributes','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','limit','order','sort','where','merge','VIRTUAL','options','findAll','rows','catch','show','params','includeAll','include','find','create','body','startsWith','exten','concat','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','inbound','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','pick','description','mandatoryDisposition','alias','context','addApplications','sequelize','transaction','Transaction','ISOLATION_LEVELS','READ_COMMITTED','VoiceExtensionId','isApp','cast(priority\x20as\x20unsigned)','lodash','ioredis','../../config/logger','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./voiceExtension.socket','sendStatus','status','end','undefined','count','offset','set','Content-Range','json','destroy','then','get','InboundRoutes','OutboundRoutes'];(function(_0x13d398,_0x3cd350){var _0x6a2c2b=function(_0x1ca8b9){while(--_0x1ca8b9){_0x13d398['push'](_0x13d398['shift']());}};_0x6a2c2b(++_0x3cd350);}(_0x8848,0xa1));var _0x8884=function(_0x725b8e,_0xc22b48){_0x725b8e=_0x725b8e-0x0;var _0x43ac45=_0x8848[_0x725b8e];return _0x43ac45;};'use strict';var _=require(_0x8884('0x0'));var Redis=require(_0x8884('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x8884('0x2'))('api');var config=require('../../config/environment');var db=require(_0x8884('0x3'))['db'];config[_0x8884('0x4')]=_[_0x8884('0x5')](config['redis'],{'host':_0x8884('0x6'),'port':0x18eb});var socket=require(_0x8884('0x7'))(new Redis(config[_0x8884('0x4')]));require(_0x8884('0x8'))['register'](socket);function respondWithStatusCode(_0x2ee36b,_0x39c713){_0x39c713=_0x39c713||0xcc;return function(_0x25c597){if(_0x25c597){return _0x2ee36b[_0x8884('0x9')](_0x39c713);}return _0x2ee36b[_0x8884('0xa')](_0x39c713)[_0x8884('0xb')]();};}function respondWithResult(_0x373199,_0x1977d8){_0x1977d8=_0x1977d8||0xc8;return function(_0x126726){if(_0x126726){return _0x373199[_0x8884('0xa')](_0x1977d8)['json'](_0x126726);}};}function respondWithFilteredResult(_0x20634e,_0x36d56a){return function(_0x58d0c2){if(_0x58d0c2){var _0x58216f=typeof _0x36d56a['offset']==='undefined'&&typeof _0x36d56a['limit']===_0x8884('0xc');var _0x3847ab=_0x58d0c2[_0x8884('0xd')];var _0x49ac6d=_0x58216f?0x0:_0x36d56a[_0x8884('0xe')];var _0x1bb995=_0x58216f?_0x58d0c2[_0x8884('0xd')]:_0x36d56a[_0x8884('0xe')]+_0x36d56a['limit'];var _0x606838;if(_0x1bb995>=_0x3847ab){_0x1bb995=_0x3847ab;_0x606838=0xc8;}else{_0x606838=0xce;}_0x20634e[_0x8884('0xa')](_0x606838);return _0x20634e[_0x8884('0xf')](_0x8884('0x10'),_0x49ac6d+'-'+_0x1bb995+'/'+_0x3847ab)[_0x8884('0x11')](_0x58d0c2);}return null;};}function removeEntity(_0x3efeac){return function(_0x1613aa){if(_0x1613aa){return _0x1613aa[_0x8884('0x12')]()[_0x8884('0x13')](function(){var _0x3ffae2=_0x1613aa[_0x8884('0x14')]({'plain':!![]});var _0x4285cb=[{'name':_0x8884('0x15'),'value':'inbound','ignore':![]},{'name':_0x8884('0x16'),'value':_0x8884('0x17'),'ignore':![]},{'name':_0x8884('0x18'),'value':_0x8884('0x19'),'ignore':![]}];var _0x56aa46=_(_0x4285cb)[_0x8884('0x1a')](_0x8884('0x1b'))[_0x8884('0x1c')](_0x8884('0x1d'))[_0x8884('0x1d')]();if(_[_0x8884('0x1e')](_0x56aa46,_0x3ffae2[_0x8884('0x1f')])){return;}var _0x10d86d=_['find'](_0x4285cb,['value',_0x3ffae2['type']])[_0x8884('0x20')];return db[_0x8884('0x21')][_0x8884('0x12')]({'where':{'type':_0x10d86d,'resourceId':_0x3ffae2['id']}})[_0x8884('0x13')](function(){return _0x1613aa;});})[_0x8884('0x13')](function(){_0x3efeac[_0x8884('0xa')](0xcc)[_0x8884('0xb')]();});}};}function handleEntityNotFound(_0x45a595){return function(_0x2a10c3){if(!_0x2a10c3){_0x45a595[_0x8884('0x9')](0x194);}return _0x2a10c3;};}function handleError(_0x3ddec8,_0x2870bc){_0x2870bc=_0x2870bc||0x1f4;return function(_0x5b94f1){logger[_0x8884('0x22')](_0x5b94f1[_0x8884('0x23')]);if(_0x5b94f1[_0x8884('0x20')]){delete _0x5b94f1[_0x8884('0x20')];}_0x3ddec8[_0x8884('0xa')](_0x2870bc)[_0x8884('0x24')](_0x5b94f1);};}exports['index']=function(_0x56ec0f,_0x16fdfa){var _0x396b46={},_0x5c8df={},_0x9f80cb={'count':0x0,'rows':[]};var _0x46c0a0=_[_0x8884('0x1c')](db[_0x8884('0x25')][_0x8884('0x26')],function(_0x2deb0e){return{'name':_0x2deb0e['fieldName'],'type':_0x2deb0e[_0x8884('0x1f')][_0x8884('0x27')]};});_0x5c8df[_0x8884('0x28')]=_[_0x8884('0x1c')](_0x46c0a0,_0x8884('0x20'));_0x5c8df[_0x8884('0x29')]=_[_0x8884('0x2a')](_0x56ec0f['query']);_0x5c8df[_0x8884('0x2b')]=_[_0x8884('0x2c')](_0x5c8df[_0x8884('0x28')],_0x5c8df[_0x8884('0x29')]);_0x396b46[_0x8884('0x2d')]=_['intersection'](_0x5c8df[_0x8884('0x28')],qs[_0x8884('0x2e')](_0x56ec0f[_0x8884('0x29')][_0x8884('0x2e')]));_0x396b46[_0x8884('0x2d')]=_0x396b46['attributes'][_0x8884('0x2f')]?_0x396b46[_0x8884('0x2d')]:_0x5c8df[_0x8884('0x28')];if(!_0x56ec0f['query'][_0x8884('0x30')]('nolimit')){_0x396b46['limit']=qs[_0x8884('0x31')](_0x56ec0f['query'][_0x8884('0x31')]);_0x396b46[_0x8884('0xe')]=qs['offset'](_0x56ec0f[_0x8884('0x29')][_0x8884('0xe')]);}_0x396b46[_0x8884('0x32')]=qs[_0x8884('0x33')](_0x56ec0f[_0x8884('0x29')][_0x8884('0x33')]);_0x396b46[_0x8884('0x34')]=qs['filters'](_['pick'](_0x56ec0f[_0x8884('0x29')],_0x5c8df[_0x8884('0x2b')]),_0x46c0a0);if(_0x56ec0f[_0x8884('0x29')][_0x8884('0x1a')]){_0x396b46[_0x8884('0x34')]=_[_0x8884('0x35')](_0x396b46[_0x8884('0x34')],{'$or':_['map'](_0x46c0a0,function(_0x118d1e){if(_0x118d1e[_0x8884('0x1f')]!==_0x8884('0x36')){var _0x464030={};_0x464030[_0x118d1e[_0x8884('0x20')]]={'$like':'%'+_0x56ec0f[_0x8884('0x29')][_0x8884('0x1a')]+'%'};return _0x464030;}})});}_0x396b46=_[_0x8884('0x35')]({},_0x396b46,_0x56ec0f[_0x8884('0x37')]);var _0x335111={'where':_0x396b46[_0x8884('0x34')]};return db[_0x8884('0x25')][_0x8884('0xd')](_0x335111)['then'](function(_0x2dddca){_0x9f80cb[_0x8884('0xd')]=_0x2dddca;if(_0x56ec0f['query']['includeAll']){_0x396b46['include']=[{'all':!![]}];}return db[_0x8884('0x25')][_0x8884('0x38')](_0x396b46);})['then'](function(_0x41ea0d){_0x9f80cb[_0x8884('0x39')]=_0x41ea0d;return _0x9f80cb;})[_0x8884('0x13')](respondWithFilteredResult(_0x16fdfa,_0x396b46))[_0x8884('0x3a')](handleError(_0x16fdfa,null));};exports[_0x8884('0x3b')]=function(_0x6f5d77,_0x405c97){var _0x5e3e02={'raw':![],'where':{'id':_0x6f5d77[_0x8884('0x3c')]['id']}},_0x3d977d={};_0x3d977d[_0x8884('0x28')]=_['keys'](db['VoiceExtension'][_0x8884('0x26')]);_0x3d977d[_0x8884('0x29')]=_[_0x8884('0x2a')](_0x6f5d77['query']);_0x3d977d[_0x8884('0x2b')]=_['intersection'](_0x3d977d[_0x8884('0x28')],_0x3d977d[_0x8884('0x29')]);_0x5e3e02[_0x8884('0x2d')]=_[_0x8884('0x2c')](_0x3d977d['model'],qs['fields'](_0x6f5d77[_0x8884('0x29')][_0x8884('0x2e')]));_0x5e3e02[_0x8884('0x2d')]=_0x5e3e02[_0x8884('0x2d')][_0x8884('0x2f')]?_0x5e3e02[_0x8884('0x2d')]:_0x3d977d[_0x8884('0x28')];if(_0x6f5d77[_0x8884('0x29')][_0x8884('0x3d')]){_0x5e3e02[_0x8884('0x3e')]=[{'all':!![]}];}_0x5e3e02=_[_0x8884('0x35')]({},_0x5e3e02,_0x6f5d77[_0x8884('0x37')]);return db['VoiceExtension'][_0x8884('0x3f')](_0x5e3e02)[_0x8884('0x13')](handleEntityNotFound(_0x405c97,null))[_0x8884('0x13')](respondWithResult(_0x405c97,null))[_0x8884('0x3a')](handleError(_0x405c97,null));};exports[_0x8884('0x12')]=function(_0x11bb1f,_0x1944c3){return db[_0x8884('0x25')]['find']({'where':{'id':_0x11bb1f['params']['id']}})[_0x8884('0x13')](handleEntityNotFound(_0x1944c3,null))[_0x8884('0x13')](removeEntity(_0x1944c3,null))['catch'](handleError(_0x1944c3,null));};exports[_0x8884('0x40')]=function(_0x23a1f9,_0x229f0e){if(_0x23a1f9[_0x8884('0x41')][_0x8884('0x1f')]==='outbound'&&!_[_0x8884('0x42')](_0x23a1f9[_0x8884('0x41')][_0x8884('0x43')],'_')){_0x23a1f9[_0x8884('0x41')]['exten']='_'[_0x8884('0x44')](_0x23a1f9[_0x8884('0x41')][_0x8884('0x43')]);}return db[_0x8884('0x25')][_0x8884('0x40')](_0x23a1f9[_0x8884('0x41')],{'raw':![]})[_0x8884('0x13')](function(_0x27cd56){var _0x384b08=_0x23a1f9['user'][_0x8884('0x14')]({'plain':!![]});if(!_0x384b08)throw new Error(_0x8884('0x45'));if(_0x384b08[_0x8884('0x46')]===_0x8884('0x47')){var _0x259eca=_0x27cd56[_0x8884('0x14')]({'plain':!![]});var _0x554044=[{'name':'InboundRoutes','value':_0x8884('0x48')},{'name':_0x8884('0x16'),'value':_0x8884('0x17')},{'name':_0x8884('0x18'),'value':'internal'}];var _0x528efd=_[_0x8884('0x3f')](_0x554044,[_0x8884('0x1d'),_0x259eca['type']])[_0x8884('0x20')];return db[_0x8884('0x49')]['find']({'where':{'name':_0x528efd,'userProfileId':_0x384b08[_0x8884('0x4a')]},'raw':!![]})[_0x8884('0x13')](function(_0x2621b0){if(_0x2621b0&&_0x2621b0[_0x8884('0x4b')]===0x0){return db[_0x8884('0x21')][_0x8884('0x40')]({'name':_0x259eca[_0x8884('0x20')],'resourceId':_0x259eca['id'],'type':_0x2621b0['name'],'sectionId':_0x2621b0['id']},{})[_0x8884('0x13')](function(){return _0x27cd56;});}else{return _0x27cd56;}})['catch'](function(_0x51ad03){logger[_0x8884('0x22')](_0x8884('0x4c'),_0x51ad03);throw _0x51ad03;});}return _0x27cd56;})[_0x8884('0x13')](respondWithResult(_0x229f0e,0xc9))[_0x8884('0x3a')](handleError(_0x229f0e,null));};exports['update']=function(_0x2d7187,_0x17d6bb){if(_0x2d7187['body']['id']){delete _0x2d7187[_0x8884('0x41')]['id'];}return db[_0x8884('0x25')][_0x8884('0x3f')]({'where':{'id':_0x2d7187['params']['id']}})[_0x8884('0x13')](handleEntityNotFound(_0x17d6bb,null))[_0x8884('0x13')](function(_0x40ba74){if(_0x40ba74){return db[_0x8884('0x25')][_0x8884('0x4d')](_[_0x8884('0x4e')](_0x2d7187[_0x8884('0x41')],[_0x8884('0x43'),'context',_0x8884('0x4f'),'recordingFormat','cutdigits',_0x8884('0x50'),_0x8884('0x51')]),{'where':{'context':_0x40ba74[_0x8884('0x52')],'exten':_0x40ba74[_0x8884('0x43')]},'individualHooks':!![]})[_0x8884('0x13')](function(_0x4553a2){return _0x4553a2;});}return null;})[_0x8884('0x13')](respondWithStatusCode(_0x17d6bb,null))['catch'](handleError(_0x17d6bb,null));};exports[_0x8884('0x53')]=function(_0x56c09f,_0x39b6a1){var _0x3856d9=_0x56c09f[_0x8884('0x3c')]['id'];var _0x228fd2=_0x56c09f[_0x8884('0x41')];var _0x25fa56=0xc8;var _0x538dca=null;return db[_0x8884('0x54')][_0x8884('0x55')]({'isolationLevel':db['sequelize'][_0x8884('0x56')][_0x8884('0x57')][_0x8884('0x58')]},function(_0x4ea6ea){return db[_0x8884('0x25')]['findOne']({'where':{'id':_0x3856d9},'transaction':_0x4ea6ea})[_0x8884('0x13')](function(_0x5e3a9f){if(_0x5e3a9f){return db[_0x8884('0x25')][_0x8884('0x12')]({'where':{'VoiceExtensionId':_0x3856d9},'transaction':_0x4ea6ea})[_0x8884('0x13')](function(){var _0x557ab0=_['map'](_0x228fd2,function(_0x513d84){_0x513d84[_0x8884('0x59')]=_0x3856d9;_0x513d84[_0x8884('0x5a')]=!![];return _0x513d84;});return _0x5e3a9f['createRouting'](_0x557ab0);})[_0x8884('0x13')](function(_0x4e9988){return db[_0x8884('0x25')]['bulkCreate'](_0x4e9988,{'transaction':_0x4ea6ea});});}else{_0x25fa56=0x194;_0x538dca=[];}});})['then'](function(){if(_0x25fa56!==0x194){return db[_0x8884('0x25')]['findAndCountAll']({'where':{'isApp':!![],'VoiceExtensionId':_0x3856d9},'order':_0x8884('0x5b')})[_0x8884('0x13')](function(_0x1889c3){_0x538dca=_0x1889c3;});}})[_0x8884('0x3a')](function(_0x587d98){_0x25fa56=0x1f4;logger['error'](_0x587d98[_0x8884('0x23')]);if(_0x587d98[_0x8884('0x20')]){delete _0x587d98[_0x8884('0x20')];}_0x538dca=_0x587d98;})['finally'](function(){if(_0x538dca===null){_0x39b6a1[_0x8884('0x9')](_0x25fa56);}else{if(_0x25fa56===0x1f4){_0x39b6a1['status'](_0x25fa56)[_0x8884('0x24')](_0x538dca);}else{_0x39b6a1[_0x8884('0xa')](_0x25fa56)['json'](_0x538dca);}}});};