Built motion from commit (unavailable).|2.5.27
[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 _0xd95e=['to-csv','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','apply','reject','save','update','then','destroy','end','error','name','send','index','map','CmHopperFinal','model','query','keys','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','where','filters','pick','merge','VIRTUAL','filter','options','count','includeAll','include','findAll','rows','show','rawAttributes','find','catch','describe','body','uniqueid','params','countContactsQueueCampaignHopperFinal','sequelize','literal','`state`\x20=\x203,\x201,\x20null','COUNT','`state`\x20=\x205,\x201,\x20null','busyContacts','`state`\x20=\x208,\x201,\x20null','congestionContacts','dropContacts','`state`\x20=\x2011\x20,\x201,\x20null','abandonedContacts','`state`\x20=\x2022,\x201,\x20null','`state`\x20=\x2019,\x201,\x20null','answeringMachine','countContactsIvrCampaignHopperFinal','noSuchContacts','noAnswerContacts','`state`\x20=\x2010\x20,\x201,\x20null','reCallContacts','moveContactsQueueCampaignHopperFinal','VoiceQueue','insert','fromQuery','scheduledAt','ContactId','ListId','VoiceQueueId','createdAt','updatedAt','select','field','ANY_VALUE(calleridnum)','phone','NOW()','ANY_VALUE(ContactId)','ANY_VALUE(ListId)','ANY_VALUE(VoiceQueueId)','VoiceQueueId\x20=\x20?','toString','state','calleridnum\x20IS\x20NOT\x20NULL','calleridnum\x20NOT\x20IN\x20(?)','from','cm_hopper','cm_hopper_final','state\x20=\x204','moveContactsIvrCampaignHopperFinal','Campaign','into','CampaignId','ANY_VALUE(CampaignId)','CampaignId\x20=\x20?','calleridnum','checkContactHopper','CmHopper','eml-format','zip-dir','fast-json-patch','moment','mustache','util','path','ejs','fs-extra','squel','crypto','desk.js'];(function(_0x1ff548,_0x2c1e89){var _0x113884=function(_0x250e83){while(--_0x250e83){_0x1ff548['push'](_0x1ff548['shift']());}};_0x113884(++_0x2c1e89);}(_0xd95e,0x171));var _0xed95=function(_0x20a584,_0xe384a5){_0x20a584=_0x20a584-0x0;var _0x4b41a8=_0xd95e[_0x20a584];return _0x4b41a8;};'use strict';var emlformat=require(_0xed95('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xed95('0x1'));var jsonpatch=require(_0xed95('0x2'));var rp=require('request-promise');var moment=require(_0xed95('0x3'));var BPromise=require('bluebird');var Mustache=require(_0xed95('0x4'));var util=require(_0xed95('0x5'));var path=require(_0xed95('0x6'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xed95('0x7'));var fs=require('fs');var fs_extra=require(_0xed95('0x8'));var _=require('lodash');var squel=require(_0xed95('0x9'));var crypto=require(_0xed95('0xa'));var jsforce=require('jsforce');var deskjs=require(_0xed95('0xb'));var toCsv=require(_0xed95('0xc'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xed95('0xd'));var authService=require(_0xed95('0xe'));var qs=require(_0xed95('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xed95('0x10'));var logger=require(_0xed95('0x11'))(_0xed95('0x12'));var utils=require(_0xed95('0x13'));var config=require('../../config/environment');var licenseUtil=require(_0xed95('0x14'));var db=require(_0xed95('0x15'))['db'];function respondWithStatusCode(_0x22b0b5,_0x137897){_0x137897=_0x137897||0xcc;return function(_0x17a5ae){if(_0x17a5ae){return _0x22b0b5[_0xed95('0x16')](_0x137897);}return _0x22b0b5['status'](_0x137897)['end']();};}function respondWithResult(_0x893cd8,_0x450d2e){_0x450d2e=_0x450d2e||0xc8;return function(_0x507c91){if(_0x507c91){return _0x893cd8[_0xed95('0x17')](_0x450d2e)[_0xed95('0x18')](_0x507c91);}};}function respondWithFilteredResult(_0x9f5373,_0x451064){return function(_0x2bf559){if(_0x2bf559){var _0xcdba45=_0x2bf559['count'],_0x3f8e04=_0x451064[_0xed95('0x19')],_0x4a6247=_0x451064[_0xed95('0x19')]+_0x451064['limit'],_0x1338c4;if(_0x4a6247>=_0xcdba45){_0x4a6247=_0xcdba45;_0x1338c4=0xc8;}else{_0x1338c4=0xce;}_0x9f5373[_0xed95('0x17')](_0x1338c4);return _0x9f5373['set']('Content-Range',_0x3f8e04+'-'+_0x4a6247+'/'+_0xcdba45)[_0xed95('0x18')](_0x2bf559);}return null;};}function patchUpdates(_0x2e0169){return function(_0xc05f3f){try{jsonpatch[_0xed95('0x1a')](_0xc05f3f,_0x2e0169,!![]);}catch(_0x1c9b14){return BPromise[_0xed95('0x1b')](_0x1c9b14);}return _0xc05f3f[_0xed95('0x1c')]();};}function saveUpdates(_0x59763e,_0x53943b){return function(_0x28ee66){if(_0x28ee66){return _0x28ee66[_0xed95('0x1d')](_0x59763e)[_0xed95('0x1e')](function(_0x1f6019){return _0x1f6019;});}return null;};}function removeEntity(_0xe53ad6,_0x47e242){return function(_0x5b60bb){if(_0x5b60bb){return _0x5b60bb[_0xed95('0x1f')]()[_0xed95('0x1e')](function(){_0xe53ad6[_0xed95('0x17')](0xcc)[_0xed95('0x20')]();});}};}function handleEntityNotFound(_0xad8543,_0x3cddde){return function(_0x811529){if(!_0x811529){_0xad8543[_0xed95('0x16')](0x194);}return _0x811529;};}function handleError(_0x4f15f2,_0x55455d){_0x55455d=_0x55455d||0x1f4;return function(_0x1b5091){logger[_0xed95('0x21')](_0x1b5091['stack']);if(_0x1b5091[_0xed95('0x22')]){delete _0x1b5091[_0xed95('0x22')];}_0x4f15f2['status'](_0x55455d)[_0xed95('0x23')](_0x1b5091);};}exports[_0xed95('0x24')]=function(_0x210ece,_0x973d43){var _0x386651={},_0x14c55e={},_0x4567a9={'count':0x0,'rows':[]};var _0x7515a8=_[_0xed95('0x25')](db[_0xed95('0x26')]['rawAttributes'],function(_0x2e3376){return{'name':_0x2e3376['fieldName'],'type':_0x2e3376['type']['key']};});_0x14c55e[_0xed95('0x27')]=_[_0xed95('0x25')](_0x7515a8,_0xed95('0x22'));_0x14c55e[_0xed95('0x28')]=_[_0xed95('0x29')](_0x210ece[_0xed95('0x28')]);_0x14c55e['filters']=_['intersection'](_0x14c55e[_0xed95('0x27')],_0x14c55e['query']);_0x386651['attributes']=_[_0xed95('0x2a')](_0x14c55e['model'],qs[_0xed95('0x2b')](_0x210ece[_0xed95('0x28')][_0xed95('0x2b')]));_0x386651[_0xed95('0x2c')]=_0x386651['attributes'][_0xed95('0x2d')]?_0x386651[_0xed95('0x2c')]:_0x14c55e[_0xed95('0x27')];if(!_0x210ece[_0xed95('0x28')][_0xed95('0x2e')](_0xed95('0x2f'))){_0x386651[_0xed95('0x30')]=qs[_0xed95('0x30')](_0x210ece[_0xed95('0x28')]['limit']);_0x386651[_0xed95('0x19')]=qs[_0xed95('0x19')](_0x210ece['query'][_0xed95('0x19')]);}_0x386651[_0xed95('0x31')]=qs[_0xed95('0x32')](_0x210ece[_0xed95('0x28')]['sort']);_0x386651[_0xed95('0x33')]=qs[_0xed95('0x34')](_[_0xed95('0x35')](_0x210ece['query'],_0x14c55e[_0xed95('0x34')]),_0x7515a8);if(_0x210ece['query']['filter']){_0x386651[_0xed95('0x33')]=_[_0xed95('0x36')](_0x386651[_0xed95('0x33')],{'$or':_[_0xed95('0x25')](_0x7515a8,function(_0x4d6468){if(_0x4d6468['type']!==_0xed95('0x37')){var _0x1e9e51={};_0x1e9e51[_0x4d6468['name']]={'$like':'%'+_0x210ece[_0xed95('0x28')][_0xed95('0x38')]+'%'};return _0x1e9e51;}})});}_0x386651=_['merge']({},_0x386651,_0x210ece[_0xed95('0x39')]);var _0x1ee195={'where':_0x386651[_0xed95('0x33')]};return db[_0xed95('0x26')]['count'](_0x1ee195)[_0xed95('0x1e')](function(_0x4ca04b){_0x4567a9[_0xed95('0x3a')]=_0x4ca04b;if(_0x210ece[_0xed95('0x28')][_0xed95('0x3b')]){_0x386651[_0xed95('0x3c')]=[{'all':!![]}];}return db[_0xed95('0x26')][_0xed95('0x3d')](_0x386651);})['then'](function(_0x39281d){_0x4567a9[_0xed95('0x3e')]=_0x39281d;return _0x4567a9;})[_0xed95('0x1e')](respondWithFilteredResult(_0x973d43,_0x386651))['catch'](handleError(_0x973d43,null));};exports[_0xed95('0x3f')]=function(_0x29d9b8,_0xe3c92){var _0x37aca5={'raw':!![],'where':{'id':_0x29d9b8['params']['id']}},_0x380e61={};_0x380e61[_0xed95('0x27')]=_[_0xed95('0x29')](db[_0xed95('0x26')][_0xed95('0x40')]);_0x380e61['query']=_[_0xed95('0x29')](_0x29d9b8[_0xed95('0x28')]);_0x380e61['filters']=_[_0xed95('0x2a')](_0x380e61['model'],_0x380e61[_0xed95('0x28')]);_0x37aca5[_0xed95('0x2c')]=_[_0xed95('0x2a')](_0x380e61[_0xed95('0x27')],qs[_0xed95('0x2b')](_0x29d9b8['query'][_0xed95('0x2b')]));_0x37aca5['attributes']=_0x37aca5[_0xed95('0x2c')][_0xed95('0x2d')]?_0x37aca5[_0xed95('0x2c')]:_0x380e61[_0xed95('0x27')];if(_0x29d9b8['query'][_0xed95('0x3b')]){_0x37aca5[_0xed95('0x3c')]=[{'all':!![]}];}_0x37aca5=_[_0xed95('0x36')]({},_0x37aca5,_0x29d9b8[_0xed95('0x39')]);return db[_0xed95('0x26')][_0xed95('0x41')](_0x37aca5)['then'](handleEntityNotFound(_0xe3c92,null))['then'](respondWithResult(_0xe3c92,null))[_0xed95('0x42')](handleError(_0xe3c92,null));};exports[_0xed95('0x43')]=function(_0xf13729,_0x46cfaf){return db[_0xed95('0x26')][_0xed95('0x43')]()[_0xed95('0x1e')](respondWithResult(_0x46cfaf,null))[_0xed95('0x42')](handleError(_0x46cfaf,null));};exports[_0xed95('0x1d')]=function(_0x3a115a,_0x2946d2){if(_0x3a115a[_0xed95('0x44')][_0xed95('0x45')]){delete _0x3a115a[_0xed95('0x44')][_0xed95('0x45')];}return db[_0xed95('0x26')][_0xed95('0x41')]({'where':{'uniqueid':_0x3a115a[_0xed95('0x46')]['id']}})[_0xed95('0x1e')](handleEntityNotFound(_0x2946d2,null))[_0xed95('0x1e')](saveUpdates(_0x3a115a[_0xed95('0x44')],null))['then'](respondWithResult(_0x2946d2,null))['catch'](handleError(_0x2946d2,null));};exports[_0xed95('0x47')]=function(_0x3a0946,_0x188373,_0x154171){return db[_0xed95('0x26')][_0xed95('0x3d')]({'raw':![],'where':{'VoiceQueueId':_0x3a0946[_0xed95('0x46')]['id']},'attributes':[[db[_0xed95('0x48')]['fn']('COUNT',db[_0xed95('0x48')]['fn']('IF',db['sequelize']['literal']('`state`\x20=\x200,\x201,\x20null'))),'noSuchContacts'],[db['sequelize']['fn']('COUNT',db['sequelize']['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x4a')))),'noAnswerContacts'],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x4c')))),_0xed95('0x4d')],[db['sequelize']['fn']('COUNT',db['sequelize']['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x4e')))),_0xed95('0x4f')],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')]('`state`\x20=\x2010\x20,\x201,\x20null'))),_0xed95('0x50')],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x51')))),_0xed95('0x52')],[db['sequelize']['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x53')))),'reCallContacts'],[db['sequelize']['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db['sequelize'][_0xed95('0x49')](_0xed95('0x54')))),_0xed95('0x55')]]})[_0xed95('0x1e')](handleEntityNotFound(_0x188373,null))[_0xed95('0x1e')](respondWithResult(_0x188373,null))[_0xed95('0x42')](handleError(_0x188373,null));};exports[_0xed95('0x56')]=function(_0x21ca9d,_0x46b725,_0x476482){return db[_0xed95('0x26')][_0xed95('0x3d')]({'raw':![],'where':{'CampaignId':_0x21ca9d[_0xed95('0x46')]['id']},'attributes':[[db['sequelize']['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')]('`state`\x20=\x200,\x201,\x20null'))),_0xed95('0x57')],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')]('`state`\x20=\x203,\x201,\x20null'))),_0xed95('0x58')],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')]['literal'](_0xed95('0x4c')))),_0xed95('0x4d')],[db['sequelize']['fn'](_0xed95('0x4b'),db['sequelize']['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x4e')))),_0xed95('0x4f')],[db['sequelize']['fn'](_0xed95('0x4b'),db['sequelize']['fn']('IF',db[_0xed95('0x48')][_0xed95('0x49')](_0xed95('0x59')))),'dropContacts'],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db[_0xed95('0x48')]['fn']('IF',db['sequelize'][_0xed95('0x49')](_0xed95('0x51')))),_0xed95('0x52')],[db[_0xed95('0x48')]['fn']('COUNT',db[_0xed95('0x48')]['fn']('IF',db[_0xed95('0x48')]['literal']('`state`\x20=\x2022,\x201,\x20null'))),_0xed95('0x5a')],[db[_0xed95('0x48')]['fn'](_0xed95('0x4b'),db['sequelize']['fn']('IF',db['sequelize'][_0xed95('0x49')](_0xed95('0x54')))),_0xed95('0x55')]]})[_0xed95('0x1e')](handleEntityNotFound(_0x46b725,null))['then'](respondWithResult(_0x46b725,null))[_0xed95('0x42')](handleError(_0x46b725,null));};exports[_0xed95('0x5b')]=function(_0x5cabb1,_0x2c8cce,_0x3ae895){return db[_0xed95('0x5c')][_0xed95('0x41')]({'where':{'id':_0x5cabb1['params']['id']}})[_0xed95('0x1e')](handleEntityNotFound(_0x2c8cce,null))['then'](function(_0x2b55e0){var _0xbf0832;if(_0x2b55e0){_0xbf0832=squel[_0xed95('0x5d')]()['into']('cm_hopper')[_0xed95('0x5e')](['phone',_0xed95('0x5f'),_0xed95('0x60'),_0xed95('0x61'),_0xed95('0x62'),_0xed95('0x63'),_0xed95('0x64')],squel[_0xed95('0x65')]()[_0xed95('0x66')](_0xed95('0x67'),_0xed95('0x68'))[_0xed95('0x66')](_0xed95('0x69'),_0xed95('0x5f'))[_0xed95('0x66')](_0xed95('0x6a'),_0xed95('0x60'))[_0xed95('0x66')](_0xed95('0x6b'),_0xed95('0x61'))[_0xed95('0x66')](_0xed95('0x6c'),_0xed95('0x62'))[_0xed95('0x66')](_0xed95('0x69'),'createdAt')[_0xed95('0x66')]('NOW()',_0xed95('0x64'))['from']('cm_hopper_final')[_0xed95('0x33')](_0xed95('0x6d'),_0x5cabb1[_0xed95('0x46')]['id'][_0xed95('0x6e')]())[_0xed95('0x33')]('state\x20=\x20?',_0x5cabb1[_0xed95('0x44')][_0xed95('0x6f')]['toString']())['where'](_0xed95('0x70'))[_0xed95('0x33')](_0xed95('0x71'),squel[_0xed95('0x65')]()[_0xed95('0x66')](_0xed95('0x68'))[_0xed95('0x72')](_0xed95('0x73'))[_0xed95('0x33')](_0xed95('0x6d'),_0x5cabb1[_0xed95('0x46')]['id'][_0xed95('0x6e')]()))[_0xed95('0x33')]('calleridnum\x20NOT\x20IN\x20(?)',squel[_0xed95('0x65')]()[_0xed95('0x66')]('calleridnum')[_0xed95('0x72')](_0xed95('0x74'))[_0xed95('0x33')](_0xed95('0x6d'),_0x5cabb1[_0xed95('0x46')]['id'][_0xed95('0x6e')]())['where'](_0xed95('0x75')))['group'](_0xed95('0x60')))['toString']();return db[_0xed95('0x48')][_0xed95('0x28')](_0xbf0832);}return[];})['then'](respondWithResult(_0x2c8cce,null))[_0xed95('0x42')](handleError(_0x2c8cce,null));};exports[_0xed95('0x76')]=function(_0x51d3c1,_0x172948,_0x686cc0){return db[_0xed95('0x77')][_0xed95('0x41')]({'where':{'id':_0x51d3c1[_0xed95('0x46')]['id']}})[_0xed95('0x1e')](handleEntityNotFound(_0x172948,null))['then'](function(_0x5b80f1){var _0x3e12be;if(_0x5b80f1){_0x3e12be=squel['insert']()[_0xed95('0x78')](_0xed95('0x73'))['fromQuery']([_0xed95('0x68'),_0xed95('0x5f'),_0xed95('0x60'),_0xed95('0x61'),_0xed95('0x79'),_0xed95('0x63'),_0xed95('0x64')],squel[_0xed95('0x65')]()['field'](_0xed95('0x67'),_0xed95('0x68'))[_0xed95('0x66')](_0xed95('0x69'),_0xed95('0x5f'))[_0xed95('0x66')]('ANY_VALUE(ContactId)',_0xed95('0x60'))[_0xed95('0x66')](_0xed95('0x6b'),_0xed95('0x61'))[_0xed95('0x66')](_0xed95('0x7a'),_0xed95('0x79'))[_0xed95('0x66')](_0xed95('0x69'),'createdAt')[_0xed95('0x66')]('NOW()',_0xed95('0x64'))['from'](_0xed95('0x74'))[_0xed95('0x33')](_0xed95('0x7b'),_0x51d3c1[_0xed95('0x46')]['id']['toString']())[_0xed95('0x33')]('state\x20=\x20?',_0x51d3c1[_0xed95('0x44')][_0xed95('0x6f')][_0xed95('0x6e')]())[_0xed95('0x33')](_0xed95('0x70'))[_0xed95('0x33')](_0xed95('0x71'),squel['select']()[_0xed95('0x66')]('phone')[_0xed95('0x72')]('cm_hopper')[_0xed95('0x33')](_0xed95('0x7b'),_0x51d3c1[_0xed95('0x46')]['id'][_0xed95('0x6e')]()))['where'](_0xed95('0x71'),squel[_0xed95('0x65')]()[_0xed95('0x66')](_0xed95('0x7c'))['from']('cm_hopper_final')[_0xed95('0x33')](_0xed95('0x7b'),_0x51d3c1[_0xed95('0x46')]['id'][_0xed95('0x6e')]())[_0xed95('0x33')](_0xed95('0x75')))['group'](_0xed95('0x60')))[_0xed95('0x6e')]();return db[_0xed95('0x48')]['query'](_0x3e12be);}return[];})['then'](respondWithResult(_0x172948,null))[_0xed95('0x42')](handleError(_0x172948,null));};exports[_0xed95('0x7d')]=function(_0x1a2ded,_0x2fc676,_0x230ebd){return db[_0xed95('0x7e')]['findOne']({'where':{'VoiceQueueId':_0x1a2ded['body'][_0xed95('0x62')],'CampaignId':_0x1a2ded['body'][_0xed95('0x79')],'ContactId':_0x1a2ded[_0xed95('0x44')][_0xed95('0x60')]}})[_0xed95('0x1e')](handleEntityNotFound(_0x2fc676,null))['then'](function(_0x1c5f76){if(_0x1c5f76){return _0x1c5f76;}return null;})[_0xed95('0x1e')](respondWithResult(_0x2fc676,null))[_0xed95('0x42')](handleError(_0x2fc676,null));};