f16a349d1854e2fde5e117079d30b55647bfbbc7
[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 _0x3f19=['create','body','update','describe','VoiceQueueId','CampaignId','omit','dataValues','updatedAt','campaign','membername','UserId','user','deletedat','YYYY-MM-DD\x20HH:mm:ss','phone','campaigntype','ivr','CmContact','ContactId','firstName','lastName','isNil','calleridname','finally','getVoiceQueues','outbound','dialOrderByScheduledAt','sequelize','transaction','format','direction','prev','isEmpty','hopperIds','priority','ASC','literal','COALESCE(CmHopper.UserId,\x200)\x20ASC','DESC','COALESCE(CmHopper.UserId,\x200)\x20DESC','findOne','scheduledat','LOCK','Contact','Company','CmList','Queue','getOpenContacts','preview','isNumber','CmHopper,\x20%s,\x20%s,\x20%s','html-pdf','fast-json-patch','moment','bluebird','mustache','util','sox','to-csv','ejs','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','count','offset','limit','set','Content-Range','save','then','destroy','error','stack','name','send','map','CmHopper','rawAttributes','type','key','model','query','keys','filters','intersection','$gt','$gte','$lte','$ne','$eq','$not','$between','$notBetween','$in','$notIn','$like','$notLike','$iLike','$overlap','$contains','$contained','$any','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','filter','merge','VIRTUAL','options','findAll','rows','catch','show','params','includeAll','include','find'];(function(_0x3bfadb,_0x53fdb8){var _0x50c3fd=function(_0x32c69f){while(--_0x32c69f){_0x3bfadb['push'](_0x3bfadb['shift']());}};_0x50c3fd(++_0x53fdb8);}(_0x3f19,0xc0));var _0x93f1=function(_0x5a89ea,_0x436388){_0x5a89ea=_0x5a89ea-0x0;var _0x21193d=_0x3f19[_0x5a89ea];return _0x21193d;};'use strict';var pdf=require(_0x93f1('0x0'));var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x93f1('0x1'));var rp=require('request-promise');var moment=require(_0x93f1('0x2'));var BPromise=require(_0x93f1('0x3'));var Mustache=require(_0x93f1('0x4'));var util=require(_0x93f1('0x5'));var path=require('path');var sox=require(_0x93f1('0x6'));var csv=require(_0x93f1('0x7'));var ejs=require(_0x93f1('0x8'));var fs=require('fs');var _=require(_0x93f1('0x9'));var squel=require(_0x93f1('0xa'));var crypto=require(_0x93f1('0xb'));var jsforce=require('jsforce');var deskjs=require(_0x93f1('0xc'));var toCsv=require(_0x93f1('0x7'));var querystring=require(_0x93f1('0xd'));var Papa=require(_0x93f1('0xe'));var Redis=require(_0x93f1('0xf'));var authService=require('../../components/auth/service');var qs=require(_0x93f1('0x10'));var as=require(_0x93f1('0x11'));var hardwareService=require(_0x93f1('0x12'));var logger=require(_0x93f1('0x13'))(_0x93f1('0x14'));var utils=require(_0x93f1('0x15'));var config=require(_0x93f1('0x16'));var licenseUtil=require(_0x93f1('0x17'));var db=require(_0x93f1('0x18'))['db'];function respondWithStatusCode(_0x3348ab,_0x5b4aa3){_0x5b4aa3=_0x5b4aa3||0xcc;return function(_0x4080a8){if(_0x4080a8){return _0x3348ab[_0x93f1('0x19')](_0x5b4aa3);}return _0x3348ab[_0x93f1('0x1a')](_0x5b4aa3)[_0x93f1('0x1b')]();};}function respondWithResult(_0x552ae6,_0x391deb){_0x391deb=_0x391deb||0xc8;return function(_0x49f4c8){if(_0x49f4c8){return _0x552ae6[_0x93f1('0x1a')](_0x391deb)[_0x93f1('0x1c')](_0x49f4c8);}};}function respondWithFilteredResult(_0x176cf2,_0x43b0bc){return function(_0x120c83){if(_0x120c83){var _0x48e0a1=_0x120c83[_0x93f1('0x1d')],_0x2c8445=_0x43b0bc[_0x93f1('0x1e')],_0x415f1a=_0x43b0bc[_0x93f1('0x1e')]+_0x43b0bc[_0x93f1('0x1f')],_0x3e3afa;if(_0x415f1a>=_0x48e0a1){_0x415f1a=_0x48e0a1;_0x3e3afa=0xc8;}else{_0x3e3afa=0xce;}_0x176cf2[_0x93f1('0x1a')](_0x3e3afa);return _0x176cf2[_0x93f1('0x20')](_0x93f1('0x21'),_0x2c8445+'-'+_0x415f1a+'/'+_0x48e0a1)[_0x93f1('0x1c')](_0x120c83);}return null;};}function patchUpdates(_0x577751){return function(_0x54880e){try{jsonpatch['apply'](_0x54880e,_0x577751,!![]);}catch(_0x36c75f){return BPromise['reject'](_0x36c75f);}return _0x54880e[_0x93f1('0x22')]();};}function saveUpdates(_0x39ae13,_0x3795b8){return function(_0x22097a){if(_0x22097a){return _0x22097a['update'](_0x39ae13)[_0x93f1('0x23')](function(_0x4cf9a8){return _0x4cf9a8;});}return null;};}function removeEntity(_0xe7cea3,_0x310e76){return function(_0x4b26bb){if(_0x4b26bb){return _0x4b26bb[_0x93f1('0x24')]()['then'](function(){_0xe7cea3['status'](0xcc)[_0x93f1('0x1b')]();});}};}function handleEntityNotFound(_0x2b2115,_0x5c30a5){return function(_0x634591){if(!_0x634591){_0x2b2115['sendStatus'](0x194);}return _0x634591;};}function handleError(_0x1a6357,_0x4caa3d){_0x4caa3d=_0x4caa3d||0x1f4;return function(_0x5272ba){logger[_0x93f1('0x25')](_0x5272ba[_0x93f1('0x26')]);if(_0x5272ba[_0x93f1('0x27')]){delete _0x5272ba[_0x93f1('0x27')];}_0x1a6357[_0x93f1('0x1a')](_0x4caa3d)[_0x93f1('0x28')](_0x5272ba);};}exports['index']=function(_0x2eff69,_0x4d501d){var _0x8c5705={},_0x3e71a7={},_0x210b24={'count':0x0,'rows':[]};var _0x46918a=_[_0x93f1('0x29')](db[_0x93f1('0x2a')][_0x93f1('0x2b')],function(_0x35a2d5){return{'name':_0x35a2d5['fieldName'],'type':_0x35a2d5[_0x93f1('0x2c')][_0x93f1('0x2d')]};});_0x3e71a7[_0x93f1('0x2e')]=_[_0x93f1('0x29')](_0x46918a,'name');_0x3e71a7[_0x93f1('0x2f')]=_[_0x93f1('0x30')](_0x2eff69['query']);_0x3e71a7[_0x93f1('0x31')]=_[_0x93f1('0x32')](_0x3e71a7['model'],_0x3e71a7['query']);_0x3e71a7[_0x93f1('0x31')]=_['concat'](_0x3e71a7['filters'],['$and','$or',_0x93f1('0x33'),_0x93f1('0x34'),'$lt',_0x93f1('0x35'),_0x93f1('0x36'),_0x93f1('0x37'),_0x93f1('0x38'),_0x93f1('0x39'),_0x93f1('0x3a'),_0x93f1('0x3b'),_0x93f1('0x3c'),_0x93f1('0x3d'),_0x93f1('0x3e'),_0x93f1('0x3f'),'$notILike','$like',_0x93f1('0x40'),_0x93f1('0x41'),_0x93f1('0x42'),_0x93f1('0x43')]);_0x8c5705['attributes']=_['intersection'](_0x3e71a7[_0x93f1('0x2e')],qs[_0x93f1('0x44')](_0x2eff69['query'][_0x93f1('0x44')]));_0x8c5705[_0x93f1('0x45')]=_0x8c5705[_0x93f1('0x45')][_0x93f1('0x46')]?_0x8c5705[_0x93f1('0x45')]:_0x3e71a7[_0x93f1('0x2e')];if(!_0x2eff69[_0x93f1('0x2f')][_0x93f1('0x47')](_0x93f1('0x48'))){_0x8c5705[_0x93f1('0x1f')]=qs['limit'](_0x2eff69['query'][_0x93f1('0x1f')]);_0x8c5705[_0x93f1('0x1e')]=qs[_0x93f1('0x1e')](_0x2eff69[_0x93f1('0x2f')][_0x93f1('0x1e')]);}_0x8c5705[_0x93f1('0x49')]=qs[_0x93f1('0x4a')](_0x2eff69[_0x93f1('0x2f')][_0x93f1('0x4a')]);_0x8c5705[_0x93f1('0x4b')]=qs[_0x93f1('0x31')](_['pick'](_0x2eff69[_0x93f1('0x2f')],_0x3e71a7['filters']),_0x46918a);if(_0x2eff69[_0x93f1('0x2f')][_0x93f1('0x4c')]){_0x8c5705[_0x93f1('0x4b')]=_[_0x93f1('0x4d')](_0x8c5705[_0x93f1('0x4b')],{'$or':_[_0x93f1('0x29')](_0x46918a,function(_0x14882a){if(_0x14882a[_0x93f1('0x2c')]!==_0x93f1('0x4e')){var _0x9c1014={};_0x9c1014[_0x14882a[_0x93f1('0x27')]]={'$like':'%'+_0x2eff69['query']['filter']+'%'};return _0x9c1014;}})});}_0x8c5705=_['merge']({},_0x8c5705,_0x2eff69[_0x93f1('0x4f')]);var _0x33a8ee={'where':_0x8c5705[_0x93f1('0x4b')]};return db[_0x93f1('0x2a')]['count'](_0x33a8ee)[_0x93f1('0x23')](function(_0xdae5c7){_0x210b24[_0x93f1('0x1d')]=_0xdae5c7;if(_0x2eff69[_0x93f1('0x2f')]['includeAll']){_0x8c5705['include']=[{'all':!![]}];}return db[_0x93f1('0x2a')][_0x93f1('0x50')](_0x8c5705);})['then'](function(_0x15999d){_0x210b24[_0x93f1('0x51')]=_0x15999d;return _0x210b24;})[_0x93f1('0x23')](respondWithFilteredResult(_0x4d501d,_0x8c5705))[_0x93f1('0x52')](handleError(_0x4d501d,null));};exports[_0x93f1('0x53')]=function(_0x682d3a,_0x26bb98){var _0x18151a={'raw':!![],'where':{'id':_0x682d3a[_0x93f1('0x54')]['id']}},_0x502f76={};_0x502f76['model']=_['keys'](db[_0x93f1('0x2a')][_0x93f1('0x2b')]);_0x502f76[_0x93f1('0x2f')]=_[_0x93f1('0x30')](_0x682d3a[_0x93f1('0x2f')]);_0x502f76['filters']=_[_0x93f1('0x32')](_0x502f76[_0x93f1('0x2e')],_0x502f76[_0x93f1('0x2f')]);_0x18151a[_0x93f1('0x45')]=_[_0x93f1('0x32')](_0x502f76[_0x93f1('0x2e')],qs['fields'](_0x682d3a[_0x93f1('0x2f')]['fields']));_0x18151a[_0x93f1('0x45')]=_0x18151a['attributes'][_0x93f1('0x46')]?_0x18151a[_0x93f1('0x45')]:_0x502f76['model'];if(_0x682d3a[_0x93f1('0x2f')][_0x93f1('0x55')]){_0x18151a[_0x93f1('0x56')]=[{'all':!![]}];}_0x18151a=_[_0x93f1('0x4d')]({},_0x18151a,_0x682d3a['options']);return db['CmHopper'][_0x93f1('0x57')](_0x18151a)[_0x93f1('0x23')](handleEntityNotFound(_0x26bb98,null))['then'](respondWithResult(_0x26bb98,null))[_0x93f1('0x52')](handleError(_0x26bb98,null));};exports[_0x93f1('0x58')]=function(_0x4d1b64,_0x30062a){return db[_0x93f1('0x2a')][_0x93f1('0x58')](_0x4d1b64[_0x93f1('0x59')],{})['then'](respondWithResult(_0x30062a,0xc9))[_0x93f1('0x52')](handleError(_0x30062a,null));};exports[_0x93f1('0x5a')]=function(_0x190c03,_0x3dff00){if(_0x190c03[_0x93f1('0x59')]['id']){delete _0x190c03[_0x93f1('0x59')]['id'];}return db[_0x93f1('0x2a')][_0x93f1('0x57')]({'where':{'id':_0x190c03[_0x93f1('0x54')]['id']}})[_0x93f1('0x23')](handleEntityNotFound(_0x3dff00,null))[_0x93f1('0x23')](saveUpdates(_0x190c03[_0x93f1('0x59')],null))['then'](respondWithResult(_0x3dff00,null))[_0x93f1('0x52')](handleError(_0x3dff00,null));};exports['describe']=function(_0x412ae7,_0x30cfbb){return db[_0x93f1('0x2a')][_0x93f1('0x5b')]()[_0x93f1('0x23')](respondWithResult(_0x30cfbb,null))[_0x93f1('0x52')](handleError(_0x30cfbb,null));};exports[_0x93f1('0x24')]=function(_0x4f1dd6,_0x30d5ad){var _0x4b10af;return db[_0x93f1('0x2a')]['find']({'where':{'id':_0x4f1dd6[_0x93f1('0x54')]['id']}})[_0x93f1('0x23')](handleEntityNotFound(_0x30d5ad,null))[_0x93f1('0x23')](function(_0x360ff7){if(_0x360ff7){return _0x360ff7[_0x93f1('0x24')]()[_0x93f1('0x23')](function(_0x2dd043){return _0x2dd043;});}})[_0x93f1('0x23')](function(_0x46daff){_0x4b10af=_0x46daff;if(_0x46daff){if(_0x46daff[_0x93f1('0x5c')]||_0x46daff[_0x93f1('0x5d')]){return db[_0x46daff[_0x93f1('0x5c')]?'VoiceQueue':'Campaign']['find']({'where':{'id':_0x46daff[_0x93f1('0x5c')]?_0x46daff[_0x93f1('0x5c')]:_0x46daff['CampaignId']},'attributes':['id','name'],'raw':!![]});}}})['then'](function(_0x56fae1){var _0x5cdc75={};if(_0x56fae1){_[_0x93f1('0x4d')](_0x5cdc75,_[_0x93f1('0x5e')](_0x4b10af[_0x93f1('0x5f')],['createdAt',_0x93f1('0x60'),'id']));_0x5cdc75['state']=0x15;_0x5cdc75['statedesc']='deleted';_0x5cdc75[_0x93f1('0x61')]=_0x56fae1['name'];_0x5cdc75[_0x93f1('0x62')]=_0x4f1dd6['user'][_0x93f1('0x27')];_0x5cdc75[_0x93f1('0x63')]=_0x4f1dd6[_0x93f1('0x64')]['id'];_0x5cdc75['deleted']=!![];_0x5cdc75[_0x93f1('0x65')]=moment()['format'](_0x93f1('0x66'));_0x5cdc75['calleridnum']=_0x4b10af[_0x93f1('0x67')];_0x5cdc75[_0x93f1('0x68')]=_0x4b10af[_0x93f1('0x5c')]?'queue':_0x93f1('0x69');return db[_0x93f1('0x6a')][_0x93f1('0x57')]({'where':{'id':_0x5cdc75[_0x93f1('0x6b')]},'attributes':[_0x93f1('0x6c'),_0x93f1('0x6d')]})[_0x93f1('0x23')](function(_0x1cb497){if(!_[_0x93f1('0x6e')](_0x30d5ad)&&!_[_0x93f1('0x6e')](_0x1cb497[_0x93f1('0x6c')])){_0x5cdc75[_0x93f1('0x6f')]=_0x1cb497[_0x93f1('0x6c')];if(!_[_0x93f1('0x6e')](_0x1cb497[_0x93f1('0x6d')])){_0x5cdc75[_0x93f1('0x6f')]+='\x20'+_0x1cb497[_0x93f1('0x6d')];}}})[_0x93f1('0x70')](function(){return db['CmHopperFinal'][_0x93f1('0x58')](_0x5cdc75);});}})[_0x93f1('0x23')](function(){return _0x30d5ad[_0x93f1('0x1a')](0xcc)[_0x93f1('0x1b')]();})[_0x93f1('0x52')](handleError(_0x30d5ad,null));};exports['getPreview']=function(_0x283b40,_0x45155c,_0x134d79){var _0x2195bf=[];return db[_0x93f1('0x6a')]['describe']()['then'](function(_0x1039ea){_0x2195bf=_[_0x93f1('0x30')](_0x1039ea);return _0x283b40[_0x93f1('0x64')][_0x93f1('0x71')]({'where':{'type':_0x93f1('0x72'),'dialActive':!![],'dialMethod':'preview'},'attributes':['id',_0x93f1('0x73')],'raw':!![]});})[_0x93f1('0x23')](function(_0x47b56a){if(_0x47b56a&&_0x47b56a['length']){return db[_0x93f1('0x74')][_0x93f1('0x75')](function(_0x43043c){var _0x35232e={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x93f1('0x76')]('YYYY-MM-DD\x20HH:mm:ss')},'VoiceQueueId':{'$in':_['map'](_0x47b56a,'id')},'$or':[{'UserId':null},{'UserId':_0x283b40[_0x93f1('0x64')]['id']}]};var _0x1b7b0a=[];if(_0x283b40[_0x93f1('0x59')][_0x93f1('0x77')]&&_0x283b40['body'][_0x93f1('0x77')]===_0x93f1('0x78')){if(!_[_0x93f1('0x79')](_0x283b40[_0x93f1('0x59')]['hopperIds'])){_0x35232e['id']={'$in':_0x283b40[_0x93f1('0x59')][_0x93f1('0x7a')]};}_0x1b7b0a=[[_0x93f1('0x7b'),'ASC'],['scheduledAt',_0x47b56a[0x0][_0x93f1('0x73')]===_0x93f1('0x7c')?'DESC':'ASC'],db[_0x93f1('0x74')][_0x93f1('0x7d')](_0x93f1('0x7e')),['id',_0x93f1('0x7f')]];}else{if(!_[_0x93f1('0x79')](_0x283b40['body']['hopperIds'])){_0x35232e['id']={'$notIn':_0x283b40[_0x93f1('0x59')][_0x93f1('0x7a')]};}_0x1b7b0a=[[_0x93f1('0x7b'),_0x93f1('0x7f')],['scheduledAt',_0x47b56a[0x0]['dialOrderByScheduledAt']],db['sequelize'][_0x93f1('0x7d')](_0x93f1('0x80')),['id',_0x93f1('0x7c')]];}return db[_0x93f1('0x2a')][_0x93f1('0x81')]({'attributes':['id',_0x93f1('0x82')],'where':_0x35232e,'order':_0x1b7b0a,'transaction':_0x43043c,'lock':_0x43043c[_0x93f1('0x83')]['UPDATE'],'include':[{'model':db[_0x93f1('0x6a')],'as':_0x93f1('0x84'),'attributes':_0x2195bf,'include':[{'model':db['CmCompany'],'as':_0x93f1('0x85'),'attributes':['id',_0x93f1('0x27')]}]},{'model':db[_0x93f1('0x86')],'as':'List','attributes':['id',_0x93f1('0x27')]},{'model':db['VoiceQueue'],'as':_0x93f1('0x87'),'attributes':['id',_0x93f1('0x27'),'dialActive']}]})[_0x93f1('0x23')](function(_0x5c5390){if(_0x5c5390){return _0x5c5390[_0x93f1('0x5a')]({'UserId':_0x283b40[_0x93f1('0x64')]['id']},{'transaction':_0x43043c});}});});}})[_0x93f1('0x23')](handleEntityNotFound(_0x45155c,null))[_0x93f1('0x23')](respondWithResult(_0x45155c,null))['catch'](handleError(_0x45155c,null));};exports[_0x93f1('0x88')]=function(_0x1430ab,_0x5800fe,_0x19d626){return _0x1430ab[_0x93f1('0x64')][_0x93f1('0x71')]({'where':{'type':_0x93f1('0x72'),'dialActive':!![],'dialMethod':_0x93f1('0x89')},'attributes':['id'],'raw':!![]})[_0x93f1('0x23')](function(_0xddc479){if(_0xddc479&&_0xddc479[_0x93f1('0x46')]){return db[_0x93f1('0x2a')][_0x93f1('0x1d')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x93f1('0x76')](_0x93f1('0x66'))},'VoiceQueueId':{'$in':_['map'](_0xddc479,'id')},'$or':[{'UserId':null},{'UserId':_0x1430ab[_0x93f1('0x64')]['id']}]}});}return 0x0;})[_0x93f1('0x23')](function(_0x5d769e){if(!_[_0x93f1('0x6e')](_0x5d769e)&&_[_0x93f1('0x8a')](_0x5d769e)){return _0x5800fe[_0x93f1('0x1a')](0xc8)[_0x93f1('0x1c')]({'contacts':_0x5d769e});}else{logger['error'](_0x93f1('0x8b'),_0x93f1('0x88'),0x194,'entity\x20not\x20found');_0x5800fe[_0x93f1('0x19')](0x194);}})[_0x93f1('0x52')](handleError(_0x5800fe,null));};