1f008527757c1fcd6fc3c41361de83d58e3d8c32
[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 _0x1b20=['$ne','$not','$between','$notBetween','$in','$like','$iLike','$notILike','$overlap','$any','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','type','VIRTUAL','merge','options','include','findAll','then','rows','show','params','find','catch','create','body','describe','destroy','VoiceQueueId','CampaignId','Campaign','omit','dataValues','createdAt','updatedAt','statedesc','deleted','campaign','membername','user','UserId','deletedat','format','YYYY-MM-DD\x20HH:mm:ss','calleridnum','ivr','CmContact','firstName','isNil','lastName','calleridname','finally','CmHopperAdditionalPhone','end','getPreview','keys','outbound','preview','dialOrderByScheduledAt','transaction','dialPreviewAutoRecallMe','direction','prev','priority','scheduledAt','DESC','ASC','sequelize','literal','COALESCE(CmHopper.UserId,\x200)\x20ASC','isEmpty','hopperIds','COALESCE(CmHopper.UserId,\x200)\x20DESC','findOne','scheduledat','phone','LOCK','UPDATE','Contact','CmCompany','Company','VoiceQueue','Queue','dialActive','recallme','CmList','List','getOpenContacts','getVoiceQueues','isNumber','CmHopper,\x20%s,\x20%s,\x20%s','getTimezones','timezones','utcOffset','getTimezone','lodash','../../components/parsers/qs','../../config/environment','../../mysqldb','status','json','offset','limit','count','Content-Range','update','sendStatus','error','stack','send','map','CmHopper','rawAttributes','fieldName','key','name','query','filters','intersection','model','concat','$or','$gt','$gte','$lt','$lte'];(function(_0x2fa022,_0x3052e8){var _0x1f0355=function(_0x46b15c){while(--_0x46b15c){_0x2fa022['push'](_0x2fa022['shift']());}};_0x1f0355(++_0x3052e8);}(_0x1b20,0x66));var _0x01b2=function(_0x59ee6f,_0x450239){_0x59ee6f=_0x59ee6f-0x0;var _0x4c0979=_0x1b20[_0x59ee6f];return _0x4c0979;};'use strict';var moment=require('moment');var _=require(_0x01b2('0x0'));var qs=require(_0x01b2('0x1'));var logger=require('../../config/logger')('api');var shared=require(_0x01b2('0x2'));var db=require(_0x01b2('0x3'))['db'];function respondWithResult(_0x2d6e99,_0x1150fc){_0x1150fc=_0x1150fc||0xc8;return function(_0x1cf757){if(_0x1cf757){return _0x2d6e99[_0x01b2('0x4')](_0x1150fc)[_0x01b2('0x5')](_0x1cf757);}};}function respondWithFilteredResult(_0x28de4a,_0x46e745){return function(_0xa64ad1){if(_0xa64ad1){var _0x43d561=typeof _0x46e745[_0x01b2('0x6')]==='undefined'&&typeof _0x46e745[_0x01b2('0x7')]==='undefined';var _0x2e975d=_0xa64ad1[_0x01b2('0x8')];var _0x3bad99=_0x43d561?0x0:_0x46e745[_0x01b2('0x6')];var _0x9c411f=_0x43d561?_0xa64ad1[_0x01b2('0x8')]:_0x46e745[_0x01b2('0x6')]+_0x46e745[_0x01b2('0x7')];var _0x55804c;if(_0x9c411f>=_0x2e975d){_0x9c411f=_0x2e975d;_0x55804c=0xc8;}else{_0x55804c=0xce;}_0x28de4a[_0x01b2('0x4')](_0x55804c);return _0x28de4a['set'](_0x01b2('0x9'),_0x3bad99+'-'+_0x9c411f+'/'+_0x2e975d)['json'](_0xa64ad1);}return null;};}function saveUpdates(_0x41a618,_0x8ac569){return function(_0x2c6d4d){if(_0x2c6d4d){return _0x2c6d4d[_0x01b2('0xa')](_0x41a618)['then'](function(_0x2f97d1){return _0x2f97d1;});}return null;};}function handleEntityNotFound(_0x2e7b31,_0x4f1c15){return function(_0x1626ba){if(!_0x1626ba){_0x2e7b31[_0x01b2('0xb')](0x194);}return _0x1626ba;};}function handleError(_0x5a36de,_0x299d0c){_0x299d0c=_0x299d0c||0x1f4;return function(_0x3d5833){logger[_0x01b2('0xc')](_0x3d5833[_0x01b2('0xd')]);if(_0x3d5833['name']){delete _0x3d5833['name'];}_0x5a36de[_0x01b2('0x4')](_0x299d0c)[_0x01b2('0xe')](_0x3d5833);};}exports['index']=function(_0x1eafc4,_0x444236){var _0x27a0bc={},_0x5986f5={},_0x25c55f={'count':0x0,'rows':[]};var _0x9f182b=_[_0x01b2('0xf')](db[_0x01b2('0x10')][_0x01b2('0x11')],function(_0x56ea3c){return{'name':_0x56ea3c[_0x01b2('0x12')],'type':_0x56ea3c['type'][_0x01b2('0x13')]};});_0x5986f5['model']=_[_0x01b2('0xf')](_0x9f182b,_0x01b2('0x14'));_0x5986f5[_0x01b2('0x15')]=_['keys'](_0x1eafc4[_0x01b2('0x15')]);_0x5986f5[_0x01b2('0x16')]=_[_0x01b2('0x17')](_0x5986f5[_0x01b2('0x18')],_0x5986f5[_0x01b2('0x15')]);_0x5986f5['filters']=_[_0x01b2('0x19')](_0x5986f5['filters'],['$and',_0x01b2('0x1a'),_0x01b2('0x1b'),_0x01b2('0x1c'),_0x01b2('0x1d'),_0x01b2('0x1e'),_0x01b2('0x1f'),'$eq',_0x01b2('0x20'),_0x01b2('0x21'),_0x01b2('0x22'),_0x01b2('0x23'),'$notIn',_0x01b2('0x24'),'$notLike',_0x01b2('0x25'),_0x01b2('0x26'),'$like',_0x01b2('0x27'),'$contains','$contained',_0x01b2('0x28')]);_0x27a0bc[_0x01b2('0x29')]=_['intersection'](_0x5986f5[_0x01b2('0x18')],qs[_0x01b2('0x2a')](_0x1eafc4[_0x01b2('0x15')][_0x01b2('0x2a')]));_0x27a0bc[_0x01b2('0x29')]=_0x27a0bc[_0x01b2('0x29')][_0x01b2('0x2b')]?_0x27a0bc[_0x01b2('0x29')]:_0x5986f5[_0x01b2('0x18')];if(!_0x1eafc4[_0x01b2('0x15')][_0x01b2('0x2c')]('nolimit')){_0x27a0bc[_0x01b2('0x7')]=qs[_0x01b2('0x7')](_0x1eafc4[_0x01b2('0x15')][_0x01b2('0x7')]);_0x27a0bc[_0x01b2('0x6')]=qs[_0x01b2('0x6')](_0x1eafc4['query']['offset']);}_0x27a0bc[_0x01b2('0x2d')]=qs['sort'](_0x1eafc4['query'][_0x01b2('0x2e')]);_0x27a0bc[_0x01b2('0x2f')]=qs[_0x01b2('0x16')](_[_0x01b2('0x30')](_0x1eafc4['query'],_0x5986f5[_0x01b2('0x16')]),_0x9f182b);if(_0x1eafc4['query'][_0x01b2('0x31')]){_0x27a0bc['where']=_['merge'](_0x27a0bc['where'],{'$or':_[_0x01b2('0xf')](_0x9f182b,function(_0x29532e){if(_0x29532e[_0x01b2('0x32')]!==_0x01b2('0x33')){var _0x4d80d0={};_0x4d80d0[_0x29532e[_0x01b2('0x14')]]={'$like':'%'+_0x1eafc4[_0x01b2('0x15')]['filter']+'%'};return _0x4d80d0;}})});}_0x27a0bc=_[_0x01b2('0x34')]({},_0x27a0bc,_0x1eafc4[_0x01b2('0x35')]);var _0x13dbb4={'where':_0x27a0bc[_0x01b2('0x2f')]};return db[_0x01b2('0x10')][_0x01b2('0x8')](_0x13dbb4)['then'](function(_0x2fb530){_0x25c55f[_0x01b2('0x8')]=_0x2fb530;if(_0x1eafc4['query']['includeAll']){_0x27a0bc[_0x01b2('0x36')]=[{'all':!![]}];}return db[_0x01b2('0x10')][_0x01b2('0x37')](_0x27a0bc);})[_0x01b2('0x38')](function(_0x34fdb2){_0x25c55f[_0x01b2('0x39')]=_0x34fdb2;return _0x25c55f;})['then'](respondWithFilteredResult(_0x444236,_0x27a0bc))['catch'](handleError(_0x444236,null));};exports[_0x01b2('0x3a')]=function(_0x4ba89b,_0xbd7259){var _0x31878={'raw':!![],'where':{'id':_0x4ba89b[_0x01b2('0x3b')]['id']}},_0x1a1e3b={};_0x1a1e3b[_0x01b2('0x18')]=_['keys'](db[_0x01b2('0x10')][_0x01b2('0x11')]);_0x1a1e3b[_0x01b2('0x15')]=_['keys'](_0x4ba89b[_0x01b2('0x15')]);_0x1a1e3b[_0x01b2('0x16')]=_[_0x01b2('0x17')](_0x1a1e3b['model'],_0x1a1e3b[_0x01b2('0x15')]);_0x31878['attributes']=_[_0x01b2('0x17')](_0x1a1e3b[_0x01b2('0x18')],qs['fields'](_0x4ba89b[_0x01b2('0x15')][_0x01b2('0x2a')]));_0x31878['attributes']=_0x31878[_0x01b2('0x29')][_0x01b2('0x2b')]?_0x31878[_0x01b2('0x29')]:_0x1a1e3b[_0x01b2('0x18')];if(_0x4ba89b[_0x01b2('0x15')]['includeAll']){_0x31878[_0x01b2('0x36')]=[{'all':!![]}];}_0x31878=_[_0x01b2('0x34')]({},_0x31878,_0x4ba89b[_0x01b2('0x35')]);return db['CmHopper'][_0x01b2('0x3c')](_0x31878)[_0x01b2('0x38')](handleEntityNotFound(_0xbd7259,null))[_0x01b2('0x38')](respondWithResult(_0xbd7259,null))[_0x01b2('0x3d')](handleError(_0xbd7259,null));};exports[_0x01b2('0x3e')]=function(_0x3f9451,_0x3eda44){return db[_0x01b2('0x10')]['create'](_0x3f9451[_0x01b2('0x3f')],{})[_0x01b2('0x38')](respondWithResult(_0x3eda44,0xc9))[_0x01b2('0x3d')](handleError(_0x3eda44,null));};exports[_0x01b2('0xa')]=function(_0xf36d42,_0x3469b3){if(_0xf36d42[_0x01b2('0x3f')]['id']){delete _0xf36d42['body']['id'];}return db[_0x01b2('0x10')][_0x01b2('0x3c')]({'where':{'id':_0xf36d42[_0x01b2('0x3b')]['id']}})[_0x01b2('0x38')](handleEntityNotFound(_0x3469b3,null))['then'](saveUpdates(_0xf36d42['body'],null))[_0x01b2('0x38')](respondWithResult(_0x3469b3,null))[_0x01b2('0x3d')](handleError(_0x3469b3,null));};exports[_0x01b2('0x40')]=function(_0x49370d,_0x7a6cae){return db[_0x01b2('0x10')][_0x01b2('0x40')]()[_0x01b2('0x38')](respondWithResult(_0x7a6cae,null))[_0x01b2('0x3d')](handleError(_0x7a6cae,null));};exports[_0x01b2('0x41')]=function(_0x5b54c5,_0x2a8b8d){var _0x5e0e70;var _0x272d0d=_0x5b54c5['params']['id'];return db['CmHopper'][_0x01b2('0x3c')]({'where':{'id':_0x5b54c5[_0x01b2('0x3b')]['id']}})[_0x01b2('0x38')](handleEntityNotFound(_0x2a8b8d,null))[_0x01b2('0x38')](function(_0x20cc59){if(_0x20cc59){return _0x20cc59['destroy']()['then'](function(_0x387954){return _0x387954;});}})[_0x01b2('0x38')](function(_0x3ee05b){_0x5e0e70=_0x3ee05b;if(_0x3ee05b){if(_0x3ee05b[_0x01b2('0x42')]||_0x3ee05b[_0x01b2('0x43')]){return db[_0x3ee05b[_0x01b2('0x42')]?'VoiceQueue':_0x01b2('0x44')][_0x01b2('0x3c')]({'where':{'id':_0x3ee05b[_0x01b2('0x42')]?_0x3ee05b[_0x01b2('0x42')]:_0x3ee05b[_0x01b2('0x43')]},'attributes':['id',_0x01b2('0x14')],'raw':!![]});}}})[_0x01b2('0x38')](function(_0x5dcafc){var _0x445fb2={};if(_0x5dcafc){_[_0x01b2('0x34')](_0x445fb2,_[_0x01b2('0x45')](_0x5e0e70[_0x01b2('0x46')],[_0x01b2('0x47'),_0x01b2('0x48'),'id']));_0x445fb2['state']=0x15;_0x445fb2[_0x01b2('0x49')]=_0x01b2('0x4a');_0x445fb2[_0x01b2('0x4b')]=_0x5dcafc[_0x01b2('0x14')];_0x445fb2[_0x01b2('0x4c')]=_0x5b54c5[_0x01b2('0x4d')][_0x01b2('0x14')];_0x445fb2[_0x01b2('0x4e')]=_0x5b54c5[_0x01b2('0x4d')]['id'];_0x445fb2[_0x01b2('0x4a')]=!![];_0x445fb2[_0x01b2('0x4f')]=moment()[_0x01b2('0x50')](_0x01b2('0x51'));_0x445fb2[_0x01b2('0x52')]=_0x5e0e70['phone'];_0x445fb2['campaigntype']=_0x5e0e70['VoiceQueueId']?'queue':_0x01b2('0x53');return db[_0x01b2('0x54')][_0x01b2('0x3c')]({'where':{'id':_0x445fb2['ContactId']},'attributes':[_0x01b2('0x55'),'lastName']})[_0x01b2('0x38')](function(_0x32371b){if(!_[_0x01b2('0x56')](_0x2a8b8d)&&!_[_0x01b2('0x56')](_0x32371b[_0x01b2('0x55')])){_0x445fb2['calleridname']=_0x32371b[_0x01b2('0x55')];if(!_['isNil'](_0x32371b[_0x01b2('0x57')])){_0x445fb2[_0x01b2('0x58')]+='\x20'+_0x32371b['lastName'];}}})[_0x01b2('0x59')](function(){return db['CmHopperFinal'][_0x01b2('0x3e')](_0x445fb2);});}})[_0x01b2('0x38')](function(_0x42c5b8){return db[_0x01b2('0x5a')][_0x01b2('0x41')]({'where':{'CmHopperId':_0x272d0d}});})[_0x01b2('0x38')](function(){return _0x2a8b8d[_0x01b2('0x4')](0xcc)[_0x01b2('0x5b')]();})[_0x01b2('0x3d')](handleError(_0x2a8b8d,null));};exports[_0x01b2('0x5c')]=function(_0x5d7da3,_0x2eb448){var _0x5655b6=[];return db[_0x01b2('0x54')][_0x01b2('0x40')]()[_0x01b2('0x38')](function(_0x58c8de){_0x5655b6=_[_0x01b2('0x5d')](_0x58c8de);return _0x5d7da3['user']['getVoiceQueues']({'where':{'type':_0x01b2('0x5e'),'dialActive':!![],'dialMethod':_0x01b2('0x5f')},'attributes':['id',_0x01b2('0x60'),'dialPreviewAutoRecallMe'],'raw':!![]});})[_0x01b2('0x38')](function(_0x330cb4){if(_0x330cb4&&_0x330cb4[_0x01b2('0x2b')]){return db['sequelize'][_0x01b2('0x61')](function(_0x429b89){var _0xcbef9c={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x01b2('0x50')](_0x01b2('0x51'))},'VoiceQueueId':{'$in':_[_0x01b2('0xf')](_0x330cb4,'id')},'$or':[{'UserId':null},{'UserId':_0x5d7da3[_0x01b2('0x4d')]['id']}]};var _0x56c794={'active':![],'recallme':!![],'VoiceQueueId':{'$in':_(_0x330cb4)['filter']([_0x01b2('0x62'),0x0])['map']('id')['value']()},'$or':[{'UserId':null},{'UserId':_0x5d7da3[_0x01b2('0x4d')]['id']}]};var _0x4ca277={'$or':[_0xcbef9c,_0x56c794]};var _0xbe104b=[];if(_0x5d7da3['body'][_0x01b2('0x63')]&&_0x5d7da3[_0x01b2('0x3f')][_0x01b2('0x63')]===_0x01b2('0x64')){if(!_['isEmpty'](_0x5d7da3[_0x01b2('0x3f')]['hopperIds'])){_0x4ca277['id']={'$in':_0x5d7da3[_0x01b2('0x3f')]['hopperIds']};}_0xbe104b=[[_0x01b2('0x65'),'ASC'],[_0x01b2('0x66'),_0x330cb4[0x0][_0x01b2('0x60')]==='ASC'?_0x01b2('0x67'):_0x01b2('0x68')],db[_0x01b2('0x69')][_0x01b2('0x6a')](_0x01b2('0x6b')),['id',_0x01b2('0x67')]];}else{if(!_[_0x01b2('0x6c')](_0x5d7da3[_0x01b2('0x3f')][_0x01b2('0x6d')])){_0x4ca277['id']={'$notIn':_0x5d7da3[_0x01b2('0x3f')][_0x01b2('0x6d')]};}_0xbe104b=[[_0x01b2('0x65'),_0x01b2('0x67')],['scheduledAt',_0x330cb4[0x0]['dialOrderByScheduledAt']],db[_0x01b2('0x69')][_0x01b2('0x6a')](_0x01b2('0x6e')),['id',_0x01b2('0x68')]];}return db[_0x01b2('0x10')][_0x01b2('0x6f')]({'attributes':['id',_0x01b2('0x70'),_0x01b2('0x71'),'recallme'],'where':_0x4ca277,'order':_0xbe104b,'transaction':_0x429b89,'lock':_0x429b89[_0x01b2('0x72')][_0x01b2('0x73')],'include':[{'model':db[_0x01b2('0x54')],'as':_0x01b2('0x74'),'attributes':_0x5655b6,'include':[{'model':db[_0x01b2('0x75')],'as':_0x01b2('0x76'),'attributes':['id',_0x01b2('0x14')]}]},{'model':db['CmList'],'as':'List','attributes':['id',_0x01b2('0x14')]},{'model':db[_0x01b2('0x77')],'as':_0x01b2('0x78'),'attributes':['id','name',_0x01b2('0x79')]}]})[_0x01b2('0x38')](function(_0x240558){if(_0x240558){return _0x240558['update']({'UserId':_0x5d7da3[_0x01b2('0x4d')]['id']},{'transaction':_0x429b89});}});});}})[_0x01b2('0x38')](handleEntityNotFound(_0x2eb448,null))[_0x01b2('0x38')](respondWithResult(_0x2eb448,null))['catch'](handleError(_0x2eb448,null));};exports['getPreviewRecall']=function(_0x2a820d,_0x4f1d6a,_0x1e1155){var _0x10d19f=[];var _0x2f41b6=_0x2a820d[_0x01b2('0x3b')]['id'];var _0x19a417=_0x2a820d[_0x01b2('0x4d')]['id'];return db['CmContact'][_0x01b2('0x40')]()[_0x01b2('0x38')](function(_0x45caa2){_0x10d19f=_[_0x01b2('0x5d')](_0x45caa2);return db[_0x01b2('0x69')][_0x01b2('0x61')](function(_0x5038b2){const _0x12f5ed={'id':_0x2f41b6};return db[_0x01b2('0x10')][_0x01b2('0x6f')]({'attributes':['id',_0x01b2('0x70'),_0x01b2('0x7a')],'where':_0x12f5ed,'transaction':_0x5038b2,'lock':_0x5038b2[_0x01b2('0x72')]['UPDATE'],'include':[{'model':db[_0x01b2('0x54')],'as':'Contact','attributes':_0x10d19f,'include':[{'model':db[_0x01b2('0x75')],'as':'Company','attributes':['id','name']}]},{'model':db[_0x01b2('0x7b')],'as':_0x01b2('0x7c'),'attributes':['id','name']},{'model':db[_0x01b2('0x77')],'as':_0x01b2('0x78'),'attributes':['id',_0x01b2('0x14'),_0x01b2('0x79')]}]})['then'](function(_0x4095c2){if(_0x4095c2){return _0x4095c2[_0x01b2('0xa')]({'UserId':_0x19a417},{'transaction':_0x5038b2});}});});})[_0x01b2('0x38')](handleEntityNotFound(_0x4f1d6a,null))[_0x01b2('0x38')](respondWithResult(_0x4f1d6a,null))[_0x01b2('0x3d')](handleError(_0x4f1d6a,null));};exports[_0x01b2('0x7d')]=function(_0x27de72,_0x44034b){return _0x27de72[_0x01b2('0x4d')][_0x01b2('0x7e')]({'where':{'type':_0x01b2('0x5e'),'dialActive':!![],'dialMethod':'preview'},'attributes':['id'],'raw':!![]})[_0x01b2('0x38')](function(_0x3e17c3){if(_0x3e17c3&&_0x3e17c3[_0x01b2('0x2b')]){return db[_0x01b2('0x10')][_0x01b2('0x8')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x01b2('0x50')](_0x01b2('0x51'))},'VoiceQueueId':{'$in':_[_0x01b2('0xf')](_0x3e17c3,'id')},'$or':[{'UserId':null},{'UserId':_0x27de72['user']['id']}]}});}return 0x0;})['then'](function(_0xd37088){if(!_[_0x01b2('0x56')](_0xd37088)&&_[_0x01b2('0x7f')](_0xd37088)){return _0x44034b[_0x01b2('0x4')](0xc8)[_0x01b2('0x5')]({'contacts':_0xd37088});}else{logger[_0x01b2('0xc')](_0x01b2('0x80'),_0x01b2('0x7d'),0x194,'entity\x20not\x20found');_0x44034b[_0x01b2('0xb')](0x194);}})[_0x01b2('0x3d')](handleError(_0x44034b,null));};exports[_0x01b2('0x81')]=function(_0x54ccc5,_0x24f9d4){var _0x2dec32=shared[_0x01b2('0x82')];var _0x35aef9=[];_['map'](_0x2dec32,function(_0x5b8abf){_0x35aef9['push']({'id':_0x5b8abf[_0x01b2('0x14')],'name':_0x5b8abf['name'],'utcOffset':_0x5b8abf[_0x01b2('0x83')]});});return _0x24f9d4[_0x01b2('0x4')](0xc8)[_0x01b2('0x5')]({'tz':_0x35aef9});};exports[_0x01b2('0x84')]=function(_0x176ef,_0x372585){var _0x46e1ef=shared[_0x01b2('0x82')];var _0x3fb15f={};if(!_[_0x01b2('0x56')](_0x46e1ef[_0x176ef['body']['id']]))_0x3fb15f=_0x46e1ef[_0x176ef[_0x01b2('0x3f')]['id']];return _0x372585[_0x01b2('0x4')](0xc8)[_0x01b2('0x5')](_0x3fb15f);};