Built motion from commit (unavailable).|2.5.30
[motion2.git] / server / components / import / cm / csv2.js
index 7f3971f..d3bf9fa 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x9346=['message','exit','errors','code','sequelize','query','spread','push','catch','send','start','binding','root','server/files/tmp','.csv','header','writeFileSync','parse','createReadStream','data','length','rows','hasOwnProperty','trim','YYYY-MM-DD\x20HH:mm:ss','isValid','scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)','isNaN','isNumber','includes','agent\x20not\x20exists','email','dateOfBirth','YYYY-MM-DD','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','firstName','appendFileSync','contact:import:','Abort\x20parser','abort','SystemError','step','duplicates','CREATE\x20TABLE\x20','\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','isNil',',\x20scheduledAt\x20=\x20NOW()','DROP\x20TABLE\x20','voice_queues_has_cm_lists','voice_queues','VoiceQueueId','voice_queues_has_cm_blacklists','campaigns_has_cm_lists','campaigns','CampaignId','campaigns_has_cm_blacklists','each','then','finish','affectedRows','emit','unlink','complete','arguments\x20<filename>\x20<parameters>\x20<agents>','argv','existsSync','file\x20doesn\x27t\x20exists','binding\x20doesn\x27t\x20exists','phone\x20is\x20mandatory','firstName\x20is\x20mandatory','ListId\x20is\x20mandatory','body\x20id\x20mandatory','map','No\x20agent\x20available','SIGINT','uncaughtException','path','util','bluebird','moment','squel','ioredis','../../../mysqldb','../../../config/environment','../../../config/logger','import','redis','defaults','localhost','socket.io-emitter','format','test','select','phone','join','.id\x20=\x20','where','cm_contacts.phone','cm_contacts.scheduledat','cm_contacts.ListId','cm_contacts.updatedAt','cm_contacts.priority','cm_contacts.UserId','into','cm_hopper','fromQuery','scheduledat','ListId','createdAt','updatedAt','priority','UserId','recallme','from','cm_contacts','cm_contacts.ListId\x20=\x20','.CmListId','cm_contacts.createdAt\x20=\x20?','cm_contacts.phone\x20NOT\x20IN\x20(?)','\x22always\x22','\x22onlyIfOpen\x22','toString','cm_contacts.createdAt','insert','cm_hopper_black','ContactId','fields','field','ListId\x20=\x20?','\x20=\x20?','keys','CompanyId','deletedAt\x20IS\x20NULL','group','info','error'];(function(_0x2d7f0e,_0x3e6fbd){var _0x110b07=function(_0x557b00){while(--_0x557b00){_0x2d7f0e['push'](_0x2d7f0e['shift']());}};_0x110b07(++_0x3e6fbd);}(_0x9346,0xd6));var _0x6934=function(_0x2f0336,_0x7eb379){_0x2f0336=_0x2f0336-0x0;var _0x4745b1=_0x9346[_0x2f0336];return _0x4745b1;};'use strict';var fs=require('fs');var path=require(_0x6934('0x0'));var _=require('lodash');var Papa=require('papaparse');var util=require(_0x6934('0x1'));var BPromise=require(_0x6934('0x2'));var moment=require(_0x6934('0x3'));var squel=require(_0x6934('0x4'));var Redis=require(_0x6934('0x5'));var db=require(_0x6934('0x6'))['db'];var config=require(_0x6934('0x7'));var logger=require(_0x6934('0x8'))(_0x6934('0x9'));config[_0x6934('0xa')]=_[_0x6934('0xb')](config[_0x6934('0xa')],{'host':_0x6934('0xc'),'port':0x18eb});var socket=require(_0x6934('0xd'))(new Redis(config[_0x6934('0xa')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()[_0x6934('0xe')]('YYYY-MM-DD\x20HH:mm:ss');function isEmail(_0x5e51b6){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))*$/[_0x6934('0xf')](_0x5e51b6);}function selectNotIn(_0x2f2ce5,_0x203ba6,_0xa6de0d,_0x1850e4,_0x34298c){return squel[_0x6934('0x10')]()['field'](_0x6934('0x11'))['from'](_0x1850e4)[_0x6934('0x12')](_0x203ba6,null,_0x203ba6+_0x6934('0x13')+_0x1850e4+'.'+_0xa6de0d)['where'](_0x1850e4+'.'+_0xa6de0d+'\x20=\x20'+_0x2f2ce5+'.'+_0xa6de0d)[_0x6934('0x14')](_0x203ba6+'.dialCheckDuplicateType\x20IN\x20('+_0x34298c[_0x6934('0x12')]()+')');}function insertCmHopper(_0x151434,_0x3bdd67,_0x2fbbd8,_0x388568,_0x4867c5){var _0x503832=[_0x6934('0x15'),_0x6934('0x16'),'cm_contacts.id',_0x6934('0x17'),_0x3bdd67+'.'+_0x388568,'cm_contacts.createdAt',_0x6934('0x18'),_0x6934('0x19'),_0x6934('0x1a'),'\x22'+_0x4867c5+'\x22'];return squel['insert']()[_0x6934('0x1b')](_0x6934('0x1c'))[_0x6934('0x1d')]([_0x6934('0x11'),_0x6934('0x1e'),'ContactId',_0x6934('0x1f'),_0x388568,_0x6934('0x20'),_0x6934('0x21'),_0x6934('0x22'),_0x6934('0x23'),_0x6934('0x24')],squel[_0x6934('0x10')]()['fields'](_0x503832)[_0x6934('0x25')](_0x3bdd67)[_0x6934('0x12')](_0x6934('0x26'),null,_0x6934('0x27')+_0x3bdd67+_0x6934('0x28'))[_0x6934('0x12')](_0x2fbbd8,null,_0x2fbbd8+'.id\x20=\x20'+_0x3bdd67+'.'+_0x388568)[_0x6934('0x14')](_0x3bdd67+'.CmListId\x20=\x20?',_0x151434)[_0x6934('0x14')](_0x6934('0x29'),NOW)[_0x6934('0x14')](_0x6934('0x2a'),selectNotIn(_0x3bdd67,_0x2fbbd8,_0x388568,_0x6934('0x1c'),[_0x6934('0x2b'),_0x6934('0x2c')]))['where'](_0x6934('0x2a'),selectNotIn(_0x3bdd67,_0x2fbbd8,_0x388568,_0x6934('0x1c'),[_0x6934('0x2b')])))[_0x6934('0x2d')]();}function insertCmHopperBlack(_0x4f6789,_0x36a5d6,_0x217b42,_0x34a032){var _0x16c34d=[_0x6934('0x15'),'cm_contacts.id',_0x6934('0x17'),_0x36a5d6+'.'+_0x34a032,_0x6934('0x2e'),'cm_contacts.updatedAt'];return squel[_0x6934('0x2f')]()[_0x6934('0x1b')](_0x6934('0x30'))[_0x6934('0x1d')](['phone',_0x6934('0x31'),_0x6934('0x1f'),_0x34a032,_0x6934('0x20'),_0x6934('0x21')],squel['select']()[_0x6934('0x32')](_0x16c34d)['from'](_0x36a5d6)[_0x6934('0x12')](_0x6934('0x26'),null,'cm_contacts.ListId\x20=\x20'+_0x36a5d6+_0x6934('0x28'))[_0x6934('0x12')](_0x217b42,null,_0x217b42+'.id\x20=\x20'+_0x36a5d6+'.'+_0x34a032)[_0x6934('0x14')](_0x36a5d6+'.CmListId\x20=\x20?',_0x4f6789)[_0x6934('0x14')](_0x6934('0x29'),NOW)['where'](_0x6934('0x2a'),squel[_0x6934('0x10')]()[_0x6934('0x33')](_0x6934('0x11'))[_0x6934('0x25')](_0x6934('0x30'))[_0x6934('0x14')](_0x6934('0x34'),_0x4f6789)[_0x6934('0x14')](_0x34a032+_0x6934('0x35'),_0x36a5d6+'.'+_0x34a032)))[_0x6934('0x2d')]();}function insertCmContact(_0x554e82,_0x38fa81,_0x3c7911,_0x5a4680){var _0x1e9f14=_['concat'](_[_0x6934('0x36')](_0x5a4680),[_0x6934('0x1f'),_0x6934('0x37'),_0x6934('0x20'),_0x6934('0x21')]);return squel[_0x6934('0x2f')]()[_0x6934('0x1b')](_0x6934('0x26'))[_0x6934('0x1d')](_0x1e9f14,squel[_0x6934('0x10')]()[_0x6934('0x32')](_0x1e9f14)[_0x6934('0x25')](_0x38fa81)['where']('('+_0x3c7911['join'](',')+')\x20NOT\x20IN\x20?',squel['select']()[_0x6934('0x32')](_0x3c7911)[_0x6934('0x25')](_0x6934('0x26'))[_0x6934('0x14')](_0x6934('0x34'),_0x554e82)[_0x6934('0x14')](_0x6934('0x38')))[_0x6934('0x14')]('id\x20IN\x20?',squel[_0x6934('0x10')]()[_0x6934('0x33')]('MIN(id)','min_id')[_0x6934('0x25')](_0x38fa81)[_0x6934('0x39')](_0x3c7911['join'](','))))[_0x6934('0x2d')]();}function handleStop(){logger[_0x6934('0x3a')]('Received\x20SIGINT');stopped=!![];}function handleUncaughtException(_0x499040){logger[_0x6934('0x3b')](_0x499040[_0x6934('0x3c')]);process[_0x6934('0x3d')](0x1);}function handleError(_0x310c14,_0x3e35e5){stats[_0x6934('0x3e')]['push']({'name':_0x3e35e5[_0x6934('0x3f')],'message':_0x3e35e5['message'],'row':_0x310c14});}function handleQuery(_0x2e9c44){logger[_0x6934('0x3a')](_0x2e9c44);return db[_0x6934('0x40')][_0x6934('0x41')](_0x2e9c44)[_0x6934('0x42')](function(_0x3b5509){results[_0x6934('0x43')](_0x3b5509);})[_0x6934('0x44')](function(_0x34bcd0){logger[_0x6934('0x3b')](_0x34bcd0[_0x6934('0x3c')]);});}function main(_0x234bd6,_0x4d9fc8,_0x5cfdc3,_0x58296d){logger[_0x6934('0x3a')]('main',_0x234bd6,JSON['stringify'](_0x4d9fc8));process[_0x6934('0x45')](_0x6934('0x46'));var _0xec47bb=_0x4d9fc8[_0x6934('0x47')];var _0x1868bd=_0x4d9fc8['socket_timestamp'];var _0x5ce2df=path[_0x6934('0x12')](config[_0x6934('0x48')],_0x6934('0x49'),_0x1868bd+_0x6934('0x4a'));var _0x2ce23f=_['keys'](_0xec47bb)['join']();logger[_0x6934('0x3a')](_0x6934('0x4b'),_0x2ce23f);fs[_0x6934('0x4c')](_0x5ce2df,_0x2ce23f+'\x0a');Papa[_0x6934('0x4d')](fs[_0x6934('0x4e')](_0x234bd6),{'header':!![],'skipEmptyLines':!![],'step':function(_0x10144f,_0x1c287d){try{for(var _0xa20cf3=0x0,_0x156b18=[];_0xa20cf3<_0x10144f[_0x6934('0x4f')][_0x6934('0x50')];_0xa20cf3++){stats[_0x6934('0x51')]+=0x1;if(_0x10144f[_0x6934('0x3e')][_0xa20cf3]){handleError(_0x10144f[_0x6934('0x4f')][_0xa20cf3],_0x10144f['errors'][_0xa20cf3]);}else{var _0x19805a=[];for(var _0x5aa493 in _0xec47bb){if(_0xec47bb[_0x6934('0x52')](_0x5aa493)){if(_0x10144f[_0x6934('0x4f')][_0xa20cf3][_0xec47bb[_0x5aa493]]){var _0x2cfb52=_[_0x6934('0x53')](_0x10144f['data'][_0xa20cf3][_0xec47bb[_0x5aa493]]['replace'](/"/g,'\x27'));switch(_0x5aa493){case _0x6934('0x1e'):var _0x422e82=moment(_0x2cfb52,_0x6934('0x54'),!![]);if(_0x422e82[_0x6934('0x55')]()){_0x156b18[_0x6934('0x43')]('\x22'+_0x2cfb52+'\x22');}else{_0x19805a[_0x6934('0x43')](_0x6934('0x56'));}break;case _0x6934('0x22'):var _0x1d535d=parseInt(_0x2cfb52);_0x2cfb52=_[_0x6934('0x57')](_0x1d535d)||_0x1d535d<0x0||_0x1d535d>0x4?_0x58296d?0x3:0x2:_0x1d535d;_0x156b18[_0x6934('0x43')]('\x22'+_0x2cfb52+'\x22');break;case _0x6934('0x23'):var _0x3574d0=parseInt(_0x2cfb52);if(_[_0x6934('0x58')](_0x3574d0)&&_[_0x6934('0x59')](_0x5cfdc3,_0x3574d0)){_0x156b18[_0x6934('0x43')]('\x22'+_0x3574d0+'\x22');}else{_0x19805a['push'](_0x6934('0x5a'));}break;case _0x6934('0x5b'):if(isEmail(_0x2cfb52)){_0x156b18[_0x6934('0x43')]('\x22'+_0x2cfb52+'\x22');}else{_0x19805a[_0x6934('0x43')]('email\x20wrong\x20format');}break;case _0x6934('0x5c'):var _0x5c7878=moment(_0x2cfb52,_0x6934('0x5d'),!![]);if(_0x5c7878[_0x6934('0x55')]()){_0x156b18[_0x6934('0x43')]('\x22'+_0x2cfb52+'\x22');}else{_0x19805a['push'](_0x6934('0x5e'));}break;default:_0x156b18[_0x6934('0x43')]('\x22'+_0x2cfb52+'\x22');}}else{switch(_0x5aa493){case _0x6934('0x5f'):case _0x6934('0x11'):_0x19805a[_0x6934('0x43')](_0x5aa493+'\x20not\x20specified');break;default:_0x156b18[_0x6934('0x43')]('\x22\x22');}}}}if(_0x19805a[_0x6934('0x50')]){handleError(_0x10144f[_0x6934('0x4f')][_0xa20cf3],{'code':'SystemRow','message':_0x19805a[_0x6934('0x12')]()});}else{fs[_0x6934('0x60')](_0x5ce2df,_0x156b18[_0x6934('0x12')]()+'\x0a');}_0x156b18=[];}if(!(stats[_0x6934('0x51')]%0xc8)){socket['emit'](_0x6934('0x61')+_0x1868bd,stats);stats['errors']=[];}}if(stopped){logger[_0x6934('0x3a')](_0x6934('0x62'));_0x1c287d[_0x6934('0x63')]();}}catch(_0x4d3ca4){handleError(_0x10144f[_0x6934('0x4f')][_0xa20cf3],{'code':_0x6934('0x64'),'message':_0x4d3ca4[_0x6934('0x3c')]});logger[_0x6934('0x3b')](_0x6934('0x65'),_0x4d3ca4[_0x6934('0x3c')]);}},'complete':function(){try{var _0x336eee=[];var _0x230c4b='cm_contacts';if(_0x4d9fc8[_0x6934('0x66')]&&_0x4d9fc8[_0x6934('0x66')][_0x6934('0x50')]){_0x230c4b='cm_contacts_'+_0x1868bd;_0x336eee[_0x6934('0x43')](_0x6934('0x67')+_0x230c4b+_0x6934('0x68'));}var _0x54401f=util[_0x6934('0xe')](_0x6934('0x69'),_0x5ce2df,_0x230c4b,_0x2ce23f,_0x4d9fc8[_0x6934('0x1f')],_0x4d9fc8[_0x6934('0x37')]||null,NOW,NOW);if(_[_0x6934('0x6a')](_0xec47bb[_0x6934('0x1e')])){_0x54401f+=_0x6934('0x6b');}_0x336eee[_0x6934('0x43')](_0x54401f);if(_0x4d9fc8[_0x6934('0x66')]&&_0x4d9fc8['duplicates'][_0x6934('0x50')]){_0x336eee[_0x6934('0x43')](insertCmContact(_0x4d9fc8['ListId'],_0x230c4b,_0x4d9fc8[_0x6934('0x66')],_0xec47bb));_0x336eee['push'](_0x6934('0x6c')+_0x230c4b+';');}_0x336eee[_0x6934('0x43')](insertCmHopper(_0x4d9fc8[_0x6934('0x1f')],_0x6934('0x6d'),_0x6934('0x6e'),_0x6934('0x6f'),_0x58296d));_0x336eee[_0x6934('0x43')](insertCmHopperBlack(_0x4d9fc8[_0x6934('0x1f')],_0x6934('0x70'),_0x6934('0x6e'),_0x6934('0x6f')));_0x336eee['push'](insertCmHopper(_0x4d9fc8[_0x6934('0x1f')],_0x6934('0x71'),_0x6934('0x72'),_0x6934('0x73'),_0x58296d));_0x336eee[_0x6934('0x43')](insertCmHopperBlack(_0x4d9fc8[_0x6934('0x1f')],_0x6934('0x74'),_0x6934('0x72'),_0x6934('0x73')));BPromise[_0x6934('0x75')](_0x336eee,handleQuery)[_0x6934('0x76')](function(_0x564734){stats[_0x6934('0x77')]=!![];stats['affectedRows']=_0x4d9fc8[_0x6934('0x66')]&&_0x4d9fc8[_0x6934('0x66')][_0x6934('0x50')]?results[0x2]['affectedRows']:results[0x0][_0x6934('0x78')];socket[_0x6934('0x79')](_0x6934('0x61')+_0x1868bd,stats);fs[_0x6934('0x7a')](_0x234bd6);fs[_0x6934('0x7a')](_0x5ce2df);process['exit'](0x0);})[_0x6934('0x44')](function(_0x37e212){logger['error'](_0x37e212['message']);process[_0x6934('0x3d')](0x1);});}catch(_0x21bede){logger[_0x6934('0x3b')](_0x6934('0x7b'),_0x21bede[_0x6934('0x3c')]);process[_0x6934('0x3d')](0x1);}},'error':function(_0x4c99c6,_0x57adbf,_0x548e65,_0x25c796){throw new Error(_0x25c796);}});}function validate(){if(process['argv']&&process['argv']['length']<0x4){throw new Error(_0x6934('0x7c'));}var _0x403d53=path[_0x6934('0x12')](config[_0x6934('0x48')],'server/files/tmp',process[_0x6934('0x7d')][0x2]);if(!fs[_0x6934('0x7e')](_0x403d53)){throw new Error(_0x6934('0x7f'));}var _0x21600e={};try{_0x21600e=JSON[_0x6934('0x4d')](process[_0x6934('0x7d')][0x3]);if(!_0x21600e[_0x6934('0x47')]){throw new Error(_0x6934('0x80'));}if(!_0x21600e[_0x6934('0x47')]['phone']){throw new Error(_0x6934('0x81'));}if(!_0x21600e[_0x6934('0x47')][_0x6934('0x5f')]){throw new Error(_0x6934('0x82'));}var _0x3772c7=0x0;if(_0x21600e[_0x6934('0x47')][_0x6934('0x23')]){_0x3772c7=0x1;}if(!_0x21600e[_0x6934('0x1f')]){throw new Error(_0x6934('0x83'));}}catch(_0x356458){throw new Error(_0x6934('0x84'));}var _0x51992c=[];try{_0x51992c=_[_0x6934('0x85')](JSON[_0x6934('0x4d')](process['argv'][0x4]),'id');}catch(_0x5114cb){logger[_0x6934('0x3a')](_0x6934('0x86'));}main(_0x403d53,_0x21600e,_0x51992c,_0x3772c7);}validate();process['on'](_0x6934('0x87'),handleStop);process['on'](_0x6934('0x88'),handleUncaughtException);
\ No newline at end of file
+var _0x365e=['server/files/tmp','.csv','writeFileSync','parse','createReadStream','data','length','hasOwnProperty','trim','isValid','scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)','priority','email','dateOfBirth','YYYY-MM-DD','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','firstName','rows','contact:import:','Abort\x20parser','abort','SystemError','step','duplicates','CREATE\x20TABLE\x20','\x20LIKE\x20cm_contacts;','format','isNil',',\x20scheduledAt\x20=\x20NOW()','DROP\x20TABLE\x20','voice_queues_has_cm_lists','voice_queues','voice_queues_has_cm_blacklists','VoiceQueueId','campaigns_has_cm_lists','campaigns','CampaignId','campaigns_has_cm_blacklists','each','then','affectedRows','emit','unlink','argv','existsSync','file\x20doesn\x27t\x20exists','binding\x20doesn\x27t\x20exists','phone\x20is\x20mandatory','ListId\x20is\x20mandatory','body\x20id\x20mandatory','map','SIGINT','uncaughtException','path','util','bluebird','moment','squel','ioredis','../../../config/environment','../../../config/logger','import','redis','localhost','socket.io-emitter','YYYY-MM-DD\x20HH:mm:ss','test','select','field','phone','from','.id\x20=\x20','where','\x20=\x20','.dialCheckDuplicateType\x20IN\x20(','cm_contacts.phone','cm_contacts.scheduledat','cm_contacts.id','cm_contacts.ListId','cm_contacts.createdAt','cm_contacts.UserId','into','cm_hopper','fromQuery','scheduledat','ContactId','updatedAt','UserId','recallme','cm_contacts.ListId\x20=\x20','join','.CmListId\x20=\x20?','cm_contacts.phone\x20NOT\x20IN\x20(?)','\x22always\x22','\x22onlyIfOpen\x22','toString','insert','cm_hopper_black','ListId','createdAt','fields','cm_contacts','.CmListId','ListId\x20=\x20?','\x20=\x20?','concat','keys','CompanyId','deletedAt\x20IS\x20NULL','MIN(id)','min_id','info','Received\x20SIGINT','error','message','exit','errors','push','code','sequelize','query','spread','catch','stringify','send','start','binding','socket_timestamp','root'];(function(_0x1a9204,_0x1c9e00){var _0x17ca68=function(_0x1f4399){while(--_0x1f4399){_0x1a9204['push'](_0x1a9204['shift']());}};_0x17ca68(++_0x1c9e00);}(_0x365e,0xb6));var _0xe365=function(_0x93ed56,_0xd7aebe){_0x93ed56=_0x93ed56-0x0;var _0x818a4f=_0x365e[_0x93ed56];return _0x818a4f;};'use strict';var fs=require('fs');var path=require(_0xe365('0x0'));var _=require('lodash');var Papa=require('papaparse');var util=require(_0xe365('0x1'));var BPromise=require(_0xe365('0x2'));var moment=require(_0xe365('0x3'));var squel=require(_0xe365('0x4'));var Redis=require(_0xe365('0x5'));var db=require('../../../mysqldb')['db'];var config=require(_0xe365('0x6'));var logger=require(_0xe365('0x7'))(_0xe365('0x8'));config['redis']=_['defaults'](config[_0xe365('0x9')],{'host':_0xe365('0xa'),'port':0x18eb});var socket=require(_0xe365('0xb'))(new Redis(config[_0xe365('0x9')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()['format'](_0xe365('0xc'));function isEmail(_0xa6ff2f){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))*$/[_0xe365('0xd')](_0xa6ff2f);}function selectNotIn(_0x2e9050,_0x3b46be,_0x332819,_0x3e4069,_0x25faec){return squel[_0xe365('0xe')]()[_0xe365('0xf')](_0xe365('0x10'))[_0xe365('0x11')](_0x3e4069)['join'](_0x3b46be,null,_0x3b46be+_0xe365('0x12')+_0x3e4069+'.'+_0x332819)[_0xe365('0x13')](_0x3e4069+'.'+_0x332819+_0xe365('0x14')+_0x2e9050+'.'+_0x332819)[_0xe365('0x13')](_0x3b46be+_0xe365('0x15')+_0x25faec['join']()+')');}function insertCmHopper(_0x3867c5,_0x248158,_0x52b54f,_0x23c18a,_0xac4803){var _0x274ae1=[_0xe365('0x16'),_0xe365('0x17'),_0xe365('0x18'),_0xe365('0x19'),_0x248158+'.'+_0x23c18a,_0xe365('0x1a'),'cm_contacts.updatedAt','cm_contacts.priority',_0xe365('0x1b'),'\x22'+_0xac4803+'\x22'];return squel['insert']()[_0xe365('0x1c')](_0xe365('0x1d'))[_0xe365('0x1e')]([_0xe365('0x10'),_0xe365('0x1f'),_0xe365('0x20'),'ListId',_0x23c18a,'createdAt',_0xe365('0x21'),'priority',_0xe365('0x22'),_0xe365('0x23')],squel[_0xe365('0xe')]()['fields'](_0x274ae1)['from'](_0x248158)['join']('cm_contacts',null,_0xe365('0x24')+_0x248158+'.CmListId')[_0xe365('0x25')](_0x52b54f,null,_0x52b54f+_0xe365('0x12')+_0x248158+'.'+_0x23c18a)[_0xe365('0x13')](_0x248158+_0xe365('0x26'),_0x3867c5)[_0xe365('0x13')]('cm_contacts.createdAt\x20=\x20?',NOW)[_0xe365('0x13')](_0xe365('0x27'),selectNotIn(_0x248158,_0x52b54f,_0x23c18a,'cm_hopper',[_0xe365('0x28'),_0xe365('0x29')]))[_0xe365('0x13')]('cm_contacts.phone\x20NOT\x20IN\x20(?)',selectNotIn(_0x248158,_0x52b54f,_0x23c18a,_0xe365('0x1d'),['\x22always\x22'])))[_0xe365('0x2a')]();}function insertCmHopperBlack(_0x4e53a3,_0x17cd80,_0x3a2d48,_0x4838c0){var _0x399dc5=['cm_contacts.phone',_0xe365('0x18'),_0xe365('0x19'),_0x17cd80+'.'+_0x4838c0,_0xe365('0x1a'),'cm_contacts.updatedAt'];return squel[_0xe365('0x2b')]()[_0xe365('0x1c')](_0xe365('0x2c'))[_0xe365('0x1e')]([_0xe365('0x10'),_0xe365('0x20'),_0xe365('0x2d'),_0x4838c0,_0xe365('0x2e'),_0xe365('0x21')],squel['select']()[_0xe365('0x2f')](_0x399dc5)['from'](_0x17cd80)[_0xe365('0x25')](_0xe365('0x30'),null,_0xe365('0x24')+_0x17cd80+_0xe365('0x31'))[_0xe365('0x25')](_0x3a2d48,null,_0x3a2d48+'.id\x20=\x20'+_0x17cd80+'.'+_0x4838c0)[_0xe365('0x13')](_0x17cd80+_0xe365('0x26'),_0x4e53a3)['where']('cm_contacts.createdAt\x20=\x20?',NOW)['where'](_0xe365('0x27'),squel['select']()[_0xe365('0xf')]('phone')[_0xe365('0x11')](_0xe365('0x2c'))[_0xe365('0x13')](_0xe365('0x32'),_0x4e53a3)['where'](_0x4838c0+_0xe365('0x33'),_0x17cd80+'.'+_0x4838c0)))['toString']();}function insertCmContact(_0x190679,_0x1493b1,_0xa0b848,_0x4f0bf8){var _0x5cf947=_[_0xe365('0x34')](_[_0xe365('0x35')](_0x4f0bf8),[_0xe365('0x2d'),_0xe365('0x36'),_0xe365('0x2e'),'updatedAt']);return squel['insert']()[_0xe365('0x1c')](_0xe365('0x30'))['fromQuery'](_0x5cf947,squel[_0xe365('0xe')]()[_0xe365('0x2f')](_0x5cf947)[_0xe365('0x11')](_0x1493b1)['where']('('+_0xa0b848[_0xe365('0x25')](',')+')\x20NOT\x20IN\x20?',squel[_0xe365('0xe')]()['fields'](_0xa0b848)['from'](_0xe365('0x30'))[_0xe365('0x13')](_0xe365('0x32'),_0x190679)[_0xe365('0x13')](_0xe365('0x37')))['where']('id\x20IN\x20?',squel[_0xe365('0xe')]()[_0xe365('0xf')](_0xe365('0x38'),_0xe365('0x39'))['from'](_0x1493b1)['group'](_0xa0b848[_0xe365('0x25')](','))))['toString']();}function handleStop(){logger[_0xe365('0x3a')](_0xe365('0x3b'));stopped=!![];}function handleUncaughtException(_0x195991){logger[_0xe365('0x3c')](_0x195991[_0xe365('0x3d')]);process[_0xe365('0x3e')](0x1);}function handleError(_0xed1bdc,_0x83f2f1){stats[_0xe365('0x3f')][_0xe365('0x40')]({'name':_0x83f2f1[_0xe365('0x41')],'message':_0x83f2f1[_0xe365('0x3d')],'row':_0xed1bdc});}function handleQuery(_0x551f70){logger[_0xe365('0x3a')](_0x551f70);return db[_0xe365('0x42')][_0xe365('0x43')](_0x551f70)[_0xe365('0x44')](function(_0xb06740){results['push'](_0xb06740);})[_0xe365('0x45')](function(_0x1c7354){logger['error'](_0x1c7354[_0xe365('0x3d')]);});}function main(_0x507ee0,_0xca2d52,_0x18bcd7,_0xe1ea16){logger['info']('main',_0x507ee0,JSON[_0xe365('0x46')](_0xca2d52));process[_0xe365('0x47')](_0xe365('0x48'));var _0x5d82b8=_0xca2d52[_0xe365('0x49')];var _0x3d08c0=_0xca2d52[_0xe365('0x4a')];var _0x339e51=path['join'](config[_0xe365('0x4b')],_0xe365('0x4c'),_0x3d08c0+_0xe365('0x4d'));var _0x43ff92=_[_0xe365('0x35')](_0x5d82b8)[_0xe365('0x25')]();logger[_0xe365('0x3a')]('header',_0x43ff92);fs[_0xe365('0x4e')](_0x339e51,_0x43ff92+'\x0a');Papa[_0xe365('0x4f')](fs[_0xe365('0x50')](_0x507ee0),{'header':!![],'skipEmptyLines':!![],'step':function(_0xd2310a,_0x167a71){try{for(var _0x413378=0x0,_0x293f05=[];_0x413378<_0xd2310a[_0xe365('0x51')][_0xe365('0x52')];_0x413378++){stats['rows']+=0x1;if(_0xd2310a[_0xe365('0x3f')][_0x413378]){handleError(_0xd2310a[_0xe365('0x51')][_0x413378],_0xd2310a[_0xe365('0x3f')][_0x413378]);}else{var _0x506e2=[];for(var _0x2d8a12 in _0x5d82b8){if(_0x5d82b8[_0xe365('0x53')](_0x2d8a12)){if(_0xd2310a[_0xe365('0x51')][_0x413378][_0x5d82b8[_0x2d8a12]]){var _0x5ce325=_[_0xe365('0x54')](_0xd2310a[_0xe365('0x51')][_0x413378][_0x5d82b8[_0x2d8a12]]['replace'](/"/g,'\x27'));switch(_0x2d8a12){case _0xe365('0x1f'):var _0x5d6841=moment(_0x5ce325,_0xe365('0xc'),!![]);if(_0x5d6841[_0xe365('0x55')]()){_0x293f05[_0xe365('0x40')]('\x22'+_0x5ce325+'\x22');}else{_0x506e2[_0xe365('0x40')](_0xe365('0x56'));}break;case _0xe365('0x57'):var _0x477c7f=parseInt(_0x5ce325);_0x5ce325=_['isNaN'](_0x477c7f)||_0x477c7f<0x0||_0x477c7f>0x4?_0xe1ea16?0x3:0x2:_0x477c7f;_0x293f05[_0xe365('0x40')]('\x22'+_0x5ce325+'\x22');break;case _0xe365('0x22'):var _0x1d2402=parseInt(_0x5ce325);if(_['isNumber'](_0x1d2402)&&_['includes'](_0x18bcd7,_0x1d2402)){_0x293f05['push']('\x22'+_0x1d2402+'\x22');}else{_0x506e2['push']('agent\x20not\x20exists');}break;case _0xe365('0x58'):if(isEmail(_0x5ce325)){_0x293f05[_0xe365('0x40')]('\x22'+_0x5ce325+'\x22');}else{_0x506e2['push']('email\x20wrong\x20format');}break;case _0xe365('0x59'):var _0x220e0b=moment(_0x5ce325,_0xe365('0x5a'),!![]);if(_0x220e0b[_0xe365('0x55')]()){_0x293f05[_0xe365('0x40')]('\x22'+_0x5ce325+'\x22');}else{_0x506e2['push'](_0xe365('0x5b'));}break;default:_0x293f05[_0xe365('0x40')]('\x22'+_0x5ce325+'\x22');}}else{switch(_0x2d8a12){case _0xe365('0x5c'):case'phone':_0x506e2[_0xe365('0x40')](_0x2d8a12+'\x20not\x20specified');break;default:_0x293f05[_0xe365('0x40')]('\x22\x22');}}}}if(_0x506e2[_0xe365('0x52')]){handleError(_0xd2310a[_0xe365('0x51')][_0x413378],{'code':'SystemRow','message':_0x506e2[_0xe365('0x25')]()});}else{fs['appendFileSync'](_0x339e51,_0x293f05[_0xe365('0x25')]()+'\x0a');}_0x293f05=[];}if(!(stats[_0xe365('0x5d')]%0xc8)){socket['emit'](_0xe365('0x5e')+_0x3d08c0,stats);stats[_0xe365('0x3f')]=[];}}if(stopped){logger['info'](_0xe365('0x5f'));_0x167a71[_0xe365('0x60')]();}}catch(_0x134d8f){handleError(_0xd2310a[_0xe365('0x51')][_0x413378],{'code':_0xe365('0x61'),'message':_0x134d8f['message']});logger[_0xe365('0x3c')](_0xe365('0x62'),_0x134d8f[_0xe365('0x3d')]);}},'complete':function(){try{var _0x54398c=[];var _0x309761='cm_contacts';if(_0xca2d52['duplicates']&&_0xca2d52[_0xe365('0x63')]['length']){_0x309761='cm_contacts_'+_0x3d08c0;_0x54398c[_0xe365('0x40')](_0xe365('0x64')+_0x309761+_0xe365('0x65'));}var _0x8e46cb=util[_0xe365('0x66')]('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',_0x339e51,_0x309761,_0x43ff92,_0xca2d52[_0xe365('0x2d')],_0xca2d52[_0xe365('0x36')]||null,NOW,NOW);if(_[_0xe365('0x67')](_0x5d82b8[_0xe365('0x1f')])){_0x8e46cb+=_0xe365('0x68');}_0x54398c[_0xe365('0x40')](_0x8e46cb);if(_0xca2d52[_0xe365('0x63')]&&_0xca2d52[_0xe365('0x63')][_0xe365('0x52')]){_0x54398c[_0xe365('0x40')](insertCmContact(_0xca2d52[_0xe365('0x2d')],_0x309761,_0xca2d52[_0xe365('0x63')],_0x5d82b8));_0x54398c['push'](_0xe365('0x69')+_0x309761+';');}_0x54398c[_0xe365('0x40')](insertCmHopper(_0xca2d52[_0xe365('0x2d')],_0xe365('0x6a'),_0xe365('0x6b'),'VoiceQueueId',_0xe1ea16));_0x54398c[_0xe365('0x40')](insertCmHopperBlack(_0xca2d52[_0xe365('0x2d')],_0xe365('0x6c'),_0xe365('0x6b'),_0xe365('0x6d')));_0x54398c['push'](insertCmHopper(_0xca2d52[_0xe365('0x2d')],_0xe365('0x6e'),_0xe365('0x6f'),_0xe365('0x70'),_0xe1ea16));_0x54398c[_0xe365('0x40')](insertCmHopperBlack(_0xca2d52['ListId'],_0xe365('0x71'),'campaigns','CampaignId'));BPromise[_0xe365('0x72')](_0x54398c,handleQuery)[_0xe365('0x73')](function(_0x938834){stats['finish']=!![];stats['affectedRows']=_0xca2d52[_0xe365('0x63')]&&_0xca2d52[_0xe365('0x63')]['length']?results[0x2][_0xe365('0x74')]:results[0x0][_0xe365('0x74')];socket[_0xe365('0x75')](_0xe365('0x5e')+_0x3d08c0,stats);fs[_0xe365('0x76')](_0x507ee0);fs[_0xe365('0x76')](_0x339e51);process[_0xe365('0x3e')](0x0);})[_0xe365('0x45')](function(_0x32e2e6){logger[_0xe365('0x3c')](_0x32e2e6[_0xe365('0x3d')]);process['exit'](0x1);});}catch(_0x34c3f3){logger[_0xe365('0x3c')]('complete',_0x34c3f3[_0xe365('0x3d')]);process[_0xe365('0x3e')](0x1);}},'error':function(_0x5eca56,_0x4aa98b,_0x1ca17d,_0x5c6755){throw new Error(_0x5c6755);}});}function validate(){if(process[_0xe365('0x77')]&&process['argv'][_0xe365('0x52')]<0x4){throw new Error('arguments\x20<filename>\x20<parameters>\x20<agents>');}var _0x5df4b2=path['join'](config['root'],_0xe365('0x4c'),process[_0xe365('0x77')][0x2]);if(!fs[_0xe365('0x78')](_0x5df4b2)){throw new Error(_0xe365('0x79'));}var _0x59fd4d={};try{_0x59fd4d=JSON[_0xe365('0x4f')](process[_0xe365('0x77')][0x3]);if(!_0x59fd4d['binding']){throw new Error(_0xe365('0x7a'));}if(!_0x59fd4d[_0xe365('0x49')][_0xe365('0x10')]){throw new Error(_0xe365('0x7b'));}if(!_0x59fd4d[_0xe365('0x49')][_0xe365('0x5c')]){throw new Error('firstName\x20is\x20mandatory');}var _0x1a8aed=0x0;if(_0x59fd4d[_0xe365('0x49')]['UserId']){_0x1a8aed=0x1;}if(!_0x59fd4d[_0xe365('0x2d')]){throw new Error(_0xe365('0x7c'));}}catch(_0x25cfbf){throw new Error(_0xe365('0x7d'));}var _0x5ac237=[];try{_0x5ac237=_[_0xe365('0x7e')](JSON[_0xe365('0x4f')](process['argv'][0x4]),'id');}catch(_0x36feaf){logger[_0xe365('0x3a')]('No\x20agent\x20available');}main(_0x5df4b2,_0x59fd4d,_0x5ac237,_0x1a8aed);}validate();process['on'](_0xe365('0x7f'),handleStop);process['on'](_0xe365('0x80'),handleUncaughtException);
\ No newline at end of file