b4161e4cb311adbee16ef45fb933ec997cd8a6f1
[motion2.git] / server / api / cmHopperFinal / cmHopperFinal.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 _0x5ed9=['../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','limit','set','Content-Range','apply','reject','save','update','then','end','error','stack','name','send','map','CmHopperFinal','rawAttributes','fieldName','type','key','keys','attributes','intersection','model','fields','query','length','nolimit','offset','filters','pick','filter','merge','where','VIRTUAL','options','count','includeAll','findAll','catch','show','params','describe','body','uniqueid','countContactsQueueCampaignHopperFinal','COUNT','sequelize','literal','noSuchContacts','`state`\x20=\x205,\x201,\x20null','busyContacts','`state`\x20=\x208,\x201,\x20null','congestionContacts','`state`\x20=\x2010\x20,\x201,\x20null','dropContacts','`state`\x20=\x2011\x20,\x201,\x20null','abandonedContacts','`state`\x20=\x2019,\x201,\x20null','countContactsIvrCampaignHopperFinal','`state`\x20=\x200,\x201,\x20null','`state`\x20=\x203,\x201,\x20null','noAnswerContacts','VoiceQueue','find','insert','into','cm_hopper','fromQuery','scheduledAt','ContactId','ListId','VoiceQueueId','createdAt','updatedAt','field','ANY_VALUE(calleridnum)','phone','NOW()','ANY_VALUE(ContactId)','ANY_VALUE(VoiceQueueId)','from','cm_hopper_final','VoiceQueueId\x20=\x20?','toString','state\x20=\x20?','state','calleridnum\x20IS\x20NOT\x20NULL','select','calleridnum\x20NOT\x20IN\x20(?)','state\x20=\x204','group','moveContactsIvrCampaignHopperFinal','Campaign','CampaignId','ANY_VALUE(ListId)','ANY_VALUE(CampaignId)','CampaignId\x20=\x20?','calleridnum','checkContactHopper','CmHopper','findOne','html-pdf','eml-format','zip-dir','fast-json-patch','request-promise','moment','mustache','util','path','to-csv','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch'];(function(_0x352797,_0x25947f){var _0x46bb5e=function(_0x2721e5){while(--_0x2721e5){_0x352797['push'](_0x352797['shift']());}};_0x46bb5e(++_0x25947f);}(_0x5ed9,0x1e9));var _0x95ed=function(_0x36eaaa,_0x326732){_0x36eaaa=_0x36eaaa-0x0;var _0x461349=_0x5ed9[_0x36eaaa];return _0x461349;};'use strict';var pdf=require(_0x95ed('0x0'));var emlformat=require(_0x95ed('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x95ed('0x2'));var jsonpatch=require(_0x95ed('0x3'));var rp=require(_0x95ed('0x4'));var moment=require(_0x95ed('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x95ed('0x6'));var util=require(_0x95ed('0x7'));var path=require(_0x95ed('0x8'));var sox=require('sox');var csv=require(_0x95ed('0x9'));var ejs=require('ejs');var fs=require('fs');var _=require(_0x95ed('0xa'));var squel=require(_0x95ed('0xb'));var crypto=require(_0x95ed('0xc'));var jsforce=require(_0x95ed('0xd'));var deskjs=require(_0x95ed('0xe'));var toCsv=require(_0x95ed('0x9'));var querystring=require('querystring');var Papa=require(_0x95ed('0xf'));var Redis=require(_0x95ed('0x10'));var authService=require(_0x95ed('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x95ed('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x95ed('0x13'))(_0x95ed('0x14'));var utils=require(_0x95ed('0x15'));var config=require(_0x95ed('0x16'));var db=require(_0x95ed('0x17'))['db'];function respondWithStatusCode(_0x5bccde,_0x5e4571){_0x5e4571=_0x5e4571||0xcc;return function(_0x3da05b){if(_0x3da05b){return _0x5bccde[_0x95ed('0x18')](_0x5e4571);}return _0x5bccde['status'](_0x5e4571)['end']();};}function respondWithResult(_0x2782a5,_0x11671d){_0x11671d=_0x11671d||0xc8;return function(_0x22211f){if(_0x22211f){return _0x2782a5[_0x95ed('0x19')](_0x11671d)['json'](_0x22211f);}};}function respondWithFilteredResult(_0x213355,_0xd3fe73){return function(_0x477b8f){if(_0x477b8f){var _0x597a04=_0x477b8f['count'],_0x5fc6b5=_0xd3fe73['offset'],_0x554c6a=_0xd3fe73['offset']+_0xd3fe73[_0x95ed('0x1a')],_0x23c63f;if(_0x554c6a>=_0x597a04){_0x554c6a=_0x597a04;_0x23c63f=0xc8;}else{_0x23c63f=0xce;}_0x213355['status'](_0x23c63f);return _0x213355[_0x95ed('0x1b')](_0x95ed('0x1c'),_0x5fc6b5+'-'+_0x554c6a+'/'+_0x597a04)['json'](_0x477b8f);}return null;};}function patchUpdates(_0xa5a91){return function(_0x3e9d1e){try{jsonpatch[_0x95ed('0x1d')](_0x3e9d1e,_0xa5a91,!![]);}catch(_0x2a8dc3){return BPromise[_0x95ed('0x1e')](_0x2a8dc3);}return _0x3e9d1e[_0x95ed('0x1f')]();};}function saveUpdates(_0x2d427e,_0x3e05cf){return function(_0x10823b){if(_0x10823b){return _0x10823b[_0x95ed('0x20')](_0x2d427e)['then'](function(_0xd798ec){return _0xd798ec;});}return null;};}function removeEntity(_0x390d09,_0x4e8a5d){return function(_0x5d5afe){if(_0x5d5afe){return _0x5d5afe['destroy']()[_0x95ed('0x21')](function(){_0x390d09[_0x95ed('0x19')](0xcc)[_0x95ed('0x22')]();});}};}function handleEntityNotFound(_0x61d85e,_0x2c0824){return function(_0x19b664){if(!_0x19b664){_0x61d85e[_0x95ed('0x18')](0x194);}return _0x19b664;};}function handleError(_0x416d23,_0x146f74){_0x146f74=_0x146f74||0x1f4;return function(_0x3bcb6d){logger[_0x95ed('0x23')](_0x3bcb6d[_0x95ed('0x24')]);if(_0x3bcb6d[_0x95ed('0x25')]){delete _0x3bcb6d[_0x95ed('0x25')];}_0x416d23['status'](_0x146f74)[_0x95ed('0x26')](_0x3bcb6d);};}exports['index']=function(_0x4c7a2d,_0x2f515d){var _0x2d6df0={},_0x45e403={},_0x3096ad={'count':0x0,'rows':[]};var _0x2d5042=_[_0x95ed('0x27')](db[_0x95ed('0x28')][_0x95ed('0x29')],function(_0x39330d){return{'name':_0x39330d[_0x95ed('0x2a')],'type':_0x39330d[_0x95ed('0x2b')][_0x95ed('0x2c')]};});_0x45e403['model']=_[_0x95ed('0x27')](_0x2d5042,_0x95ed('0x25'));_0x45e403['query']=_[_0x95ed('0x2d')](_0x4c7a2d['query']);_0x45e403['filters']=_['intersection'](_0x45e403['model'],_0x45e403['query']);_0x2d6df0[_0x95ed('0x2e')]=_[_0x95ed('0x2f')](_0x45e403[_0x95ed('0x30')],qs[_0x95ed('0x31')](_0x4c7a2d[_0x95ed('0x32')][_0x95ed('0x31')]));_0x2d6df0[_0x95ed('0x2e')]=_0x2d6df0['attributes'][_0x95ed('0x33')]?_0x2d6df0[_0x95ed('0x2e')]:_0x45e403[_0x95ed('0x30')];if(!_0x4c7a2d['query']['hasOwnProperty'](_0x95ed('0x34'))){_0x2d6df0['limit']=qs['limit'](_0x4c7a2d['query']['limit']);_0x2d6df0[_0x95ed('0x35')]=qs['offset'](_0x4c7a2d[_0x95ed('0x32')][_0x95ed('0x35')]);}_0x2d6df0['order']=qs['sort'](_0x4c7a2d['query']['sort']);_0x2d6df0['where']=qs[_0x95ed('0x36')](_[_0x95ed('0x37')](_0x4c7a2d[_0x95ed('0x32')],_0x45e403[_0x95ed('0x36')]),_0x2d5042);if(_0x4c7a2d[_0x95ed('0x32')][_0x95ed('0x38')]){_0x2d6df0['where']=_[_0x95ed('0x39')](_0x2d6df0[_0x95ed('0x3a')],{'$or':_[_0x95ed('0x27')](_0x2d5042,function(_0x5218a3){if(_0x5218a3[_0x95ed('0x2b')]!==_0x95ed('0x3b')){var _0x355623={};_0x355623[_0x5218a3[_0x95ed('0x25')]]={'$like':'%'+_0x4c7a2d[_0x95ed('0x32')][_0x95ed('0x38')]+'%'};return _0x355623;}})});}_0x2d6df0=_[_0x95ed('0x39')]({},_0x2d6df0,_0x4c7a2d[_0x95ed('0x3c')]);var _0x4f9306={'where':_0x2d6df0['where']};return db['CmHopperFinal'][_0x95ed('0x3d')](_0x4f9306)[_0x95ed('0x21')](function(_0x237e8b){_0x3096ad[_0x95ed('0x3d')]=_0x237e8b;if(_0x4c7a2d[_0x95ed('0x32')][_0x95ed('0x3e')]){_0x2d6df0['include']=[{'all':!![]}];}return db[_0x95ed('0x28')][_0x95ed('0x3f')](_0x2d6df0);})[_0x95ed('0x21')](function(_0x5aeb8b){_0x3096ad['rows']=_0x5aeb8b;return _0x3096ad;})[_0x95ed('0x21')](respondWithFilteredResult(_0x2f515d,_0x2d6df0))[_0x95ed('0x40')](handleError(_0x2f515d,null));};exports[_0x95ed('0x41')]=function(_0x3a81df,_0x3754a7){var _0x5c4c54={'raw':!![],'where':{'id':_0x3a81df[_0x95ed('0x42')]['id']}},_0x5c068f={};_0x5c068f[_0x95ed('0x30')]=_[_0x95ed('0x2d')](db[_0x95ed('0x28')]['rawAttributes']);_0x5c068f[_0x95ed('0x32')]=_['keys'](_0x3a81df[_0x95ed('0x32')]);_0x5c068f[_0x95ed('0x36')]=_[_0x95ed('0x2f')](_0x5c068f['model'],_0x5c068f[_0x95ed('0x32')]);_0x5c4c54[_0x95ed('0x2e')]=_[_0x95ed('0x2f')](_0x5c068f['model'],qs['fields'](_0x3a81df[_0x95ed('0x32')]['fields']));_0x5c4c54[_0x95ed('0x2e')]=_0x5c4c54['attributes'][_0x95ed('0x33')]?_0x5c4c54[_0x95ed('0x2e')]:_0x5c068f[_0x95ed('0x30')];if(_0x3a81df[_0x95ed('0x32')][_0x95ed('0x3e')]){_0x5c4c54['include']=[{'all':!![]}];}_0x5c4c54=_[_0x95ed('0x39')]({},_0x5c4c54,_0x3a81df[_0x95ed('0x3c')]);return db[_0x95ed('0x28')]['find'](_0x5c4c54)[_0x95ed('0x21')](handleEntityNotFound(_0x3754a7,null))['then'](respondWithResult(_0x3754a7,null))[_0x95ed('0x40')](handleError(_0x3754a7,null));};exports[_0x95ed('0x43')]=function(_0x5f2444,_0x34e5e4){return db[_0x95ed('0x28')]['describe']()['then'](respondWithResult(_0x34e5e4,null))[_0x95ed('0x40')](handleError(_0x34e5e4,null));};exports['update']=function(_0x257a23,_0x5288e6){if(_0x257a23[_0x95ed('0x44')][_0x95ed('0x45')]){delete _0x257a23[_0x95ed('0x44')][_0x95ed('0x45')];}return db[_0x95ed('0x28')]['find']({'where':{'uniqueid':_0x257a23[_0x95ed('0x42')]['id']}})[_0x95ed('0x21')](handleEntityNotFound(_0x5288e6,null))[_0x95ed('0x21')](saveUpdates(_0x257a23[_0x95ed('0x44')],null))[_0x95ed('0x21')](respondWithResult(_0x5288e6,null))[_0x95ed('0x40')](handleError(_0x5288e6,null));};exports[_0x95ed('0x46')]=function(_0x26cd04,_0x485ab9,_0x3a77b0){return db[_0x95ed('0x28')][_0x95ed('0x3f')]({'raw':![],'where':{'VoiceQueueId':_0x26cd04[_0x95ed('0x42')]['id']},'attributes':[[db['sequelize']['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db['sequelize'][_0x95ed('0x49')]('`state`\x20=\x200,\x201,\x20null'))),_0x95ed('0x4a')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')]('`state`\x20=\x203,\x201,\x20null'))),'noAnswerContacts'],[db[_0x95ed('0x48')]['fn']('COUNT',db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')]['literal'](_0x95ed('0x4b')))),_0x95ed('0x4c')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db['sequelize']['fn']('IF',db['sequelize']['literal'](_0x95ed('0x4d')))),_0x95ed('0x4e')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')]['literal'](_0x95ed('0x4f')))),_0x95ed('0x50')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db['sequelize']['literal'](_0x95ed('0x51')))),_0x95ed('0x52')],[db['sequelize']['fn']('COUNT',db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')]('`state`\x20=\x2022,\x201,\x20null'))),'reCallContacts'],[db['sequelize']['fn'](_0x95ed('0x47'),db['sequelize']['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')](_0x95ed('0x53')))),'answeringMachine']]})[_0x95ed('0x21')](handleEntityNotFound(_0x485ab9,null))[_0x95ed('0x21')](respondWithResult(_0x485ab9,null))[_0x95ed('0x40')](handleError(_0x485ab9,null));};exports[_0x95ed('0x54')]=function(_0x3e5925,_0x12a7b0,_0x2203c0){return db['CmHopperFinal'][_0x95ed('0x3f')]({'raw':![],'where':{'CampaignId':_0x3e5925[_0x95ed('0x42')]['id']},'attributes':[[db[_0x95ed('0x48')]['fn']('COUNT',db['sequelize']['fn']('IF',db[_0x95ed('0x48')]['literal'](_0x95ed('0x55')))),_0x95ed('0x4a')],[db[_0x95ed('0x48')]['fn']('COUNT',db['sequelize']['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')](_0x95ed('0x56')))),_0x95ed('0x57')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')](_0x95ed('0x4b')))),_0x95ed('0x4c')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db['sequelize']['fn']('IF',db['sequelize'][_0x95ed('0x49')]('`state`\x20=\x208,\x201,\x20null'))),_0x95ed('0x4e')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')](_0x95ed('0x4f')))),_0x95ed('0x50')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db['sequelize'][_0x95ed('0x49')](_0x95ed('0x51')))),_0x95ed('0x52')],[db[_0x95ed('0x48')]['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')]['literal']('`state`\x20=\x2022,\x201,\x20null'))),'reCallContacts'],[db['sequelize']['fn'](_0x95ed('0x47'),db[_0x95ed('0x48')]['fn']('IF',db[_0x95ed('0x48')][_0x95ed('0x49')]('`state`\x20=\x2019,\x201,\x20null'))),'answeringMachine']]})[_0x95ed('0x21')](handleEntityNotFound(_0x12a7b0,null))['then'](respondWithResult(_0x12a7b0,null))[_0x95ed('0x40')](handleError(_0x12a7b0,null));};exports['moveContactsQueueCampaignHopperFinal']=function(_0x4c858f,_0x95e046,_0x2ebbc8){return db[_0x95ed('0x58')][_0x95ed('0x59')]({'where':{'id':_0x4c858f[_0x95ed('0x42')]['id']}})[_0x95ed('0x21')](handleEntityNotFound(_0x95e046,null))[_0x95ed('0x21')](function(_0x36e7e2){var _0x339a9a;if(_0x36e7e2){_0x339a9a=squel[_0x95ed('0x5a')]()[_0x95ed('0x5b')](_0x95ed('0x5c'))[_0x95ed('0x5d')](['phone',_0x95ed('0x5e'),_0x95ed('0x5f'),_0x95ed('0x60'),_0x95ed('0x61'),_0x95ed('0x62'),_0x95ed('0x63')],squel['select']()[_0x95ed('0x64')](_0x95ed('0x65'),_0x95ed('0x66'))['field'](_0x95ed('0x67'),_0x95ed('0x5e'))[_0x95ed('0x64')](_0x95ed('0x68'),_0x95ed('0x5f'))[_0x95ed('0x64')]('ANY_VALUE(ListId)',_0x95ed('0x60'))['field'](_0x95ed('0x69'),_0x95ed('0x61'))['field'](_0x95ed('0x67'),'createdAt')[_0x95ed('0x64')]('NOW()','updatedAt')[_0x95ed('0x6a')](_0x95ed('0x6b'))[_0x95ed('0x3a')](_0x95ed('0x6c'),_0x4c858f[_0x95ed('0x42')]['id'][_0x95ed('0x6d')]())[_0x95ed('0x3a')](_0x95ed('0x6e'),_0x4c858f[_0x95ed('0x44')][_0x95ed('0x6f')][_0x95ed('0x6d')]())[_0x95ed('0x3a')](_0x95ed('0x70'))[_0x95ed('0x3a')]('calleridnum\x20NOT\x20IN\x20(?)',squel[_0x95ed('0x71')]()['field']('phone')[_0x95ed('0x6a')](_0x95ed('0x5c'))['where']('VoiceQueueId\x20=\x20?',_0x4c858f[_0x95ed('0x42')]['id'][_0x95ed('0x6d')]()))[_0x95ed('0x3a')](_0x95ed('0x72'),squel[_0x95ed('0x71')]()[_0x95ed('0x64')]('calleridnum')['from']('cm_hopper_final')[_0x95ed('0x3a')]('VoiceQueueId\x20=\x20?',_0x4c858f['params']['id'][_0x95ed('0x6d')]())[_0x95ed('0x3a')](_0x95ed('0x73')))[_0x95ed('0x74')](_0x95ed('0x5f')))[_0x95ed('0x6d')]();return db[_0x95ed('0x48')][_0x95ed('0x32')](_0x339a9a);}return[];})[_0x95ed('0x21')](respondWithResult(_0x95e046,null))[_0x95ed('0x40')](handleError(_0x95e046,null));};exports[_0x95ed('0x75')]=function(_0x528258,_0x9319af,_0x2df4f7){return db[_0x95ed('0x76')][_0x95ed('0x59')]({'where':{'id':_0x528258[_0x95ed('0x42')]['id']}})[_0x95ed('0x21')](handleEntityNotFound(_0x9319af,null))[_0x95ed('0x21')](function(_0x45caec){var _0x598291;if(_0x45caec){_0x598291=squel[_0x95ed('0x5a')]()[_0x95ed('0x5b')](_0x95ed('0x5c'))[_0x95ed('0x5d')]([_0x95ed('0x66'),_0x95ed('0x5e'),_0x95ed('0x5f'),_0x95ed('0x60'),_0x95ed('0x77'),_0x95ed('0x62'),_0x95ed('0x63')],squel['select']()[_0x95ed('0x64')](_0x95ed('0x65'),_0x95ed('0x66'))[_0x95ed('0x64')](_0x95ed('0x67'),'scheduledAt')[_0x95ed('0x64')](_0x95ed('0x68'),_0x95ed('0x5f'))[_0x95ed('0x64')](_0x95ed('0x78'),_0x95ed('0x60'))[_0x95ed('0x64')](_0x95ed('0x79'),_0x95ed('0x77'))[_0x95ed('0x64')]('NOW()',_0x95ed('0x62'))[_0x95ed('0x64')](_0x95ed('0x67'),_0x95ed('0x63'))[_0x95ed('0x6a')](_0x95ed('0x6b'))[_0x95ed('0x3a')](_0x95ed('0x7a'),_0x528258[_0x95ed('0x42')]['id'][_0x95ed('0x6d')]())[_0x95ed('0x3a')](_0x95ed('0x6e'),_0x528258['body']['state']['toString']())['where'](_0x95ed('0x70'))[_0x95ed('0x3a')]('calleridnum\x20NOT\x20IN\x20(?)',squel['select']()[_0x95ed('0x64')](_0x95ed('0x66'))[_0x95ed('0x6a')]('cm_hopper')['where'](_0x95ed('0x7a'),_0x528258['params']['id']['toString']()))['where']('calleridnum\x20NOT\x20IN\x20(?)',squel[_0x95ed('0x71')]()[_0x95ed('0x64')](_0x95ed('0x7b'))[_0x95ed('0x6a')](_0x95ed('0x6b'))['where']('CampaignId\x20=\x20?',_0x528258['params']['id'][_0x95ed('0x6d')]())['where'](_0x95ed('0x73')))[_0x95ed('0x74')]('ContactId'))[_0x95ed('0x6d')]();return db[_0x95ed('0x48')][_0x95ed('0x32')](_0x598291);}return[];})[_0x95ed('0x21')](respondWithResult(_0x9319af,null))[_0x95ed('0x40')](handleError(_0x9319af,null));};exports[_0x95ed('0x7c')]=function(_0xe508b5,_0x52d165,_0x1f496e){return db[_0x95ed('0x7d')][_0x95ed('0x7e')]({'where':{'VoiceQueueId':_0xe508b5[_0x95ed('0x44')][_0x95ed('0x61')],'CampaignId':_0xe508b5['body'][_0x95ed('0x77')],'ContactId':_0xe508b5['body']['ContactId']}})[_0x95ed('0x21')](handleEntityNotFound(_0x52d165,null))[_0x95ed('0x21')](function(_0x559250){if(_0x559250){return _0x559250;}return null;})[_0x95ed('0x21')](respondWithResult(_0x52d165,null))[_0x95ed('0x40')](handleError(_0x52d165,null));};