Built motion from commit (unavailable).|2.3.98
[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 _0x878f=['rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','lodash','squel','to-csv','../../components/auth/service','../../config/license/hardware','ioredis','../../config/logger','api','../../config/environment','status','end','json','count','offset','limit','set','apply','reject','save','update','destroy','then','error','stack','name','CmHopperFinal','rawAttributes','model','keys','query','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','map','type','key','options','includeAll','include','findAll','rows','catch','params','merge','find','describe','uniqueid','body','COUNT','sequelize','literal','noSuchContacts','`state`\x20=\x203,\x201,\x20null','noAnswerContacts','busyContacts','`state`\x20=\x208,\x201,\x20null','congestionContacts','`state`\x20=\x2011\x20,\x201,\x20null','`state`\x20=\x2022,\x201,\x20null','reCallContacts','answeringMachine','countContactsIvrCampaignHopperFinal','`state`\x20=\x200,\x201,\x20null','`state`\x20=\x205,\x201,\x20null','`state`\x20=\x2010\x20,\x201,\x20null','dropContacts','abandonedContacts','`state`\x20=\x2019,\x201,\x20null','moveContactsQueueCampaignHopperFinal','insert','into','cm_hopper','fromQuery','phone','scheduledAt','ContactId','ListId','VoiceQueueId','createdAt','updatedAt','select','ANY_VALUE(calleridnum)','field','NOW()','ANY_VALUE(ContactId)','ANY_VALUE(VoiceQueueId)','cm_hopper_final','VoiceQueueId\x20=\x20?','state\x20=\x20?','state','calleridnum\x20NOT\x20IN\x20(?)','calleridnum','from','group','moveContactsIvrCampaignHopperFinal','Campaign','CampaignId','ANY_VALUE(ListId)','CampaignId\x20=\x20?','toString','calleridnum\x20IS\x20NOT\x20NULL','state\x20=\x204','checkContactHopper','findOne','eml-format'];(function(_0x5d99ad,_0x15d2b5){var _0x453124=function(_0x57c8cf){while(--_0x57c8cf){_0x5d99ad['push'](_0x5d99ad['shift']());}};_0x453124(++_0x15d2b5);}(_0x878f,0x173));var _0xf878=function(_0x10df3d,_0x5d183c){_0x10df3d=_0x10df3d-0x0;var _0x5a8f95=_0x878f[_0x10df3d];return _0x5a8f95;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xf878('0x0'));var rimraf=require(_0xf878('0x1'));var zipdir=require(_0xf878('0x2'));var jsonpatch=require(_0xf878('0x3'));var rp=require(_0xf878('0x4'));var moment=require(_0xf878('0x5'));var BPromise=require(_0xf878('0x6'));var Mustache=require(_0xf878('0x7'));var util=require(_0xf878('0x8'));var path=require(_0xf878('0x9'));var sox=require(_0xf878('0xa'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0xf878('0xb'));var squel=require(_0xf878('0xc'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xf878('0xd'));var querystring=require('querystring');var Papa=require('papaparse');var authService=require(_0xf878('0xe'));var hardwareService=require(_0xf878('0xf'));var Redis=require(_0xf878('0x10'));var qs=require('../../components/parsers/qs');var logger=require(_0xf878('0x11'))(_0xf878('0x12'));var utils=require('../../config/utils');var config=require(_0xf878('0x13'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x31d3d2,_0x97c396){_0x97c396=_0x97c396||0xcc;return function(_0x339c9c){if(_0x339c9c){return _0x31d3d2['sendStatus'](_0x97c396);}return _0x31d3d2[_0xf878('0x14')](_0x97c396)[_0xf878('0x15')]();};}function respondWithResult(_0x39b1bb,_0x570dcc){_0x570dcc=_0x570dcc||0xc8;return function(_0xe2c2de){if(_0xe2c2de){return _0x39b1bb[_0xf878('0x14')](_0x570dcc)[_0xf878('0x16')](_0xe2c2de);}};}function respondWithFilteredResult(_0x492260,_0x568a3a){return function(_0x4c0948){if(_0x4c0948){var _0x35ff2d=_0x4c0948[_0xf878('0x17')],_0x31d888=_0x568a3a[_0xf878('0x18')],_0x6dbeca=_0x568a3a[_0xf878('0x18')]+_0x568a3a[_0xf878('0x19')],_0x44aacc;if(_0x6dbeca>=_0x35ff2d){_0x6dbeca=_0x35ff2d;_0x44aacc=0xc8;}else{_0x44aacc=0xce;}_0x492260[_0xf878('0x14')](_0x44aacc);return _0x492260[_0xf878('0x1a')]('Content-Range',_0x31d888+'-'+_0x6dbeca+'/'+_0x35ff2d)[_0xf878('0x16')](_0x4c0948);}return null;};}function patchUpdates(_0x34ae59){return function(_0x412de1){try{jsonpatch[_0xf878('0x1b')](_0x412de1,_0x34ae59,!![]);}catch(_0x3714f6){return BPromise[_0xf878('0x1c')](_0x3714f6);}return _0x412de1[_0xf878('0x1d')]();};}function saveUpdates(_0x5677a8,_0x73f6bf){return function(_0x140866){if(_0x140866){return _0x140866[_0xf878('0x1e')](_0x5677a8)['then'](function(_0x432e5d){return _0x432e5d;});}return null;};}function removeEntity(_0x23590c,_0xacc440){return function(_0x392822){if(_0x392822){return _0x392822[_0xf878('0x1f')]()[_0xf878('0x20')](function(){_0x23590c[_0xf878('0x14')](0xcc)[_0xf878('0x15')]();});}};}function handleEntityNotFound(_0x212436,_0x1a6506){return function(_0x45e61f){if(!_0x45e61f){_0x212436['sendStatus'](0x194);}return _0x45e61f;};}function handleError(_0x2ac38b,_0x5b0b06){_0x5b0b06=_0x5b0b06||0x1f4;return function(_0xa3d3d3){logger[_0xf878('0x21')](_0xa3d3d3[_0xf878('0x22')]);if(_0xa3d3d3[_0xf878('0x23')]){delete _0xa3d3d3[_0xf878('0x23')];}_0x2ac38b['status'](_0x5b0b06)['send'](_0xa3d3d3);};}exports['index']=function(_0x242926,_0x3baba4){var _0x38fa87={},_0x5af87f={},_0xfa3e11={'count':0x0,'rows':[]};var _0x2e2256=db[_0xf878('0x24')][_0xf878('0x25')];_0x5af87f[_0xf878('0x26')]=_['keys'](_0x2e2256);_0x5af87f['query']=_[_0xf878('0x27')](_0x242926[_0xf878('0x28')]);_0x5af87f['filters']=_[_0xf878('0x29')](_0x5af87f['model'],_0x5af87f[_0xf878('0x28')]);_0x38fa87['attributes']=_[_0xf878('0x29')](_0x5af87f['model'],qs[_0xf878('0x2a')](_0x242926['query'][_0xf878('0x2a')]));_0x38fa87[_0xf878('0x2b')]=_0x38fa87[_0xf878('0x2b')][_0xf878('0x2c')]?_0x38fa87[_0xf878('0x2b')]:_0x5af87f[_0xf878('0x26')];if(!_0x242926['query'][_0xf878('0x2d')](_0xf878('0x2e'))){_0x38fa87[_0xf878('0x19')]=qs[_0xf878('0x19')](_0x242926[_0xf878('0x28')]['limit']);_0x38fa87[_0xf878('0x18')]=qs[_0xf878('0x18')](_0x242926['query'][_0xf878('0x18')]);}_0x38fa87[_0xf878('0x2f')]=qs[_0xf878('0x30')](_0x242926[_0xf878('0x28')][_0xf878('0x30')]);_0x38fa87['where']=qs[_0xf878('0x31')](_[_0xf878('0x32')](_0x242926[_0xf878('0x28')],_0x5af87f[_0xf878('0x31')]));if(_0x242926['query'][_0xf878('0x33')]){_0x38fa87['where']=_['merge'](_0x38fa87[_0xf878('0x34')],{'$or':_[_0xf878('0x35')](_0x2e2256,function(_0x8b6230){if(_0x8b6230[_0xf878('0x36')][_0xf878('0x37')]!=='VIRTUAL'){var _0xc2d7ef={};_0xc2d7ef[_0x8b6230['field']]={'$like':'%'+_0x242926[_0xf878('0x28')][_0xf878('0x33')]+'%'};return _0xc2d7ef;}})});}_0x38fa87=_['merge']({},_0x38fa87,_0x242926[_0xf878('0x38')]);var _0x365e88={'where':_0x38fa87['where']};return db[_0xf878('0x24')][_0xf878('0x17')](_0x365e88)['then'](function(_0x4129c2){_0xfa3e11[_0xf878('0x17')]=_0x4129c2;if(_0x242926[_0xf878('0x28')][_0xf878('0x39')]){_0x38fa87[_0xf878('0x3a')]=[{'all':!![]}];}return db[_0xf878('0x24')][_0xf878('0x3b')](_0x38fa87);})['then'](function(_0x1179fb){_0xfa3e11[_0xf878('0x3c')]=_0x1179fb;return _0xfa3e11;})['then'](respondWithFilteredResult(_0x3baba4,_0x38fa87))[_0xf878('0x3d')](handleError(_0x3baba4,null));};exports['show']=function(_0x489964,_0x2ec109){var _0x379fc1={'raw':!![],'where':{'id':_0x489964[_0xf878('0x3e')]['id']}},_0x43b03e={};_0x43b03e[_0xf878('0x26')]=_['keys'](db[_0xf878('0x24')]['rawAttributes']);_0x43b03e[_0xf878('0x28')]=_[_0xf878('0x27')](_0x489964['query']);_0x43b03e['filters']=_[_0xf878('0x29')](_0x43b03e[_0xf878('0x26')],_0x43b03e[_0xf878('0x28')]);_0x379fc1[_0xf878('0x2b')]=_[_0xf878('0x29')](_0x43b03e[_0xf878('0x26')],qs[_0xf878('0x2a')](_0x489964[_0xf878('0x28')][_0xf878('0x2a')]));_0x379fc1['attributes']=_0x379fc1[_0xf878('0x2b')][_0xf878('0x2c')]?_0x379fc1[_0xf878('0x2b')]:_0x43b03e[_0xf878('0x26')];if(_0x489964[_0xf878('0x28')][_0xf878('0x39')]){_0x379fc1['include']=[{'all':!![]}];}_0x379fc1=_[_0xf878('0x3f')]({},_0x379fc1,_0x489964[_0xf878('0x38')]);return db[_0xf878('0x24')][_0xf878('0x40')](_0x379fc1)[_0xf878('0x20')](handleEntityNotFound(_0x2ec109,null))['then'](respondWithResult(_0x2ec109,null))[_0xf878('0x3d')](handleError(_0x2ec109,null));};exports[_0xf878('0x41')]=function(_0x2429e6,_0x1ec3aa){return db['CmHopperFinal'][_0xf878('0x41')]()[_0xf878('0x20')](respondWithResult(_0x1ec3aa,null))[_0xf878('0x3d')](handleError(_0x1ec3aa,null));};exports['update']=function(_0x4fe92d,_0x9c2fa3){if(_0x4fe92d['body']['uniqueid']){delete _0x4fe92d['body'][_0xf878('0x42')];}return db['CmHopperFinal'][_0xf878('0x40')]({'where':{'uniqueid':_0x4fe92d[_0xf878('0x3e')]['id']}})[_0xf878('0x20')](handleEntityNotFound(_0x9c2fa3,null))[_0xf878('0x20')](saveUpdates(_0x4fe92d[_0xf878('0x43')],null))[_0xf878('0x20')](respondWithResult(_0x9c2fa3,null))[_0xf878('0x3d')](handleError(_0x9c2fa3,null));};exports['countContactsQueueCampaignHopperFinal']=function(_0xb0bf4c,_0x361ac8,_0x40d912){return db[_0xf878('0x24')][_0xf878('0x3b')]({'raw':![],'where':{'VoiceQueueId':_0xb0bf4c['params']['id']},'attributes':[[db['sequelize']['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db['sequelize'][_0xf878('0x46')]('`state`\x20=\x200,\x201,\x20null'))),_0xf878('0x47')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db['sequelize'][_0xf878('0x46')](_0xf878('0x48')))),_0xf878('0x49')],[db['sequelize']['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')]('`state`\x20=\x205,\x201,\x20null'))),_0xf878('0x4a')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db['sequelize']['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x4b')))),_0xf878('0x4c')],[db['sequelize']['fn']('COUNT',db['sequelize']['fn']('IF',db[_0xf878('0x45')]['literal']('`state`\x20=\x2010\x20,\x201,\x20null'))),'dropContacts'],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db['sequelize']['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x4d')))),'abandonedContacts'],[db['sequelize']['fn'](_0xf878('0x44'),db['sequelize']['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x4e')))),_0xf878('0x4f')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db['sequelize']['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')]('`state`\x20=\x2019,\x201,\x20null'))),_0xf878('0x50')]]})[_0xf878('0x20')](handleEntityNotFound(_0x361ac8,null))[_0xf878('0x20')](respondWithResult(_0x361ac8,null))[_0xf878('0x3d')](handleError(_0x361ac8,null));};exports[_0xf878('0x51')]=function(_0x36b000,_0x339141,_0x5acc1c){return db[_0xf878('0x24')][_0xf878('0x3b')]({'raw':![],'where':{'CampaignId':_0x36b000[_0xf878('0x3e')]['id']},'attributes':[[db['sequelize']['fn'](_0xf878('0x44'),db['sequelize']['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x52')))),_0xf878('0x47')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x48')))),'noAnswerContacts'],[db['sequelize']['fn']('COUNT',db[_0xf878('0x45')]['fn']('IF',db['sequelize'][_0xf878('0x46')](_0xf878('0x53')))),'busyContacts'],[db['sequelize']['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x4b')))),_0xf878('0x4c')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x54')))),_0xf878('0x55')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')]('`state`\x20=\x2011\x20,\x201,\x20null'))),_0xf878('0x56')],[db['sequelize']['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db[_0xf878('0x45')][_0xf878('0x46')](_0xf878('0x4e')))),_0xf878('0x4f')],[db[_0xf878('0x45')]['fn'](_0xf878('0x44'),db[_0xf878('0x45')]['fn']('IF',db['sequelize'][_0xf878('0x46')](_0xf878('0x57')))),_0xf878('0x50')]]})[_0xf878('0x20')](handleEntityNotFound(_0x339141,null))['then'](respondWithResult(_0x339141,null))[_0xf878('0x3d')](handleError(_0x339141,null));};exports[_0xf878('0x58')]=function(_0x934f9c,_0x21678a,_0x3befe1){return db['VoiceQueue']['find']({'where':{'id':_0x934f9c[_0xf878('0x3e')]['id']}})[_0xf878('0x20')](handleEntityNotFound(_0x21678a,null))[_0xf878('0x20')](function(_0x251613){var _0x33b88a;if(_0x251613){_0x33b88a=squel[_0xf878('0x59')]()[_0xf878('0x5a')](_0xf878('0x5b'))[_0xf878('0x5c')]([_0xf878('0x5d'),_0xf878('0x5e'),_0xf878('0x5f'),_0xf878('0x60'),_0xf878('0x61'),_0xf878('0x62'),_0xf878('0x63')],squel[_0xf878('0x64')]()['field'](_0xf878('0x65'),_0xf878('0x5d'))[_0xf878('0x66')](_0xf878('0x67'),_0xf878('0x5e'))[_0xf878('0x66')](_0xf878('0x68'),_0xf878('0x5f'))[_0xf878('0x66')]('ANY_VALUE(ListId)',_0xf878('0x60'))[_0xf878('0x66')](_0xf878('0x69'),_0xf878('0x61'))[_0xf878('0x66')](_0xf878('0x67'),_0xf878('0x62'))['field']('NOW()',_0xf878('0x63'))['from'](_0xf878('0x6a'))[_0xf878('0x34')](_0xf878('0x6b'),_0x934f9c[_0xf878('0x3e')]['id']['toString']())[_0xf878('0x34')](_0xf878('0x6c'),_0x934f9c[_0xf878('0x43')][_0xf878('0x6d')]['toString']())[_0xf878('0x34')]('calleridnum\x20IS\x20NOT\x20NULL')[_0xf878('0x34')](_0xf878('0x6e'),squel[_0xf878('0x64')]()[_0xf878('0x66')](_0xf878('0x5d'))['from'](_0xf878('0x5b'))[_0xf878('0x34')](_0xf878('0x6b'),_0x934f9c[_0xf878('0x3e')]['id']['toString']()))[_0xf878('0x34')]('calleridnum\x20NOT\x20IN\x20(?)',squel[_0xf878('0x64')]()[_0xf878('0x66')](_0xf878('0x6f'))[_0xf878('0x70')](_0xf878('0x6a'))[_0xf878('0x34')](_0xf878('0x6b'),_0x934f9c[_0xf878('0x3e')]['id']['toString']())['where']('state\x20=\x204'))[_0xf878('0x71')](_0xf878('0x5f')))['toString']();return db['sequelize'][_0xf878('0x28')](_0x33b88a);}return[];})['then'](respondWithResult(_0x21678a,null))[_0xf878('0x3d')](handleError(_0x21678a,null));};exports[_0xf878('0x72')]=function(_0x2b9cd7,_0x1ca178,_0x2be42d){return db[_0xf878('0x73')][_0xf878('0x40')]({'where':{'id':_0x2b9cd7[_0xf878('0x3e')]['id']}})[_0xf878('0x20')](handleEntityNotFound(_0x1ca178,null))['then'](function(_0x39dd2d){var _0x170a6a;if(_0x39dd2d){_0x170a6a=squel[_0xf878('0x59')]()['into'](_0xf878('0x5b'))[_0xf878('0x5c')](['phone',_0xf878('0x5e'),_0xf878('0x5f'),_0xf878('0x60'),_0xf878('0x74'),_0xf878('0x62'),_0xf878('0x63')],squel[_0xf878('0x64')]()[_0xf878('0x66')]('ANY_VALUE(calleridnum)',_0xf878('0x5d'))['field']('NOW()',_0xf878('0x5e'))[_0xf878('0x66')]('ANY_VALUE(ContactId)',_0xf878('0x5f'))[_0xf878('0x66')](_0xf878('0x75'),_0xf878('0x60'))[_0xf878('0x66')]('ANY_VALUE(CampaignId)',_0xf878('0x74'))[_0xf878('0x66')]('NOW()',_0xf878('0x62'))[_0xf878('0x66')](_0xf878('0x67'),_0xf878('0x63'))['from']('cm_hopper_final')[_0xf878('0x34')](_0xf878('0x76'),_0x2b9cd7[_0xf878('0x3e')]['id'][_0xf878('0x77')]())[_0xf878('0x34')](_0xf878('0x6c'),_0x2b9cd7[_0xf878('0x43')][_0xf878('0x6d')][_0xf878('0x77')]())[_0xf878('0x34')](_0xf878('0x78'))[_0xf878('0x34')](_0xf878('0x6e'),squel[_0xf878('0x64')]()[_0xf878('0x66')]('phone')[_0xf878('0x70')](_0xf878('0x5b'))[_0xf878('0x34')](_0xf878('0x76'),_0x2b9cd7['params']['id'][_0xf878('0x77')]()))['where'](_0xf878('0x6e'),squel[_0xf878('0x64')]()['field'](_0xf878('0x6f'))[_0xf878('0x70')]('cm_hopper_final')['where'](_0xf878('0x76'),_0x2b9cd7[_0xf878('0x3e')]['id'][_0xf878('0x77')]())[_0xf878('0x34')](_0xf878('0x79')))[_0xf878('0x71')](_0xf878('0x5f')))[_0xf878('0x77')]();return db[_0xf878('0x45')][_0xf878('0x28')](_0x170a6a);}return[];})['then'](respondWithResult(_0x1ca178,null))[_0xf878('0x3d')](handleError(_0x1ca178,null));};exports[_0xf878('0x7a')]=function(_0x895679,_0x54cc44,_0x3d4e43){return db['CmHopper'][_0xf878('0x7b')]({'where':{'VoiceQueueId':_0x895679[_0xf878('0x43')]['VoiceQueueId'],'CampaignId':_0x895679[_0xf878('0x43')][_0xf878('0x74')],'ContactId':_0x895679[_0xf878('0x43')][_0xf878('0x5f')]}})[_0xf878('0x20')](handleEntityNotFound(_0x54cc44,null))[_0xf878('0x20')](function(_0x427e21){if(_0x427e21){return _0x427e21;}return null;})['then'](respondWithResult(_0x54cc44,null))[_0xf878('0x3d')](handleError(_0x54cc44,null));};