Built motion from commit (unavailable).|2.6.0
[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 _0x7f85=['mustache','util','sox','ejs','fs-extra','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/license/util','../../mysqldb','end','status','offset','undefined','count','limit','set','Content-Range','json','reject','save','update','destroy','then','sendStatus','error','name','send','index','map','CmHopper','fieldName','type','query','keys','filters','intersection','model','$and','$or','$gte','$lte','$ne','$between','$notBetween','$in','$notIn','$like','$notLike','$notILike','$overlap','$contains','$contained','$any','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','include','findAll','catch','show','params','rawAttributes','find','create','body','describe','VoiceQueueId','CampaignId','VoiceQueue','Campaign','omit','dataValues','createdAt','updatedAt','state','statedesc','deleted','membername','user','deletedat','format','calleridnum','campaigntype','queue','ivr','ContactId','firstName','isNil','lastName','calleridname','finally','CmHopperAdditionalPhone','getPreview','CmContact','outbound','dialOrderByScheduledAt','prev','hopperIds','priority','ASC','scheduledAt','DESC','sequelize','literal','isEmpty','COALESCE(CmHopper.UserId,\x200)\x20DESC','findOne','phone','LOCK','UPDATE','Contact','CmCompany','Company','CmList','Queue','getOpenContacts','getVoiceQueues','preview','YYYY-MM-DD\x20HH:mm:ss','CmHopper,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment'];(function(_0x59ee6f,_0x450239){var _0x4c0979=function(_0x565e92){while(--_0x565e92){_0x59ee6f['push'](_0x59ee6f['shift']());}};_0x4c0979(++_0x450239);}(_0x7f85,0x120));var _0x57f8=function(_0x4ef77b,_0x6a3df){_0x4ef77b=_0x4ef77b-0x0;var _0x58d1a6=_0x7f85[_0x4ef77b];return _0x58d1a6;};'use strict';var emlformat=require(_0x57f8('0x0'));var rimraf=require(_0x57f8('0x1'));var zipdir=require(_0x57f8('0x2'));var jsonpatch=require(_0x57f8('0x3'));var rp=require(_0x57f8('0x4'));var moment=require(_0x57f8('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x57f8('0x6'));var util=require(_0x57f8('0x7'));var path=require('path');var sox=require(_0x57f8('0x8'));var csv=require('to-csv');var ejs=require(_0x57f8('0x9'));var fs=require('fs');var fs_extra=require(_0x57f8('0xa'));var _=require('lodash');var squel=require('squel');var crypto=require(_0x57f8('0xb'));var jsforce=require(_0x57f8('0xc'));var deskjs=require(_0x57f8('0xd'));var toCsv=require('to-csv');var querystring=require(_0x57f8('0xe'));var Papa=require(_0x57f8('0xf'));var Redis=require(_0x57f8('0x10'));var authService=require(_0x57f8('0x11'));var qs=require(_0x57f8('0x12'));var as=require(_0x57f8('0x13'));var hardwareService=require(_0x57f8('0x14'));var logger=require('../../config/logger')(_0x57f8('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x57f8('0x16'));var db=require(_0x57f8('0x17'))['db'];function respondWithStatusCode(_0x5df589,_0xfb8bf9){_0xfb8bf9=_0xfb8bf9||0xcc;return function(_0x321713){if(_0x321713){return _0x5df589['sendStatus'](_0xfb8bf9);}return _0x5df589['status'](_0xfb8bf9)[_0x57f8('0x18')]();};}function respondWithResult(_0x299eee,_0x44885c){_0x44885c=_0x44885c||0xc8;return function(_0x1ea16b){if(_0x1ea16b){return _0x299eee[_0x57f8('0x19')](_0x44885c)['json'](_0x1ea16b);}};}function respondWithFilteredResult(_0x13ca5b,_0x4be046){return function(_0x31126e){if(_0x31126e){var _0x13c337=typeof _0x4be046[_0x57f8('0x1a')]===_0x57f8('0x1b')&&typeof _0x4be046['limit']===_0x57f8('0x1b');var _0x531b1d=_0x31126e[_0x57f8('0x1c')];var _0xe2f0c7=_0x13c337?0x0:_0x4be046['offset'];var _0x175217=_0x13c337?_0x31126e[_0x57f8('0x1c')]:_0x4be046['offset']+_0x4be046[_0x57f8('0x1d')];var _0x1554de;if(_0x175217>=_0x531b1d){_0x175217=_0x531b1d;_0x1554de=0xc8;}else{_0x1554de=0xce;}_0x13ca5b[_0x57f8('0x19')](_0x1554de);return _0x13ca5b[_0x57f8('0x1e')](_0x57f8('0x1f'),_0xe2f0c7+'-'+_0x175217+'/'+_0x531b1d)[_0x57f8('0x20')](_0x31126e);}return null;};}function patchUpdates(_0x1bdfe1){return function(_0x1dd414){try{jsonpatch['apply'](_0x1dd414,_0x1bdfe1,!![]);}catch(_0x21866c){return BPromise[_0x57f8('0x21')](_0x21866c);}return _0x1dd414[_0x57f8('0x22')]();};}function saveUpdates(_0x636c86,_0x23ffc7){return function(_0x1c19ac){if(_0x1c19ac){return _0x1c19ac[_0x57f8('0x23')](_0x636c86)['then'](function(_0x1570ed){return _0x1570ed;});}return null;};}function removeEntity(_0x157672,_0x3cae96){return function(_0x76e8fa){if(_0x76e8fa){return _0x76e8fa[_0x57f8('0x24')]()[_0x57f8('0x25')](function(){_0x157672[_0x57f8('0x19')](0xcc)[_0x57f8('0x18')]();});}};}function handleEntityNotFound(_0x433030,_0x1465d3){return function(_0x2570dc){if(!_0x2570dc){_0x433030[_0x57f8('0x26')](0x194);}return _0x2570dc;};}function handleError(_0x300607,_0x58999e){_0x58999e=_0x58999e||0x1f4;return function(_0x4f7b15){logger[_0x57f8('0x27')](_0x4f7b15['stack']);if(_0x4f7b15[_0x57f8('0x28')]){delete _0x4f7b15['name'];}_0x300607['status'](_0x58999e)[_0x57f8('0x29')](_0x4f7b15);};}exports[_0x57f8('0x2a')]=function(_0x165d2a,_0x2419a5){var _0x24c895={},_0x2f005c={},_0x683b29={'count':0x0,'rows':[]};var _0x34f01c=_[_0x57f8('0x2b')](db[_0x57f8('0x2c')]['rawAttributes'],function(_0x417baf){return{'name':_0x417baf[_0x57f8('0x2d')],'type':_0x417baf[_0x57f8('0x2e')]['key']};});_0x2f005c['model']=_['map'](_0x34f01c,_0x57f8('0x28'));_0x2f005c[_0x57f8('0x2f')]=_[_0x57f8('0x30')](_0x165d2a[_0x57f8('0x2f')]);_0x2f005c[_0x57f8('0x31')]=_[_0x57f8('0x32')](_0x2f005c[_0x57f8('0x33')],_0x2f005c['query']);_0x2f005c['filters']=_['concat'](_0x2f005c[_0x57f8('0x31')],[_0x57f8('0x34'),_0x57f8('0x35'),'$gt',_0x57f8('0x36'),'$lt',_0x57f8('0x37'),_0x57f8('0x38'),'$eq','$not',_0x57f8('0x39'),_0x57f8('0x3a'),_0x57f8('0x3b'),_0x57f8('0x3c'),_0x57f8('0x3d'),_0x57f8('0x3e'),'$iLike',_0x57f8('0x3f'),'$like',_0x57f8('0x40'),_0x57f8('0x41'),_0x57f8('0x42'),_0x57f8('0x43')]);_0x24c895['attributes']=_['intersection'](_0x2f005c[_0x57f8('0x33')],qs[_0x57f8('0x44')](_0x165d2a[_0x57f8('0x2f')][_0x57f8('0x44')]));_0x24c895[_0x57f8('0x45')]=_0x24c895[_0x57f8('0x45')][_0x57f8('0x46')]?_0x24c895[_0x57f8('0x45')]:_0x2f005c[_0x57f8('0x33')];if(!_0x165d2a[_0x57f8('0x2f')][_0x57f8('0x47')](_0x57f8('0x48'))){_0x24c895[_0x57f8('0x1d')]=qs['limit'](_0x165d2a['query'][_0x57f8('0x1d')]);_0x24c895['offset']=qs['offset'](_0x165d2a['query'][_0x57f8('0x1a')]);}_0x24c895[_0x57f8('0x49')]=qs[_0x57f8('0x4a')](_0x165d2a[_0x57f8('0x2f')][_0x57f8('0x4a')]);_0x24c895[_0x57f8('0x4b')]=qs[_0x57f8('0x31')](_[_0x57f8('0x4c')](_0x165d2a[_0x57f8('0x2f')],_0x2f005c['filters']),_0x34f01c);if(_0x165d2a[_0x57f8('0x2f')][_0x57f8('0x4d')]){_0x24c895[_0x57f8('0x4b')]=_['merge'](_0x24c895[_0x57f8('0x4b')],{'$or':_[_0x57f8('0x2b')](_0x34f01c,function(_0x380db9){if(_0x380db9[_0x57f8('0x2e')]!==_0x57f8('0x4e')){var _0x5b30a0={};_0x5b30a0[_0x380db9[_0x57f8('0x28')]]={'$like':'%'+_0x165d2a['query'][_0x57f8('0x4d')]+'%'};return _0x5b30a0;}})});}_0x24c895=_[_0x57f8('0x4f')]({},_0x24c895,_0x165d2a[_0x57f8('0x50')]);var _0x26530e={'where':_0x24c895['where']};return db[_0x57f8('0x2c')][_0x57f8('0x1c')](_0x26530e)[_0x57f8('0x25')](function(_0x45b715){_0x683b29[_0x57f8('0x1c')]=_0x45b715;if(_0x165d2a[_0x57f8('0x2f')][_0x57f8('0x51')]){_0x24c895[_0x57f8('0x52')]=[{'all':!![]}];}return db['CmHopper'][_0x57f8('0x53')](_0x24c895);})[_0x57f8('0x25')](function(_0x309762){_0x683b29['rows']=_0x309762;return _0x683b29;})[_0x57f8('0x25')](respondWithFilteredResult(_0x2419a5,_0x24c895))[_0x57f8('0x54')](handleError(_0x2419a5,null));};exports[_0x57f8('0x55')]=function(_0x1919ea,_0x58d13d){var _0xb90d18={'raw':!![],'where':{'id':_0x1919ea[_0x57f8('0x56')]['id']}},_0x166a8b={};_0x166a8b[_0x57f8('0x33')]=_[_0x57f8('0x30')](db[_0x57f8('0x2c')][_0x57f8('0x57')]);_0x166a8b[_0x57f8('0x2f')]=_[_0x57f8('0x30')](_0x1919ea[_0x57f8('0x2f')]);_0x166a8b[_0x57f8('0x31')]=_[_0x57f8('0x32')](_0x166a8b[_0x57f8('0x33')],_0x166a8b[_0x57f8('0x2f')]);_0xb90d18[_0x57f8('0x45')]=_[_0x57f8('0x32')](_0x166a8b[_0x57f8('0x33')],qs[_0x57f8('0x44')](_0x1919ea['query'][_0x57f8('0x44')]));_0xb90d18['attributes']=_0xb90d18[_0x57f8('0x45')]['length']?_0xb90d18['attributes']:_0x166a8b[_0x57f8('0x33')];if(_0x1919ea[_0x57f8('0x2f')][_0x57f8('0x51')]){_0xb90d18[_0x57f8('0x52')]=[{'all':!![]}];}_0xb90d18=_[_0x57f8('0x4f')]({},_0xb90d18,_0x1919ea[_0x57f8('0x50')]);return db['CmHopper'][_0x57f8('0x58')](_0xb90d18)[_0x57f8('0x25')](handleEntityNotFound(_0x58d13d,null))[_0x57f8('0x25')](respondWithResult(_0x58d13d,null))['catch'](handleError(_0x58d13d,null));};exports[_0x57f8('0x59')]=function(_0x3c674c,_0x56038f){return db[_0x57f8('0x2c')]['create'](_0x3c674c[_0x57f8('0x5a')],{})[_0x57f8('0x25')](respondWithResult(_0x56038f,0xc9))[_0x57f8('0x54')](handleError(_0x56038f,null));};exports[_0x57f8('0x23')]=function(_0x5c379f,_0x22cf73){if(_0x5c379f[_0x57f8('0x5a')]['id']){delete _0x5c379f[_0x57f8('0x5a')]['id'];}return db[_0x57f8('0x2c')][_0x57f8('0x58')]({'where':{'id':_0x5c379f['params']['id']}})[_0x57f8('0x25')](handleEntityNotFound(_0x22cf73,null))['then'](saveUpdates(_0x5c379f[_0x57f8('0x5a')],null))['then'](respondWithResult(_0x22cf73,null))[_0x57f8('0x54')](handleError(_0x22cf73,null));};exports[_0x57f8('0x5b')]=function(_0x43a842,_0x35ddbb){return db[_0x57f8('0x2c')][_0x57f8('0x5b')]()[_0x57f8('0x25')](respondWithResult(_0x35ddbb,null))['catch'](handleError(_0x35ddbb,null));};exports[_0x57f8('0x24')]=function(_0x117086,_0x952b50){var _0x4e491c;var _0x3eafc5=_0x117086[_0x57f8('0x56')]['id'];return db['CmHopper'][_0x57f8('0x58')]({'where':{'id':_0x117086[_0x57f8('0x56')]['id']}})[_0x57f8('0x25')](handleEntityNotFound(_0x952b50,null))['then'](function(_0x5890b7){if(_0x5890b7){return _0x5890b7[_0x57f8('0x24')]()['then'](function(_0x6432a0){return _0x6432a0;});}})['then'](function(_0x46dd4e){_0x4e491c=_0x46dd4e;if(_0x46dd4e){if(_0x46dd4e[_0x57f8('0x5c')]||_0x46dd4e[_0x57f8('0x5d')]){return db[_0x46dd4e[_0x57f8('0x5c')]?_0x57f8('0x5e'):_0x57f8('0x5f')][_0x57f8('0x58')]({'where':{'id':_0x46dd4e[_0x57f8('0x5c')]?_0x46dd4e[_0x57f8('0x5c')]:_0x46dd4e['CampaignId']},'attributes':['id','name'],'raw':!![]});}}})[_0x57f8('0x25')](function(_0xd2759a){var _0x3563f0={};if(_0xd2759a){_['merge'](_0x3563f0,_[_0x57f8('0x60')](_0x4e491c[_0x57f8('0x61')],[_0x57f8('0x62'),_0x57f8('0x63'),'id']));_0x3563f0[_0x57f8('0x64')]=0x15;_0x3563f0[_0x57f8('0x65')]=_0x57f8('0x66');_0x3563f0['campaign']=_0xd2759a['name'];_0x3563f0[_0x57f8('0x67')]=_0x117086['user']['name'];_0x3563f0['UserId']=_0x117086[_0x57f8('0x68')]['id'];_0x3563f0[_0x57f8('0x66')]=!![];_0x3563f0[_0x57f8('0x69')]=moment()[_0x57f8('0x6a')]('YYYY-MM-DD\x20HH:mm:ss');_0x3563f0[_0x57f8('0x6b')]=_0x4e491c['phone'];_0x3563f0[_0x57f8('0x6c')]=_0x4e491c[_0x57f8('0x5c')]?_0x57f8('0x6d'):_0x57f8('0x6e');return db['CmContact'][_0x57f8('0x58')]({'where':{'id':_0x3563f0[_0x57f8('0x6f')]},'attributes':[_0x57f8('0x70'),'lastName']})[_0x57f8('0x25')](function(_0x13cd79){if(!_[_0x57f8('0x71')](_0x952b50)&&!_['isNil'](_0x13cd79[_0x57f8('0x70')])){_0x3563f0['calleridname']=_0x13cd79['firstName'];if(!_['isNil'](_0x13cd79[_0x57f8('0x72')])){_0x3563f0[_0x57f8('0x73')]+='\x20'+_0x13cd79[_0x57f8('0x72')];}}})[_0x57f8('0x74')](function(){return db['CmHopperFinal'][_0x57f8('0x59')](_0x3563f0);});}})[_0x57f8('0x25')](function(_0x398186){return db[_0x57f8('0x75')][_0x57f8('0x24')]({'where':{'CmHopperId':_0x3eafc5}});})[_0x57f8('0x25')](function(){return _0x952b50[_0x57f8('0x19')](0xcc)[_0x57f8('0x18')]();})['catch'](handleError(_0x952b50,null));};exports[_0x57f8('0x76')]=function(_0x123879,_0x504dc8,_0x564487){var _0x22341c=[];return db[_0x57f8('0x77')]['describe']()['then'](function(_0x2b70a5){_0x22341c=_['keys'](_0x2b70a5);return _0x123879[_0x57f8('0x68')]['getVoiceQueues']({'where':{'type':_0x57f8('0x78'),'dialActive':!![],'dialMethod':'preview'},'attributes':['id',_0x57f8('0x79')],'raw':!![]});})[_0x57f8('0x25')](function(_0x1efa2b){if(_0x1efa2b&&_0x1efa2b[_0x57f8('0x46')]){return db['sequelize']['transaction'](function(_0x37ab9d){var _0x5a031e={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x57f8('0x6a')]('YYYY-MM-DD\x20HH:mm:ss')},'VoiceQueueId':{'$in':_[_0x57f8('0x2b')](_0x1efa2b,'id')},'$or':[{'UserId':null},{'UserId':_0x123879[_0x57f8('0x68')]['id']}]};var _0x3a1717=[];if(_0x123879[_0x57f8('0x5a')]['direction']&&_0x123879[_0x57f8('0x5a')]['direction']===_0x57f8('0x7a')){if(!_['isEmpty'](_0x123879[_0x57f8('0x5a')]['hopperIds'])){_0x5a031e['id']={'$in':_0x123879[_0x57f8('0x5a')][_0x57f8('0x7b')]};}_0x3a1717=[[_0x57f8('0x7c'),_0x57f8('0x7d')],[_0x57f8('0x7e'),_0x1efa2b[0x0][_0x57f8('0x79')]==='ASC'?_0x57f8('0x7f'):_0x57f8('0x7d')],db[_0x57f8('0x80')][_0x57f8('0x81')]('COALESCE(CmHopper.UserId,\x200)\x20ASC'),['id',_0x57f8('0x7f')]];}else{if(!_[_0x57f8('0x82')](_0x123879[_0x57f8('0x5a')][_0x57f8('0x7b')])){_0x5a031e['id']={'$notIn':_0x123879[_0x57f8('0x5a')][_0x57f8('0x7b')]};}_0x3a1717=[['priority',_0x57f8('0x7f')],[_0x57f8('0x7e'),_0x1efa2b[0x0][_0x57f8('0x79')]],db['sequelize'][_0x57f8('0x81')](_0x57f8('0x83')),['id','ASC']];}return db[_0x57f8('0x2c')][_0x57f8('0x84')]({'attributes':['id','scheduledat',_0x57f8('0x85')],'where':_0x5a031e,'order':_0x3a1717,'transaction':_0x37ab9d,'lock':_0x37ab9d[_0x57f8('0x86')][_0x57f8('0x87')],'include':[{'model':db[_0x57f8('0x77')],'as':_0x57f8('0x88'),'attributes':_0x22341c,'include':[{'model':db[_0x57f8('0x89')],'as':_0x57f8('0x8a'),'attributes':['id',_0x57f8('0x28')]}]},{'model':db[_0x57f8('0x8b')],'as':'List','attributes':['id',_0x57f8('0x28')]},{'model':db[_0x57f8('0x5e')],'as':_0x57f8('0x8c'),'attributes':['id',_0x57f8('0x28'),'dialActive']}]})[_0x57f8('0x25')](function(_0x451f6d){if(_0x451f6d){return _0x451f6d[_0x57f8('0x23')]({'UserId':_0x123879[_0x57f8('0x68')]['id']},{'transaction':_0x37ab9d});}});});}})[_0x57f8('0x25')](handleEntityNotFound(_0x504dc8,null))[_0x57f8('0x25')](respondWithResult(_0x504dc8,null))[_0x57f8('0x54')](handleError(_0x504dc8,null));};exports[_0x57f8('0x8d')]=function(_0x4aa606,_0x3bc5b3,_0x157c7e){return _0x4aa606[_0x57f8('0x68')][_0x57f8('0x8e')]({'where':{'type':_0x57f8('0x78'),'dialActive':!![],'dialMethod':_0x57f8('0x8f')},'attributes':['id'],'raw':!![]})[_0x57f8('0x25')](function(_0x18c7c7){if(_0x18c7c7&&_0x18c7c7['length']){return db['CmHopper'][_0x57f8('0x1c')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()['format'](_0x57f8('0x90'))},'VoiceQueueId':{'$in':_['map'](_0x18c7c7,'id')},'$or':[{'UserId':null},{'UserId':_0x4aa606[_0x57f8('0x68')]['id']}]}});}return 0x0;})[_0x57f8('0x25')](function(_0x525e5e){if(!_[_0x57f8('0x71')](_0x525e5e)&&_['isNumber'](_0x525e5e)){return _0x3bc5b3[_0x57f8('0x19')](0xc8)[_0x57f8('0x20')]({'contacts':_0x525e5e});}else{logger[_0x57f8('0x27')](_0x57f8('0x91'),_0x57f8('0x8d'),0x194,_0x57f8('0x92'));_0x3bc5b3[_0x57f8('0x26')](0x194);}})[_0x57f8('0x54')](handleError(_0x3bc5b3,null));};