fb84a02bd34e5ff628042c2e9e2200032c3d2f0f
[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 _0xf61e=['rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','json','offset','undefined','count','limit','set','Content-Range','apply','destroy','then','status','error','stack','name','send','index','map','CmHopperFinal','rawAttributes','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','order','where','filter','VIRTUAL','merge','options','includeAll','include','findAll','catch','show','find','describe','update','body','uniqueid','params','countContactsQueueCampaignHopperFinal','COUNT','sequelize','literal','noSuchContacts','noAnswerContacts','busyContacts','congestionContacts','`state`\x20=\x2010\x20,\x201,\x20null','dropContacts','`state`\x20=\x2011\x20,\x201,\x20null','abandonedContacts','`state`\x20=\x2022,\x201,\x20null','reCallContacts','`state`\x20=\x2019,\x201,\x20null','answeringMachine','countContactsIvrCampaignHopperFinal','`state`\x20=\x203,\x201,\x20null','`state`\x20=\x205,\x201,\x20null','`state`\x20=\x208,\x201,\x20null','moveContactsQueueCampaignHopperFinal','insert','cm_hopper','phone','scheduledAt','VoiceQueueId','createdAt','updatedAt','select','field','NOW()','ANY_VALUE(ContactId)','ListId','ANY_VALUE(VoiceQueueId)','from','cm_hopper_final','toString','state\x20=\x20?','state','calleridnum\x20IS\x20NOT\x20NULL','calleridnum\x20NOT\x20IN\x20(?)','VoiceQueueId\x20=\x20?','state\x20=\x204','group','moveContactsIvrCampaignHopperFinal','Campaign','into','fromQuery','ContactId','CampaignId','ANY_VALUE(calleridnum)','ANY_VALUE(ListId)','ANY_VALUE(CampaignId)','CampaignId\x20=\x20?','calleridnum','checkContactHopper','findOne','eml-format'];(function(_0x1f9f37,_0x18776e){var _0x21c080=function(_0x4acb93){while(--_0x4acb93){_0x1f9f37['push'](_0x1f9f37['shift']());}};_0x21c080(++_0x18776e);}(_0xf61e,0x191));var _0xef61=function(_0x10f9f5,_0x20a147){_0x10f9f5=_0x10f9f5-0x0;var _0x46a06e=_0xf61e[_0x10f9f5];return _0x46a06e;};'use strict';var emlformat=require(_0xef61('0x0'));var rimraf=require(_0xef61('0x1'));var zipdir=require(_0xef61('0x2'));var jsonpatch=require(_0xef61('0x3'));var rp=require(_0xef61('0x4'));var moment=require(_0xef61('0x5'));var BPromise=require(_0xef61('0x6'));var Mustache=require(_0xef61('0x7'));var util=require(_0xef61('0x8'));var path=require(_0xef61('0x9'));var sox=require(_0xef61('0xa'));var csv=require(_0xef61('0xb'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xef61('0xc'));var _=require(_0xef61('0xd'));var squel=require('squel');var crypto=require(_0xef61('0xe'));var jsforce=require(_0xef61('0xf'));var deskjs=require(_0xef61('0x10'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0xef61('0x11'));var Redis=require(_0xef61('0x12'));var authService=require(_0xef61('0x13'));var qs=require(_0xef61('0x14'));var as=require(_0xef61('0x15'));var hardwareService=require(_0xef61('0x16'));var logger=require(_0xef61('0x17'))(_0xef61('0x18'));var utils=require(_0xef61('0x19'));var config=require(_0xef61('0x1a'));var licenseUtil=require(_0xef61('0x1b'));var db=require(_0xef61('0x1c'))['db'];function respondWithStatusCode(_0x114e1f,_0x318f1c){_0x318f1c=_0x318f1c||0xcc;return function(_0x5de159){if(_0x5de159){return _0x114e1f[_0xef61('0x1d')](_0x318f1c);}return _0x114e1f['status'](_0x318f1c)[_0xef61('0x1e')]();};}function respondWithResult(_0x1db2a9,_0x370ee0){_0x370ee0=_0x370ee0||0xc8;return function(_0x76ef9b){if(_0x76ef9b){return _0x1db2a9['status'](_0x370ee0)[_0xef61('0x1f')](_0x76ef9b);}};}function respondWithFilteredResult(_0x435beb,_0x1224c2){return function(_0x78ff22){if(_0x78ff22){var _0x588a61=typeof _0x1224c2[_0xef61('0x20')]===_0xef61('0x21')&&typeof _0x1224c2['limit']===_0xef61('0x21');var _0x408541=_0x78ff22[_0xef61('0x22')];var _0x22dc1b=_0x588a61?0x0:_0x1224c2[_0xef61('0x20')];var _0x28315a=_0x588a61?_0x78ff22[_0xef61('0x22')]:_0x1224c2['offset']+_0x1224c2[_0xef61('0x23')];var _0x2c8dad;if(_0x28315a>=_0x408541){_0x28315a=_0x408541;_0x2c8dad=0xc8;}else{_0x2c8dad=0xce;}_0x435beb['status'](_0x2c8dad);return _0x435beb[_0xef61('0x24')](_0xef61('0x25'),_0x22dc1b+'-'+_0x28315a+'/'+_0x408541)[_0xef61('0x1f')](_0x78ff22);}return null;};}function patchUpdates(_0x26cf42){return function(_0xb0d7a4){try{jsonpatch[_0xef61('0x26')](_0xb0d7a4,_0x26cf42,!![]);}catch(_0x92ea33){return BPromise['reject'](_0x92ea33);}return _0xb0d7a4['save']();};}function saveUpdates(_0x3d31c1,_0x58bd7d){return function(_0x389d6d){if(_0x389d6d){return _0x389d6d['update'](_0x3d31c1)['then'](function(_0x2f0569){return _0x2f0569;});}return null;};}function removeEntity(_0x5ce99d,_0x53181b){return function(_0xfbee94){if(_0xfbee94){return _0xfbee94[_0xef61('0x27')]()[_0xef61('0x28')](function(){_0x5ce99d[_0xef61('0x29')](0xcc)['end']();});}};}function handleEntityNotFound(_0x16b144,_0x4e738f){return function(_0x2a6832){if(!_0x2a6832){_0x16b144[_0xef61('0x1d')](0x194);}return _0x2a6832;};}function handleError(_0x2419a9,_0xf4c7c9){_0xf4c7c9=_0xf4c7c9||0x1f4;return function(_0x2155f1){logger[_0xef61('0x2a')](_0x2155f1[_0xef61('0x2b')]);if(_0x2155f1[_0xef61('0x2c')]){delete _0x2155f1[_0xef61('0x2c')];}_0x2419a9[_0xef61('0x29')](_0xf4c7c9)[_0xef61('0x2d')](_0x2155f1);};}exports[_0xef61('0x2e')]=function(_0xeb9a62,_0x63edd4){var _0x1f3572={},_0x85978={},_0x39c15e={'count':0x0,'rows':[]};var _0x393ea9=_[_0xef61('0x2f')](db[_0xef61('0x30')][_0xef61('0x31')],function(_0x53fa3b){return{'name':_0x53fa3b['fieldName'],'type':_0x53fa3b['type'][_0xef61('0x32')]};});_0x85978[_0xef61('0x33')]=_[_0xef61('0x2f')](_0x393ea9,'name');_0x85978[_0xef61('0x34')]=_[_0xef61('0x35')](_0xeb9a62['query']);_0x85978[_0xef61('0x36')]=_['intersection'](_0x85978['model'],_0x85978[_0xef61('0x34')]);_0x1f3572['attributes']=_[_0xef61('0x37')](_0x85978['model'],qs[_0xef61('0x38')](_0xeb9a62[_0xef61('0x34')][_0xef61('0x38')]));_0x1f3572[_0xef61('0x39')]=_0x1f3572[_0xef61('0x39')][_0xef61('0x3a')]?_0x1f3572[_0xef61('0x39')]:_0x85978[_0xef61('0x33')];if(!_0xeb9a62[_0xef61('0x34')][_0xef61('0x3b')]('nolimit')){_0x1f3572[_0xef61('0x23')]=qs[_0xef61('0x23')](_0xeb9a62[_0xef61('0x34')][_0xef61('0x23')]);_0x1f3572[_0xef61('0x20')]=qs['offset'](_0xeb9a62[_0xef61('0x34')][_0xef61('0x20')]);}_0x1f3572[_0xef61('0x3c')]=qs['sort'](_0xeb9a62['query']['sort']);_0x1f3572[_0xef61('0x3d')]=qs['filters'](_['pick'](_0xeb9a62['query'],_0x85978[_0xef61('0x36')]),_0x393ea9);if(_0xeb9a62[_0xef61('0x34')][_0xef61('0x3e')]){_0x1f3572['where']=_['merge'](_0x1f3572['where'],{'$or':_[_0xef61('0x2f')](_0x393ea9,function(_0x32ad44){if(_0x32ad44['type']!==_0xef61('0x3f')){var _0x12f442={};_0x12f442[_0x32ad44[_0xef61('0x2c')]]={'$like':'%'+_0xeb9a62[_0xef61('0x34')]['filter']+'%'};return _0x12f442;}})});}_0x1f3572=_[_0xef61('0x40')]({},_0x1f3572,_0xeb9a62[_0xef61('0x41')]);var _0x3103d4={'where':_0x1f3572[_0xef61('0x3d')]};return db['CmHopperFinal']['count'](_0x3103d4)[_0xef61('0x28')](function(_0x462972){_0x39c15e[_0xef61('0x22')]=_0x462972;if(_0xeb9a62['query'][_0xef61('0x42')]){_0x1f3572[_0xef61('0x43')]=[{'all':!![]}];}return db[_0xef61('0x30')][_0xef61('0x44')](_0x1f3572);})['then'](function(_0x545597){_0x39c15e['rows']=_0x545597;return _0x39c15e;})[_0xef61('0x28')](respondWithFilteredResult(_0x63edd4,_0x1f3572))[_0xef61('0x45')](handleError(_0x63edd4,null));};exports[_0xef61('0x46')]=function(_0xe40d,_0x3f81a1){var _0x272143={'raw':!![],'where':{'id':_0xe40d['params']['id']}},_0xc9b467={};_0xc9b467[_0xef61('0x33')]=_[_0xef61('0x35')](db[_0xef61('0x30')][_0xef61('0x31')]);_0xc9b467['query']=_[_0xef61('0x35')](_0xe40d[_0xef61('0x34')]);_0xc9b467[_0xef61('0x36')]=_[_0xef61('0x37')](_0xc9b467[_0xef61('0x33')],_0xc9b467[_0xef61('0x34')]);_0x272143['attributes']=_[_0xef61('0x37')](_0xc9b467[_0xef61('0x33')],qs[_0xef61('0x38')](_0xe40d['query'][_0xef61('0x38')]));_0x272143[_0xef61('0x39')]=_0x272143[_0xef61('0x39')][_0xef61('0x3a')]?_0x272143[_0xef61('0x39')]:_0xc9b467[_0xef61('0x33')];if(_0xe40d['query'][_0xef61('0x42')]){_0x272143[_0xef61('0x43')]=[{'all':!![]}];}_0x272143=_['merge']({},_0x272143,_0xe40d[_0xef61('0x41')]);return db[_0xef61('0x30')][_0xef61('0x47')](_0x272143)[_0xef61('0x28')](handleEntityNotFound(_0x3f81a1,null))['then'](respondWithResult(_0x3f81a1,null))[_0xef61('0x45')](handleError(_0x3f81a1,null));};exports[_0xef61('0x48')]=function(_0x5e614b,_0x47b09d){return db[_0xef61('0x30')][_0xef61('0x48')]()['then'](respondWithResult(_0x47b09d,null))[_0xef61('0x45')](handleError(_0x47b09d,null));};exports[_0xef61('0x49')]=function(_0x13d0f8,_0x2904e0){if(_0x13d0f8[_0xef61('0x4a')][_0xef61('0x4b')]){delete _0x13d0f8[_0xef61('0x4a')]['uniqueid'];}return db[_0xef61('0x30')]['find']({'where':{'uniqueid':_0x13d0f8[_0xef61('0x4c')]['id']}})[_0xef61('0x28')](handleEntityNotFound(_0x2904e0,null))[_0xef61('0x28')](saveUpdates(_0x13d0f8[_0xef61('0x4a')],null))[_0xef61('0x28')](respondWithResult(_0x2904e0,null))[_0xef61('0x45')](handleError(_0x2904e0,null));};exports[_0xef61('0x4d')]=function(_0x1565c7,_0x264877,_0x4af2f3){return db[_0xef61('0x30')][_0xef61('0x44')]({'raw':![],'where':{'VoiceQueueId':_0x1565c7['params']['id']},'attributes':[[db['sequelize']['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db['sequelize'][_0xef61('0x50')]('`state`\x20=\x200,\x201,\x20null'))),_0xef61('0x51')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')]('`state`\x20=\x203,\x201,\x20null'))),_0xef61('0x52')],[db[_0xef61('0x4f')]['fn']('COUNT',db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')]['literal']('`state`\x20=\x205,\x201,\x20null'))),_0xef61('0x53')],[db['sequelize']['fn']('COUNT',db['sequelize']['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')]('`state`\x20=\x208,\x201,\x20null'))),_0xef61('0x54')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x55')))),_0xef61('0x56')],[db['sequelize']['fn']('COUNT',db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x57')))),_0xef61('0x58')],[db['sequelize']['fn'](_0xef61('0x4e'),db['sequelize']['fn']('IF',db['sequelize'][_0xef61('0x50')](_0xef61('0x59')))),_0xef61('0x5a')],[db['sequelize']['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x5b')))),_0xef61('0x5c')]]})[_0xef61('0x28')](handleEntityNotFound(_0x264877,null))[_0xef61('0x28')](respondWithResult(_0x264877,null))[_0xef61('0x45')](handleError(_0x264877,null));};exports[_0xef61('0x5d')]=function(_0x5507ee,_0x15a8c5,_0x4b26e6){return db['CmHopperFinal'][_0xef61('0x44')]({'raw':![],'where':{'CampaignId':_0x5507ee[_0xef61('0x4c')]['id']},'attributes':[[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')]['literal']('`state`\x20=\x200,\x201,\x20null'))),_0xef61('0x51')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x5e')))),_0xef61('0x52')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db['sequelize'][_0xef61('0x50')](_0xef61('0x5f')))),'busyContacts'],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x60')))),_0xef61('0x54')],[db[_0xef61('0x4f')]['fn']('COUNT',db[_0xef61('0x4f')]['fn']('IF',db['sequelize']['literal']('`state`\x20=\x2010\x20,\x201,\x20null'))),_0xef61('0x56')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db['sequelize']['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x57')))),_0xef61('0x58')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x59')))),_0xef61('0x5a')],[db[_0xef61('0x4f')]['fn'](_0xef61('0x4e'),db[_0xef61('0x4f')]['fn']('IF',db[_0xef61('0x4f')][_0xef61('0x50')](_0xef61('0x5b')))),_0xef61('0x5c')]]})['then'](handleEntityNotFound(_0x15a8c5,null))[_0xef61('0x28')](respondWithResult(_0x15a8c5,null))[_0xef61('0x45')](handleError(_0x15a8c5,null));};exports[_0xef61('0x61')]=function(_0x5ab4fe,_0x38117b,_0x346752){return db['VoiceQueue'][_0xef61('0x47')]({'where':{'id':_0x5ab4fe[_0xef61('0x4c')]['id']}})['then'](handleEntityNotFound(_0x38117b,null))[_0xef61('0x28')](function(_0x54777a){var _0xe62ec2;if(_0x54777a){_0xe62ec2=squel[_0xef61('0x62')]()['into'](_0xef61('0x63'))['fromQuery']([_0xef61('0x64'),_0xef61('0x65'),'ContactId','ListId',_0xef61('0x66'),_0xef61('0x67'),_0xef61('0x68')],squel[_0xef61('0x69')]()[_0xef61('0x6a')]('ANY_VALUE(calleridnum)',_0xef61('0x64'))[_0xef61('0x6a')](_0xef61('0x6b'),_0xef61('0x65'))[_0xef61('0x6a')](_0xef61('0x6c'),'ContactId')[_0xef61('0x6a')]('ANY_VALUE(ListId)',_0xef61('0x6d'))[_0xef61('0x6a')](_0xef61('0x6e'),_0xef61('0x66'))[_0xef61('0x6a')](_0xef61('0x6b'),_0xef61('0x67'))[_0xef61('0x6a')](_0xef61('0x6b'),_0xef61('0x68'))[_0xef61('0x6f')](_0xef61('0x70'))[_0xef61('0x3d')]('VoiceQueueId\x20=\x20?',_0x5ab4fe[_0xef61('0x4c')]['id'][_0xef61('0x71')]())[_0xef61('0x3d')](_0xef61('0x72'),_0x5ab4fe['body'][_0xef61('0x73')]['toString']())['where'](_0xef61('0x74'))['where'](_0xef61('0x75'),squel['select']()['field'](_0xef61('0x64'))[_0xef61('0x6f')]('cm_hopper')[_0xef61('0x3d')]('VoiceQueueId\x20=\x20?',_0x5ab4fe[_0xef61('0x4c')]['id'][_0xef61('0x71')]()))[_0xef61('0x3d')](_0xef61('0x75'),squel['select']()[_0xef61('0x6a')]('calleridnum')[_0xef61('0x6f')](_0xef61('0x70'))[_0xef61('0x3d')](_0xef61('0x76'),_0x5ab4fe[_0xef61('0x4c')]['id'][_0xef61('0x71')]())[_0xef61('0x3d')](_0xef61('0x77')))[_0xef61('0x78')]('ContactId'))[_0xef61('0x71')]();return db['sequelize'][_0xef61('0x34')](_0xe62ec2);}return[];})[_0xef61('0x28')](respondWithResult(_0x38117b,null))['catch'](handleError(_0x38117b,null));};exports[_0xef61('0x79')]=function(_0x5b86e4,_0x52f7c5,_0x465fb5){return db[_0xef61('0x7a')]['find']({'where':{'id':_0x5b86e4[_0xef61('0x4c')]['id']}})[_0xef61('0x28')](handleEntityNotFound(_0x52f7c5,null))['then'](function(_0x352633){var _0x2cb8a8;if(_0x352633){_0x2cb8a8=squel['insert']()[_0xef61('0x7b')]('cm_hopper')[_0xef61('0x7c')]([_0xef61('0x64'),_0xef61('0x65'),_0xef61('0x7d'),'ListId',_0xef61('0x7e'),_0xef61('0x67'),_0xef61('0x68')],squel['select']()[_0xef61('0x6a')](_0xef61('0x7f'),'phone')['field'](_0xef61('0x6b'),_0xef61('0x65'))[_0xef61('0x6a')](_0xef61('0x6c'),_0xef61('0x7d'))[_0xef61('0x6a')](_0xef61('0x80'),_0xef61('0x6d'))[_0xef61('0x6a')](_0xef61('0x81'),_0xef61('0x7e'))[_0xef61('0x6a')](_0xef61('0x6b'),_0xef61('0x67'))[_0xef61('0x6a')](_0xef61('0x6b'),'updatedAt')[_0xef61('0x6f')](_0xef61('0x70'))[_0xef61('0x3d')](_0xef61('0x82'),_0x5b86e4[_0xef61('0x4c')]['id'][_0xef61('0x71')]())[_0xef61('0x3d')](_0xef61('0x72'),_0x5b86e4[_0xef61('0x4a')][_0xef61('0x73')][_0xef61('0x71')]())[_0xef61('0x3d')]('calleridnum\x20IS\x20NOT\x20NULL')[_0xef61('0x3d')](_0xef61('0x75'),squel['select']()[_0xef61('0x6a')](_0xef61('0x64'))[_0xef61('0x6f')](_0xef61('0x63'))[_0xef61('0x3d')]('CampaignId\x20=\x20?',_0x5b86e4[_0xef61('0x4c')]['id']['toString']()))[_0xef61('0x3d')](_0xef61('0x75'),squel[_0xef61('0x69')]()[_0xef61('0x6a')](_0xef61('0x83'))[_0xef61('0x6f')](_0xef61('0x70'))[_0xef61('0x3d')]('CampaignId\x20=\x20?',_0x5b86e4[_0xef61('0x4c')]['id'][_0xef61('0x71')]())[_0xef61('0x3d')](_0xef61('0x77')))[_0xef61('0x78')](_0xef61('0x7d')))[_0xef61('0x71')]();return db[_0xef61('0x4f')][_0xef61('0x34')](_0x2cb8a8);}return[];})['then'](respondWithResult(_0x52f7c5,null))[_0xef61('0x45')](handleError(_0x52f7c5,null));};exports[_0xef61('0x84')]=function(_0x22006a,_0x317c4a,_0x5d8ee9){return db['CmHopper'][_0xef61('0x85')]({'where':{'VoiceQueueId':_0x22006a[_0xef61('0x4a')][_0xef61('0x66')],'CampaignId':_0x22006a[_0xef61('0x4a')][_0xef61('0x7e')],'ContactId':_0x22006a[_0xef61('0x4a')][_0xef61('0x7d')]}})['then'](handleEntityNotFound(_0x317c4a,null))[_0xef61('0x28')](function(_0x660c71){if(_0x660c71){return _0x660c71;}return null;})[_0xef61('0x28')](respondWithResult(_0x317c4a,null))[_0xef61('0x45')](handleError(_0x317c4a,null));};