Built motion from commit (unavailable).|2.4.4
[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 _0xdace=['statedesc','deleted','campaign','membername','UserId','user','deletedat','format','YYYY-MM-DD\x20HH:mm:ss','calleridnum','phone','campaigntype','ivr','CmContact','ContactId','firstName','isNil','lastName','calleridname','finally','CmHopperFinal','create','getPreview','getVoiceQueues','outbound','preview','dialOrderByScheduledAt','sequelize','transaction','direction','prev','hopperIds','priority','ASC','DESC','COALESCE(CmHopper.UserId,\x200)\x20ASC','scheduledAt','literal','COALESCE(CmHopper.UserId,\x200)\x20DESC','findOne','LOCK','UPDATE','Contact','CmCompany','Company','CmList','List','VoiceQueue','Queue','dialActive','getOpenContacts','isNumber','CmHopper,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','html-pdf','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','sox','to-csv','ejs','squel','crypto','jsforce','querystring','papaparse','../../components/parsers/qs','../../config/license/hardware','../../config/utils','../../config/environment','sendStatus','status','end','offset','limit','json','apply','save','update','then','error','stack','name','send','index','CmHopper','model','keys','query','filters','intersection','concat','$and','$or','$gt','$gte','$lte','$ne','$eq','$between','$notBetween','$notIn','$like','$iLike','$notILike','$overlap','$contains','$contained','$any','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','merge','map','key','field','filter','options','count','includeAll','include','findAll','rows','catch','body','find','params','describe','destroy','VoiceQueueId','Campaign','dataValues','updatedAt','state'];(function(_0x36301e,_0x970c42){var _0x586b6c=function(_0x7818d5){while(--_0x7818d5){_0x36301e['push'](_0x36301e['shift']());}};_0x586b6c(++_0x970c42);}(_0xdace,0xc5));var _0xedac=function(_0x3df223,_0x1740fc){_0x3df223=_0x3df223-0x0;var _0x562d11=_0xdace[_0x3df223];return _0x562d11;};'use strict';var pdf=require(_0xedac('0x0'));var emlformat=require('eml-format');var rimraf=require(_0xedac('0x1'));var zipdir=require(_0xedac('0x2'));var jsonpatch=require(_0xedac('0x3'));var rp=require('request-promise');var moment=require(_0xedac('0x4'));var BPromise=require(_0xedac('0x5'));var Mustache=require(_0xedac('0x6'));var util=require(_0xedac('0x7'));var path=require('path');var sox=require(_0xedac('0x8'));var csv=require(_0xedac('0x9'));var ejs=require(_0xedac('0xa'));var fs=require('fs');var _=require('lodash');var squel=require(_0xedac('0xb'));var crypto=require(_0xedac('0xc'));var jsforce=require(_0xedac('0xd'));var deskjs=require('desk.js');var toCsv=require(_0xedac('0x9'));var querystring=require(_0xedac('0xe'));var Papa=require(_0xedac('0xf'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0xedac('0x10'));var hardwareService=require(_0xedac('0x11'));var logger=require('../../config/logger')('api');var utils=require(_0xedac('0x12'));var config=require(_0xedac('0x13'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x261401,_0x4fa1ac){_0x4fa1ac=_0x4fa1ac||0xcc;return function(_0x21c6ae){if(_0x21c6ae){return _0x261401[_0xedac('0x14')](_0x4fa1ac);}return _0x261401[_0xedac('0x15')](_0x4fa1ac)[_0xedac('0x16')]();};}function respondWithResult(_0x9a79a0,_0x473c3e){_0x473c3e=_0x473c3e||0xc8;return function(_0x28444a){if(_0x28444a){return _0x9a79a0[_0xedac('0x15')](_0x473c3e)['json'](_0x28444a);}};}function respondWithFilteredResult(_0x110546,_0x4651d4){return function(_0x2f0c7c){if(_0x2f0c7c){var _0x2676c3=_0x2f0c7c['count'],_0x15068=_0x4651d4[_0xedac('0x17')],_0x590e9b=_0x4651d4[_0xedac('0x17')]+_0x4651d4[_0xedac('0x18')],_0xed25bb;if(_0x590e9b>=_0x2676c3){_0x590e9b=_0x2676c3;_0xed25bb=0xc8;}else{_0xed25bb=0xce;}_0x110546[_0xedac('0x15')](_0xed25bb);return _0x110546['set']('Content-Range',_0x15068+'-'+_0x590e9b+'/'+_0x2676c3)[_0xedac('0x19')](_0x2f0c7c);}return null;};}function patchUpdates(_0x2adefa){return function(_0x25fe64){try{jsonpatch[_0xedac('0x1a')](_0x25fe64,_0x2adefa,!![]);}catch(_0x5a5438){return BPromise['reject'](_0x5a5438);}return _0x25fe64[_0xedac('0x1b')]();};}function saveUpdates(_0x17766c,_0x23c609){return function(_0xcc1605){if(_0xcc1605){return _0xcc1605[_0xedac('0x1c')](_0x17766c)[_0xedac('0x1d')](function(_0x160be7){return _0x160be7;});}return null;};}function removeEntity(_0x928ebe,_0x5353e3){return function(_0x4593e8){if(_0x4593e8){return _0x4593e8['destroy']()[_0xedac('0x1d')](function(){_0x928ebe[_0xedac('0x15')](0xcc)['end']();});}};}function handleEntityNotFound(_0x46e09d,_0x404b8d){return function(_0x1c87bf){if(!_0x1c87bf){_0x46e09d[_0xedac('0x14')](0x194);}return _0x1c87bf;};}function handleError(_0x6b13ca,_0x167d7e){_0x167d7e=_0x167d7e||0x1f4;return function(_0x5d7c31){logger[_0xedac('0x1e')](_0x5d7c31[_0xedac('0x1f')]);if(_0x5d7c31[_0xedac('0x20')]){delete _0x5d7c31[_0xedac('0x20')];}_0x6b13ca[_0xedac('0x15')](_0x167d7e)[_0xedac('0x21')](_0x5d7c31);};}exports[_0xedac('0x22')]=function(_0x26c6f7,_0x409825){var _0x4663c0={},_0x4c5566={},_0x25d7f5={'count':0x0,'rows':[]};var _0xf0a2ce=db[_0xedac('0x23')]['rawAttributes'];_0x4c5566[_0xedac('0x24')]=_['keys'](_0xf0a2ce);_0x4c5566['query']=_[_0xedac('0x25')](_0x26c6f7[_0xedac('0x26')]);_0x4c5566[_0xedac('0x27')]=_[_0xedac('0x28')](_0x4c5566['model'],_0x4c5566['query']);_0x4c5566[_0xedac('0x27')]=_[_0xedac('0x29')](_0x4c5566[_0xedac('0x27')],[_0xedac('0x2a'),_0xedac('0x2b'),_0xedac('0x2c'),_0xedac('0x2d'),'$lt',_0xedac('0x2e'),_0xedac('0x2f'),_0xedac('0x30'),'$not',_0xedac('0x31'),_0xedac('0x32'),'$in',_0xedac('0x33'),_0xedac('0x34'),'$notLike',_0xedac('0x35'),_0xedac('0x36'),_0xedac('0x34'),_0xedac('0x37'),_0xedac('0x38'),_0xedac('0x39'),_0xedac('0x3a')]);_0x4663c0[_0xedac('0x3b')]=_[_0xedac('0x28')](_0x4c5566[_0xedac('0x24')],qs['fields'](_0x26c6f7[_0xedac('0x26')][_0xedac('0x3c')]));_0x4663c0[_0xedac('0x3b')]=_0x4663c0[_0xedac('0x3b')][_0xedac('0x3d')]?_0x4663c0[_0xedac('0x3b')]:_0x4c5566[_0xedac('0x24')];if(!_0x26c6f7[_0xedac('0x26')][_0xedac('0x3e')](_0xedac('0x3f'))){_0x4663c0[_0xedac('0x18')]=qs[_0xedac('0x18')](_0x26c6f7[_0xedac('0x26')][_0xedac('0x18')]);_0x4663c0[_0xedac('0x17')]=qs['offset'](_0x26c6f7[_0xedac('0x26')][_0xedac('0x17')]);}_0x4663c0[_0xedac('0x40')]=qs[_0xedac('0x41')](_0x26c6f7['query'][_0xedac('0x41')]);_0x4663c0[_0xedac('0x42')]=qs[_0xedac('0x27')](_['pick'](_0x26c6f7[_0xedac('0x26')],_0x4c5566[_0xedac('0x27')]));if(_0x26c6f7[_0xedac('0x26')]['filter']){_0x4663c0[_0xedac('0x42')]=_[_0xedac('0x43')](_0x4663c0['where'],{'$or':_[_0xedac('0x44')](_0xf0a2ce,function(_0x2fa832){if(_0x2fa832['type'][_0xedac('0x45')]!=='VIRTUAL'){var _0x18c114={};_0x18c114[_0x2fa832[_0xedac('0x46')]]={'$like':'%'+_0x26c6f7[_0xedac('0x26')][_0xedac('0x47')]+'%'};return _0x18c114;}})});}_0x4663c0=_[_0xedac('0x43')]({},_0x4663c0,_0x26c6f7[_0xedac('0x48')]);var _0x5030a9={'where':_0x4663c0[_0xedac('0x42')]};return db[_0xedac('0x23')][_0xedac('0x49')](_0x5030a9)[_0xedac('0x1d')](function(_0x2e7d89){_0x25d7f5[_0xedac('0x49')]=_0x2e7d89;if(_0x26c6f7[_0xedac('0x26')][_0xedac('0x4a')]){_0x4663c0[_0xedac('0x4b')]=[{'all':!![]}];}return db[_0xedac('0x23')][_0xedac('0x4c')](_0x4663c0);})[_0xedac('0x1d')](function(_0x75a57a){_0x25d7f5[_0xedac('0x4d')]=_0x75a57a;return _0x25d7f5;})[_0xedac('0x1d')](respondWithFilteredResult(_0x409825,_0x4663c0))[_0xedac('0x4e')](handleError(_0x409825,null));};exports['show']=function(_0x2fb637,_0x3989f5){var _0x144ff7={'raw':!![],'where':{'id':_0x2fb637['params']['id']}},_0x4d2de3={};_0x4d2de3['model']=_[_0xedac('0x25')](db[_0xedac('0x23')]['rawAttributes']);_0x4d2de3[_0xedac('0x26')]=_['keys'](_0x2fb637[_0xedac('0x26')]);_0x4d2de3['filters']=_[_0xedac('0x28')](_0x4d2de3[_0xedac('0x24')],_0x4d2de3[_0xedac('0x26')]);_0x144ff7[_0xedac('0x3b')]=_[_0xedac('0x28')](_0x4d2de3['model'],qs['fields'](_0x2fb637[_0xedac('0x26')]['fields']));_0x144ff7[_0xedac('0x3b')]=_0x144ff7[_0xedac('0x3b')][_0xedac('0x3d')]?_0x144ff7['attributes']:_0x4d2de3[_0xedac('0x24')];if(_0x2fb637[_0xedac('0x26')][_0xedac('0x4a')]){_0x144ff7['include']=[{'all':!![]}];}_0x144ff7=_[_0xedac('0x43')]({},_0x144ff7,_0x2fb637[_0xedac('0x48')]);return db[_0xedac('0x23')]['find'](_0x144ff7)['then'](handleEntityNotFound(_0x3989f5,null))[_0xedac('0x1d')](respondWithResult(_0x3989f5,null))[_0xedac('0x4e')](handleError(_0x3989f5,null));};exports['create']=function(_0x5a6fc4,_0x3338de){return db[_0xedac('0x23')]['create'](_0x5a6fc4[_0xedac('0x4f')],{})['then'](respondWithResult(_0x3338de,0xc9))[_0xedac('0x4e')](handleError(_0x3338de,null));};exports[_0xedac('0x1c')]=function(_0x368aeb,_0x104842){if(_0x368aeb[_0xedac('0x4f')]['id']){delete _0x368aeb[_0xedac('0x4f')]['id'];}return db['CmHopper'][_0xedac('0x50')]({'where':{'id':_0x368aeb[_0xedac('0x51')]['id']}})['then'](handleEntityNotFound(_0x104842,null))[_0xedac('0x1d')](saveUpdates(_0x368aeb[_0xedac('0x4f')],null))[_0xedac('0x1d')](respondWithResult(_0x104842,null))['catch'](handleError(_0x104842,null));};exports['describe']=function(_0xe016d4,_0x3b8153){return db['CmHopper'][_0xedac('0x52')]()[_0xedac('0x1d')](respondWithResult(_0x3b8153,null))[_0xedac('0x4e')](handleError(_0x3b8153,null));};exports[_0xedac('0x53')]=function(_0x584beb,_0x3dbcff){var _0x22c793;return db[_0xedac('0x23')]['find']({'where':{'id':_0x584beb['params']['id']}})[_0xedac('0x1d')](handleEntityNotFound(_0x3dbcff,null))[_0xedac('0x1d')](function(_0x44a7c0){if(_0x44a7c0){return _0x44a7c0['destroy']()[_0xedac('0x1d')](function(_0x2af1aa){return _0x2af1aa;});}})['then'](function(_0x4f44e){_0x22c793=_0x4f44e;if(_0x4f44e){if(_0x4f44e[_0xedac('0x54')]||_0x4f44e['CampaignId']){return db[_0x4f44e['VoiceQueueId']?'VoiceQueue':_0xedac('0x55')]['find']({'where':{'id':_0x4f44e[_0xedac('0x54')]?_0x4f44e['VoiceQueueId']:_0x4f44e['CampaignId']},'attributes':['id',_0xedac('0x20')],'raw':!![]});}}})[_0xedac('0x1d')](function(_0xf67603){var _0x35b046={};if(_0xf67603){_[_0xedac('0x43')](_0x35b046,_['omit'](_0x22c793[_0xedac('0x56')],['createdAt',_0xedac('0x57'),'id']));_0x35b046[_0xedac('0x58')]=0x15;_0x35b046[_0xedac('0x59')]=_0xedac('0x5a');_0x35b046[_0xedac('0x5b')]=_0xf67603[_0xedac('0x20')];_0x35b046[_0xedac('0x5c')]=_0x584beb['user'][_0xedac('0x20')];_0x35b046[_0xedac('0x5d')]=_0x584beb[_0xedac('0x5e')]['id'];_0x35b046[_0xedac('0x5a')]=!![];_0x35b046[_0xedac('0x5f')]=moment()[_0xedac('0x60')](_0xedac('0x61'));_0x35b046[_0xedac('0x62')]=_0x22c793[_0xedac('0x63')];_0x35b046[_0xedac('0x64')]=_0x22c793['VoiceQueueId']?'queue':_0xedac('0x65');return db[_0xedac('0x66')][_0xedac('0x50')]({'where':{'id':_0x35b046[_0xedac('0x67')]},'attributes':[_0xedac('0x68'),'lastName']})[_0xedac('0x1d')](function(_0x4ac8b0){if(!_[_0xedac('0x69')](_0x3dbcff)&&!_[_0xedac('0x69')](_0x4ac8b0['firstName'])){_0x35b046['calleridname']=_0x4ac8b0['firstName'];if(!_[_0xedac('0x69')](_0x4ac8b0[_0xedac('0x6a')])){_0x35b046[_0xedac('0x6b')]+='\x20'+_0x4ac8b0[_0xedac('0x6a')];}}})[_0xedac('0x6c')](function(){return db[_0xedac('0x6d')][_0xedac('0x6e')](_0x35b046);});}})[_0xedac('0x1d')](function(){return _0x3dbcff[_0xedac('0x15')](0xcc)[_0xedac('0x16')]();})['catch'](handleError(_0x3dbcff,null));};exports[_0xedac('0x6f')]=function(_0x30f8ca,_0x4b1299,_0x3b8d23){var _0x347461=[];return db[_0xedac('0x66')][_0xedac('0x52')]()[_0xedac('0x1d')](function(_0x122850){_0x347461=_[_0xedac('0x25')](_0x122850);return _0x30f8ca[_0xedac('0x5e')][_0xedac('0x70')]({'where':{'type':_0xedac('0x71'),'dialActive':!![],'dialMethod':_0xedac('0x72')},'attributes':['id',_0xedac('0x73')],'raw':!![]});})[_0xedac('0x1d')](function(_0x3a1d68){if(_0x3a1d68&&_0x3a1d68[_0xedac('0x3d')]){return db[_0xedac('0x74')][_0xedac('0x75')](function(_0x20cc00){var _0x493214={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0xedac('0x60')]('YYYY-MM-DD\x20HH:mm:ss')},'VoiceQueueId':{'$in':_[_0xedac('0x44')](_0x3a1d68,'id')},'$or':[{'UserId':null},{'UserId':_0x30f8ca['user']['id']}]};var _0x2380ee=[];if(_0x30f8ca[_0xedac('0x4f')][_0xedac('0x76')]&&_0x30f8ca[_0xedac('0x4f')][_0xedac('0x76')]===_0xedac('0x77')){if(!_['isEmpty'](_0x30f8ca[_0xedac('0x4f')][_0xedac('0x78')])){_0x493214['id']={'$in':_0x30f8ca[_0xedac('0x4f')][_0xedac('0x78')]};}_0x2380ee=[[_0xedac('0x79'),_0xedac('0x7a')],['scheduledAt',_0x3a1d68[0x0][_0xedac('0x73')]==='ASC'?_0xedac('0x7b'):_0xedac('0x7a')],db[_0xedac('0x74')]['literal'](_0xedac('0x7c')),['id','DESC']];}else{if(!_['isEmpty'](_0x30f8ca[_0xedac('0x4f')][_0xedac('0x78')])){_0x493214['id']={'$notIn':_0x30f8ca[_0xedac('0x4f')][_0xedac('0x78')]};}_0x2380ee=[[_0xedac('0x79'),'DESC'],[_0xedac('0x7d'),_0x3a1d68[0x0]['dialOrderByScheduledAt']],db[_0xedac('0x74')][_0xedac('0x7e')](_0xedac('0x7f')),['id',_0xedac('0x7a')]];}return db[_0xedac('0x23')][_0xedac('0x80')]({'attributes':['id','scheduledat'],'where':_0x493214,'order':_0x2380ee,'transaction':_0x20cc00,'lock':_0x20cc00[_0xedac('0x81')][_0xedac('0x82')],'include':[{'model':db[_0xedac('0x66')],'as':_0xedac('0x83'),'attributes':_0x347461,'include':[{'model':db[_0xedac('0x84')],'as':_0xedac('0x85'),'attributes':['id',_0xedac('0x20')]}]},{'model':db[_0xedac('0x86')],'as':_0xedac('0x87'),'attributes':['id',_0xedac('0x20')]},{'model':db[_0xedac('0x88')],'as':_0xedac('0x89'),'attributes':['id','name',_0xedac('0x8a')]}]})['then'](function(_0x5cba66){if(_0x5cba66){return _0x5cba66[_0xedac('0x1c')]({'UserId':_0x30f8ca['user']['id']},{'transaction':_0x20cc00});}});});}})[_0xedac('0x1d')](handleEntityNotFound(_0x4b1299,null))[_0xedac('0x1d')](respondWithResult(_0x4b1299,null))['catch'](handleError(_0x4b1299,null));};exports[_0xedac('0x8b')]=function(_0x53d008,_0x48832e,_0x51d88b){return _0x53d008[_0xedac('0x5e')]['getVoiceQueues']({'where':{'type':_0xedac('0x71'),'dialActive':!![],'dialMethod':_0xedac('0x72')},'attributes':['id'],'raw':!![]})['then'](function(_0x1dfc0b){if(_0x1dfc0b&&_0x1dfc0b['length']){return db[_0xedac('0x23')][_0xedac('0x49')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0xedac('0x60')](_0xedac('0x61'))},'VoiceQueueId':{'$in':_[_0xedac('0x44')](_0x1dfc0b,'id')},'$or':[{'UserId':null},{'UserId':_0x53d008['user']['id']}]}});}return 0x0;})[_0xedac('0x1d')](function(_0x4a0223){if(!_[_0xedac('0x69')](_0x4a0223)&&_[_0xedac('0x8c')](_0x4a0223)){return _0x48832e[_0xedac('0x15')](0xc8)[_0xedac('0x19')]({'contacts':_0x4a0223});}else{logger[_0xedac('0x1e')](_0xedac('0x8d'),'getOpenContacts',0x194,_0xedac('0x8e'));_0x48832e[_0xedac('0x14')](0x194);}})['catch'](handleError(_0x48832e,null));};