Built motion from commit 08fb8613.|2.6.31
[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 _0xf120=['$notBetween','$in','$notIn','$notLike','$iLike','$notILike','$overlap','$contains','$contained','$any','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','options','include','findAll','catch','keys','rawAttributes','intersection','includeAll','find','create','body','describe','destroy','params','VoiceQueueId','CampaignId','VoiceQueue','omit','dataValues','createdAt','updatedAt','statedesc','deleted','campaign','UserId','user','deletedat','format','YYYY-MM-DD\x20HH:mm:ss','calleridnum','campaigntype','ivr','CmContact','firstName','lastName','isNil','calleridname','finally','CmHopperFinal','CmHopperAdditionalPhone','end','getPreview','getVoiceQueues','outbound','preview','dialOrderByScheduledAt','dialPreviewAutoRecallMe','length','sequelize','transaction','filter','value','direction','prev','hopperIds','ASC','scheduledAt','DESC','literal','COALESCE(CmHopper.UserId,\x200)\x20ASC','isEmpty','priority','COALESCE(CmHopper.UserId,\x200)\x20DESC','scheduledat','phone','recallme','LOCK','UPDATE','Contact','CmCompany','CmList','List','Queue','dialActive','findOne','Company','getOpenContacts','CmHopper,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','getTimezones','timezones','push','utcOffset','lodash','../../components/parsers/qs','../../config/logger','../../config/environment','../../mysqldb','status','json','offset','limit','count','set','Content-Range','update','then','sendStatus','error','stack','name','send','index','CmHopper','fieldName','type','key','map','query','filters','model','concat','$or','$lte','$ne','$eq','$not'];(function(_0x5bd406,_0x4b9907){var _0x598672=function(_0xb37de9){while(--_0xb37de9){_0x5bd406['push'](_0x5bd406['shift']());}};_0x598672(++_0x4b9907);}(_0xf120,0x179));var _0x0f12=function(_0x3f406c,_0x5c2a15){_0x3f406c=_0x3f406c-0x0;var _0x3a500a=_0xf120[_0x3f406c];return _0x3a500a;};'use strict';var moment=require('moment');var _=require(_0x0f12('0x0'));var qs=require(_0x0f12('0x1'));var logger=require(_0x0f12('0x2'))('api');var shared=require(_0x0f12('0x3'));var db=require(_0x0f12('0x4'))['db'];function respondWithResult(_0x163c58,_0x5a263b){_0x5a263b=_0x5a263b||0xc8;return function(_0x1a4043){if(_0x1a4043){return _0x163c58[_0x0f12('0x5')](_0x5a263b)[_0x0f12('0x6')](_0x1a4043);}};}function respondWithFilteredResult(_0x308941,_0x21f951){return function(_0x341516){if(_0x341516){var _0x1f32b7=typeof _0x21f951[_0x0f12('0x7')]==='undefined'&&typeof _0x21f951[_0x0f12('0x8')]==='undefined';var _0x3b909b=_0x341516[_0x0f12('0x9')];var _0x45a714=_0x1f32b7?0x0:_0x21f951['offset'];var _0x5f158a=_0x1f32b7?_0x341516[_0x0f12('0x9')]:_0x21f951[_0x0f12('0x7')]+_0x21f951[_0x0f12('0x8')];var _0xd85187;if(_0x5f158a>=_0x3b909b){_0x5f158a=_0x3b909b;_0xd85187=0xc8;}else{_0xd85187=0xce;}_0x308941[_0x0f12('0x5')](_0xd85187);return _0x308941[_0x0f12('0xa')](_0x0f12('0xb'),_0x45a714+'-'+_0x5f158a+'/'+_0x3b909b)['json'](_0x341516);}return null;};}function saveUpdates(_0xf75042,_0x11a236){return function(_0xb80905){if(_0xb80905){return _0xb80905[_0x0f12('0xc')](_0xf75042)[_0x0f12('0xd')](function(_0xc019be){return _0xc019be;});}return null;};}function handleEntityNotFound(_0x35ef6f,_0x7f1ae7){return function(_0x215a7a){if(!_0x215a7a){_0x35ef6f[_0x0f12('0xe')](0x194);}return _0x215a7a;};}function handleError(_0x23adc0,_0x3f2716){_0x3f2716=_0x3f2716||0x1f4;return function(_0xe61624){logger[_0x0f12('0xf')](_0xe61624[_0x0f12('0x10')]);if(_0xe61624['name']){delete _0xe61624[_0x0f12('0x11')];}_0x23adc0['status'](_0x3f2716)[_0x0f12('0x12')](_0xe61624);};}exports[_0x0f12('0x13')]=function(_0x28119e,_0x12eb6c){var _0x60daff={},_0xa2168f={},_0x5f4290={'count':0x0,'rows':[]};var _0x2fdc6c=_['map'](db[_0x0f12('0x14')]['rawAttributes'],function(_0x4867c8){return{'name':_0x4867c8[_0x0f12('0x15')],'type':_0x4867c8[_0x0f12('0x16')][_0x0f12('0x17')]};});_0xa2168f['model']=_[_0x0f12('0x18')](_0x2fdc6c,'name');_0xa2168f[_0x0f12('0x19')]=_['keys'](_0x28119e[_0x0f12('0x19')]);_0xa2168f[_0x0f12('0x1a')]=_['intersection'](_0xa2168f[_0x0f12('0x1b')],_0xa2168f[_0x0f12('0x19')]);_0xa2168f[_0x0f12('0x1a')]=_[_0x0f12('0x1c')](_0xa2168f[_0x0f12('0x1a')],['$and',_0x0f12('0x1d'),'$gt','$gte','$lt',_0x0f12('0x1e'),_0x0f12('0x1f'),_0x0f12('0x20'),_0x0f12('0x21'),'$between',_0x0f12('0x22'),_0x0f12('0x23'),_0x0f12('0x24'),'$like',_0x0f12('0x25'),_0x0f12('0x26'),_0x0f12('0x27'),'$like',_0x0f12('0x28'),_0x0f12('0x29'),_0x0f12('0x2a'),_0x0f12('0x2b')]);_0x60daff[_0x0f12('0x2c')]=_['intersection'](_0xa2168f['model'],qs[_0x0f12('0x2d')](_0x28119e['query'][_0x0f12('0x2d')]));_0x60daff[_0x0f12('0x2c')]=_0x60daff[_0x0f12('0x2c')]['length']?_0x60daff[_0x0f12('0x2c')]:_0xa2168f[_0x0f12('0x1b')];if(!_0x28119e[_0x0f12('0x19')][_0x0f12('0x2e')](_0x0f12('0x2f'))){_0x60daff[_0x0f12('0x8')]=qs['limit'](_0x28119e[_0x0f12('0x19')][_0x0f12('0x8')]);_0x60daff[_0x0f12('0x7')]=qs['offset'](_0x28119e[_0x0f12('0x19')][_0x0f12('0x7')]);}_0x60daff[_0x0f12('0x30')]=qs[_0x0f12('0x31')](_0x28119e['query'][_0x0f12('0x31')]);_0x60daff[_0x0f12('0x32')]=qs[_0x0f12('0x1a')](_[_0x0f12('0x33')](_0x28119e[_0x0f12('0x19')],_0xa2168f[_0x0f12('0x1a')]),_0x2fdc6c);if(_0x28119e[_0x0f12('0x19')]['filter']){_0x60daff[_0x0f12('0x32')]=_[_0x0f12('0x34')](_0x60daff['where'],{'$or':_[_0x0f12('0x18')](_0x2fdc6c,function(_0x40715e){if(_0x40715e['type']!==_0x0f12('0x35')){var _0x2ca92b={};_0x2ca92b[_0x40715e['name']]={'$like':'%'+_0x28119e[_0x0f12('0x19')]['filter']+'%'};return _0x2ca92b;}})});}_0x60daff=_[_0x0f12('0x34')]({},_0x60daff,_0x28119e[_0x0f12('0x36')]);var _0x2ed69c={'where':_0x60daff[_0x0f12('0x32')]};return db[_0x0f12('0x14')][_0x0f12('0x9')](_0x2ed69c)[_0x0f12('0xd')](function(_0x1d48a7){_0x5f4290[_0x0f12('0x9')]=_0x1d48a7;if(_0x28119e[_0x0f12('0x19')]['includeAll']){_0x60daff[_0x0f12('0x37')]=[{'all':!![]}];}return db[_0x0f12('0x14')][_0x0f12('0x38')](_0x60daff);})['then'](function(_0x5bea02){_0x5f4290['rows']=_0x5bea02;return _0x5f4290;})[_0x0f12('0xd')](respondWithFilteredResult(_0x12eb6c,_0x60daff))[_0x0f12('0x39')](handleError(_0x12eb6c,null));};exports['show']=function(_0x4bf268,_0x21be62){var _0x1a43c6={'raw':!![],'where':{'id':_0x4bf268['params']['id']}},_0x3acc80={};_0x3acc80[_0x0f12('0x1b')]=_[_0x0f12('0x3a')](db['CmHopper'][_0x0f12('0x3b')]);_0x3acc80[_0x0f12('0x19')]=_[_0x0f12('0x3a')](_0x4bf268[_0x0f12('0x19')]);_0x3acc80[_0x0f12('0x1a')]=_[_0x0f12('0x3c')](_0x3acc80[_0x0f12('0x1b')],_0x3acc80['query']);_0x1a43c6[_0x0f12('0x2c')]=_['intersection'](_0x3acc80[_0x0f12('0x1b')],qs[_0x0f12('0x2d')](_0x4bf268['query'][_0x0f12('0x2d')]));_0x1a43c6[_0x0f12('0x2c')]=_0x1a43c6[_0x0f12('0x2c')]['length']?_0x1a43c6['attributes']:_0x3acc80[_0x0f12('0x1b')];if(_0x4bf268[_0x0f12('0x19')][_0x0f12('0x3d')]){_0x1a43c6[_0x0f12('0x37')]=[{'all':!![]}];}_0x1a43c6=_['merge']({},_0x1a43c6,_0x4bf268[_0x0f12('0x36')]);return db[_0x0f12('0x14')][_0x0f12('0x3e')](_0x1a43c6)[_0x0f12('0xd')](handleEntityNotFound(_0x21be62,null))['then'](respondWithResult(_0x21be62,null))[_0x0f12('0x39')](handleError(_0x21be62,null));};exports[_0x0f12('0x3f')]=function(_0x54ec90,_0x4b7599){return db[_0x0f12('0x14')][_0x0f12('0x3f')](_0x54ec90[_0x0f12('0x40')],{})['then'](respondWithResult(_0x4b7599,0xc9))['catch'](handleError(_0x4b7599,null));};exports[_0x0f12('0xc')]=function(_0x108ade,_0x569c2f){if(_0x108ade[_0x0f12('0x40')]['id']){delete _0x108ade['body']['id'];}return db[_0x0f12('0x14')][_0x0f12('0x3e')]({'where':{'id':_0x108ade['params']['id']}})['then'](handleEntityNotFound(_0x569c2f,null))[_0x0f12('0xd')](saveUpdates(_0x108ade[_0x0f12('0x40')],null))[_0x0f12('0xd')](respondWithResult(_0x569c2f,null))[_0x0f12('0x39')](handleError(_0x569c2f,null));};exports[_0x0f12('0x41')]=function(_0x2f561c,_0x294314){return db[_0x0f12('0x14')]['describe']()[_0x0f12('0xd')](respondWithResult(_0x294314,null))['catch'](handleError(_0x294314,null));};exports[_0x0f12('0x42')]=function(_0x52f1c2,_0x4add76){var _0xbbacc1;var _0x5cfd51=_0x52f1c2[_0x0f12('0x43')]['id'];return db[_0x0f12('0x14')][_0x0f12('0x3e')]({'where':{'id':_0x52f1c2[_0x0f12('0x43')]['id']}})['then'](handleEntityNotFound(_0x4add76,null))[_0x0f12('0xd')](function(_0x90f918){if(_0x90f918){return _0x90f918[_0x0f12('0x42')]()[_0x0f12('0xd')](function(_0x48acec){return _0x48acec;});}})['then'](function(_0x122a57){_0xbbacc1=_0x122a57;if(_0x122a57){if(_0x122a57[_0x0f12('0x44')]||_0x122a57[_0x0f12('0x45')]){return db[_0x122a57[_0x0f12('0x44')]?_0x0f12('0x46'):'Campaign']['find']({'where':{'id':_0x122a57['VoiceQueueId']?_0x122a57[_0x0f12('0x44')]:_0x122a57[_0x0f12('0x45')]},'attributes':['id',_0x0f12('0x11')],'raw':!![]});}}})[_0x0f12('0xd')](function(_0x18c68b){var _0x33af1f={};if(_0x18c68b){_[_0x0f12('0x34')](_0x33af1f,_[_0x0f12('0x47')](_0xbbacc1[_0x0f12('0x48')],[_0x0f12('0x49'),_0x0f12('0x4a'),'id']));_0x33af1f['state']=0x15;_0x33af1f[_0x0f12('0x4b')]=_0x0f12('0x4c');_0x33af1f[_0x0f12('0x4d')]=_0x18c68b[_0x0f12('0x11')];_0x33af1f['membername']=_0x52f1c2['user'][_0x0f12('0x11')];_0x33af1f[_0x0f12('0x4e')]=_0x52f1c2[_0x0f12('0x4f')]['id'];_0x33af1f[_0x0f12('0x4c')]=!![];_0x33af1f[_0x0f12('0x50')]=moment()[_0x0f12('0x51')](_0x0f12('0x52'));_0x33af1f[_0x0f12('0x53')]=_0xbbacc1['phone'];_0x33af1f[_0x0f12('0x54')]=_0xbbacc1[_0x0f12('0x44')]?'queue':_0x0f12('0x55');return db[_0x0f12('0x56')][_0x0f12('0x3e')]({'where':{'id':_0x33af1f['ContactId']},'attributes':[_0x0f12('0x57'),_0x0f12('0x58')]})[_0x0f12('0xd')](function(_0x2ca6bb){if(!_[_0x0f12('0x59')](_0x4add76)&&!_['isNil'](_0x2ca6bb[_0x0f12('0x57')])){_0x33af1f['calleridname']=_0x2ca6bb[_0x0f12('0x57')];if(!_['isNil'](_0x2ca6bb[_0x0f12('0x58')])){_0x33af1f[_0x0f12('0x5a')]+='\x20'+_0x2ca6bb['lastName'];}}})[_0x0f12('0x5b')](function(){return db[_0x0f12('0x5c')]['create'](_0x33af1f);});}})[_0x0f12('0xd')](function(_0x5498aa){return db[_0x0f12('0x5d')]['destroy']({'where':{'CmHopperId':_0x5cfd51}});})[_0x0f12('0xd')](function(){return _0x4add76[_0x0f12('0x5')](0xcc)[_0x0f12('0x5e')]();})['catch'](handleError(_0x4add76,null));};exports[_0x0f12('0x5f')]=function(_0x2df09e,_0x2cc2d6){var _0x5cf716=[];return db[_0x0f12('0x56')][_0x0f12('0x41')]()['then'](function(_0x155ead){_0x5cf716=_[_0x0f12('0x3a')](_0x155ead);return _0x2df09e['user'][_0x0f12('0x60')]({'where':{'type':_0x0f12('0x61'),'dialActive':!![],'dialMethod':_0x0f12('0x62')},'attributes':['id',_0x0f12('0x63'),_0x0f12('0x64')],'raw':!![]});})[_0x0f12('0xd')](function(_0x9b2714){if(_0x9b2714&&_0x9b2714[_0x0f12('0x65')]){return db[_0x0f12('0x66')][_0x0f12('0x67')](function(_0x13db9c){var _0x2f3392={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()['format'](_0x0f12('0x52'))},'VoiceQueueId':{'$in':_[_0x0f12('0x18')](_0x9b2714,'id')},'$or':[{'UserId':null},{'UserId':_0x2df09e[_0x0f12('0x4f')]['id']}]};var _0x1f6c14={'active':![],'recallme':!![],'VoiceQueueId':{'$in':_(_0x9b2714)[_0x0f12('0x68')](['dialPreviewAutoRecallMe',0x0])[_0x0f12('0x18')]('id')[_0x0f12('0x69')]()},'$or':[{'UserId':null},{'UserId':_0x2df09e[_0x0f12('0x4f')]['id']}]};var _0x5967c2={'$or':[_0x2f3392,_0x1f6c14]};var _0x128af9=[];if(_0x2df09e['body'][_0x0f12('0x6a')]&&_0x2df09e[_0x0f12('0x40')][_0x0f12('0x6a')]===_0x0f12('0x6b')){if(!_['isEmpty'](_0x2df09e[_0x0f12('0x40')]['hopperIds'])){_0x5967c2['id']={'$in':_0x2df09e[_0x0f12('0x40')][_0x0f12('0x6c')]};}_0x128af9=[['priority',_0x0f12('0x6d')],[_0x0f12('0x6e'),_0x9b2714[0x0][_0x0f12('0x63')]===_0x0f12('0x6d')?_0x0f12('0x6f'):_0x0f12('0x6d')],db['sequelize'][_0x0f12('0x70')](_0x0f12('0x71')),['id',_0x0f12('0x6f')]];}else{if(!_[_0x0f12('0x72')](_0x2df09e[_0x0f12('0x40')][_0x0f12('0x6c')])){_0x5967c2['id']={'$notIn':_0x2df09e[_0x0f12('0x40')][_0x0f12('0x6c')]};}_0x128af9=[[_0x0f12('0x73'),_0x0f12('0x6f')],[_0x0f12('0x6e'),_0x9b2714[0x0]['dialOrderByScheduledAt']],db[_0x0f12('0x66')]['literal'](_0x0f12('0x74')),['id',_0x0f12('0x6d')]];}return db[_0x0f12('0x14')]['findOne']({'attributes':['id',_0x0f12('0x75'),_0x0f12('0x76'),_0x0f12('0x77')],'where':_0x5967c2,'order':_0x128af9,'transaction':_0x13db9c,'lock':_0x13db9c[_0x0f12('0x78')][_0x0f12('0x79')],'include':[{'model':db[_0x0f12('0x56')],'as':_0x0f12('0x7a'),'attributes':_0x5cf716,'include':[{'model':db[_0x0f12('0x7b')],'as':'Company','attributes':['id',_0x0f12('0x11')]}]},{'model':db[_0x0f12('0x7c')],'as':_0x0f12('0x7d'),'attributes':['id',_0x0f12('0x11')]},{'model':db[_0x0f12('0x46')],'as':_0x0f12('0x7e'),'attributes':['id',_0x0f12('0x11'),_0x0f12('0x7f')]}]})[_0x0f12('0xd')](function(_0x498966){if(_0x498966){return _0x498966['update']({'UserId':_0x2df09e[_0x0f12('0x4f')]['id']},{'transaction':_0x13db9c});}});});}})[_0x0f12('0xd')](handleEntityNotFound(_0x2cc2d6,null))[_0x0f12('0xd')](respondWithResult(_0x2cc2d6,null))[_0x0f12('0x39')](handleError(_0x2cc2d6,null));};exports['getPreviewRecall']=function(_0x3a169b,_0x1557db,_0x3567db){var _0x5a3255=[];var _0x8e211=_0x3a169b[_0x0f12('0x43')]['id'];var _0xd18662=_0x3a169b[_0x0f12('0x4f')]['id'];return db[_0x0f12('0x56')][_0x0f12('0x41')]()['then'](function(_0x2981db){_0x5a3255=_[_0x0f12('0x3a')](_0x2981db);return db[_0x0f12('0x66')]['transaction'](function(_0x2cc0b7){const _0x1b28c2={'id':_0x8e211};return db[_0x0f12('0x14')][_0x0f12('0x80')]({'attributes':['id',_0x0f12('0x75'),_0x0f12('0x77')],'where':_0x1b28c2,'transaction':_0x2cc0b7,'lock':_0x2cc0b7['LOCK'][_0x0f12('0x79')],'include':[{'model':db[_0x0f12('0x56')],'as':'Contact','attributes':_0x5a3255,'include':[{'model':db[_0x0f12('0x7b')],'as':_0x0f12('0x81'),'attributes':['id',_0x0f12('0x11')]}]},{'model':db[_0x0f12('0x7c')],'as':_0x0f12('0x7d'),'attributes':['id',_0x0f12('0x11')]},{'model':db[_0x0f12('0x46')],'as':_0x0f12('0x7e'),'attributes':['id','name','dialActive']}]})[_0x0f12('0xd')](function(_0x1782a9){if(_0x1782a9){return _0x1782a9[_0x0f12('0xc')]({'UserId':_0xd18662},{'transaction':_0x2cc0b7});}});});})[_0x0f12('0xd')](handleEntityNotFound(_0x1557db,null))['then'](respondWithResult(_0x1557db,null))['catch'](handleError(_0x1557db,null));};exports[_0x0f12('0x82')]=function(_0x310af3,_0x597a9b){return _0x310af3[_0x0f12('0x4f')]['getVoiceQueues']({'where':{'type':_0x0f12('0x61'),'dialActive':!![],'dialMethod':_0x0f12('0x62')},'attributes':['id'],'raw':!![]})[_0x0f12('0xd')](function(_0x4ec86a){if(_0x4ec86a&&_0x4ec86a[_0x0f12('0x65')]){return db['CmHopper']['count']({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x0f12('0x51')](_0x0f12('0x52'))},'VoiceQueueId':{'$in':_['map'](_0x4ec86a,'id')},'$or':[{'UserId':null},{'UserId':_0x310af3[_0x0f12('0x4f')]['id']}]}});}return 0x0;})[_0x0f12('0xd')](function(_0x130b70){if(!_[_0x0f12('0x59')](_0x130b70)&&_['isNumber'](_0x130b70)){return _0x597a9b[_0x0f12('0x5')](0xc8)[_0x0f12('0x6')]({'contacts':_0x130b70});}else{logger[_0x0f12('0xf')](_0x0f12('0x83'),_0x0f12('0x82'),0x194,_0x0f12('0x84'));_0x597a9b['sendStatus'](0x194);}})[_0x0f12('0x39')](handleError(_0x597a9b,null));};exports[_0x0f12('0x85')]=function(_0x3093a0,_0x57b290){var _0x4c2c6e=shared[_0x0f12('0x86')];var _0x169fbb=[];_[_0x0f12('0x18')](_0x4c2c6e,function(_0x10e218){_0x169fbb[_0x0f12('0x87')]({'id':_0x10e218[_0x0f12('0x11')],'name':_0x10e218[_0x0f12('0x11')],'utcOffset':_0x10e218[_0x0f12('0x88')]});});return _0x57b290[_0x0f12('0x5')](0xc8)[_0x0f12('0x6')]({'tz':_0x169fbb});};exports['getTimezone']=function(_0x259652,_0x1c0610){var _0x404bc7=shared[_0x0f12('0x86')];var _0x322067={};if(!_[_0x0f12('0x59')](_0x404bc7[_0x259652[_0x0f12('0x40')]['id']]))_0x322067=_0x404bc7[_0x259652[_0x0f12('0x40')]['id']];return _0x1c0610[_0x0f12('0x5')](0xc8)['json'](_0x322067);};