Built motion from commit bab6f49e.|2.6.17
[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 _0x3e74=['error','name','send','map','CmHopper','rawAttributes','fieldName','type','key','keys','query','filters','intersection','model','concat','$or','$gt','$lt','$ne','$not','$between','$notBetween','$in','$notLike','$notILike','$like','$overlap','$contains','$any','attributes','fields','length','hasOwnProperty','order','sort','pick','filter','merge','options','where','includeAll','include','findAll','then','catch','params','create','update','body','find','describe','destroy','CampaignId','VoiceQueueId','VoiceQueue','Campaign','dataValues','updatedAt','state','statedesc','deleted','campaign','user','UserId','deletedat','format','YYYY-MM-DD\x20HH:mm:ss','calleridnum','phone','campaigntype','queue','ivr','ContactId','firstName','isNil','lastName','calleridname','finally','CmHopperAdditionalPhone','getPreview','outbound','preview','dialOrderByScheduledAt','dialPreviewAutoRecallMe','sequelize','transaction','direction','prev','isEmpty','hopperIds','priority','ASC','literal','DESC','scheduledAt','findOne','scheduledat','recallme','LOCK','UPDATE','Contact','CmCompany','Company','CmList','Queue','dialActive','CmContact','getOpenContacts','getVoiceQueues','CmHopper,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','sendStatus','getTimezones','timezones','push','utcOffset','getTimezone','moment','../../components/parsers/qs','../../config/logger','api','status','json','offset','undefined','limit','count','set'];(function(_0x125430,_0x4046a2){var _0xa1a407=function(_0x5f0ec1){while(--_0x5f0ec1){_0x125430['push'](_0x125430['shift']());}};_0xa1a407(++_0x4046a2);}(_0x3e74,0x175));var _0x43e7=function(_0x3f1d4e,_0x19edb6){_0x3f1d4e=_0x3f1d4e-0x0;var _0x971b76=_0x3e74[_0x3f1d4e];return _0x971b76;};'use strict';var moment=require(_0x43e7('0x0'));var _=require('lodash');var qs=require(_0x43e7('0x1'));var logger=require(_0x43e7('0x2'))(_0x43e7('0x3'));var shared=require('../../config/environment');var db=require('../../mysqldb')['db'];function respondWithResult(_0xc58479,_0x4f9bbe){_0x4f9bbe=_0x4f9bbe||0xc8;return function(_0x128b0f){if(_0x128b0f){return _0xc58479[_0x43e7('0x4')](_0x4f9bbe)[_0x43e7('0x5')](_0x128b0f);}};}function respondWithFilteredResult(_0x1c503b,_0xc98b9c){return function(_0x503698){if(_0x503698){var _0x45a0d1=typeof _0xc98b9c[_0x43e7('0x6')]===_0x43e7('0x7')&&typeof _0xc98b9c[_0x43e7('0x8')]===_0x43e7('0x7');var _0x516b4d=_0x503698[_0x43e7('0x9')];var _0x554d44=_0x45a0d1?0x0:_0xc98b9c['offset'];var _0x39c8bd=_0x45a0d1?_0x503698[_0x43e7('0x9')]:_0xc98b9c['offset']+_0xc98b9c[_0x43e7('0x8')];var _0x513707;if(_0x39c8bd>=_0x516b4d){_0x39c8bd=_0x516b4d;_0x513707=0xc8;}else{_0x513707=0xce;}_0x1c503b[_0x43e7('0x4')](_0x513707);return _0x1c503b[_0x43e7('0xa')]('Content-Range',_0x554d44+'-'+_0x39c8bd+'/'+_0x516b4d)[_0x43e7('0x5')](_0x503698);}return null;};}function saveUpdates(_0xc960ea,_0x2718d3){return function(_0x3bb83f){if(_0x3bb83f){return _0x3bb83f['update'](_0xc960ea)['then'](function(_0x2fcb36){return _0x2fcb36;});}return null;};}function handleEntityNotFound(_0x21fdd3,_0x44b0bd){return function(_0x51b974){if(!_0x51b974){_0x21fdd3['sendStatus'](0x194);}return _0x51b974;};}function handleError(_0x16849c,_0x14c146){_0x14c146=_0x14c146||0x1f4;return function(_0x203378){logger[_0x43e7('0xb')](_0x203378['stack']);if(_0x203378['name']){delete _0x203378[_0x43e7('0xc')];}_0x16849c[_0x43e7('0x4')](_0x14c146)[_0x43e7('0xd')](_0x203378);};}exports['index']=function(_0x4b5a6b,_0x446986){var _0x1ca758={},_0x9037d3={},_0x140657={'count':0x0,'rows':[]};var _0x3776f5=_[_0x43e7('0xe')](db[_0x43e7('0xf')][_0x43e7('0x10')],function(_0x2377a3){return{'name':_0x2377a3[_0x43e7('0x11')],'type':_0x2377a3[_0x43e7('0x12')][_0x43e7('0x13')]};});_0x9037d3['model']=_[_0x43e7('0xe')](_0x3776f5,_0x43e7('0xc'));_0x9037d3['query']=_[_0x43e7('0x14')](_0x4b5a6b[_0x43e7('0x15')]);_0x9037d3[_0x43e7('0x16')]=_[_0x43e7('0x17')](_0x9037d3[_0x43e7('0x18')],_0x9037d3[_0x43e7('0x15')]);_0x9037d3['filters']=_[_0x43e7('0x19')](_0x9037d3['filters'],['$and',_0x43e7('0x1a'),_0x43e7('0x1b'),'$gte',_0x43e7('0x1c'),'$lte',_0x43e7('0x1d'),'$eq',_0x43e7('0x1e'),_0x43e7('0x1f'),_0x43e7('0x20'),_0x43e7('0x21'),'$notIn','$like',_0x43e7('0x22'),'$iLike',_0x43e7('0x23'),_0x43e7('0x24'),_0x43e7('0x25'),_0x43e7('0x26'),'$contained',_0x43e7('0x27')]);_0x1ca758[_0x43e7('0x28')]=_[_0x43e7('0x17')](_0x9037d3[_0x43e7('0x18')],qs[_0x43e7('0x29')](_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x29')]));_0x1ca758[_0x43e7('0x28')]=_0x1ca758[_0x43e7('0x28')][_0x43e7('0x2a')]?_0x1ca758[_0x43e7('0x28')]:_0x9037d3[_0x43e7('0x18')];if(!_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x2b')]('nolimit')){_0x1ca758[_0x43e7('0x8')]=qs[_0x43e7('0x8')](_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x8')]);_0x1ca758[_0x43e7('0x6')]=qs['offset'](_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x6')]);}_0x1ca758[_0x43e7('0x2c')]=qs[_0x43e7('0x2d')](_0x4b5a6b['query']['sort']);_0x1ca758['where']=qs[_0x43e7('0x16')](_[_0x43e7('0x2e')](_0x4b5a6b[_0x43e7('0x15')],_0x9037d3[_0x43e7('0x16')]),_0x3776f5);if(_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x2f')]){_0x1ca758['where']=_['merge'](_0x1ca758['where'],{'$or':_['map'](_0x3776f5,function(_0x4e8e86){if(_0x4e8e86['type']!=='VIRTUAL'){var _0x14d888={};_0x14d888[_0x4e8e86['name']]={'$like':'%'+_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x2f')]+'%'};return _0x14d888;}})});}_0x1ca758=_[_0x43e7('0x30')]({},_0x1ca758,_0x4b5a6b[_0x43e7('0x31')]);var _0x40f72f={'where':_0x1ca758[_0x43e7('0x32')]};return db[_0x43e7('0xf')]['count'](_0x40f72f)['then'](function(_0x5d84f4){_0x140657[_0x43e7('0x9')]=_0x5d84f4;if(_0x4b5a6b[_0x43e7('0x15')][_0x43e7('0x33')]){_0x1ca758[_0x43e7('0x34')]=[{'all':!![]}];}return db[_0x43e7('0xf')][_0x43e7('0x35')](_0x1ca758);})[_0x43e7('0x36')](function(_0x2ffd43){_0x140657['rows']=_0x2ffd43;return _0x140657;})[_0x43e7('0x36')](respondWithFilteredResult(_0x446986,_0x1ca758))[_0x43e7('0x37')](handleError(_0x446986,null));};exports['show']=function(_0x2d54c1,_0x4c0c7a){var _0x44d8e4={'raw':!![],'where':{'id':_0x2d54c1[_0x43e7('0x38')]['id']}},_0x2ad008={};_0x2ad008[_0x43e7('0x18')]=_[_0x43e7('0x14')](db[_0x43e7('0xf')]['rawAttributes']);_0x2ad008['query']=_[_0x43e7('0x14')](_0x2d54c1[_0x43e7('0x15')]);_0x2ad008[_0x43e7('0x16')]=_[_0x43e7('0x17')](_0x2ad008[_0x43e7('0x18')],_0x2ad008['query']);_0x44d8e4[_0x43e7('0x28')]=_[_0x43e7('0x17')](_0x2ad008['model'],qs[_0x43e7('0x29')](_0x2d54c1['query'][_0x43e7('0x29')]));_0x44d8e4['attributes']=_0x44d8e4[_0x43e7('0x28')][_0x43e7('0x2a')]?_0x44d8e4[_0x43e7('0x28')]:_0x2ad008[_0x43e7('0x18')];if(_0x2d54c1['query']['includeAll']){_0x44d8e4[_0x43e7('0x34')]=[{'all':!![]}];}_0x44d8e4=_[_0x43e7('0x30')]({},_0x44d8e4,_0x2d54c1[_0x43e7('0x31')]);return db[_0x43e7('0xf')]['find'](_0x44d8e4)['then'](handleEntityNotFound(_0x4c0c7a,null))['then'](respondWithResult(_0x4c0c7a,null))[_0x43e7('0x37')](handleError(_0x4c0c7a,null));};exports['create']=function(_0x2ab022,_0x4b3a59){return db['CmHopper'][_0x43e7('0x39')](_0x2ab022['body'],{})[_0x43e7('0x36')](respondWithResult(_0x4b3a59,0xc9))[_0x43e7('0x37')](handleError(_0x4b3a59,null));};exports[_0x43e7('0x3a')]=function(_0x99b9e1,_0x4fc5e8){if(_0x99b9e1[_0x43e7('0x3b')]['id']){delete _0x99b9e1[_0x43e7('0x3b')]['id'];}return db[_0x43e7('0xf')][_0x43e7('0x3c')]({'where':{'id':_0x99b9e1[_0x43e7('0x38')]['id']}})[_0x43e7('0x36')](handleEntityNotFound(_0x4fc5e8,null))['then'](saveUpdates(_0x99b9e1['body'],null))[_0x43e7('0x36')](respondWithResult(_0x4fc5e8,null))[_0x43e7('0x37')](handleError(_0x4fc5e8,null));};exports[_0x43e7('0x3d')]=function(_0x4c00a8,_0x5c1c18){return db['CmHopper'][_0x43e7('0x3d')]()[_0x43e7('0x36')](respondWithResult(_0x5c1c18,null))['catch'](handleError(_0x5c1c18,null));};exports[_0x43e7('0x3e')]=function(_0x47686e,_0x3c96fe){var _0x560329;var _0x593155=_0x47686e['params']['id'];return db['CmHopper'][_0x43e7('0x3c')]({'where':{'id':_0x47686e[_0x43e7('0x38')]['id']}})['then'](handleEntityNotFound(_0x3c96fe,null))[_0x43e7('0x36')](function(_0x82d32f){if(_0x82d32f){return _0x82d32f[_0x43e7('0x3e')]()[_0x43e7('0x36')](function(_0x4a0ce5){return _0x4a0ce5;});}})[_0x43e7('0x36')](function(_0x483f20){_0x560329=_0x483f20;if(_0x483f20){if(_0x483f20['VoiceQueueId']||_0x483f20[_0x43e7('0x3f')]){return db[_0x483f20[_0x43e7('0x40')]?_0x43e7('0x41'):_0x43e7('0x42')][_0x43e7('0x3c')]({'where':{'id':_0x483f20[_0x43e7('0x40')]?_0x483f20[_0x43e7('0x40')]:_0x483f20[_0x43e7('0x3f')]},'attributes':['id',_0x43e7('0xc')],'raw':!![]});}}})[_0x43e7('0x36')](function(_0x58dfd0){var _0x119d34={};if(_0x58dfd0){_[_0x43e7('0x30')](_0x119d34,_['omit'](_0x560329[_0x43e7('0x43')],['createdAt',_0x43e7('0x44'),'id']));_0x119d34[_0x43e7('0x45')]=0x15;_0x119d34[_0x43e7('0x46')]=_0x43e7('0x47');_0x119d34[_0x43e7('0x48')]=_0x58dfd0['name'];_0x119d34['membername']=_0x47686e[_0x43e7('0x49')][_0x43e7('0xc')];_0x119d34[_0x43e7('0x4a')]=_0x47686e[_0x43e7('0x49')]['id'];_0x119d34[_0x43e7('0x47')]=!![];_0x119d34[_0x43e7('0x4b')]=moment()[_0x43e7('0x4c')](_0x43e7('0x4d'));_0x119d34[_0x43e7('0x4e')]=_0x560329[_0x43e7('0x4f')];_0x119d34[_0x43e7('0x50')]=_0x560329['VoiceQueueId']?_0x43e7('0x51'):_0x43e7('0x52');return db['CmContact'][_0x43e7('0x3c')]({'where':{'id':_0x119d34[_0x43e7('0x53')]},'attributes':[_0x43e7('0x54'),'lastName']})['then'](function(_0x459c13){if(!_[_0x43e7('0x55')](_0x3c96fe)&&!_['isNil'](_0x459c13[_0x43e7('0x54')])){_0x119d34['calleridname']=_0x459c13['firstName'];if(!_[_0x43e7('0x55')](_0x459c13[_0x43e7('0x56')])){_0x119d34[_0x43e7('0x57')]+='\x20'+_0x459c13[_0x43e7('0x56')];}}})[_0x43e7('0x58')](function(){return db['CmHopperFinal'][_0x43e7('0x39')](_0x119d34);});}})[_0x43e7('0x36')](function(_0x4d89bd){return db[_0x43e7('0x59')][_0x43e7('0x3e')]({'where':{'CmHopperId':_0x593155}});})[_0x43e7('0x36')](function(){return _0x3c96fe['status'](0xcc)['end']();})['catch'](handleError(_0x3c96fe,null));};exports[_0x43e7('0x5a')]=function(_0x4058ce,_0x323cf7){var _0x3a6404=[];return db['CmContact']['describe']()[_0x43e7('0x36')](function(_0x8b50bb){_0x3a6404=_[_0x43e7('0x14')](_0x8b50bb);return _0x4058ce[_0x43e7('0x49')]['getVoiceQueues']({'where':{'type':_0x43e7('0x5b'),'dialActive':!![],'dialMethod':_0x43e7('0x5c')},'attributes':['id',_0x43e7('0x5d'),_0x43e7('0x5e')],'raw':!![]});})[_0x43e7('0x36')](function(_0xd41e79){if(_0xd41e79&&_0xd41e79[_0x43e7('0x2a')]){return db[_0x43e7('0x5f')][_0x43e7('0x60')](function(_0x381208){var _0x252f00={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()['format']('YYYY-MM-DD\x20HH:mm:ss')},'VoiceQueueId':{'$in':_[_0x43e7('0xe')](_0xd41e79,'id')},'$or':[{'UserId':null},{'UserId':_0x4058ce['user']['id']}]};var _0x501616={'active':![],'recallme':!![],'VoiceQueueId':{'$in':_(_0xd41e79)['filter']([_0x43e7('0x5e'),0x0])[_0x43e7('0xe')]('id')['value']()},'$or':[{'UserId':null},{'UserId':_0x4058ce[_0x43e7('0x49')]['id']}]};var _0x135791={'$or':[_0x252f00,_0x501616]};var _0x1d8438=[];if(_0x4058ce['body'][_0x43e7('0x61')]&&_0x4058ce[_0x43e7('0x3b')][_0x43e7('0x61')]===_0x43e7('0x62')){if(!_[_0x43e7('0x63')](_0x4058ce[_0x43e7('0x3b')][_0x43e7('0x64')])){_0x135791['id']={'$in':_0x4058ce['body'][_0x43e7('0x64')]};}_0x1d8438=[[_0x43e7('0x65'),_0x43e7('0x66')],['scheduledAt',_0xd41e79[0x0]['dialOrderByScheduledAt']===_0x43e7('0x66')?'DESC':'ASC'],db[_0x43e7('0x5f')][_0x43e7('0x67')]('COALESCE(CmHopper.UserId,\x200)\x20ASC'),['id',_0x43e7('0x68')]];}else{if(!_[_0x43e7('0x63')](_0x4058ce[_0x43e7('0x3b')][_0x43e7('0x64')])){_0x135791['id']={'$notIn':_0x4058ce[_0x43e7('0x3b')][_0x43e7('0x64')]};}_0x1d8438=[[_0x43e7('0x65'),'DESC'],[_0x43e7('0x69'),_0xd41e79[0x0][_0x43e7('0x5d')]],db['sequelize'][_0x43e7('0x67')]('COALESCE(CmHopper.UserId,\x200)\x20DESC'),['id',_0x43e7('0x66')]];}return db[_0x43e7('0xf')][_0x43e7('0x6a')]({'attributes':['id',_0x43e7('0x6b'),'phone',_0x43e7('0x6c')],'where':_0x135791,'order':_0x1d8438,'transaction':_0x381208,'lock':_0x381208[_0x43e7('0x6d')][_0x43e7('0x6e')],'include':[{'model':db['CmContact'],'as':_0x43e7('0x6f'),'attributes':_0x3a6404,'include':[{'model':db[_0x43e7('0x70')],'as':_0x43e7('0x71'),'attributes':['id',_0x43e7('0xc')]}]},{'model':db[_0x43e7('0x72')],'as':'List','attributes':['id',_0x43e7('0xc')]},{'model':db[_0x43e7('0x41')],'as':_0x43e7('0x73'),'attributes':['id',_0x43e7('0xc'),_0x43e7('0x74')]}]})[_0x43e7('0x36')](function(_0x438754){if(_0x438754){return _0x438754['update']({'UserId':_0x4058ce['user']['id']},{'transaction':_0x381208});}});});}})[_0x43e7('0x36')](handleEntityNotFound(_0x323cf7,null))['then'](respondWithResult(_0x323cf7,null))['catch'](handleError(_0x323cf7,null));};exports['getPreviewRecall']=function(_0x43daec,_0x88be29,_0x230fda){var _0x3041c3=[];var _0x18713b=_0x43daec[_0x43e7('0x38')]['id'];var _0x594315=_0x43daec[_0x43e7('0x49')]['id'];return db[_0x43e7('0x75')][_0x43e7('0x3d')]()['then'](function(_0x2abca0){_0x3041c3=_[_0x43e7('0x14')](_0x2abca0);return db[_0x43e7('0x5f')][_0x43e7('0x60')](function(_0x1afea6){const _0xeb6340={'id':_0x18713b};return db[_0x43e7('0xf')][_0x43e7('0x6a')]({'attributes':['id','scheduledat','recallme'],'where':_0xeb6340,'transaction':_0x1afea6,'lock':_0x1afea6['LOCK'][_0x43e7('0x6e')],'include':[{'model':db[_0x43e7('0x75')],'as':'Contact','attributes':_0x3041c3,'include':[{'model':db[_0x43e7('0x70')],'as':_0x43e7('0x71'),'attributes':['id',_0x43e7('0xc')]}]},{'model':db[_0x43e7('0x72')],'as':'List','attributes':['id','name']},{'model':db['VoiceQueue'],'as':_0x43e7('0x73'),'attributes':['id',_0x43e7('0xc'),'dialActive']}]})['then'](function(_0x21adf7){if(_0x21adf7){return _0x21adf7[_0x43e7('0x3a')]({'UserId':_0x594315},{'transaction':_0x1afea6});}});});})[_0x43e7('0x36')](handleEntityNotFound(_0x88be29,null))['then'](respondWithResult(_0x88be29,null))[_0x43e7('0x37')](handleError(_0x88be29,null));};exports[_0x43e7('0x76')]=function(_0x401654,_0xb04082){return _0x401654['user'][_0x43e7('0x77')]({'where':{'type':'outbound','dialActive':!![],'dialMethod':_0x43e7('0x5c')},'attributes':['id'],'raw':!![]})[_0x43e7('0x36')](function(_0x24a168){if(_0x24a168&&_0x24a168[_0x43e7('0x2a')]){return db[_0x43e7('0xf')][_0x43e7('0x9')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x43e7('0x4c')]('YYYY-MM-DD\x20HH:mm:ss')},'VoiceQueueId':{'$in':_[_0x43e7('0xe')](_0x24a168,'id')},'$or':[{'UserId':null},{'UserId':_0x401654[_0x43e7('0x49')]['id']}]}});}return 0x0;})[_0x43e7('0x36')](function(_0x6fb694){if(!_['isNil'](_0x6fb694)&&_['isNumber'](_0x6fb694)){return _0xb04082['status'](0xc8)[_0x43e7('0x5')]({'contacts':_0x6fb694});}else{logger['error'](_0x43e7('0x78'),_0x43e7('0x76'),0x194,_0x43e7('0x79'));_0xb04082[_0x43e7('0x7a')](0x194);}})[_0x43e7('0x37')](handleError(_0xb04082,null));};exports[_0x43e7('0x7b')]=function(_0x28a962,_0x1ea46d){var _0x363af5=shared[_0x43e7('0x7c')];var _0x3de22e=[];_['map'](_0x363af5,function(_0xeaef5c){_0x3de22e[_0x43e7('0x7d')]({'id':_0xeaef5c[_0x43e7('0xc')],'name':_0xeaef5c[_0x43e7('0xc')],'utcOffset':_0xeaef5c[_0x43e7('0x7e')]});});return _0x1ea46d['status'](0xc8)['json']({'tz':_0x3de22e});};exports[_0x43e7('0x7f')]=function(_0x3e3c82,_0x5c7f0a){var _0x100c4d=shared[_0x43e7('0x7c')];var _0x5a27da={};if(!_['isNil'](_0x100c4d[_0x3e3c82[_0x43e7('0x3b')]['id']]))_0x5a27da=_0x100c4d[_0x3e3c82[_0x43e7('0x3b')]['id']];return _0x5c7f0a[_0x43e7('0x4')](0xc8)[_0x43e7('0x5')](_0x5a27da);};