Built motion from commit 19a84241.|2.5.34
[motion2.git] / server / components / import / cm / csv2.js
index 5df44c1..609e4ae 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x0ccc=['file\x20doesn\x27t\x20exists','parse','binding\x20doesn\x27t\x20exists','ListId\x20is\x20mandatory','body\x20id\x20mandatory','map','uncaughtException','path','lodash','papaparse','util','bluebird','moment','squel','../../../mysqldb','../../../config/logger','import','redis','defaults','localhost','test','select','field','from','join','.id\x20=\x20','\x20=\x20','where','cm_contacts.scheduledat','cm_contacts.id','cm_contacts.ListId','cm_contacts.createdAt','cm_contacts.updatedAt','cm_contacts.priority','cm_contacts.UserId','into','fromQuery','phone','ContactId','ListId','createdAt','updatedAt','priority','UserId','recallme','cm_contacts','.CmListId','.CmListId\x20=\x20?','cm_contacts.createdAt\x20=\x20?','cm_contacts.phone\x20NOT\x20IN\x20(?)','\x22onlyIfOpen\x22','cm_hopper','toString','cm_contacts.phone','insert','cm_hopper_black','fields','ListId\x20=\x20?','\x20=\x20?','concat','keys','CompanyId','deletedAt\x20IS\x20NULL','id\x20IN\x20?','min_id','group','info','Received\x20SIGINT','message','errors','push','code','sequelize','catch','main','send','binding','root','server/files/tmp','.csv','writeFileSync','createReadStream','data','length','rows','replace','scheduledat','isValid','scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)','includes','agent\x20not\x20exists','email','email\x20wrong\x20format','dateOfBirth','YYYY-MM-DD','firstName','\x20not\x20specified','SystemRow','Abort\x20parser','SystemError','duplicates','cm_contacts_','CREATE\x20TABLE\x20','isNil',',\x20scheduledAt\x20=\x20NOW()','DROP\x20TABLE\x20','voice_queues','voice_queues_has_cm_blacklists','VoiceQueueId','campaigns','campaigns_has_cm_blacklists','CampaignId','then','affectedRows','emit','unlink','error','exit','complete','argv'];(function(_0x3c1591,_0x8d3189){var _0x55fdb1=function(_0x2bea8c){while(--_0x2bea8c){_0x3c1591['push'](_0x3c1591['shift']());}};_0x55fdb1(++_0x8d3189);}(_0x0ccc,0x7f));var _0xc0cc=function(_0x58fa0f,_0x4b4d98){_0x58fa0f=_0x58fa0f-0x0;var _0x8a1aac=_0x0ccc[_0x58fa0f];return _0x8a1aac;};'use strict';var fs=require('fs');var path=require(_0xc0cc('0x0'));var _=require(_0xc0cc('0x1'));var Papa=require(_0xc0cc('0x2'));var util=require(_0xc0cc('0x3'));var BPromise=require(_0xc0cc('0x4'));var moment=require(_0xc0cc('0x5'));var squel=require(_0xc0cc('0x6'));var Redis=require('ioredis');var db=require(_0xc0cc('0x7'))['db'];var config=require('../../../config/environment');var logger=require(_0xc0cc('0x8'))(_0xc0cc('0x9'));config[_0xc0cc('0xa')]=_[_0xc0cc('0xb')](config[_0xc0cc('0xa')],{'host':_0xc0cc('0xc'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()['format']('YYYY-MM-DD\x20HH:mm:ss');function isEmail(_0x1a685a){return/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/[_0xc0cc('0xd')](_0x1a685a);}function selectNotIn(_0x241ff1,_0x8686e9,_0x4f1b19,_0x4cb016,_0x3de6b5){return squel[_0xc0cc('0xe')]()[_0xc0cc('0xf')]('phone')[_0xc0cc('0x10')](_0x4cb016)[_0xc0cc('0x11')](_0x8686e9,null,_0x8686e9+_0xc0cc('0x12')+_0x4cb016+'.'+_0x4f1b19)['where'](_0x4cb016+'.'+_0x4f1b19+_0xc0cc('0x13')+_0x241ff1+'.'+_0x4f1b19)[_0xc0cc('0x14')](_0x8686e9+'.dialCheckDuplicateType\x20IN\x20('+_0x3de6b5[_0xc0cc('0x11')]()+')');}function insertCmHopper(_0x39b1b5,_0x38125c,_0xeb7340,_0x531685,_0x58d7c8){var _0x4c4e0c=['cm_contacts.phone',_0xc0cc('0x15'),_0xc0cc('0x16'),_0xc0cc('0x17'),_0x38125c+'.'+_0x531685,_0xc0cc('0x18'),_0xc0cc('0x19'),_0xc0cc('0x1a'),_0xc0cc('0x1b'),'\x22'+_0x58d7c8+'\x22'];return squel['insert']()[_0xc0cc('0x1c')]('cm_hopper')[_0xc0cc('0x1d')]([_0xc0cc('0x1e'),'scheduledat',_0xc0cc('0x1f'),_0xc0cc('0x20'),_0x531685,_0xc0cc('0x21'),_0xc0cc('0x22'),_0xc0cc('0x23'),_0xc0cc('0x24'),_0xc0cc('0x25')],squel[_0xc0cc('0xe')]()['fields'](_0x4c4e0c)['from'](_0x38125c)[_0xc0cc('0x11')](_0xc0cc('0x26'),null,'cm_contacts.ListId\x20=\x20'+_0x38125c+_0xc0cc('0x27'))[_0xc0cc('0x11')](_0xeb7340,null,_0xeb7340+_0xc0cc('0x12')+_0x38125c+'.'+_0x531685)[_0xc0cc('0x14')](_0x38125c+_0xc0cc('0x28'),_0x39b1b5)['where'](_0xc0cc('0x29'),NOW)['where'](_0xc0cc('0x2a'),selectNotIn(_0x38125c,_0xeb7340,_0x531685,'cm_hopper',['\x22always\x22',_0xc0cc('0x2b')]))[_0xc0cc('0x14')](_0xc0cc('0x2a'),selectNotIn(_0x38125c,_0xeb7340,_0x531685,_0xc0cc('0x2c'),['\x22always\x22'])))[_0xc0cc('0x2d')]();}function insertCmHopperBlack(_0x480070,_0x59f8af,_0xb2c46f,_0x40ea93){var _0x15a589=[_0xc0cc('0x2e'),_0xc0cc('0x16'),_0xc0cc('0x17'),_0x59f8af+'.'+_0x40ea93,_0xc0cc('0x18'),_0xc0cc('0x19')];return squel[_0xc0cc('0x2f')]()[_0xc0cc('0x1c')](_0xc0cc('0x30'))[_0xc0cc('0x1d')]([_0xc0cc('0x1e'),_0xc0cc('0x1f'),_0xc0cc('0x20'),_0x40ea93,_0xc0cc('0x21'),_0xc0cc('0x22')],squel['select']()[_0xc0cc('0x31')](_0x15a589)[_0xc0cc('0x10')](_0x59f8af)[_0xc0cc('0x11')](_0xc0cc('0x26'),null,'cm_contacts.ListId\x20=\x20'+_0x59f8af+_0xc0cc('0x27'))['join'](_0xb2c46f,null,_0xb2c46f+'.id\x20=\x20'+_0x59f8af+'.'+_0x40ea93)['where'](_0x59f8af+_0xc0cc('0x28'),_0x480070)[_0xc0cc('0x14')](_0xc0cc('0x29'),NOW)[_0xc0cc('0x14')](_0xc0cc('0x2a'),squel[_0xc0cc('0xe')]()[_0xc0cc('0xf')]('phone')[_0xc0cc('0x10')]('cm_hopper_black')[_0xc0cc('0x14')](_0xc0cc('0x32'),_0x480070)[_0xc0cc('0x14')](_0x40ea93+_0xc0cc('0x33'),_0x59f8af+'.'+_0x40ea93)))[_0xc0cc('0x2d')]();}function insertCmContact(_0x5d21d2,_0xc1d3c3,_0x26e396,_0x41067d){var _0x2267cb=_[_0xc0cc('0x34')](_[_0xc0cc('0x35')](_0x41067d),[_0xc0cc('0x20'),_0xc0cc('0x36'),_0xc0cc('0x21'),'updatedAt']);return squel[_0xc0cc('0x2f')]()[_0xc0cc('0x1c')](_0xc0cc('0x26'))[_0xc0cc('0x1d')](_0x2267cb,squel[_0xc0cc('0xe')]()[_0xc0cc('0x31')](_0x2267cb)[_0xc0cc('0x10')](_0xc1d3c3)[_0xc0cc('0x14')]('('+_0x26e396[_0xc0cc('0x11')](',')+')\x20NOT\x20IN\x20?',squel[_0xc0cc('0xe')]()[_0xc0cc('0x31')](_0x26e396)[_0xc0cc('0x10')](_0xc0cc('0x26'))[_0xc0cc('0x14')](_0xc0cc('0x32'),_0x5d21d2)[_0xc0cc('0x14')](_0xc0cc('0x37')))[_0xc0cc('0x14')](_0xc0cc('0x38'),squel[_0xc0cc('0xe')]()[_0xc0cc('0xf')]('MIN(id)',_0xc0cc('0x39'))[_0xc0cc('0x10')](_0xc1d3c3)[_0xc0cc('0x3a')](_0x26e396['join'](','))))[_0xc0cc('0x2d')]();}function handleStop(){logger[_0xc0cc('0x3b')](_0xc0cc('0x3c'));stopped=!![];}function handleUncaughtException(_0x3290f5){logger['error'](_0x3290f5[_0xc0cc('0x3d')]);process['exit'](0x1);}function handleError(_0x4d8cd2,_0x566b47){stats[_0xc0cc('0x3e')][_0xc0cc('0x3f')]({'name':_0x566b47[_0xc0cc('0x40')],'message':_0x566b47['message'],'row':_0x4d8cd2});}function handleQuery(_0xc72e92){logger[_0xc0cc('0x3b')](_0xc72e92);return db[_0xc0cc('0x41')]['query'](_0xc72e92)['spread'](function(_0x23d28a){results[_0xc0cc('0x3f')](_0x23d28a);})[_0xc0cc('0x42')](function(_0x2a55b0){logger['error'](_0x2a55b0[_0xc0cc('0x3d')]);});}function main(_0x3c260c,_0x286652,_0x55ecc8,_0x30b328){logger[_0xc0cc('0x3b')](_0xc0cc('0x43'),_0x3c260c,JSON['stringify'](_0x286652));process[_0xc0cc('0x44')]('start');var _0x1929f8=_0x286652[_0xc0cc('0x45')];var _0x2e4586=_0x286652['socket_timestamp'];var _0x146f33=path['join'](config[_0xc0cc('0x46')],_0xc0cc('0x47'),_0x2e4586+_0xc0cc('0x48'));var _0x29f579=_[_0xc0cc('0x35')](_0x1929f8)[_0xc0cc('0x11')]();logger['info']('header',_0x29f579);fs[_0xc0cc('0x49')](_0x146f33,_0x29f579+'\x0a');Papa['parse'](fs[_0xc0cc('0x4a')](_0x3c260c),{'header':!![],'skipEmptyLines':!![],'step':function(_0x3f1902,_0x101abc){try{for(var _0x42e646=0x0,_0x1c3ef6=[];_0x42e646<_0x3f1902[_0xc0cc('0x4b')][_0xc0cc('0x4c')];_0x42e646++){stats[_0xc0cc('0x4d')]+=0x1;if(_0x3f1902[_0xc0cc('0x3e')][_0x42e646]){handleError(_0x3f1902[_0xc0cc('0x4b')][_0x42e646],_0x3f1902[_0xc0cc('0x3e')][_0x42e646]);}else{var _0x32b278=[];for(var _0x486f29 in _0x1929f8){if(_0x1929f8['hasOwnProperty'](_0x486f29)){if(_0x3f1902[_0xc0cc('0x4b')][_0x42e646][_0x1929f8[_0x486f29]]){var _0x81395a=_['trim'](_0x3f1902[_0xc0cc('0x4b')][_0x42e646][_0x1929f8[_0x486f29]][_0xc0cc('0x4e')](/"/g,'\x27'));switch(_0x486f29){case _0xc0cc('0x4f'):var _0x8116f2=moment(_0x81395a,'YYYY-MM-DD\x20HH:mm:ss',!![]);if(_0x8116f2[_0xc0cc('0x50')]()){_0x1c3ef6[_0xc0cc('0x3f')]('\x22'+_0x81395a+'\x22');}else{_0x32b278[_0xc0cc('0x3f')](_0xc0cc('0x51'));}break;case _0xc0cc('0x23'):var _0x1b0774=parseInt(_0x81395a);_0x81395a=_['isNaN'](_0x1b0774)||_0x1b0774<0x0||_0x1b0774>0x4?_0x30b328?0x3:0x2:_0x1b0774;_0x1c3ef6[_0xc0cc('0x3f')]('\x22'+_0x81395a+'\x22');break;case _0xc0cc('0x24'):var _0x244079=parseInt(_0x81395a);if(_['isNumber'](_0x244079)&&_[_0xc0cc('0x52')](_0x55ecc8,_0x244079)){_0x1c3ef6[_0xc0cc('0x3f')]('\x22'+_0x244079+'\x22');}else{_0x32b278[_0xc0cc('0x3f')](_0xc0cc('0x53'));}break;case _0xc0cc('0x54'):if(isEmail(_0x81395a)){_0x1c3ef6[_0xc0cc('0x3f')]('\x22'+_0x81395a+'\x22');}else{_0x32b278[_0xc0cc('0x3f')](_0xc0cc('0x55'));}break;case _0xc0cc('0x56'):var _0x309b58=moment(_0x81395a,_0xc0cc('0x57'),!![]);if(_0x309b58['isValid']()){_0x1c3ef6[_0xc0cc('0x3f')]('\x22'+_0x81395a+'\x22');}else{_0x32b278['push']('dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)');}break;default:_0x1c3ef6[_0xc0cc('0x3f')]('\x22'+_0x81395a+'\x22');}}else{switch(_0x486f29){case _0xc0cc('0x58'):case _0xc0cc('0x1e'):_0x32b278['push'](_0x486f29+_0xc0cc('0x59'));break;default:_0x1c3ef6[_0xc0cc('0x3f')]('\x22\x22');}}}}if(_0x32b278[_0xc0cc('0x4c')]){handleError(_0x3f1902[_0xc0cc('0x4b')][_0x42e646],{'code':_0xc0cc('0x5a'),'message':_0x32b278[_0xc0cc('0x11')]()});}else{fs['appendFileSync'](_0x146f33,_0x1c3ef6[_0xc0cc('0x11')]()+'\x0a');}_0x1c3ef6=[];}if(!(stats[_0xc0cc('0x4d')]%0xc8)){socket['emit']('contact:import:'+_0x2e4586,stats);stats[_0xc0cc('0x3e')]=[];}}if(stopped){logger[_0xc0cc('0x3b')](_0xc0cc('0x5b'));_0x101abc['abort']();}}catch(_0x4dd59f){handleError(_0x3f1902[_0xc0cc('0x4b')][_0x42e646],{'code':_0xc0cc('0x5c'),'message':_0x4dd59f[_0xc0cc('0x3d')]});logger['error']('step',_0x4dd59f[_0xc0cc('0x3d')]);}},'complete':function(){try{var _0x32a573=[];var _0x465547=_0xc0cc('0x26');if(_0x286652[_0xc0cc('0x5d')]&&_0x286652[_0xc0cc('0x5d')][_0xc0cc('0x4c')]){_0x465547=_0xc0cc('0x5e')+_0x2e4586;_0x32a573[_0xc0cc('0x3f')](_0xc0cc('0x5f')+_0x465547+'\x20LIKE\x20cm_contacts;');}var _0xed250a=util['format']('LOAD\x20DATA\x20LOCAL\x20INFILE\x20\x27%s\x27\x20INTO\x20TABLE\x20%s\x20CHARACTER\x20SET\x20UTF8\x20FIELDS\x20TERMINATED\x20BY\x20\x27,\x27\x20ENCLOSED\x20BY\x20\x27\x22\x27\x20LINES\x20TERMINATED\x20BY\x20\x27\x0a\x27\x20IGNORE\x201\x20ROWS\x20(%s)\x20SET\x20id\x20=\x20NULL,\x20ListId\x20=\x20%s,\x20CompanyId\x20=\x20%s,\x20createdAt\x20=\x20\x27%s\x27,\x20updatedAt\x20=\x20\x27%s\x27',_0x146f33,_0x465547,_0x29f579,_0x286652[_0xc0cc('0x20')],_0x286652[_0xc0cc('0x36')]||null,NOW,NOW);if(_[_0xc0cc('0x60')](_0x1929f8[_0xc0cc('0x4f')])){_0xed250a+=_0xc0cc('0x61');}_0x32a573[_0xc0cc('0x3f')](_0xed250a);if(_0x286652[_0xc0cc('0x5d')]&&_0x286652[_0xc0cc('0x5d')][_0xc0cc('0x4c')]){_0x32a573[_0xc0cc('0x3f')](insertCmContact(_0x286652[_0xc0cc('0x20')],_0x465547,_0x286652['duplicates'],_0x1929f8));_0x32a573[_0xc0cc('0x3f')](_0xc0cc('0x62')+_0x465547+';');}_0x32a573[_0xc0cc('0x3f')](insertCmHopper(_0x286652[_0xc0cc('0x20')],'voice_queues_has_cm_lists',_0xc0cc('0x63'),'VoiceQueueId',_0x30b328));_0x32a573[_0xc0cc('0x3f')](insertCmHopperBlack(_0x286652[_0xc0cc('0x20')],_0xc0cc('0x64'),_0xc0cc('0x63'),_0xc0cc('0x65')));_0x32a573[_0xc0cc('0x3f')](insertCmHopper(_0x286652['ListId'],'campaigns_has_cm_lists',_0xc0cc('0x66'),'CampaignId',_0x30b328));_0x32a573[_0xc0cc('0x3f')](insertCmHopperBlack(_0x286652[_0xc0cc('0x20')],_0xc0cc('0x67'),_0xc0cc('0x66'),_0xc0cc('0x68')));BPromise['each'](_0x32a573,handleQuery)[_0xc0cc('0x69')](function(_0xcb089b){stats['finish']=!![];stats[_0xc0cc('0x6a')]=_0x286652[_0xc0cc('0x5d')]&&_0x286652[_0xc0cc('0x5d')]['length']?results[0x2]['affectedRows']:results[0x0][_0xc0cc('0x6a')];socket[_0xc0cc('0x6b')]('contact:import:'+_0x2e4586,stats);fs[_0xc0cc('0x6c')](_0x3c260c);fs[_0xc0cc('0x6c')](_0x146f33);process['exit'](0x0);})['catch'](function(_0x552154){logger[_0xc0cc('0x6d')](_0x552154[_0xc0cc('0x3d')]);process[_0xc0cc('0x6e')](0x1);});}catch(_0x2546dd){logger[_0xc0cc('0x6d')](_0xc0cc('0x6f'),_0x2546dd['message']);process[_0xc0cc('0x6e')](0x1);}},'error':function(_0x2d8d2c,_0xc7eeda,_0x19c050,_0x167177){throw new Error(_0x167177);}});}function validate(){if(process['argv']&&process['argv']['length']<0x4){throw new Error('arguments\x20<filename>\x20<parameters>\x20<agents>');}var _0x516fed=path[_0xc0cc('0x11')](config['root'],_0xc0cc('0x47'),process[_0xc0cc('0x70')][0x2]);if(!fs['existsSync'](_0x516fed)){throw new Error(_0xc0cc('0x71'));}var _0xb18055={};try{_0xb18055=JSON[_0xc0cc('0x72')](process[_0xc0cc('0x70')][0x3]);if(!_0xb18055['binding']){throw new Error(_0xc0cc('0x73'));}if(!_0xb18055[_0xc0cc('0x45')]['phone']){throw new Error('phone\x20is\x20mandatory');}if(!_0xb18055['binding'][_0xc0cc('0x58')]){throw new Error('firstName\x20is\x20mandatory');}var _0x3d724d=0x0;if(_0xb18055[_0xc0cc('0x45')]['UserId']){_0x3d724d=0x1;}if(!_0xb18055[_0xc0cc('0x20')]){throw new Error(_0xc0cc('0x74'));}}catch(_0x5a1341){throw new Error(_0xc0cc('0x75'));}var _0x104921=[];try{_0x104921=_[_0xc0cc('0x76')](JSON[_0xc0cc('0x72')](process['argv'][0x4]),'id');}catch(_0x3dcd28){logger['info']('No\x20agent\x20available');}main(_0x516fed,_0xb18055,_0x104921,_0x3d724d);}validate();process['on']('SIGINT',handleStop);process['on'](_0xc0cc('0x77'),handleUncaughtException);
\ No newline at end of file
+var _0xeac6=['papaparse','bluebird','squel','ioredis','../../../mysqldb','../../../config/environment','../../../config/logger','import','localhost','socket.io-emitter','redis','format','YYYY-MM-DD\x20HH:mm:ss','test','select','phone','from','where','.dialCheckDuplicateType\x20IN\x20(','join','cm_contacts.phone','cm_contacts.scheduledat','cm_contacts.ListId','cm_contacts.createdAt','cm_contacts.priority','cm_contacts.UserId','insert','into','cm_hopper','scheduledat','ContactId','ListId','createdAt','priority','UserId','recallme','fields','cm_contacts','cm_contacts.ListId\x20=\x20','.id\x20=\x20','.CmListId\x20=\x20?','cm_contacts.createdAt\x20=\x20?','cm_contacts.phone\x20NOT\x20IN\x20(?)','\x22always\x22','\x22onlyIfOpen\x22','toString','cm_contacts.id','cm_contacts.updatedAt','cm_hopper_black','updatedAt','field','\x20=\x20?','concat','keys','fromQuery',')\x20NOT\x20IN\x20?','ListId\x20=\x20?','deletedAt\x20IS\x20NULL','id\x20IN\x20?','MIN(id)','min_id','group','info','Received\x20SIGINT','error','message','exit','errors','push','sequelize','spread','catch','main','stringify','send','binding','server/files/tmp','writeFileSync','parse','createReadStream','data','length','rows','hasOwnProperty','trim','replace','isValid','isNumber','agent\x20not\x20exists','email','email\x20wrong\x20format','YYYY-MM-DD','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','firstName','\x20not\x20specified','emit','contact:import:','Abort\x20parser','abort','SystemError','step','duplicates','\x20LIKE\x20cm_contacts;','LOAD\x20DATA\x20LOCAL\x20INFILE\x20\x27%s\x27\x20INTO\x20TABLE\x20%s\x20CHARACTER\x20SET\x20UTF8\x20FIELDS\x20TERMINATED\x20BY\x20\x27,\x27\x20ENCLOSED\x20BY\x20\x27\x22\x27\x20LINES\x20TERMINATED\x20BY\x20\x27\x0a\x27\x20IGNORE\x201\x20ROWS\x20(%s)\x20SET\x20id\x20=\x20NULL,\x20ListId\x20=\x20%s,\x20CompanyId\x20=\x20%s,\x20createdAt\x20=\x20\x27%s\x27,\x20updatedAt\x20=\x20\x27%s\x27','CompanyId','isNil','voice_queues_has_cm_lists','VoiceQueueId','voice_queues_has_cm_blacklists','campaigns_has_cm_lists','campaigns','CampaignId','each','finish','unlink','complete','argv','root','existsSync','file\x20doesn\x27t\x20exists','binding\x20doesn\x27t\x20exists','firstName\x20is\x20mandatory','ListId\x20is\x20mandatory','body\x20id\x20mandatory','map','SIGINT','uncaughtException','lodash'];(function(_0x44c9f8,_0x130004){var _0x1ff0d5=function(_0x2f8d9c){while(--_0x2f8d9c){_0x44c9f8['push'](_0x44c9f8['shift']());}};_0x1ff0d5(++_0x130004);}(_0xeac6,0x7f));var _0x6eac=function(_0x4ccfd6,_0x43fecf){_0x4ccfd6=_0x4ccfd6-0x0;var _0x80beb6=_0xeac6[_0x4ccfd6];return _0x80beb6;};'use strict';var fs=require('fs');var path=require('path');var _=require(_0x6eac('0x0'));var Papa=require(_0x6eac('0x1'));var util=require('util');var BPromise=require(_0x6eac('0x2'));var moment=require('moment');var squel=require(_0x6eac('0x3'));var Redis=require(_0x6eac('0x4'));var db=require(_0x6eac('0x5'))['db'];var config=require(_0x6eac('0x6'));var logger=require(_0x6eac('0x7'))(_0x6eac('0x8'));config['redis']=_['defaults'](config['redis'],{'host':_0x6eac('0x9'),'port':0x18eb});var socket=require(_0x6eac('0xa'))(new Redis(config[_0x6eac('0xb')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()[_0x6eac('0xc')](_0x6eac('0xd'));function isEmail(_0x189e32){return/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/[_0x6eac('0xe')](_0x189e32);}function selectNotIn(_0x198c05,_0x3c857f,_0x508e6c,_0x4bf461,_0x96c574){return squel[_0x6eac('0xf')]()['field'](_0x6eac('0x10'))[_0x6eac('0x11')](_0x4bf461)['join'](_0x3c857f,null,_0x3c857f+'.id\x20=\x20'+_0x4bf461+'.'+_0x508e6c)['where'](_0x4bf461+'.'+_0x508e6c+'\x20=\x20'+_0x198c05+'.'+_0x508e6c)[_0x6eac('0x12')](_0x3c857f+_0x6eac('0x13')+_0x96c574[_0x6eac('0x14')]()+')');}function insertCmHopper(_0x48a6fa,_0xafaa39,_0x450528,_0x566e81,_0x4f6f11){var _0x558fd0=[_0x6eac('0x15'),_0x6eac('0x16'),'cm_contacts.id',_0x6eac('0x17'),_0xafaa39+'.'+_0x566e81,_0x6eac('0x18'),'cm_contacts.updatedAt',_0x6eac('0x19'),_0x6eac('0x1a'),'\x22'+_0x4f6f11+'\x22'];return squel[_0x6eac('0x1b')]()[_0x6eac('0x1c')](_0x6eac('0x1d'))['fromQuery']([_0x6eac('0x10'),_0x6eac('0x1e'),_0x6eac('0x1f'),_0x6eac('0x20'),_0x566e81,_0x6eac('0x21'),'updatedAt',_0x6eac('0x22'),_0x6eac('0x23'),_0x6eac('0x24')],squel[_0x6eac('0xf')]()[_0x6eac('0x25')](_0x558fd0)['from'](_0xafaa39)[_0x6eac('0x14')](_0x6eac('0x26'),null,_0x6eac('0x27')+_0xafaa39+'.CmListId')[_0x6eac('0x14')](_0x450528,null,_0x450528+_0x6eac('0x28')+_0xafaa39+'.'+_0x566e81)[_0x6eac('0x12')](_0xafaa39+_0x6eac('0x29'),_0x48a6fa)[_0x6eac('0x12')](_0x6eac('0x2a'),NOW)['where'](_0x6eac('0x2b'),selectNotIn(_0xafaa39,_0x450528,_0x566e81,_0x6eac('0x1d'),[_0x6eac('0x2c'),_0x6eac('0x2d')]))[_0x6eac('0x12')](_0x6eac('0x2b'),selectNotIn(_0xafaa39,_0x450528,_0x566e81,'cm_hopper',[_0x6eac('0x2c')])))[_0x6eac('0x2e')]();}function insertCmHopperBlack(_0x1fafb3,_0x1849b2,_0x56f994,_0x2a7e54){var _0x123f8d=[_0x6eac('0x15'),_0x6eac('0x2f'),_0x6eac('0x17'),_0x1849b2+'.'+_0x2a7e54,'cm_contacts.createdAt',_0x6eac('0x30')];return squel[_0x6eac('0x1b')]()['into'](_0x6eac('0x31'))['fromQuery'](['phone',_0x6eac('0x1f'),_0x6eac('0x20'),_0x2a7e54,_0x6eac('0x21'),_0x6eac('0x32')],squel[_0x6eac('0xf')]()[_0x6eac('0x25')](_0x123f8d)[_0x6eac('0x11')](_0x1849b2)[_0x6eac('0x14')]('cm_contacts',null,_0x6eac('0x27')+_0x1849b2+'.CmListId')[_0x6eac('0x14')](_0x56f994,null,_0x56f994+_0x6eac('0x28')+_0x1849b2+'.'+_0x2a7e54)[_0x6eac('0x12')](_0x1849b2+_0x6eac('0x29'),_0x1fafb3)[_0x6eac('0x12')](_0x6eac('0x2a'),NOW)[_0x6eac('0x12')](_0x6eac('0x2b'),squel[_0x6eac('0xf')]()[_0x6eac('0x33')](_0x6eac('0x10'))[_0x6eac('0x11')](_0x6eac('0x31'))[_0x6eac('0x12')]('ListId\x20=\x20?',_0x1fafb3)['where'](_0x2a7e54+_0x6eac('0x34'),_0x1849b2+'.'+_0x2a7e54)))['toString']();}function insertCmContact(_0x4bdf4a,_0x340753,_0x1380c6,_0x322931){var _0x5f319c=_[_0x6eac('0x35')](_[_0x6eac('0x36')](_0x322931),['ListId','CompanyId',_0x6eac('0x21'),_0x6eac('0x32')]);return squel['insert']()[_0x6eac('0x1c')](_0x6eac('0x26'))[_0x6eac('0x37')](_0x5f319c,squel[_0x6eac('0xf')]()['fields'](_0x5f319c)[_0x6eac('0x11')](_0x340753)['where']('('+_0x1380c6[_0x6eac('0x14')](',')+_0x6eac('0x38'),squel[_0x6eac('0xf')]()['fields'](_0x1380c6)[_0x6eac('0x11')](_0x6eac('0x26'))[_0x6eac('0x12')](_0x6eac('0x39'),_0x4bdf4a)[_0x6eac('0x12')](_0x6eac('0x3a')))[_0x6eac('0x12')](_0x6eac('0x3b'),squel[_0x6eac('0xf')]()[_0x6eac('0x33')](_0x6eac('0x3c'),_0x6eac('0x3d'))[_0x6eac('0x11')](_0x340753)[_0x6eac('0x3e')](_0x1380c6[_0x6eac('0x14')](','))))[_0x6eac('0x2e')]();}function handleStop(){logger[_0x6eac('0x3f')](_0x6eac('0x40'));stopped=!![];}function handleUncaughtException(_0x2ef38f){logger[_0x6eac('0x41')](_0x2ef38f[_0x6eac('0x42')]);process[_0x6eac('0x43')](0x1);}function handleError(_0x175f37,_0x396845){stats[_0x6eac('0x44')][_0x6eac('0x45')]({'name':_0x396845['code'],'message':_0x396845[_0x6eac('0x42')],'row':_0x175f37});}function handleQuery(_0x4b734b){logger[_0x6eac('0x3f')](_0x4b734b);return db[_0x6eac('0x46')]['query'](_0x4b734b)[_0x6eac('0x47')](function(_0x3d7012){results['push'](_0x3d7012);})[_0x6eac('0x48')](function(_0x6db618){logger[_0x6eac('0x41')](_0x6db618['message']);});}function main(_0x43d9b1,_0x4c34b2,_0x5e808a,_0xdb2feb){logger['info'](_0x6eac('0x49'),_0x43d9b1,JSON[_0x6eac('0x4a')](_0x4c34b2));process[_0x6eac('0x4b')]('start');var _0x533f48=_0x4c34b2[_0x6eac('0x4c')];var _0x3ace76=_0x4c34b2['socket_timestamp'];var _0x1a6cdd=path[_0x6eac('0x14')](config['root'],_0x6eac('0x4d'),_0x3ace76+'.csv');var _0x43009c=_[_0x6eac('0x36')](_0x533f48)[_0x6eac('0x14')]();logger[_0x6eac('0x3f')]('header',_0x43009c);fs[_0x6eac('0x4e')](_0x1a6cdd,_0x43009c+'\x0a');Papa[_0x6eac('0x4f')](fs[_0x6eac('0x50')](_0x43d9b1),{'header':!![],'skipEmptyLines':!![],'step':function(_0xec87bb,_0x418f0b){try{for(var _0x53c210=0x0,_0x542625=[];_0x53c210<_0xec87bb[_0x6eac('0x51')][_0x6eac('0x52')];_0x53c210++){stats[_0x6eac('0x53')]+=0x1;if(_0xec87bb[_0x6eac('0x44')][_0x53c210]){handleError(_0xec87bb[_0x6eac('0x51')][_0x53c210],_0xec87bb[_0x6eac('0x44')][_0x53c210]);}else{var _0x60aa7b=[];for(var _0x48a3e6 in _0x533f48){if(_0x533f48[_0x6eac('0x54')](_0x48a3e6)){if(_0xec87bb[_0x6eac('0x51')][_0x53c210][_0x533f48[_0x48a3e6]]){var _0x49c964=_[_0x6eac('0x55')](_0xec87bb[_0x6eac('0x51')][_0x53c210][_0x533f48[_0x48a3e6]][_0x6eac('0x56')](/"/g,'\x27'));switch(_0x48a3e6){case _0x6eac('0x1e'):var _0x5e5203=moment(_0x49c964,_0x6eac('0xd'),!![]);if(_0x5e5203[_0x6eac('0x57')]()){_0x542625[_0x6eac('0x45')]('\x22'+_0x49c964+'\x22');}else{_0x60aa7b[_0x6eac('0x45')]('scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)');}break;case'priority':var _0x2c55bb=parseInt(_0x49c964);_0x49c964=_['isNaN'](_0x2c55bb)||_0x2c55bb<0x0||_0x2c55bb>0x4?_0xdb2feb?0x3:0x2:_0x2c55bb;_0x542625['push']('\x22'+_0x49c964+'\x22');break;case'UserId':var _0x5782d7=parseInt(_0x49c964);if(_[_0x6eac('0x58')](_0x5782d7)&&_['includes'](_0x5e808a,_0x5782d7)){_0x542625['push']('\x22'+_0x5782d7+'\x22');}else{_0x60aa7b[_0x6eac('0x45')](_0x6eac('0x59'));}break;case _0x6eac('0x5a'):if(isEmail(_0x49c964)){_0x542625[_0x6eac('0x45')]('\x22'+_0x49c964+'\x22');}else{_0x60aa7b[_0x6eac('0x45')](_0x6eac('0x5b'));}break;case'dateOfBirth':var _0x3f8295=moment(_0x49c964,_0x6eac('0x5c'),!![]);if(_0x3f8295[_0x6eac('0x57')]()){_0x542625['push']('\x22'+_0x49c964+'\x22');}else{_0x60aa7b['push'](_0x6eac('0x5d'));}break;default:_0x542625[_0x6eac('0x45')]('\x22'+_0x49c964+'\x22');}}else{switch(_0x48a3e6){case _0x6eac('0x5e'):case _0x6eac('0x10'):_0x60aa7b[_0x6eac('0x45')](_0x48a3e6+_0x6eac('0x5f'));break;default:_0x542625[_0x6eac('0x45')]('\x22\x22');}}}}if(_0x60aa7b['length']){handleError(_0xec87bb[_0x6eac('0x51')][_0x53c210],{'code':'SystemRow','message':_0x60aa7b['join']()});}else{fs['appendFileSync'](_0x1a6cdd,_0x542625[_0x6eac('0x14')]()+'\x0a');}_0x542625=[];}if(!(stats[_0x6eac('0x53')]%0xc8)){socket[_0x6eac('0x60')](_0x6eac('0x61')+_0x3ace76,stats);stats[_0x6eac('0x44')]=[];}}if(stopped){logger[_0x6eac('0x3f')](_0x6eac('0x62'));_0x418f0b[_0x6eac('0x63')]();}}catch(_0x294cc6){handleError(_0xec87bb[_0x6eac('0x51')][_0x53c210],{'code':_0x6eac('0x64'),'message':_0x294cc6[_0x6eac('0x42')]});logger[_0x6eac('0x41')](_0x6eac('0x65'),_0x294cc6[_0x6eac('0x42')]);}},'complete':function(){try{var _0x4a1a5a=[];var _0x3fde3a='cm_contacts';if(_0x4c34b2[_0x6eac('0x66')]&&_0x4c34b2[_0x6eac('0x66')][_0x6eac('0x52')]){_0x3fde3a='cm_contacts_'+_0x3ace76;_0x4a1a5a[_0x6eac('0x45')]('CREATE\x20TABLE\x20'+_0x3fde3a+_0x6eac('0x67'));}var _0x2634f5=util[_0x6eac('0xc')](_0x6eac('0x68'),_0x1a6cdd,_0x3fde3a,_0x43009c,_0x4c34b2[_0x6eac('0x20')],_0x4c34b2[_0x6eac('0x69')]||null,NOW,NOW);if(_[_0x6eac('0x6a')](_0x533f48[_0x6eac('0x1e')])){_0x2634f5+=',\x20scheduledAt\x20=\x20NOW()';}_0x4a1a5a['push'](_0x2634f5);if(_0x4c34b2['duplicates']&&_0x4c34b2[_0x6eac('0x66')][_0x6eac('0x52')]){_0x4a1a5a[_0x6eac('0x45')](insertCmContact(_0x4c34b2[_0x6eac('0x20')],_0x3fde3a,_0x4c34b2[_0x6eac('0x66')],_0x533f48));_0x4a1a5a[_0x6eac('0x45')]('DROP\x20TABLE\x20'+_0x3fde3a+';');}_0x4a1a5a[_0x6eac('0x45')](insertCmHopper(_0x4c34b2[_0x6eac('0x20')],_0x6eac('0x6b'),'voice_queues',_0x6eac('0x6c'),_0xdb2feb));_0x4a1a5a[_0x6eac('0x45')](insertCmHopperBlack(_0x4c34b2[_0x6eac('0x20')],_0x6eac('0x6d'),'voice_queues',_0x6eac('0x6c')));_0x4a1a5a[_0x6eac('0x45')](insertCmHopper(_0x4c34b2['ListId'],_0x6eac('0x6e'),_0x6eac('0x6f'),_0x6eac('0x70'),_0xdb2feb));_0x4a1a5a[_0x6eac('0x45')](insertCmHopperBlack(_0x4c34b2[_0x6eac('0x20')],'campaigns_has_cm_blacklists',_0x6eac('0x6f'),_0x6eac('0x70')));BPromise[_0x6eac('0x71')](_0x4a1a5a,handleQuery)['then'](function(_0x56f801){stats[_0x6eac('0x72')]=!![];stats['affectedRows']=_0x4c34b2[_0x6eac('0x66')]&&_0x4c34b2[_0x6eac('0x66')][_0x6eac('0x52')]?results[0x2]['affectedRows']:results[0x0]['affectedRows'];socket['emit']('contact:import:'+_0x3ace76,stats);fs[_0x6eac('0x73')](_0x43d9b1);fs[_0x6eac('0x73')](_0x1a6cdd);process[_0x6eac('0x43')](0x0);})[_0x6eac('0x48')](function(_0xbe9076){logger[_0x6eac('0x41')](_0xbe9076[_0x6eac('0x42')]);process[_0x6eac('0x43')](0x1);});}catch(_0x2d8eca){logger[_0x6eac('0x41')](_0x6eac('0x74'),_0x2d8eca[_0x6eac('0x42')]);process[_0x6eac('0x43')](0x1);}},'error':function(_0x226928,_0x51135c,_0x42f294,_0xac9b2e){throw new Error(_0xac9b2e);}});}function validate(){if(process[_0x6eac('0x75')]&&process[_0x6eac('0x75')][_0x6eac('0x52')]<0x4){throw new Error('arguments\x20<filename>\x20<parameters>\x20<agents>');}var _0x4598dd=path[_0x6eac('0x14')](config[_0x6eac('0x76')],_0x6eac('0x4d'),process[_0x6eac('0x75')][0x2]);if(!fs[_0x6eac('0x77')](_0x4598dd)){throw new Error(_0x6eac('0x78'));}var _0x3a4a6d={};try{_0x3a4a6d=JSON[_0x6eac('0x4f')](process[_0x6eac('0x75')][0x3]);if(!_0x3a4a6d[_0x6eac('0x4c')]){throw new Error(_0x6eac('0x79'));}if(!_0x3a4a6d[_0x6eac('0x4c')]['phone']){throw new Error('phone\x20is\x20mandatory');}if(!_0x3a4a6d[_0x6eac('0x4c')][_0x6eac('0x5e')]){throw new Error(_0x6eac('0x7a'));}var _0x4f9fb7=0x0;if(_0x3a4a6d[_0x6eac('0x4c')][_0x6eac('0x23')]){_0x4f9fb7=0x1;}if(!_0x3a4a6d[_0x6eac('0x20')]){throw new Error(_0x6eac('0x7b'));}}catch(_0x137fb1){throw new Error(_0x6eac('0x7c'));}var _0x10e087=[];try{_0x10e087=_[_0x6eac('0x7d')](JSON['parse'](process['argv'][0x4]),'id');}catch(_0x397c5d){logger[_0x6eac('0x3f')]('No\x20agent\x20available');}main(_0x4598dd,_0x3a4a6d,_0x10e087,_0x4f9fb7);}validate();process['on'](_0x6eac('0x7e'),handleStop);process['on'](_0x6eac('0x7f'),handleUncaughtException);
\ No newline at end of file