Built motion from commit ce9a7520.|2.5.39
[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 _0xb63f=['literal','COALESCE(CmHopper.UserId,\x200)\x20ASC','DESC','COALESCE(CmHopper.UserId,\x200)\x20DESC','findOne','scheduledat','LOCK','UPDATE','Contact','CmCompany','Company','CmList','List','dialActive','getOpenContacts','isNumber','error','CmHopper,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','lodash','squel','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','status','end','json','offset','undefined','limit','count','set','apply','save','update','then','destroy','sendStatus','stack','name','send','index','map','CmHopper','rawAttributes','fieldName','type','model','query','keys','intersection','filters','$and','$or','$gt','$gte','$lt','$lte','$ne','$between','$in','$notIn','$like','$notLike','$iLike','$overlap','$contains','fields','attributes','length','order','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','params','create','body','find','describe','VoiceQueueId','CampaignId','VoiceQueue','Campaign','omit','createdAt','updatedAt','state','deleted','campaign','membername','format','YYYY-MM-DD\x20HH:mm:ss','calleridnum','campaigntype','queue','ivr','CmContact','ContactId','firstName','isNil','calleridname','lastName','CmHopperFinal','outbound','preview','dialOrderByScheduledAt','dialPreviewAutoRecallMe','transaction','user','value','direction','prev','hopperIds','priority','ASC','scheduledAt','sequelize'];(function(_0x169e06,_0x45465a){var _0xc12655=function(_0x4db92f){while(--_0x4db92f){_0x169e06['push'](_0x169e06['shift']());}};_0xc12655(++_0x45465a);}(_0xb63f,0xa3));var _0xfb63=function(_0x11ec6c,_0x1a8169){_0x11ec6c=_0x11ec6c-0x0;var _0xb59506=_0xb63f[_0x11ec6c];return _0xb59506;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0xfb63('0x0'));var jsonpatch=require(_0xfb63('0x1'));var rp=require(_0xfb63('0x2'));var moment=require(_0xfb63('0x3'));var BPromise=require(_0xfb63('0x4'));var Mustache=require(_0xfb63('0x5'));var util=require(_0xfb63('0x6'));var path=require(_0xfb63('0x7'));var sox=require(_0xfb63('0x8'));var csv=require('to-csv');var ejs=require(_0xfb63('0x9'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xfb63('0xa'));var squel=require(_0xfb63('0xb'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xfb63('0xc'));var querystring=require(_0xfb63('0xd'));var Papa=require(_0xfb63('0xe'));var Redis=require('ioredis');var authService=require(_0xfb63('0xf'));var qs=require(_0xfb63('0x10'));var as=require(_0xfb63('0x11'));var hardwareService=require(_0xfb63('0x12'));var logger=require(_0xfb63('0x13'))(_0xfb63('0x14'));var utils=require(_0xfb63('0x15'));var config=require(_0xfb63('0x16'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0xee69d,_0x233ec3){_0x233ec3=_0x233ec3||0xcc;return function(_0xa2f034){if(_0xa2f034){return _0xee69d['sendStatus'](_0x233ec3);}return _0xee69d[_0xfb63('0x17')](_0x233ec3)[_0xfb63('0x18')]();};}function respondWithResult(_0x48a22c,_0x4575b6){_0x4575b6=_0x4575b6||0xc8;return function(_0x377d1e){if(_0x377d1e){return _0x48a22c[_0xfb63('0x17')](_0x4575b6)[_0xfb63('0x19')](_0x377d1e);}};}function respondWithFilteredResult(_0x51e7ef,_0x44619e){return function(_0x48e2c0){if(_0x48e2c0){var _0x19f196=typeof _0x44619e[_0xfb63('0x1a')]===_0xfb63('0x1b')&&typeof _0x44619e[_0xfb63('0x1c')]===_0xfb63('0x1b');var _0x49081d=_0x48e2c0[_0xfb63('0x1d')];var _0x5dfd32=_0x19f196?0x0:_0x44619e[_0xfb63('0x1a')];var _0x4f7508=_0x19f196?_0x48e2c0[_0xfb63('0x1d')]:_0x44619e[_0xfb63('0x1a')]+_0x44619e[_0xfb63('0x1c')];var _0x452f1e;if(_0x4f7508>=_0x49081d){_0x4f7508=_0x49081d;_0x452f1e=0xc8;}else{_0x452f1e=0xce;}_0x51e7ef['status'](_0x452f1e);return _0x51e7ef[_0xfb63('0x1e')]('Content-Range',_0x5dfd32+'-'+_0x4f7508+'/'+_0x49081d)[_0xfb63('0x19')](_0x48e2c0);}return null;};}function patchUpdates(_0x3ef0cf){return function(_0x564052){try{jsonpatch[_0xfb63('0x1f')](_0x564052,_0x3ef0cf,!![]);}catch(_0x23c4fb){return BPromise['reject'](_0x23c4fb);}return _0x564052[_0xfb63('0x20')]();};}function saveUpdates(_0x43bf68,_0x267953){return function(_0x34e22c){if(_0x34e22c){return _0x34e22c[_0xfb63('0x21')](_0x43bf68)[_0xfb63('0x22')](function(_0x17f81e){return _0x17f81e;});}return null;};}function removeEntity(_0x1bbedc,_0x43c245){return function(_0xa7612f){if(_0xa7612f){return _0xa7612f[_0xfb63('0x23')]()[_0xfb63('0x22')](function(){_0x1bbedc[_0xfb63('0x17')](0xcc)[_0xfb63('0x18')]();});}};}function handleEntityNotFound(_0x26036d,_0x19ea95){return function(_0x1b8f0a){if(!_0x1b8f0a){_0x26036d[_0xfb63('0x24')](0x194);}return _0x1b8f0a;};}function handleError(_0x49482f,_0x2bbe9a){_0x2bbe9a=_0x2bbe9a||0x1f4;return function(_0x4c80c1){logger['error'](_0x4c80c1[_0xfb63('0x25')]);if(_0x4c80c1[_0xfb63('0x26')]){delete _0x4c80c1[_0xfb63('0x26')];}_0x49482f[_0xfb63('0x17')](_0x2bbe9a)[_0xfb63('0x27')](_0x4c80c1);};}exports[_0xfb63('0x28')]=function(_0x594962,_0x45e231){var _0x2a008e={},_0x441dd7={},_0x188501={'count':0x0,'rows':[]};var _0x3c910b=_[_0xfb63('0x29')](db[_0xfb63('0x2a')][_0xfb63('0x2b')],function(_0x293909){return{'name':_0x293909[_0xfb63('0x2c')],'type':_0x293909[_0xfb63('0x2d')]['key']};});_0x441dd7[_0xfb63('0x2e')]=_['map'](_0x3c910b,_0xfb63('0x26'));_0x441dd7[_0xfb63('0x2f')]=_[_0xfb63('0x30')](_0x594962['query']);_0x441dd7['filters']=_[_0xfb63('0x31')](_0x441dd7['model'],_0x441dd7['query']);_0x441dd7[_0xfb63('0x32')]=_['concat'](_0x441dd7[_0xfb63('0x32')],[_0xfb63('0x33'),_0xfb63('0x34'),_0xfb63('0x35'),_0xfb63('0x36'),_0xfb63('0x37'),_0xfb63('0x38'),_0xfb63('0x39'),'$eq','$not',_0xfb63('0x3a'),'$notBetween',_0xfb63('0x3b'),_0xfb63('0x3c'),_0xfb63('0x3d'),_0xfb63('0x3e'),_0xfb63('0x3f'),'$notILike','$like',_0xfb63('0x40'),_0xfb63('0x41'),'$contained','$any']);_0x2a008e['attributes']=_[_0xfb63('0x31')](_0x441dd7[_0xfb63('0x2e')],qs[_0xfb63('0x42')](_0x594962['query'][_0xfb63('0x42')]));_0x2a008e[_0xfb63('0x43')]=_0x2a008e[_0xfb63('0x43')][_0xfb63('0x44')]?_0x2a008e[_0xfb63('0x43')]:_0x441dd7[_0xfb63('0x2e')];if(!_0x594962[_0xfb63('0x2f')]['hasOwnProperty']('nolimit')){_0x2a008e[_0xfb63('0x1c')]=qs['limit'](_0x594962[_0xfb63('0x2f')][_0xfb63('0x1c')]);_0x2a008e[_0xfb63('0x1a')]=qs[_0xfb63('0x1a')](_0x594962[_0xfb63('0x2f')][_0xfb63('0x1a')]);}_0x2a008e[_0xfb63('0x45')]=qs[_0xfb63('0x46')](_0x594962[_0xfb63('0x2f')][_0xfb63('0x46')]);_0x2a008e[_0xfb63('0x47')]=qs[_0xfb63('0x32')](_[_0xfb63('0x48')](_0x594962[_0xfb63('0x2f')],_0x441dd7[_0xfb63('0x32')]),_0x3c910b);if(_0x594962[_0xfb63('0x2f')][_0xfb63('0x49')]){_0x2a008e[_0xfb63('0x47')]=_[_0xfb63('0x4a')](_0x2a008e[_0xfb63('0x47')],{'$or':_[_0xfb63('0x29')](_0x3c910b,function(_0x480f7f){if(_0x480f7f['type']!==_0xfb63('0x4b')){var _0x54346d={};_0x54346d[_0x480f7f[_0xfb63('0x26')]]={'$like':'%'+_0x594962[_0xfb63('0x2f')][_0xfb63('0x49')]+'%'};return _0x54346d;}})});}_0x2a008e=_[_0xfb63('0x4a')]({},_0x2a008e,_0x594962[_0xfb63('0x4c')]);var _0x1956bd={'where':_0x2a008e[_0xfb63('0x47')]};return db[_0xfb63('0x2a')][_0xfb63('0x1d')](_0x1956bd)[_0xfb63('0x22')](function(_0x837794){_0x188501[_0xfb63('0x1d')]=_0x837794;if(_0x594962['query'][_0xfb63('0x4d')]){_0x2a008e[_0xfb63('0x4e')]=[{'all':!![]}];}return db[_0xfb63('0x2a')][_0xfb63('0x4f')](_0x2a008e);})[_0xfb63('0x22')](function(_0x2c8e54){_0x188501[_0xfb63('0x50')]=_0x2c8e54;return _0x188501;})[_0xfb63('0x22')](respondWithFilteredResult(_0x45e231,_0x2a008e))[_0xfb63('0x51')](handleError(_0x45e231,null));};exports['show']=function(_0xa39bf3,_0x393f63){var _0x339a03={'raw':!![],'where':{'id':_0xa39bf3[_0xfb63('0x52')]['id']}},_0x3ebdb4={};_0x3ebdb4[_0xfb63('0x2e')]=_['keys'](db[_0xfb63('0x2a')][_0xfb63('0x2b')]);_0x3ebdb4[_0xfb63('0x2f')]=_[_0xfb63('0x30')](_0xa39bf3[_0xfb63('0x2f')]);_0x3ebdb4[_0xfb63('0x32')]=_[_0xfb63('0x31')](_0x3ebdb4[_0xfb63('0x2e')],_0x3ebdb4[_0xfb63('0x2f')]);_0x339a03['attributes']=_[_0xfb63('0x31')](_0x3ebdb4[_0xfb63('0x2e')],qs[_0xfb63('0x42')](_0xa39bf3[_0xfb63('0x2f')][_0xfb63('0x42')]));_0x339a03[_0xfb63('0x43')]=_0x339a03[_0xfb63('0x43')][_0xfb63('0x44')]?_0x339a03[_0xfb63('0x43')]:_0x3ebdb4['model'];if(_0xa39bf3[_0xfb63('0x2f')][_0xfb63('0x4d')]){_0x339a03[_0xfb63('0x4e')]=[{'all':!![]}];}_0x339a03=_[_0xfb63('0x4a')]({},_0x339a03,_0xa39bf3[_0xfb63('0x4c')]);return db[_0xfb63('0x2a')]['find'](_0x339a03)[_0xfb63('0x22')](handleEntityNotFound(_0x393f63,null))[_0xfb63('0x22')](respondWithResult(_0x393f63,null))['catch'](handleError(_0x393f63,null));};exports[_0xfb63('0x53')]=function(_0x20793c,_0x8290a8){return db[_0xfb63('0x2a')][_0xfb63('0x53')](_0x20793c[_0xfb63('0x54')],{})[_0xfb63('0x22')](respondWithResult(_0x8290a8,0xc9))[_0xfb63('0x51')](handleError(_0x8290a8,null));};exports[_0xfb63('0x21')]=function(_0x1ccb6a,_0x69a7eb){if(_0x1ccb6a['body']['id']){delete _0x1ccb6a[_0xfb63('0x54')]['id'];}return db[_0xfb63('0x2a')][_0xfb63('0x55')]({'where':{'id':_0x1ccb6a['params']['id']}})['then'](handleEntityNotFound(_0x69a7eb,null))[_0xfb63('0x22')](saveUpdates(_0x1ccb6a[_0xfb63('0x54')],null))[_0xfb63('0x22')](respondWithResult(_0x69a7eb,null))[_0xfb63('0x51')](handleError(_0x69a7eb,null));};exports['describe']=function(_0x360528,_0x3a071a){return db[_0xfb63('0x2a')][_0xfb63('0x56')]()[_0xfb63('0x22')](respondWithResult(_0x3a071a,null))[_0xfb63('0x51')](handleError(_0x3a071a,null));};exports[_0xfb63('0x23')]=function(_0x24bead,_0x57dd5e){var _0x3ce651;return db[_0xfb63('0x2a')][_0xfb63('0x55')]({'where':{'id':_0x24bead[_0xfb63('0x52')]['id']}})['then'](handleEntityNotFound(_0x57dd5e,null))[_0xfb63('0x22')](function(_0x3710d8){if(_0x3710d8){return _0x3710d8[_0xfb63('0x23')]()[_0xfb63('0x22')](function(_0x4343b3){return _0x4343b3;});}})['then'](function(_0x53b803){_0x3ce651=_0x53b803;if(_0x53b803){if(_0x53b803[_0xfb63('0x57')]||_0x53b803[_0xfb63('0x58')]){return db[_0x53b803[_0xfb63('0x57')]?_0xfb63('0x59'):_0xfb63('0x5a')]['find']({'where':{'id':_0x53b803[_0xfb63('0x57')]?_0x53b803['VoiceQueueId']:_0x53b803[_0xfb63('0x58')]},'attributes':['id',_0xfb63('0x26')],'raw':!![]});}}})[_0xfb63('0x22')](function(_0x459156){var _0x1256f7={};if(_0x459156){_[_0xfb63('0x4a')](_0x1256f7,_[_0xfb63('0x5b')](_0x3ce651['dataValues'],[_0xfb63('0x5c'),_0xfb63('0x5d'),'id']));_0x1256f7[_0xfb63('0x5e')]=0x15;_0x1256f7['statedesc']=_0xfb63('0x5f');_0x1256f7[_0xfb63('0x60')]=_0x459156[_0xfb63('0x26')];_0x1256f7[_0xfb63('0x61')]=_0x24bead['user']['name'];_0x1256f7['UserId']=_0x24bead['user']['id'];_0x1256f7[_0xfb63('0x5f')]=!![];_0x1256f7['deletedat']=moment()[_0xfb63('0x62')](_0xfb63('0x63'));_0x1256f7[_0xfb63('0x64')]=_0x3ce651['phone'];_0x1256f7[_0xfb63('0x65')]=_0x3ce651[_0xfb63('0x57')]?_0xfb63('0x66'):_0xfb63('0x67');return db[_0xfb63('0x68')]['find']({'where':{'id':_0x1256f7[_0xfb63('0x69')]},'attributes':[_0xfb63('0x6a'),'lastName']})['then'](function(_0x2efe73){if(!_[_0xfb63('0x6b')](_0x57dd5e)&&!_[_0xfb63('0x6b')](_0x2efe73['firstName'])){_0x1256f7[_0xfb63('0x6c')]=_0x2efe73[_0xfb63('0x6a')];if(!_[_0xfb63('0x6b')](_0x2efe73[_0xfb63('0x6d')])){_0x1256f7[_0xfb63('0x6c')]+='\x20'+_0x2efe73['lastName'];}}})['finally'](function(){return db[_0xfb63('0x6e')][_0xfb63('0x53')](_0x1256f7);});}})[_0xfb63('0x22')](function(){return _0x57dd5e[_0xfb63('0x17')](0xcc)[_0xfb63('0x18')]();})[_0xfb63('0x51')](handleError(_0x57dd5e,null));};exports['getPreview']=function(_0x10977d,_0x1a5936,_0x1be43f){var _0x1f7e8f=[];return db[_0xfb63('0x68')][_0xfb63('0x56')]()[_0xfb63('0x22')](function(_0x30f60f){_0x1f7e8f=_['keys'](_0x30f60f);return _0x10977d['user']['getVoiceQueues']({'where':{'type':_0xfb63('0x6f'),'dialActive':!![],'dialMethod':_0xfb63('0x70')},'attributes':['id',_0xfb63('0x71'),_0xfb63('0x72')],'raw':!![]});})[_0xfb63('0x22')](function(_0x592452){if(_0x592452&&_0x592452['length']){return db['sequelize'][_0xfb63('0x73')](function(_0x43ea48){var _0x5c07d7={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()['format'](_0xfb63('0x63'))},'VoiceQueueId':{'$in':_['map'](_0x592452,'id')},'$or':[{'UserId':null},{'UserId':_0x10977d[_0xfb63('0x74')]['id']}]};var _0x8494ea={'active':![],'recallme':!![],'VoiceQueueId':{'$in':_(_0x592452)[_0xfb63('0x49')](['dialPreviewAutoRecallMe',0x0])[_0xfb63('0x29')]('id')[_0xfb63('0x75')]()},'$or':[{'UserId':null},{'UserId':_0x10977d[_0xfb63('0x74')]['id']}]};var _0x1d892b={'$or':[_0x5c07d7,_0x8494ea]};var _0x1764d8=[];if(_0x10977d[_0xfb63('0x54')]['direction']&&_0x10977d[_0xfb63('0x54')][_0xfb63('0x76')]===_0xfb63('0x77')){if(!_['isEmpty'](_0x10977d['body'][_0xfb63('0x78')])){_0x1d892b['id']={'$in':_0x10977d[_0xfb63('0x54')][_0xfb63('0x78')]};}_0x1764d8=[[_0xfb63('0x79'),_0xfb63('0x7a')],[_0xfb63('0x7b'),_0x592452[0x0][_0xfb63('0x71')]==='ASC'?'DESC':_0xfb63('0x7a')],db[_0xfb63('0x7c')][_0xfb63('0x7d')](_0xfb63('0x7e')),['id',_0xfb63('0x7f')]];}else{if(!_['isEmpty'](_0x10977d[_0xfb63('0x54')][_0xfb63('0x78')])){_0x1d892b['id']={'$notIn':_0x10977d[_0xfb63('0x54')]['hopperIds']};}_0x1764d8=[[_0xfb63('0x79'),_0xfb63('0x7f')],[_0xfb63('0x7b'),_0x592452[0x0][_0xfb63('0x71')]],db[_0xfb63('0x7c')]['literal'](_0xfb63('0x80')),['id',_0xfb63('0x7a')]];}return db[_0xfb63('0x2a')][_0xfb63('0x81')]({'attributes':['id',_0xfb63('0x82'),'recallme'],'where':_0x1d892b,'order':_0x1764d8,'transaction':_0x43ea48,'lock':_0x43ea48[_0xfb63('0x83')][_0xfb63('0x84')],'include':[{'model':db['CmContact'],'as':_0xfb63('0x85'),'attributes':_0x1f7e8f,'include':[{'model':db[_0xfb63('0x86')],'as':_0xfb63('0x87'),'attributes':['id',_0xfb63('0x26')]}]},{'model':db[_0xfb63('0x88')],'as':_0xfb63('0x89'),'attributes':['id',_0xfb63('0x26')]},{'model':db[_0xfb63('0x59')],'as':'Queue','attributes':['id',_0xfb63('0x26'),_0xfb63('0x8a')]}]})[_0xfb63('0x22')](function(_0x9dd78c){if(_0x9dd78c){return _0x9dd78c[_0xfb63('0x21')]({'UserId':_0x10977d['user']['id']},{'transaction':_0x43ea48});}});});}})[_0xfb63('0x22')](handleEntityNotFound(_0x1a5936,null))[_0xfb63('0x22')](respondWithResult(_0x1a5936,null))[_0xfb63('0x51')](handleError(_0x1a5936,null));};exports[_0xfb63('0x8b')]=function(_0x599e21,_0x3ccdb5,_0x327773){return _0x599e21[_0xfb63('0x74')]['getVoiceQueues']({'where':{'type':_0xfb63('0x6f'),'dialActive':!![],'dialMethod':_0xfb63('0x70')},'attributes':['id'],'raw':!![]})['then'](function(_0x53f2da){if(_0x53f2da&&_0x53f2da[_0xfb63('0x44')]){return db[_0xfb63('0x2a')][_0xfb63('0x1d')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()['format'](_0xfb63('0x63'))},'VoiceQueueId':{'$in':_[_0xfb63('0x29')](_0x53f2da,'id')},'$or':[{'UserId':null},{'UserId':_0x599e21[_0xfb63('0x74')]['id']}]}});}return 0x0;})[_0xfb63('0x22')](function(_0x849adc){if(!_['isNil'](_0x849adc)&&_[_0xfb63('0x8c')](_0x849adc)){return _0x3ccdb5[_0xfb63('0x17')](0xc8)[_0xfb63('0x19')]({'contacts':_0x849adc});}else{logger[_0xfb63('0x8d')](_0xfb63('0x8e'),_0xfb63('0x8b'),0x194,_0xfb63('0x8f'));_0x3ccdb5['sendStatus'](0x194);}})['catch'](handleError(_0x3ccdb5,null));};