Built motion from commit d69f4380.|2.6.29
[motion2.git] / server / api / cmHopper / cmHopper.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 _0x5fc3=['CmHopperFinal','CmHopperAdditionalPhone','end','preview','dialOrderByScheduledAt','dialPreviewAutoRecallMe','sequelize','transaction','YYYY-MM-DD\x20HH:mm:ss','value','prev','priority','ASC','scheduledAt','DESC','COALESCE(CmHopper.UserId,\x200)\x20ASC','isEmpty','hopperIds','COALESCE(CmHopper.UserId,\x200)\x20DESC','scheduledat','recallme','LOCK','UPDATE','Contact','CmCompany','Company','CmList','List','Queue','dialActive','getOpenContacts','isNumber','CmHopper,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','sendStatus','getTimezones','timezones','getTimezone','moment','lodash','../../config/logger','api','../../config/environment','../../mysqldb','status','json','offset','undefined','limit','count','set','Content-Range','update','then','error','stack','name','index','map','CmHopper','rawAttributes','fieldName','type','key','model','query','keys','filters','concat','$or','$gt','$lt','$lte','$ne','$not','$notBetween','$in','$notLike','$notILike','$like','$contains','$contained','$any','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','include','findAll','rows','show','params','catch','create','body','find','describe','destroy','VoiceQueueId','CampaignId','VoiceQueue','Campaign','createdAt','updatedAt','state','statedesc','campaign','user','deleted','deletedat','format','campaigntype','ivr','CmContact','ContactId','firstName','isNil','calleridname','lastName','finally'];(function(_0x4edb1b,_0x57f3d4){var _0x1f4b95=function(_0x21409f){while(--_0x21409f){_0x4edb1b['push'](_0x4edb1b['shift']());}};_0x1f4b95(++_0x57f3d4);}(_0x5fc3,0xa8));var _0x35fc=function(_0x42bafd,_0x3f0fe3){_0x42bafd=_0x42bafd-0x0;var _0x2ed705=_0x5fc3[_0x42bafd];return _0x2ed705;};'use strict';var moment=require(_0x35fc('0x0'));var _=require(_0x35fc('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x35fc('0x2'))(_0x35fc('0x3'));var shared=require(_0x35fc('0x4'));var db=require(_0x35fc('0x5'))['db'];function respondWithResult(_0xb4f9ce,_0x5e75e6){_0x5e75e6=_0x5e75e6||0xc8;return function(_0x2d1404){if(_0x2d1404){return _0xb4f9ce[_0x35fc('0x6')](_0x5e75e6)[_0x35fc('0x7')](_0x2d1404);}};}function respondWithFilteredResult(_0x5b85ad,_0x5754be){return function(_0x19a0d1){if(_0x19a0d1){var _0x508b32=typeof _0x5754be[_0x35fc('0x8')]===_0x35fc('0x9')&&typeof _0x5754be[_0x35fc('0xa')]===_0x35fc('0x9');var _0x34ff19=_0x19a0d1[_0x35fc('0xb')];var _0x2051ab=_0x508b32?0x0:_0x5754be['offset'];var _0x1083a9=_0x508b32?_0x19a0d1[_0x35fc('0xb')]:_0x5754be['offset']+_0x5754be[_0x35fc('0xa')];var _0x33f491;if(_0x1083a9>=_0x34ff19){_0x1083a9=_0x34ff19;_0x33f491=0xc8;}else{_0x33f491=0xce;}_0x5b85ad['status'](_0x33f491);return _0x5b85ad[_0x35fc('0xc')](_0x35fc('0xd'),_0x2051ab+'-'+_0x1083a9+'/'+_0x34ff19)['json'](_0x19a0d1);}return null;};}function saveUpdates(_0x4cbe74,_0x409357){return function(_0x5d462a){if(_0x5d462a){return _0x5d462a[_0x35fc('0xe')](_0x4cbe74)[_0x35fc('0xf')](function(_0x2a6ad2){return _0x2a6ad2;});}return null;};}function handleEntityNotFound(_0x29730f,_0x38d9f2){return function(_0x5cfe94){if(!_0x5cfe94){_0x29730f['sendStatus'](0x194);}return _0x5cfe94;};}function handleError(_0x5eac0f,_0x558364){_0x558364=_0x558364||0x1f4;return function(_0x398b67){logger[_0x35fc('0x10')](_0x398b67[_0x35fc('0x11')]);if(_0x398b67[_0x35fc('0x12')]){delete _0x398b67[_0x35fc('0x12')];}_0x5eac0f[_0x35fc('0x6')](_0x558364)['send'](_0x398b67);};}exports[_0x35fc('0x13')]=function(_0x45c8b7,_0x553677){var _0x41e165={},_0x363e47={},_0x2e513b={'count':0x0,'rows':[]};var _0x5f56c6=_[_0x35fc('0x14')](db[_0x35fc('0x15')][_0x35fc('0x16')],function(_0x488234){return{'name':_0x488234[_0x35fc('0x17')],'type':_0x488234[_0x35fc('0x18')][_0x35fc('0x19')]};});_0x363e47[_0x35fc('0x1a')]=_[_0x35fc('0x14')](_0x5f56c6,_0x35fc('0x12'));_0x363e47[_0x35fc('0x1b')]=_[_0x35fc('0x1c')](_0x45c8b7[_0x35fc('0x1b')]);_0x363e47[_0x35fc('0x1d')]=_['intersection'](_0x363e47[_0x35fc('0x1a')],_0x363e47[_0x35fc('0x1b')]);_0x363e47[_0x35fc('0x1d')]=_[_0x35fc('0x1e')](_0x363e47[_0x35fc('0x1d')],['$and',_0x35fc('0x1f'),_0x35fc('0x20'),'$gte',_0x35fc('0x21'),_0x35fc('0x22'),_0x35fc('0x23'),'$eq',_0x35fc('0x24'),'$between',_0x35fc('0x25'),_0x35fc('0x26'),'$notIn','$like',_0x35fc('0x27'),'$iLike',_0x35fc('0x28'),_0x35fc('0x29'),'$overlap',_0x35fc('0x2a'),_0x35fc('0x2b'),_0x35fc('0x2c')]);_0x41e165[_0x35fc('0x2d')]=_[_0x35fc('0x2e')](_0x363e47[_0x35fc('0x1a')],qs[_0x35fc('0x2f')](_0x45c8b7[_0x35fc('0x1b')]['fields']));_0x41e165[_0x35fc('0x2d')]=_0x41e165[_0x35fc('0x2d')][_0x35fc('0x30')]?_0x41e165['attributes']:_0x363e47[_0x35fc('0x1a')];if(!_0x45c8b7[_0x35fc('0x1b')][_0x35fc('0x31')](_0x35fc('0x32'))){_0x41e165[_0x35fc('0xa')]=qs[_0x35fc('0xa')](_0x45c8b7[_0x35fc('0x1b')][_0x35fc('0xa')]);_0x41e165[_0x35fc('0x8')]=qs[_0x35fc('0x8')](_0x45c8b7['query']['offset']);}_0x41e165[_0x35fc('0x33')]=qs['sort'](_0x45c8b7[_0x35fc('0x1b')][_0x35fc('0x34')]);_0x41e165[_0x35fc('0x35')]=qs[_0x35fc('0x1d')](_[_0x35fc('0x36')](_0x45c8b7[_0x35fc('0x1b')],_0x363e47['filters']),_0x5f56c6);if(_0x45c8b7[_0x35fc('0x1b')][_0x35fc('0x37')]){_0x41e165[_0x35fc('0x35')]=_[_0x35fc('0x38')](_0x41e165[_0x35fc('0x35')],{'$or':_[_0x35fc('0x14')](_0x5f56c6,function(_0x553597){if(_0x553597[_0x35fc('0x18')]!==_0x35fc('0x39')){var _0x1fecdf={};_0x1fecdf[_0x553597['name']]={'$like':'%'+_0x45c8b7[_0x35fc('0x1b')]['filter']+'%'};return _0x1fecdf;}})});}_0x41e165=_[_0x35fc('0x38')]({},_0x41e165,_0x45c8b7[_0x35fc('0x3a')]);var _0x266a11={'where':_0x41e165[_0x35fc('0x35')]};return db['CmHopper']['count'](_0x266a11)['then'](function(_0x20f5fe){_0x2e513b['count']=_0x20f5fe;if(_0x45c8b7[_0x35fc('0x1b')]['includeAll']){_0x41e165[_0x35fc('0x3b')]=[{'all':!![]}];}return db[_0x35fc('0x15')][_0x35fc('0x3c')](_0x41e165);})[_0x35fc('0xf')](function(_0x1c10e0){_0x2e513b[_0x35fc('0x3d')]=_0x1c10e0;return _0x2e513b;})[_0x35fc('0xf')](respondWithFilteredResult(_0x553677,_0x41e165))['catch'](handleError(_0x553677,null));};exports[_0x35fc('0x3e')]=function(_0x16087d,_0x4e0ce3){var _0x1feda5={'raw':!![],'where':{'id':_0x16087d[_0x35fc('0x3f')]['id']}},_0x1ccc41={};_0x1ccc41[_0x35fc('0x1a')]=_[_0x35fc('0x1c')](db[_0x35fc('0x15')]['rawAttributes']);_0x1ccc41[_0x35fc('0x1b')]=_[_0x35fc('0x1c')](_0x16087d[_0x35fc('0x1b')]);_0x1ccc41['filters']=_[_0x35fc('0x2e')](_0x1ccc41[_0x35fc('0x1a')],_0x1ccc41['query']);_0x1feda5[_0x35fc('0x2d')]=_[_0x35fc('0x2e')](_0x1ccc41[_0x35fc('0x1a')],qs[_0x35fc('0x2f')](_0x16087d[_0x35fc('0x1b')][_0x35fc('0x2f')]));_0x1feda5[_0x35fc('0x2d')]=_0x1feda5[_0x35fc('0x2d')]['length']?_0x1feda5['attributes']:_0x1ccc41[_0x35fc('0x1a')];if(_0x16087d[_0x35fc('0x1b')]['includeAll']){_0x1feda5['include']=[{'all':!![]}];}_0x1feda5=_['merge']({},_0x1feda5,_0x16087d[_0x35fc('0x3a')]);return db[_0x35fc('0x15')]['find'](_0x1feda5)[_0x35fc('0xf')](handleEntityNotFound(_0x4e0ce3,null))[_0x35fc('0xf')](respondWithResult(_0x4e0ce3,null))[_0x35fc('0x40')](handleError(_0x4e0ce3,null));};exports[_0x35fc('0x41')]=function(_0x5c52be,_0x57105c){return db['CmHopper']['create'](_0x5c52be['body'],{})[_0x35fc('0xf')](respondWithResult(_0x57105c,0xc9))[_0x35fc('0x40')](handleError(_0x57105c,null));};exports['update']=function(_0x119925,_0x56cf1f){if(_0x119925[_0x35fc('0x42')]['id']){delete _0x119925[_0x35fc('0x42')]['id'];}return db[_0x35fc('0x15')][_0x35fc('0x43')]({'where':{'id':_0x119925[_0x35fc('0x3f')]['id']}})['then'](handleEntityNotFound(_0x56cf1f,null))[_0x35fc('0xf')](saveUpdates(_0x119925[_0x35fc('0x42')],null))['then'](respondWithResult(_0x56cf1f,null))[_0x35fc('0x40')](handleError(_0x56cf1f,null));};exports[_0x35fc('0x44')]=function(_0x2a8a09,_0x211746){return db['CmHopper'][_0x35fc('0x44')]()['then'](respondWithResult(_0x211746,null))['catch'](handleError(_0x211746,null));};exports[_0x35fc('0x45')]=function(_0x9db7e8,_0x18dc78){var _0xca8aef;var _0xf2ca20=_0x9db7e8[_0x35fc('0x3f')]['id'];return db[_0x35fc('0x15')][_0x35fc('0x43')]({'where':{'id':_0x9db7e8['params']['id']}})[_0x35fc('0xf')](handleEntityNotFound(_0x18dc78,null))[_0x35fc('0xf')](function(_0xeca02e){if(_0xeca02e){return _0xeca02e[_0x35fc('0x45')]()[_0x35fc('0xf')](function(_0x1e3221){return _0x1e3221;});}})[_0x35fc('0xf')](function(_0x347a4a){_0xca8aef=_0x347a4a;if(_0x347a4a){if(_0x347a4a[_0x35fc('0x46')]||_0x347a4a[_0x35fc('0x47')]){return db[_0x347a4a[_0x35fc('0x46')]?_0x35fc('0x48'):_0x35fc('0x49')][_0x35fc('0x43')]({'where':{'id':_0x347a4a['VoiceQueueId']?_0x347a4a['VoiceQueueId']:_0x347a4a[_0x35fc('0x47')]},'attributes':['id',_0x35fc('0x12')],'raw':!![]});}}})[_0x35fc('0xf')](function(_0x2bf0b0){var _0x3ee296={};if(_0x2bf0b0){_[_0x35fc('0x38')](_0x3ee296,_['omit'](_0xca8aef['dataValues'],[_0x35fc('0x4a'),_0x35fc('0x4b'),'id']));_0x3ee296[_0x35fc('0x4c')]=0x15;_0x3ee296[_0x35fc('0x4d')]='deleted';_0x3ee296[_0x35fc('0x4e')]=_0x2bf0b0[_0x35fc('0x12')];_0x3ee296['membername']=_0x9db7e8[_0x35fc('0x4f')][_0x35fc('0x12')];_0x3ee296['UserId']=_0x9db7e8[_0x35fc('0x4f')]['id'];_0x3ee296[_0x35fc('0x50')]=!![];_0x3ee296[_0x35fc('0x51')]=moment()[_0x35fc('0x52')]('YYYY-MM-DD\x20HH:mm:ss');_0x3ee296['calleridnum']=_0xca8aef['phone'];_0x3ee296[_0x35fc('0x53')]=_0xca8aef[_0x35fc('0x46')]?'queue':_0x35fc('0x54');return db[_0x35fc('0x55')][_0x35fc('0x43')]({'where':{'id':_0x3ee296[_0x35fc('0x56')]},'attributes':[_0x35fc('0x57'),'lastName']})[_0x35fc('0xf')](function(_0x757aa6){if(!_[_0x35fc('0x58')](_0x18dc78)&&!_['isNil'](_0x757aa6[_0x35fc('0x57')])){_0x3ee296[_0x35fc('0x59')]=_0x757aa6['firstName'];if(!_[_0x35fc('0x58')](_0x757aa6[_0x35fc('0x5a')])){_0x3ee296[_0x35fc('0x59')]+='\x20'+_0x757aa6[_0x35fc('0x5a')];}}})[_0x35fc('0x5b')](function(){return db[_0x35fc('0x5c')][_0x35fc('0x41')](_0x3ee296);});}})[_0x35fc('0xf')](function(_0x53a79d){return db[_0x35fc('0x5d')][_0x35fc('0x45')]({'where':{'CmHopperId':_0xf2ca20}});})[_0x35fc('0xf')](function(){return _0x18dc78['status'](0xcc)[_0x35fc('0x5e')]();})['catch'](handleError(_0x18dc78,null));};exports['getPreview']=function(_0x5d4a39,_0x23b54f){var _0xb1c68a=[];return db[_0x35fc('0x55')][_0x35fc('0x44')]()[_0x35fc('0xf')](function(_0x4bbea0){_0xb1c68a=_['keys'](_0x4bbea0);return _0x5d4a39['user']['getVoiceQueues']({'where':{'type':'outbound','dialActive':!![],'dialMethod':_0x35fc('0x5f')},'attributes':['id',_0x35fc('0x60'),_0x35fc('0x61')],'raw':!![]});})[_0x35fc('0xf')](function(_0x511e8b){if(_0x511e8b&&_0x511e8b[_0x35fc('0x30')]){return db[_0x35fc('0x62')][_0x35fc('0x63')](function(_0x1b0063){var _0x37bea1={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x35fc('0x52')](_0x35fc('0x64'))},'VoiceQueueId':{'$in':_[_0x35fc('0x14')](_0x511e8b,'id')},'$or':[{'UserId':null},{'UserId':_0x5d4a39[_0x35fc('0x4f')]['id']}]};var _0x4c3ace={'active':![],'recallme':!![],'VoiceQueueId':{'$in':_(_0x511e8b)[_0x35fc('0x37')](['dialPreviewAutoRecallMe',0x0])[_0x35fc('0x14')]('id')[_0x35fc('0x65')]()},'$or':[{'UserId':null},{'UserId':_0x5d4a39[_0x35fc('0x4f')]['id']}]};var _0x2bc76c={'$or':[_0x37bea1,_0x4c3ace]};var _0x4bdde7=[];if(_0x5d4a39[_0x35fc('0x42')]['direction']&&_0x5d4a39['body']['direction']===_0x35fc('0x66')){if(!_['isEmpty'](_0x5d4a39[_0x35fc('0x42')]['hopperIds'])){_0x2bc76c['id']={'$in':_0x5d4a39[_0x35fc('0x42')]['hopperIds']};}_0x4bdde7=[[_0x35fc('0x67'),_0x35fc('0x68')],[_0x35fc('0x69'),_0x511e8b[0x0][_0x35fc('0x60')]==='ASC'?_0x35fc('0x6a'):_0x35fc('0x68')],db[_0x35fc('0x62')]['literal'](_0x35fc('0x6b')),['id',_0x35fc('0x6a')]];}else{if(!_[_0x35fc('0x6c')](_0x5d4a39[_0x35fc('0x42')][_0x35fc('0x6d')])){_0x2bc76c['id']={'$notIn':_0x5d4a39[_0x35fc('0x42')][_0x35fc('0x6d')]};}_0x4bdde7=[[_0x35fc('0x67'),'DESC'],[_0x35fc('0x69'),_0x511e8b[0x0][_0x35fc('0x60')]],db[_0x35fc('0x62')]['literal'](_0x35fc('0x6e')),['id',_0x35fc('0x68')]];}return db['CmHopper']['findOne']({'attributes':['id',_0x35fc('0x6f'),'phone',_0x35fc('0x70')],'where':_0x2bc76c,'order':_0x4bdde7,'transaction':_0x1b0063,'lock':_0x1b0063[_0x35fc('0x71')][_0x35fc('0x72')],'include':[{'model':db[_0x35fc('0x55')],'as':_0x35fc('0x73'),'attributes':_0xb1c68a,'include':[{'model':db[_0x35fc('0x74')],'as':_0x35fc('0x75'),'attributes':['id',_0x35fc('0x12')]}]},{'model':db[_0x35fc('0x76')],'as':_0x35fc('0x77'),'attributes':['id',_0x35fc('0x12')]},{'model':db[_0x35fc('0x48')],'as':_0x35fc('0x78'),'attributes':['id',_0x35fc('0x12'),_0x35fc('0x79')]}]})[_0x35fc('0xf')](function(_0x2d8219){if(_0x2d8219){return _0x2d8219[_0x35fc('0xe')]({'UserId':_0x5d4a39[_0x35fc('0x4f')]['id']},{'transaction':_0x1b0063});}});});}})[_0x35fc('0xf')](handleEntityNotFound(_0x23b54f,null))['then'](respondWithResult(_0x23b54f,null))[_0x35fc('0x40')](handleError(_0x23b54f,null));};exports['getPreviewRecall']=function(_0x88d681,_0x6c5ef,_0x2185ce){var _0x516b26=[];var _0x25af78=_0x88d681[_0x35fc('0x3f')]['id'];var _0x2913ba=_0x88d681[_0x35fc('0x4f')]['id'];return db[_0x35fc('0x55')][_0x35fc('0x44')]()[_0x35fc('0xf')](function(_0x580062){_0x516b26=_[_0x35fc('0x1c')](_0x580062);return db[_0x35fc('0x62')][_0x35fc('0x63')](function(_0x1ca01f){const _0x3a973d={'id':_0x25af78};return db[_0x35fc('0x15')]['findOne']({'attributes':['id',_0x35fc('0x6f'),_0x35fc('0x70')],'where':_0x3a973d,'transaction':_0x1ca01f,'lock':_0x1ca01f[_0x35fc('0x71')][_0x35fc('0x72')],'include':[{'model':db[_0x35fc('0x55')],'as':_0x35fc('0x73'),'attributes':_0x516b26,'include':[{'model':db[_0x35fc('0x74')],'as':_0x35fc('0x75'),'attributes':['id',_0x35fc('0x12')]}]},{'model':db['CmList'],'as':_0x35fc('0x77'),'attributes':['id','name']},{'model':db[_0x35fc('0x48')],'as':_0x35fc('0x78'),'attributes':['id','name',_0x35fc('0x79')]}]})[_0x35fc('0xf')](function(_0xcaa49){if(_0xcaa49){return _0xcaa49[_0x35fc('0xe')]({'UserId':_0x2913ba},{'transaction':_0x1ca01f});}});});})[_0x35fc('0xf')](handleEntityNotFound(_0x6c5ef,null))[_0x35fc('0xf')](respondWithResult(_0x6c5ef,null))[_0x35fc('0x40')](handleError(_0x6c5ef,null));};exports[_0x35fc('0x7a')]=function(_0x25ee61,_0x5806bf){return _0x25ee61['user']['getVoiceQueues']({'where':{'type':'outbound','dialActive':!![],'dialMethod':_0x35fc('0x5f')},'attributes':['id'],'raw':!![]})[_0x35fc('0xf')](function(_0x8fc410){if(_0x8fc410&&_0x8fc410[_0x35fc('0x30')]){return db[_0x35fc('0x15')]['count']({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x35fc('0x52')](_0x35fc('0x64'))},'VoiceQueueId':{'$in':_[_0x35fc('0x14')](_0x8fc410,'id')},'$or':[{'UserId':null},{'UserId':_0x25ee61[_0x35fc('0x4f')]['id']}]}});}return 0x0;})['then'](function(_0x15e936){if(!_[_0x35fc('0x58')](_0x15e936)&&_[_0x35fc('0x7b')](_0x15e936)){return _0x5806bf['status'](0xc8)[_0x35fc('0x7')]({'contacts':_0x15e936});}else{logger[_0x35fc('0x10')](_0x35fc('0x7c'),_0x35fc('0x7a'),0x194,_0x35fc('0x7d'));_0x5806bf[_0x35fc('0x7e')](0x194);}})[_0x35fc('0x40')](handleError(_0x5806bf,null));};exports[_0x35fc('0x7f')]=function(_0x4b6c03,_0x2faff7){var _0xa2c5f8=shared[_0x35fc('0x80')];var _0x2799c8=[];_[_0x35fc('0x14')](_0xa2c5f8,function(_0x1fcd36){_0x2799c8['push']({'id':_0x1fcd36[_0x35fc('0x12')],'name':_0x1fcd36['name'],'utcOffset':_0x1fcd36['utcOffset']});});return _0x2faff7[_0x35fc('0x6')](0xc8)['json']({'tz':_0x2799c8});};exports[_0x35fc('0x81')]=function(_0x233b7d,_0x2885b9){var _0x54cbcb=shared[_0x35fc('0x80')];var _0x2b3edc={};if(!_[_0x35fc('0x58')](_0x54cbcb[_0x233b7d[_0x35fc('0x42')]['id']]))_0x2b3edc=_0x54cbcb[_0x233b7d[_0x35fc('0x42')]['id']];return _0x2885b9['status'](0xc8)['json'](_0x2b3edc);};