cc30684a22576223af099011448eb062f72e7f26
[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 _0xe198=['key','model','query','keys','filters','intersection','concat','$or','$gt','$gte','$lte','$ne','$eq','$not','$between','$notBetween','$notIn','$like','$notLike','$iLike','$notILike','$overlap','$any','attributes','fields','hasOwnProperty','order','sort','pick','filter','where','merge','VIRTUAL','options','findAll','catch','show','length','includeAll','include','find','create','body','update','describe','destroy','params','VoiceQueueId','CampaignId','VoiceQueue','Campaign','omit','dataValues','createdAt','updatedAt','state','statedesc','deleted','campaign','membername','user','format','calleridnum','campaigntype','queue','ivr','CmContact','ContactId','firstName','isNil','lastName','calleridname','finally','CmHopperAdditionalPhone','end','preview','dialOrderByScheduledAt','sequelize','transaction','YYYY-MM-DD\x20HH:mm:ss','direction','prev','isEmpty','hopperIds','priority','DESC','ASC','literal','scheduledAt','COALESCE(CmHopper.UserId,\x200)\x20DESC','findOne','phone','recallme','LOCK','UPDATE','Contact','CmCompany','Company','List','Queue','getPreviewRecall','CmList','dialActive','getOpenContacts','getVoiceQueues','outbound','CmHopper,\x20%s,\x20%s,\x20%s','getTimezones','push','utcOffset','getTimezone','moment','lodash','api','../../config/environment','../../mysqldb','status','json','offset','undefined','limit','count','set','Content-Range','then','sendStatus','error','name','send','index','map','CmHopper','rawAttributes','fieldName','type'];(function(_0xe66cf2,_0x32ba5f){var _0x2d84e7=function(_0x58a7d8){while(--_0x58a7d8){_0xe66cf2['push'](_0xe66cf2['shift']());}};_0x2d84e7(++_0x32ba5f);}(_0xe198,0x6f));var _0x8e19=function(_0x4fde08,_0x463a36){_0x4fde08=_0x4fde08-0x0;var _0x12e725=_0xe198[_0x4fde08];return _0x12e725;};'use strict';var moment=require(_0x8e19('0x0'));var _=require(_0x8e19('0x1'));var qs=require('../../components/parsers/qs');var logger=require('../../config/logger')(_0x8e19('0x2'));var shared=require(_0x8e19('0x3'));var db=require(_0x8e19('0x4'))['db'];function respondWithResult(_0x529200,_0x32f52f){_0x32f52f=_0x32f52f||0xc8;return function(_0x4e3f58){if(_0x4e3f58){return _0x529200[_0x8e19('0x5')](_0x32f52f)[_0x8e19('0x6')](_0x4e3f58);}};}function respondWithFilteredResult(_0x2fb138,_0x188ce0){return function(_0x1175d6){if(_0x1175d6){var _0x4db35e=typeof _0x188ce0[_0x8e19('0x7')]===_0x8e19('0x8')&&typeof _0x188ce0[_0x8e19('0x9')]===_0x8e19('0x8');var _0x3fbe35=_0x1175d6[_0x8e19('0xa')];var _0x375054=_0x4db35e?0x0:_0x188ce0['offset'];var _0x146989=_0x4db35e?_0x1175d6[_0x8e19('0xa')]:_0x188ce0[_0x8e19('0x7')]+_0x188ce0[_0x8e19('0x9')];var _0x358d90;if(_0x146989>=_0x3fbe35){_0x146989=_0x3fbe35;_0x358d90=0xc8;}else{_0x358d90=0xce;}_0x2fb138[_0x8e19('0x5')](_0x358d90);return _0x2fb138[_0x8e19('0xb')](_0x8e19('0xc'),_0x375054+'-'+_0x146989+'/'+_0x3fbe35)['json'](_0x1175d6);}return null;};}function saveUpdates(_0x51a0c4,_0x3f4a6d){return function(_0x4b1b8d){if(_0x4b1b8d){return _0x4b1b8d['update'](_0x51a0c4)[_0x8e19('0xd')](function(_0x2c2d05){return _0x2c2d05;});}return null;};}function handleEntityNotFound(_0x16b584,_0x76ccdb){return function(_0x4a75e0){if(!_0x4a75e0){_0x16b584[_0x8e19('0xe')](0x194);}return _0x4a75e0;};}function handleError(_0x32f3ef,_0x5c474d){_0x5c474d=_0x5c474d||0x1f4;return function(_0x34a7c9){logger[_0x8e19('0xf')](_0x34a7c9['stack']);if(_0x34a7c9[_0x8e19('0x10')]){delete _0x34a7c9[_0x8e19('0x10')];}_0x32f3ef[_0x8e19('0x5')](_0x5c474d)[_0x8e19('0x11')](_0x34a7c9);};}exports[_0x8e19('0x12')]=function(_0x2e61e9,_0x27be43){var _0x1dde49={},_0x28d288={},_0x1c64f9={'count':0x0,'rows':[]};var _0x18a9d0=_[_0x8e19('0x13')](db[_0x8e19('0x14')][_0x8e19('0x15')],function(_0x4aa341){return{'name':_0x4aa341[_0x8e19('0x16')],'type':_0x4aa341[_0x8e19('0x17')][_0x8e19('0x18')]};});_0x28d288[_0x8e19('0x19')]=_[_0x8e19('0x13')](_0x18a9d0,_0x8e19('0x10'));_0x28d288[_0x8e19('0x1a')]=_[_0x8e19('0x1b')](_0x2e61e9[_0x8e19('0x1a')]);_0x28d288[_0x8e19('0x1c')]=_[_0x8e19('0x1d')](_0x28d288[_0x8e19('0x19')],_0x28d288[_0x8e19('0x1a')]);_0x28d288['filters']=_[_0x8e19('0x1e')](_0x28d288['filters'],['$and',_0x8e19('0x1f'),_0x8e19('0x20'),_0x8e19('0x21'),'$lt',_0x8e19('0x22'),_0x8e19('0x23'),_0x8e19('0x24'),_0x8e19('0x25'),_0x8e19('0x26'),_0x8e19('0x27'),'$in',_0x8e19('0x28'),_0x8e19('0x29'),_0x8e19('0x2a'),_0x8e19('0x2b'),_0x8e19('0x2c'),_0x8e19('0x29'),_0x8e19('0x2d'),'$contains','$contained',_0x8e19('0x2e')]);_0x1dde49[_0x8e19('0x2f')]=_[_0x8e19('0x1d')](_0x28d288[_0x8e19('0x19')],qs[_0x8e19('0x30')](_0x2e61e9[_0x8e19('0x1a')]['fields']));_0x1dde49['attributes']=_0x1dde49[_0x8e19('0x2f')]['length']?_0x1dde49[_0x8e19('0x2f')]:_0x28d288[_0x8e19('0x19')];if(!_0x2e61e9[_0x8e19('0x1a')][_0x8e19('0x31')]('nolimit')){_0x1dde49[_0x8e19('0x9')]=qs[_0x8e19('0x9')](_0x2e61e9['query']['limit']);_0x1dde49[_0x8e19('0x7')]=qs[_0x8e19('0x7')](_0x2e61e9[_0x8e19('0x1a')][_0x8e19('0x7')]);}_0x1dde49[_0x8e19('0x32')]=qs[_0x8e19('0x33')](_0x2e61e9[_0x8e19('0x1a')][_0x8e19('0x33')]);_0x1dde49['where']=qs[_0x8e19('0x1c')](_[_0x8e19('0x34')](_0x2e61e9['query'],_0x28d288[_0x8e19('0x1c')]),_0x18a9d0);if(_0x2e61e9[_0x8e19('0x1a')][_0x8e19('0x35')]){_0x1dde49[_0x8e19('0x36')]=_[_0x8e19('0x37')](_0x1dde49[_0x8e19('0x36')],{'$or':_[_0x8e19('0x13')](_0x18a9d0,function(_0x547368){if(_0x547368[_0x8e19('0x17')]!==_0x8e19('0x38')){var _0x398b9d={};_0x398b9d[_0x547368[_0x8e19('0x10')]]={'$like':'%'+_0x2e61e9[_0x8e19('0x1a')][_0x8e19('0x35')]+'%'};return _0x398b9d;}})});}_0x1dde49=_[_0x8e19('0x37')]({},_0x1dde49,_0x2e61e9[_0x8e19('0x39')]);var _0x15e4e6={'where':_0x1dde49[_0x8e19('0x36')]};return db[_0x8e19('0x14')][_0x8e19('0xa')](_0x15e4e6)['then'](function(_0x841669){_0x1c64f9['count']=_0x841669;if(_0x2e61e9['query']['includeAll']){_0x1dde49['include']=[{'all':!![]}];}return db[_0x8e19('0x14')][_0x8e19('0x3a')](_0x1dde49);})[_0x8e19('0xd')](function(_0xc15b5){_0x1c64f9['rows']=_0xc15b5;return _0x1c64f9;})[_0x8e19('0xd')](respondWithFilteredResult(_0x27be43,_0x1dde49))[_0x8e19('0x3b')](handleError(_0x27be43,null));};exports[_0x8e19('0x3c')]=function(_0x39aee0,_0x40bab6){var _0x1aa40b={'raw':!![],'where':{'id':_0x39aee0['params']['id']}},_0x5b71bb={};_0x5b71bb[_0x8e19('0x19')]=_[_0x8e19('0x1b')](db[_0x8e19('0x14')][_0x8e19('0x15')]);_0x5b71bb['query']=_[_0x8e19('0x1b')](_0x39aee0[_0x8e19('0x1a')]);_0x5b71bb['filters']=_[_0x8e19('0x1d')](_0x5b71bb[_0x8e19('0x19')],_0x5b71bb[_0x8e19('0x1a')]);_0x1aa40b['attributes']=_['intersection'](_0x5b71bb[_0x8e19('0x19')],qs[_0x8e19('0x30')](_0x39aee0[_0x8e19('0x1a')]['fields']));_0x1aa40b[_0x8e19('0x2f')]=_0x1aa40b[_0x8e19('0x2f')][_0x8e19('0x3d')]?_0x1aa40b[_0x8e19('0x2f')]:_0x5b71bb['model'];if(_0x39aee0[_0x8e19('0x1a')][_0x8e19('0x3e')]){_0x1aa40b[_0x8e19('0x3f')]=[{'all':!![]}];}_0x1aa40b=_[_0x8e19('0x37')]({},_0x1aa40b,_0x39aee0['options']);return db[_0x8e19('0x14')][_0x8e19('0x40')](_0x1aa40b)[_0x8e19('0xd')](handleEntityNotFound(_0x40bab6,null))[_0x8e19('0xd')](respondWithResult(_0x40bab6,null))[_0x8e19('0x3b')](handleError(_0x40bab6,null));};exports[_0x8e19('0x41')]=function(_0x1042d7,_0x118b2b){return db[_0x8e19('0x14')]['create'](_0x1042d7[_0x8e19('0x42')],{})['then'](respondWithResult(_0x118b2b,0xc9))[_0x8e19('0x3b')](handleError(_0x118b2b,null));};exports[_0x8e19('0x43')]=function(_0x2324f8,_0x241104){if(_0x2324f8[_0x8e19('0x42')]['id']){delete _0x2324f8[_0x8e19('0x42')]['id'];}return db[_0x8e19('0x14')][_0x8e19('0x40')]({'where':{'id':_0x2324f8['params']['id']}})[_0x8e19('0xd')](handleEntityNotFound(_0x241104,null))[_0x8e19('0xd')](saveUpdates(_0x2324f8[_0x8e19('0x42')],null))[_0x8e19('0xd')](respondWithResult(_0x241104,null))[_0x8e19('0x3b')](handleError(_0x241104,null));};exports[_0x8e19('0x44')]=function(_0x1bb5c8,_0x3edb29){return db[_0x8e19('0x14')]['describe']()[_0x8e19('0xd')](respondWithResult(_0x3edb29,null))[_0x8e19('0x3b')](handleError(_0x3edb29,null));};exports[_0x8e19('0x45')]=function(_0x19831c,_0xe30216){var _0xd9fa7d;var _0x3445cc=_0x19831c['params']['id'];return db['CmHopper']['find']({'where':{'id':_0x19831c[_0x8e19('0x46')]['id']}})[_0x8e19('0xd')](handleEntityNotFound(_0xe30216,null))[_0x8e19('0xd')](function(_0x271502){if(_0x271502){return _0x271502[_0x8e19('0x45')]()['then'](function(_0x2aaf42){return _0x2aaf42;});}})[_0x8e19('0xd')](function(_0x63e5db){_0xd9fa7d=_0x63e5db;if(_0x63e5db){if(_0x63e5db[_0x8e19('0x47')]||_0x63e5db[_0x8e19('0x48')]){return db[_0x63e5db[_0x8e19('0x47')]?_0x8e19('0x49'):_0x8e19('0x4a')]['find']({'where':{'id':_0x63e5db['VoiceQueueId']?_0x63e5db[_0x8e19('0x47')]:_0x63e5db[_0x8e19('0x48')]},'attributes':['id',_0x8e19('0x10')],'raw':!![]});}}})[_0x8e19('0xd')](function(_0x3197d1){var _0x3bdc42={};if(_0x3197d1){_[_0x8e19('0x37')](_0x3bdc42,_[_0x8e19('0x4b')](_0xd9fa7d[_0x8e19('0x4c')],[_0x8e19('0x4d'),_0x8e19('0x4e'),'id']));_0x3bdc42[_0x8e19('0x4f')]=0x15;_0x3bdc42[_0x8e19('0x50')]=_0x8e19('0x51');_0x3bdc42[_0x8e19('0x52')]=_0x3197d1[_0x8e19('0x10')];_0x3bdc42[_0x8e19('0x53')]=_0x19831c[_0x8e19('0x54')]['name'];_0x3bdc42['UserId']=_0x19831c[_0x8e19('0x54')]['id'];_0x3bdc42[_0x8e19('0x51')]=!![];_0x3bdc42['deletedat']=moment()[_0x8e19('0x55')]('YYYY-MM-DD\x20HH:mm:ss');_0x3bdc42[_0x8e19('0x56')]=_0xd9fa7d['phone'];_0x3bdc42[_0x8e19('0x57')]=_0xd9fa7d[_0x8e19('0x47')]?_0x8e19('0x58'):_0x8e19('0x59');return db[_0x8e19('0x5a')][_0x8e19('0x40')]({'where':{'id':_0x3bdc42[_0x8e19('0x5b')]},'attributes':[_0x8e19('0x5c'),'lastName']})[_0x8e19('0xd')](function(_0xdbc7fa){if(!_[_0x8e19('0x5d')](_0xe30216)&&!_[_0x8e19('0x5d')](_0xdbc7fa[_0x8e19('0x5c')])){_0x3bdc42['calleridname']=_0xdbc7fa[_0x8e19('0x5c')];if(!_['isNil'](_0xdbc7fa[_0x8e19('0x5e')])){_0x3bdc42[_0x8e19('0x5f')]+='\x20'+_0xdbc7fa[_0x8e19('0x5e')];}}})[_0x8e19('0x60')](function(){return db['CmHopperFinal']['create'](_0x3bdc42);});}})[_0x8e19('0xd')](function(_0x568302){return db[_0x8e19('0x61')][_0x8e19('0x45')]({'where':{'CmHopperId':_0x3445cc}});})[_0x8e19('0xd')](function(){return _0xe30216[_0x8e19('0x5')](0xcc)[_0x8e19('0x62')]();})[_0x8e19('0x3b')](handleError(_0xe30216,null));};exports['getPreview']=function(_0x4e63e4,_0x2cab3b){var _0xca2cab=[];return db[_0x8e19('0x5a')][_0x8e19('0x44')]()[_0x8e19('0xd')](function(_0x238783){_0xca2cab=_['keys'](_0x238783);return _0x4e63e4['user']['getVoiceQueues']({'where':{'type':'outbound','dialActive':!![],'dialMethod':_0x8e19('0x63')},'attributes':['id',_0x8e19('0x64'),'dialPreviewAutoRecallMe'],'raw':!![]});})[_0x8e19('0xd')](function(_0x17f614){if(_0x17f614&&_0x17f614['length']){return db[_0x8e19('0x65')][_0x8e19('0x66')](function(_0x50308e){var _0x550da4={'active':![],'recallme':![],'scheduledAt':{'$lte':moment()[_0x8e19('0x55')](_0x8e19('0x67'))},'VoiceQueueId':{'$in':_[_0x8e19('0x13')](_0x17f614,'id')},'$or':[{'UserId':null},{'UserId':_0x4e63e4[_0x8e19('0x54')]['id']}]};var _0x5cb961={'active':![],'recallme':!![],'VoiceQueueId':{'$in':_(_0x17f614)[_0x8e19('0x35')](['dialPreviewAutoRecallMe',0x0])[_0x8e19('0x13')]('id')['value']()},'$or':[{'UserId':null},{'UserId':_0x4e63e4['user']['id']}]};var _0x1e9cd2={'$or':[_0x550da4,_0x5cb961]};var _0x27c021=[];if(_0x4e63e4['body'][_0x8e19('0x68')]&&_0x4e63e4[_0x8e19('0x42')][_0x8e19('0x68')]===_0x8e19('0x69')){if(!_[_0x8e19('0x6a')](_0x4e63e4['body']['hopperIds'])){_0x1e9cd2['id']={'$in':_0x4e63e4[_0x8e19('0x42')][_0x8e19('0x6b')]};}_0x27c021=[[_0x8e19('0x6c'),'ASC'],['scheduledAt',_0x17f614[0x0][_0x8e19('0x64')]==='ASC'?_0x8e19('0x6d'):_0x8e19('0x6e')],db['sequelize'][_0x8e19('0x6f')]('COALESCE(CmHopper.UserId,\x200)\x20ASC'),['id','DESC']];}else{if(!_[_0x8e19('0x6a')](_0x4e63e4[_0x8e19('0x42')]['hopperIds'])){_0x1e9cd2['id']={'$notIn':_0x4e63e4[_0x8e19('0x42')]['hopperIds']};}_0x27c021=[[_0x8e19('0x6c'),_0x8e19('0x6d')],[_0x8e19('0x70'),_0x17f614[0x0][_0x8e19('0x64')]],db[_0x8e19('0x65')][_0x8e19('0x6f')](_0x8e19('0x71')),['id',_0x8e19('0x6e')]];}return db[_0x8e19('0x14')][_0x8e19('0x72')]({'attributes':['id','scheduledat',_0x8e19('0x73'),_0x8e19('0x74')],'where':_0x1e9cd2,'order':_0x27c021,'transaction':_0x50308e,'lock':_0x50308e[_0x8e19('0x75')][_0x8e19('0x76')],'include':[{'model':db[_0x8e19('0x5a')],'as':_0x8e19('0x77'),'attributes':_0xca2cab,'include':[{'model':db[_0x8e19('0x78')],'as':_0x8e19('0x79'),'attributes':['id',_0x8e19('0x10')]}]},{'model':db['CmList'],'as':_0x8e19('0x7a'),'attributes':['id',_0x8e19('0x10')]},{'model':db[_0x8e19('0x49')],'as':_0x8e19('0x7b'),'attributes':['id',_0x8e19('0x10'),'dialActive']}]})[_0x8e19('0xd')](function(_0x1235aa){if(_0x1235aa){return _0x1235aa[_0x8e19('0x43')]({'UserId':_0x4e63e4[_0x8e19('0x54')]['id']},{'transaction':_0x50308e});}});});}})[_0x8e19('0xd')](handleEntityNotFound(_0x2cab3b,null))[_0x8e19('0xd')](respondWithResult(_0x2cab3b,null))['catch'](handleError(_0x2cab3b,null));};exports[_0x8e19('0x7c')]=function(_0x37b474,_0x8fddd2,_0x50c11a){var _0x1b7a5a=[];var _0x39f694=_0x37b474[_0x8e19('0x46')]['id'];var _0x1f0d29=_0x37b474['user']['id'];return db['CmContact'][_0x8e19('0x44')]()[_0x8e19('0xd')](function(_0x573ab9){_0x1b7a5a=_[_0x8e19('0x1b')](_0x573ab9);return db['sequelize'][_0x8e19('0x66')](function(_0x1f4e62){const _0x3e9d2d={'id':_0x39f694};return db[_0x8e19('0x14')][_0x8e19('0x72')]({'attributes':['id','scheduledat',_0x8e19('0x74')],'where':_0x3e9d2d,'transaction':_0x1f4e62,'lock':_0x1f4e62[_0x8e19('0x75')]['UPDATE'],'include':[{'model':db[_0x8e19('0x5a')],'as':'Contact','attributes':_0x1b7a5a,'include':[{'model':db[_0x8e19('0x78')],'as':_0x8e19('0x79'),'attributes':['id','name']}]},{'model':db[_0x8e19('0x7d')],'as':'List','attributes':['id',_0x8e19('0x10')]},{'model':db[_0x8e19('0x49')],'as':_0x8e19('0x7b'),'attributes':['id','name',_0x8e19('0x7e')]}]})[_0x8e19('0xd')](function(_0x228c0c){if(_0x228c0c){return _0x228c0c[_0x8e19('0x43')]({'UserId':_0x1f0d29},{'transaction':_0x1f4e62});}});});})['then'](handleEntityNotFound(_0x8fddd2,null))[_0x8e19('0xd')](respondWithResult(_0x8fddd2,null))[_0x8e19('0x3b')](handleError(_0x8fddd2,null));};exports[_0x8e19('0x7f')]=function(_0x4af320,_0x3e0480){return _0x4af320[_0x8e19('0x54')][_0x8e19('0x80')]({'where':{'type':_0x8e19('0x81'),'dialActive':!![],'dialMethod':_0x8e19('0x63')},'attributes':['id'],'raw':!![]})['then'](function(_0x3af920){if(_0x3af920&&_0x3af920[_0x8e19('0x3d')]){return db[_0x8e19('0x14')][_0x8e19('0xa')]({'where':{'active':![],'recallme':![],'scheduledAt':{'$lte':moment()['format'](_0x8e19('0x67'))},'VoiceQueueId':{'$in':_[_0x8e19('0x13')](_0x3af920,'id')},'$or':[{'UserId':null},{'UserId':_0x4af320[_0x8e19('0x54')]['id']}]}});}return 0x0;})[_0x8e19('0xd')](function(_0x4030c0){if(!_[_0x8e19('0x5d')](_0x4030c0)&&_['isNumber'](_0x4030c0)){return _0x3e0480[_0x8e19('0x5')](0xc8)['json']({'contacts':_0x4030c0});}else{logger[_0x8e19('0xf')](_0x8e19('0x82'),_0x8e19('0x7f'),0x194,'entity\x20not\x20found');_0x3e0480[_0x8e19('0xe')](0x194);}})[_0x8e19('0x3b')](handleError(_0x3e0480,null));};exports[_0x8e19('0x83')]=function(_0x45d292,_0x5a0465){var _0x140022=shared['timezones'];var _0x2f4637=[];_[_0x8e19('0x13')](_0x140022,function(_0x5fafca){_0x2f4637[_0x8e19('0x84')]({'id':_0x5fafca[_0x8e19('0x10')],'name':_0x5fafca['name'],'utcOffset':_0x5fafca[_0x8e19('0x85')]});});return _0x5a0465[_0x8e19('0x5')](0xc8)[_0x8e19('0x6')]({'tz':_0x2f4637});};exports[_0x8e19('0x86')]=function(_0x572868,_0x330096){var _0x3ad259=shared['timezones'];var _0x284368={};if(!_['isNil'](_0x3ad259[_0x572868[_0x8e19('0x42')]['id']]))_0x284368=_0x3ad259[_0x572868[_0x8e19('0x42')]['id']];return _0x330096['status'](0xc8)['json'](_0x284368);};