Built motion from commit (unavailable).|2.5.4
[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 _0x7880=['CampaignId\x20=\x20?','state','CmHopper','findOne','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','json','offset','limit','set','Content-Range','apply','reject','update','then','destroy','status','error','stack','send','map','CmHopperFinal','fieldName','type','name','query','keys','filters','intersection','model','attributes','fields','length','order','sort','filter','merge','where','VIRTUAL','count','include','findAll','catch','show','params','describe','body','uniqueid','find','sequelize','COUNT','`state`\x20=\x200,\x201,\x20null','noSuchContacts','`state`\x20=\x203,\x201,\x20null','noAnswerContacts','busyContacts','literal','`state`\x20=\x208,\x201,\x20null','congestionContacts','`state`\x20=\x2010\x20,\x201,\x20null','dropContacts','`state`\x20=\x2011\x20,\x201,\x20null','abandonedContacts','`state`\x20=\x2022,\x201,\x20null','reCallContacts','countContactsIvrCampaignHopperFinal','`state`\x20=\x205,\x201,\x20null','`state`\x20=\x2019,\x201,\x20null','answeringMachine','into','cm_hopper','fromQuery','phone','ContactId','ListId','VoiceQueueId','createdAt','field','ANY_VALUE(calleridnum)','NOW()','scheduledAt','ANY_VALUE(ContactId)','ANY_VALUE(ListId)','updatedAt','from','cm_hopper_final','VoiceQueueId\x20=\x20?','state\x20=\x20?','toString','calleridnum\x20IS\x20NOT\x20NULL','calleridnum\x20NOT\x20IN\x20(?)','select','calleridnum','state\x20=\x204','group','moveContactsIvrCampaignHopperFinal','Campaign','insert','CampaignId'];(function(_0x188703,_0x596b26){var _0x1a0c81=function(_0x1cd22d){while(--_0x1cd22d){_0x188703['push'](_0x188703['shift']());}};_0x1a0c81(++_0x596b26);}(_0x7880,0x1f4));var _0x0788=function(_0x2cad63,_0x15a505){_0x2cad63=_0x2cad63-0x0;var _0x2ac69e=_0x7880[_0x2cad63];return _0x2ac69e;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x0788('0x0'));var rimraf=require(_0x0788('0x1'));var zipdir=require(_0x0788('0x2'));var jsonpatch=require(_0x0788('0x3'));var rp=require(_0x0788('0x4'));var moment=require(_0x0788('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x0788('0x6'));var util=require(_0x0788('0x7'));var path=require(_0x0788('0x8'));var sox=require(_0x0788('0x9'));var csv=require(_0x0788('0xa'));var ejs=require(_0x0788('0xb'));var fs=require('fs');var _=require(_0x0788('0xc'));var squel=require(_0x0788('0xd'));var crypto=require(_0x0788('0xe'));var jsforce=require(_0x0788('0xf'));var deskjs=require('desk.js');var toCsv=require(_0x0788('0xa'));var querystring=require('querystring');var Papa=require(_0x0788('0x10'));var Redis=require('ioredis');var authService=require(_0x0788('0x11'));var qs=require(_0x0788('0x12'));var as=require(_0x0788('0x13'));var hardwareService=require(_0x0788('0x14'));var logger=require(_0x0788('0x15'))('api');var utils=require(_0x0788('0x16'));var config=require(_0x0788('0x17'));var db=require(_0x0788('0x18'))['db'];function respondWithStatusCode(_0x2d8568,_0x311729){_0x311729=_0x311729||0xcc;return function(_0xce4342){if(_0xce4342){return _0x2d8568[_0x0788('0x19')](_0x311729);}return _0x2d8568['status'](_0x311729)[_0x0788('0x1a')]();};}function respondWithResult(_0x7e3407,_0x3bdcdf){_0x3bdcdf=_0x3bdcdf||0xc8;return function(_0x13cf07){if(_0x13cf07){return _0x7e3407['status'](_0x3bdcdf)[_0x0788('0x1b')](_0x13cf07);}};}function respondWithFilteredResult(_0x4ed024,_0x195f14){return function(_0x170d56){if(_0x170d56){var _0x3fd1e4=_0x170d56['count'],_0xb406fb=_0x195f14['offset'],_0x2ddd1a=_0x195f14[_0x0788('0x1c')]+_0x195f14[_0x0788('0x1d')],_0x430e34;if(_0x2ddd1a>=_0x3fd1e4){_0x2ddd1a=_0x3fd1e4;_0x430e34=0xc8;}else{_0x430e34=0xce;}_0x4ed024['status'](_0x430e34);return _0x4ed024[_0x0788('0x1e')](_0x0788('0x1f'),_0xb406fb+'-'+_0x2ddd1a+'/'+_0x3fd1e4)[_0x0788('0x1b')](_0x170d56);}return null;};}function patchUpdates(_0x52c339){return function(_0x42fe14){try{jsonpatch[_0x0788('0x20')](_0x42fe14,_0x52c339,!![]);}catch(_0x492c2c){return BPromise[_0x0788('0x21')](_0x492c2c);}return _0x42fe14['save']();};}function saveUpdates(_0x7d0869,_0x48bc5f){return function(_0x17cca3){if(_0x17cca3){return _0x17cca3[_0x0788('0x22')](_0x7d0869)[_0x0788('0x23')](function(_0x3cd197){return _0x3cd197;});}return null;};}function removeEntity(_0x3aa142,_0x4f0005){return function(_0x48ec43){if(_0x48ec43){return _0x48ec43[_0x0788('0x24')]()[_0x0788('0x23')](function(){_0x3aa142[_0x0788('0x25')](0xcc)[_0x0788('0x1a')]();});}};}function handleEntityNotFound(_0x5ba1da,_0x4d5c93){return function(_0x79204e){if(!_0x79204e){_0x5ba1da[_0x0788('0x19')](0x194);}return _0x79204e;};}function handleError(_0x40309d,_0x50b65c){_0x50b65c=_0x50b65c||0x1f4;return function(_0x4b2693){logger[_0x0788('0x26')](_0x4b2693[_0x0788('0x27')]);if(_0x4b2693['name']){delete _0x4b2693['name'];}_0x40309d[_0x0788('0x25')](_0x50b65c)[_0x0788('0x28')](_0x4b2693);};}exports['index']=function(_0x286730,_0x40e7a6){var _0x467d52={},_0x18fc0e={},_0xf9ba32={'count':0x0,'rows':[]};var _0xb2f4a5=_[_0x0788('0x29')](db[_0x0788('0x2a')]['rawAttributes'],function(_0x5a5178){return{'name':_0x5a5178[_0x0788('0x2b')],'type':_0x5a5178[_0x0788('0x2c')]['key']};});_0x18fc0e['model']=_[_0x0788('0x29')](_0xb2f4a5,_0x0788('0x2d'));_0x18fc0e[_0x0788('0x2e')]=_[_0x0788('0x2f')](_0x286730[_0x0788('0x2e')]);_0x18fc0e[_0x0788('0x30')]=_[_0x0788('0x31')](_0x18fc0e[_0x0788('0x32')],_0x18fc0e[_0x0788('0x2e')]);_0x467d52[_0x0788('0x33')]=_['intersection'](_0x18fc0e[_0x0788('0x32')],qs[_0x0788('0x34')](_0x286730[_0x0788('0x2e')][_0x0788('0x34')]));_0x467d52[_0x0788('0x33')]=_0x467d52[_0x0788('0x33')][_0x0788('0x35')]?_0x467d52[_0x0788('0x33')]:_0x18fc0e[_0x0788('0x32')];if(!_0x286730[_0x0788('0x2e')]['hasOwnProperty']('nolimit')){_0x467d52['limit']=qs['limit'](_0x286730['query']['limit']);_0x467d52[_0x0788('0x1c')]=qs[_0x0788('0x1c')](_0x286730[_0x0788('0x2e')]['offset']);}_0x467d52[_0x0788('0x36')]=qs[_0x0788('0x37')](_0x286730[_0x0788('0x2e')][_0x0788('0x37')]);_0x467d52['where']=qs[_0x0788('0x30')](_['pick'](_0x286730[_0x0788('0x2e')],_0x18fc0e['filters']),_0xb2f4a5);if(_0x286730[_0x0788('0x2e')][_0x0788('0x38')]){_0x467d52['where']=_[_0x0788('0x39')](_0x467d52[_0x0788('0x3a')],{'$or':_['map'](_0xb2f4a5,function(_0x17240e){if(_0x17240e['type']!==_0x0788('0x3b')){var _0x5576c4={};_0x5576c4[_0x17240e[_0x0788('0x2d')]]={'$like':'%'+_0x286730['query'][_0x0788('0x38')]+'%'};return _0x5576c4;}})});}_0x467d52=_['merge']({},_0x467d52,_0x286730['options']);var _0x3e31f8={'where':_0x467d52[_0x0788('0x3a')]};return db[_0x0788('0x2a')][_0x0788('0x3c')](_0x3e31f8)[_0x0788('0x23')](function(_0x53da74){_0xf9ba32['count']=_0x53da74;if(_0x286730['query']['includeAll']){_0x467d52[_0x0788('0x3d')]=[{'all':!![]}];}return db[_0x0788('0x2a')][_0x0788('0x3e')](_0x467d52);})[_0x0788('0x23')](function(_0x362c49){_0xf9ba32['rows']=_0x362c49;return _0xf9ba32;})['then'](respondWithFilteredResult(_0x40e7a6,_0x467d52))[_0x0788('0x3f')](handleError(_0x40e7a6,null));};exports[_0x0788('0x40')]=function(_0x330421,_0x29e09a){var _0x236b63={'raw':!![],'where':{'id':_0x330421[_0x0788('0x41')]['id']}},_0x4316aa={};_0x4316aa[_0x0788('0x32')]=_[_0x0788('0x2f')](db['CmHopperFinal']['rawAttributes']);_0x4316aa[_0x0788('0x2e')]=_[_0x0788('0x2f')](_0x330421[_0x0788('0x2e')]);_0x4316aa['filters']=_['intersection'](_0x4316aa['model'],_0x4316aa[_0x0788('0x2e')]);_0x236b63[_0x0788('0x33')]=_[_0x0788('0x31')](_0x4316aa[_0x0788('0x32')],qs[_0x0788('0x34')](_0x330421[_0x0788('0x2e')][_0x0788('0x34')]));_0x236b63[_0x0788('0x33')]=_0x236b63['attributes'][_0x0788('0x35')]?_0x236b63[_0x0788('0x33')]:_0x4316aa[_0x0788('0x32')];if(_0x330421[_0x0788('0x2e')]['includeAll']){_0x236b63[_0x0788('0x3d')]=[{'all':!![]}];}_0x236b63=_[_0x0788('0x39')]({},_0x236b63,_0x330421['options']);return db['CmHopperFinal']['find'](_0x236b63)[_0x0788('0x23')](handleEntityNotFound(_0x29e09a,null))['then'](respondWithResult(_0x29e09a,null))[_0x0788('0x3f')](handleError(_0x29e09a,null));};exports[_0x0788('0x42')]=function(_0x127ab4,_0x329508){return db[_0x0788('0x2a')][_0x0788('0x42')]()[_0x0788('0x23')](respondWithResult(_0x329508,null))[_0x0788('0x3f')](handleError(_0x329508,null));};exports['update']=function(_0x4c6366,_0x39c3cc){if(_0x4c6366[_0x0788('0x43')][_0x0788('0x44')]){delete _0x4c6366['body'][_0x0788('0x44')];}return db[_0x0788('0x2a')][_0x0788('0x45')]({'where':{'uniqueid':_0x4c6366[_0x0788('0x41')]['id']}})[_0x0788('0x23')](handleEntityNotFound(_0x39c3cc,null))['then'](saveUpdates(_0x4c6366['body'],null))[_0x0788('0x23')](respondWithResult(_0x39c3cc,null))[_0x0788('0x3f')](handleError(_0x39c3cc,null));};exports['countContactsQueueCampaignHopperFinal']=function(_0x2b1479,_0x4a6cde,_0x2103aa){return db[_0x0788('0x2a')][_0x0788('0x3e')]({'raw':![],'where':{'VoiceQueueId':_0x2b1479[_0x0788('0x41')]['id']},'attributes':[[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')]['literal'](_0x0788('0x48')))),_0x0788('0x49')],[db[_0x0788('0x46')]['fn']('COUNT',db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')]['literal'](_0x0788('0x4a')))),_0x0788('0x4b')],[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')]['literal']('`state`\x20=\x205,\x201,\x20null'))),_0x0788('0x4c')],[db[_0x0788('0x46')]['fn']('COUNT',db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x4e')))),_0x0788('0x4f')],[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x50')))),_0x0788('0x51')],[db['sequelize']['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')]['literal'](_0x0788('0x52')))),_0x0788('0x53')],[db['sequelize']['fn']('COUNT',db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x54')))),_0x0788('0x55')],[db[_0x0788('0x46')]['fn']('COUNT',db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')]('`state`\x20=\x2019,\x201,\x20null'))),'answeringMachine']]})[_0x0788('0x23')](handleEntityNotFound(_0x4a6cde,null))['then'](respondWithResult(_0x4a6cde,null))[_0x0788('0x3f')](handleError(_0x4a6cde,null));};exports[_0x0788('0x56')]=function(_0x223767,_0x13a021,_0x101e8b){return db[_0x0788('0x2a')][_0x0788('0x3e')]({'raw':![],'where':{'CampaignId':_0x223767[_0x0788('0x41')]['id']},'attributes':[[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db['sequelize']['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x48')))),_0x0788('0x49')],[db['sequelize']['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x4a')))),'noAnswerContacts'],[db[_0x0788('0x46')]['fn']('COUNT',db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x57')))),_0x0788('0x4c')],[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')]['literal'](_0x0788('0x4e')))),_0x0788('0x4f')],[db[_0x0788('0x46')]['fn']('COUNT',db['sequelize']['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x50')))),'dropContacts'],[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')]['literal']('`state`\x20=\x2011\x20,\x201,\x20null'))),'abandonedContacts'],[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db['sequelize']['literal'](_0x0788('0x54')))),'reCallContacts'],[db[_0x0788('0x46')]['fn'](_0x0788('0x47'),db[_0x0788('0x46')]['fn']('IF',db[_0x0788('0x46')][_0x0788('0x4d')](_0x0788('0x58')))),_0x0788('0x59')]]})[_0x0788('0x23')](handleEntityNotFound(_0x13a021,null))[_0x0788('0x23')](respondWithResult(_0x13a021,null))[_0x0788('0x3f')](handleError(_0x13a021,null));};exports['moveContactsQueueCampaignHopperFinal']=function(_0x240210,_0x30248c,_0x52926f){return db['VoiceQueue'][_0x0788('0x45')]({'where':{'id':_0x240210[_0x0788('0x41')]['id']}})[_0x0788('0x23')](handleEntityNotFound(_0x30248c,null))[_0x0788('0x23')](function(_0x3628d6){var _0x192798;if(_0x3628d6){_0x192798=squel['insert']()[_0x0788('0x5a')](_0x0788('0x5b'))[_0x0788('0x5c')]([_0x0788('0x5d'),'scheduledAt',_0x0788('0x5e'),_0x0788('0x5f'),_0x0788('0x60'),_0x0788('0x61'),'updatedAt'],squel['select']()[_0x0788('0x62')](_0x0788('0x63'),_0x0788('0x5d'))[_0x0788('0x62')](_0x0788('0x64'),_0x0788('0x65'))[_0x0788('0x62')](_0x0788('0x66'),_0x0788('0x5e'))['field'](_0x0788('0x67'),_0x0788('0x5f'))[_0x0788('0x62')]('ANY_VALUE(VoiceQueueId)','VoiceQueueId')[_0x0788('0x62')]('NOW()',_0x0788('0x61'))[_0x0788('0x62')](_0x0788('0x64'),_0x0788('0x68'))[_0x0788('0x69')](_0x0788('0x6a'))[_0x0788('0x3a')](_0x0788('0x6b'),_0x240210[_0x0788('0x41')]['id']['toString']())[_0x0788('0x3a')](_0x0788('0x6c'),_0x240210[_0x0788('0x43')]['state'][_0x0788('0x6d')]())[_0x0788('0x3a')](_0x0788('0x6e'))['where'](_0x0788('0x6f'),squel[_0x0788('0x70')]()[_0x0788('0x62')](_0x0788('0x5d'))[_0x0788('0x69')](_0x0788('0x5b'))[_0x0788('0x3a')](_0x0788('0x6b'),_0x240210[_0x0788('0x41')]['id']['toString']()))[_0x0788('0x3a')](_0x0788('0x6f'),squel[_0x0788('0x70')]()[_0x0788('0x62')](_0x0788('0x71'))['from'](_0x0788('0x6a'))[_0x0788('0x3a')](_0x0788('0x6b'),_0x240210['params']['id'][_0x0788('0x6d')]())[_0x0788('0x3a')](_0x0788('0x72')))[_0x0788('0x73')](_0x0788('0x5e')))['toString']();return db[_0x0788('0x46')][_0x0788('0x2e')](_0x192798);}return[];})['then'](respondWithResult(_0x30248c,null))[_0x0788('0x3f')](handleError(_0x30248c,null));};exports[_0x0788('0x74')]=function(_0x537a69,_0xc597d7,_0xe14a8e){return db[_0x0788('0x75')]['find']({'where':{'id':_0x537a69[_0x0788('0x41')]['id']}})[_0x0788('0x23')](handleEntityNotFound(_0xc597d7,null))[_0x0788('0x23')](function(_0x53730a){var _0xddfb97;if(_0x53730a){_0xddfb97=squel[_0x0788('0x76')]()[_0x0788('0x5a')](_0x0788('0x5b'))[_0x0788('0x5c')]([_0x0788('0x5d'),_0x0788('0x65'),_0x0788('0x5e'),'ListId',_0x0788('0x77'),_0x0788('0x61'),_0x0788('0x68')],squel[_0x0788('0x70')]()['field']('ANY_VALUE(calleridnum)',_0x0788('0x5d'))[_0x0788('0x62')]('NOW()',_0x0788('0x65'))[_0x0788('0x62')](_0x0788('0x66'),'ContactId')['field'](_0x0788('0x67'),_0x0788('0x5f'))['field']('ANY_VALUE(CampaignId)','CampaignId')[_0x0788('0x62')](_0x0788('0x64'),'createdAt')[_0x0788('0x62')](_0x0788('0x64'),_0x0788('0x68'))[_0x0788('0x69')](_0x0788('0x6a'))['where'](_0x0788('0x78'),_0x537a69['params']['id'][_0x0788('0x6d')]())['where'](_0x0788('0x6c'),_0x537a69[_0x0788('0x43')][_0x0788('0x79')][_0x0788('0x6d')]())[_0x0788('0x3a')](_0x0788('0x6e'))[_0x0788('0x3a')](_0x0788('0x6f'),squel[_0x0788('0x70')]()[_0x0788('0x62')](_0x0788('0x5d'))[_0x0788('0x69')](_0x0788('0x5b'))['where']('CampaignId\x20=\x20?',_0x537a69['params']['id'][_0x0788('0x6d')]()))[_0x0788('0x3a')](_0x0788('0x6f'),squel[_0x0788('0x70')]()[_0x0788('0x62')](_0x0788('0x71'))[_0x0788('0x69')](_0x0788('0x6a'))[_0x0788('0x3a')](_0x0788('0x78'),_0x537a69[_0x0788('0x41')]['id']['toString']())['where'](_0x0788('0x72')))[_0x0788('0x73')](_0x0788('0x5e')))[_0x0788('0x6d')]();return db[_0x0788('0x46')][_0x0788('0x2e')](_0xddfb97);}return[];})[_0x0788('0x23')](respondWithResult(_0xc597d7,null))[_0x0788('0x3f')](handleError(_0xc597d7,null));};exports['checkContactHopper']=function(_0x48b82c,_0x3b5c80,_0x22e770){return db[_0x0788('0x7a')][_0x0788('0x7b')]({'where':{'VoiceQueueId':_0x48b82c[_0x0788('0x43')][_0x0788('0x60')],'CampaignId':_0x48b82c['body'][_0x0788('0x77')],'ContactId':_0x48b82c[_0x0788('0x43')][_0x0788('0x5e')]}})[_0x0788('0x23')](handleEntityNotFound(_0x3b5c80,null))[_0x0788('0x23')](function(_0x1191e7){if(_0x1191e7){return _0x1191e7;}return null;})[_0x0788('0x23')](respondWithResult(_0x3b5c80,null))[_0x0788('0x3f')](handleError(_0x3b5c80,null));};