Built motion from commit a9fb64cc6defd8fb3a5cc199221f878e91c0e969. Version 3.0.0...
[motion-next.git] / server / components / import / cm / csv2.js
1 const a831_0x1b9a=['lodash','util','isNil','SystemError','createdAt','query','dateOfBirth','cm_contacts.phone','isNumber','VoiceQueueId','cm_contacts.createdAt','test','cm_contacts.phone\x20NOT\x20IN\x20(?)','updatedAt','voice_queues_has_cm_blacklists','cm_contacts','abort','phone\x20is\x20mandatory','field','existsSync','cm_hopper','stringify','ListId\x20=\x20?','map','voice_queues','YYYY-MM-DD','binding\x20doesn\x27t\x20exists','start','campaigns','id\x20IN\x20?','cm_contacts.createdAt\x20=\x20?','sequelize','No\x20agent\x20available','.dialCheckDuplicateType\x20IN\x20(','errors','cm_contacts.scheduledat','CampaignId','isValid','each','rows','cm_contacts.ListId\x20=\x20','socket_timestamp','CREATE\x20TABLE\x20','squel','cm_hopper_black','.id\x20=\x20','fields','main','catch','length','ContactId',')\x20NOT\x20IN\x20?','join','contact:import:','../../../config/logger','Abort\x20parser','cm_contacts.id','priority','server/files/tmp','DROP\x20TABLE\x20','message','includes','email','import','group','push','body\x20id\x20mandatory','insert','firstName','argv','format','createReadStream','then','keys','email\x20wrong\x20format','emit','campaigns_has_cm_lists','cm_contacts.ListId','duplicates','binding','info','ListId\x20is\x20mandatory','CompanyId','appendFileSync','replace','papaparse','toString','trim','SystemRow','file\x20doesn\x27t\x20exists','SIGINT','undefined','select','from','firstName\x20is\x20mandatory','unlink','recallme','code','\x20=\x20','error','parse','step','isNaN','fromQuery','\x20LIKE\x20cm_contacts;','send','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','UserId','uncaughtException','path','cm_contacts_','scheduledat','\x20not\x20specified','MIN(id)','.CmListId\x20=\x20?','files/tmp','where','cm_contacts.UserId','phone','../../../config/environment','root','into','arguments\x20<filename>\x20<parameters>\x20<agents>','data','YYYY-MM-DD\x20HH:mm:ss','ListId','\x22always\x22','affectedRows','concat','\x22onlyIfOpen\x22','\x20=\x20?'];(function(_0xcf1b19,_0x1b9a2a){const _0x27cd1c=function(_0x1b0130){while(--_0x1b0130){_0xcf1b19['push'](_0xcf1b19['shift']());}};_0x27cd1c(++_0x1b9a2a);}(a831_0x1b9a,0x1f0));const a831_0x27cd=function(_0xcf1b19,_0x1b9a2a){_0xcf1b19=_0xcf1b19-0x0;let _0x27cd1c=a831_0x1b9a[_0xcf1b19];return _0x27cd1c;};const _0x15264b=a831_0x27cd;'use strict';const fs=require('fs'),path=require(_0x15264b('0x6')),_=require(_0x15264b('0x1c')),Papa=require(_0x15264b('0x71')),util=require(_0x15264b('0x1d')),moment=require('moment'),squel=require(_0x15264b('0x47')),db=require('../../../mysqldb')['db'],config=require(_0x15264b('0x10')),logger=require(_0x15264b('0x52'))(_0x15264b('0x5b')),{getSocket}=require('../../../rediscache'),socket=getSocket(),stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0},results=[];let stopped=![];const NOW=moment()['format']('YYYY-MM-DD\x20HH:mm:ss');function isEmail(_0x1b0130){const _0x215bfd=_0x15264b;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))*$/[_0x215bfd('0x27')](_0x1b0130);}function selectNotIn(_0x5f0b90,_0xaaeb74,_0x580ef6,_0x426df3,_0x241125){const _0x105b69=_0x15264b;return squel['select']()[_0x105b69('0x2e')](_0x105b69('0xf'))[_0x105b69('0x79')](_0x426df3)[_0x105b69('0x50')](_0xaaeb74,null,_0xaaeb74+_0x105b69('0x49')+_0x426df3+'.'+_0x580ef6)['where'](_0x426df3+'.'+_0x580ef6+_0x105b69('0x7e')+_0x5f0b90+'.'+_0x580ef6)[_0x105b69('0xd')](_0xaaeb74+_0x105b69('0x3d')+_0x241125['join']()+')');}function insertCmHopper(_0x559c24,_0x4d8b4b,_0x1cc4d5,_0x524e99,_0x78b9d2){const _0x5e604e=_0x15264b,_0x12449b=[_0x5e604e('0x23'),_0x5e604e('0x3f'),'cm_contacts.id','cm_contacts.ListId',_0x4d8b4b+'.'+_0x524e99,_0x5e604e('0x26'),'cm_contacts.updatedAt','cm_contacts.priority',_0x5e604e('0xe'),'\x22'+_0x78b9d2+'\x22'];return squel[_0x5e604e('0x5f')]()[_0x5e604e('0x12')](_0x5e604e('0x30'))['fromQuery']([_0x5e604e('0xf'),_0x5e604e('0x8'),'ContactId',_0x5e604e('0x16'),_0x524e99,_0x5e604e('0x20'),_0x5e604e('0x29'),_0x5e604e('0x55'),_0x5e604e('0x4'),_0x5e604e('0x7c')],squel['select']()[_0x5e604e('0x4a')](_0x12449b)[_0x5e604e('0x79')](_0x4d8b4b)['join']('cm_contacts',null,'cm_contacts.ListId\x20=\x20'+_0x4d8b4b+'.CmListId')[_0x5e604e('0x50')](_0x1cc4d5,null,_0x1cc4d5+_0x5e604e('0x49')+_0x4d8b4b+'.'+_0x524e99)[_0x5e604e('0xd')](_0x4d8b4b+_0x5e604e('0xb'),_0x559c24)[_0x5e604e('0xd')](_0x5e604e('0x3a'),NOW)[_0x5e604e('0xd')]('cm_contacts.phone\x20NOT\x20IN\x20(?)',selectNotIn(_0x4d8b4b,_0x1cc4d5,_0x524e99,'cm_hopper',[_0x5e604e('0x17'),_0x5e604e('0x1a')]))[_0x5e604e('0xd')](_0x5e604e('0x28'),selectNotIn(_0x4d8b4b,_0x1cc4d5,_0x524e99,'cm_hopper',[_0x5e604e('0x17')])))[_0x5e604e('0x72')]();}function insertCmHopperBlack(_0x4038e7,_0x2d0d9b,_0x2a3692,_0x498124){const _0x48c104=_0x15264b,_0x1e665b=[_0x48c104('0x23'),_0x48c104('0x54'),_0x48c104('0x69'),_0x2d0d9b+'.'+_0x498124,'cm_contacts.createdAt','cm_contacts.updatedAt'];return squel[_0x48c104('0x5f')]()[_0x48c104('0x12')](_0x48c104('0x48'))[_0x48c104('0x0')]([_0x48c104('0xf'),_0x48c104('0x4e'),_0x48c104('0x16'),_0x498124,_0x48c104('0x20'),_0x48c104('0x29')],squel[_0x48c104('0x78')]()[_0x48c104('0x4a')](_0x1e665b)[_0x48c104('0x79')](_0x2d0d9b)[_0x48c104('0x50')](_0x48c104('0x2b'),null,_0x48c104('0x44')+_0x2d0d9b+'.CmListId')[_0x48c104('0x50')](_0x2a3692,null,_0x2a3692+_0x48c104('0x49')+_0x2d0d9b+'.'+_0x498124)[_0x48c104('0xd')](_0x2d0d9b+_0x48c104('0xb'),_0x4038e7)[_0x48c104('0xd')]('cm_contacts.createdAt\x20=\x20?',NOW)[_0x48c104('0xd')](_0x48c104('0x28'),squel[_0x48c104('0x78')]()['field']('phone')[_0x48c104('0x79')](_0x48c104('0x48'))[_0x48c104('0xd')](_0x48c104('0x32'),_0x4038e7)[_0x48c104('0xd')](_0x498124+_0x48c104('0x1b'),_0x2d0d9b+'.'+_0x498124)))['toString']();}function insertCmContact(_0xa7120d,_0x104b6f,_0x341973,_0xa3abba){const _0x561160=_0x15264b,_0x10c0e7=_[_0x561160('0x19')](_['keys'](_0xa3abba),[_0x561160('0x16'),_0x561160('0x6e'),_0x561160('0x20'),'updatedAt']);return squel[_0x561160('0x5f')]()['into'](_0x561160('0x2b'))['fromQuery'](_0x10c0e7,squel[_0x561160('0x78')]()['fields'](_0x10c0e7)['from'](_0x104b6f)['where']('('+_0x341973[_0x561160('0x50')](',')+_0x561160('0x4f'),squel['select']()[_0x561160('0x4a')](_0x341973)['from'](_0x561160('0x2b'))['where'](_0x561160('0x32'),_0xa7120d)[_0x561160('0xd')]('deletedAt\x20IS\x20NULL'))['where'](_0x561160('0x39'),squel[_0x561160('0x78')]()[_0x561160('0x2e')](_0x561160('0xa'),'min_id')[_0x561160('0x79')](_0x104b6f)[_0x561160('0x5c')](_0x341973[_0x561160('0x50')](','))))[_0x561160('0x72')]();}function handleStop(){const _0x33987e=_0x15264b;logger[_0x33987e('0x6c')]('Received\x20SIGINT'),stopped=!![];}function handleUncaughtException(_0x1d4182){const _0x3ffec3=_0x15264b;logger[_0x3ffec3('0x7f')](_0x1d4182[_0x3ffec3('0x58')]);throw _0x1d4182;}function handleError(_0x23d082,_0x461cad){const _0x177396=_0x15264b;stats[_0x177396('0x3e')][_0x177396('0x5d')]({'name':_0x461cad[_0x177396('0x7d')],'message':_0x461cad[_0x177396('0x58')],'row':_0x23d082});}function handleQuery(_0x553435){const _0x552059=_0x15264b;return logger[_0x552059('0x6c')](_0x553435),db[_0x552059('0x3b')][_0x552059('0x21')](_0x553435)[_0x552059('0x64')](function([_0x24ada7]){const _0x321271=_0x552059;results[_0x321271('0x5d')](_0x24ada7);})['catch'](function(_0x460261){const _0x29abc4=_0x552059;logger[_0x29abc4('0x7f')](_0x460261['message']);});}function main(_0x4cd943,_0x4cea17,_0x128dce,_0x3d926e){const _0x40dcbf=_0x15264b;logger[_0x40dcbf('0x6c')](_0x40dcbf('0x4b'),_0x4cd943,JSON[_0x40dcbf('0x31')](_0x4cea17)),process[_0x40dcbf('0x2')](_0x40dcbf('0x37'));const _0x3544fc=_0x4cea17[_0x40dcbf('0x6b')],_0x4091ce=_0x4cea17[_0x40dcbf('0x45')],_0x2e72ef=path[_0x40dcbf('0x50')](config[_0x40dcbf('0x11')],_0x40dcbf('0x56'),_0x4091ce+'.csv'),_0x9e1b51=_[_0x40dcbf('0x65')](_0x3544fc)[_0x40dcbf('0x50')]();logger['info']('header',_0x9e1b51),fs['writeFileSync'](_0x2e72ef,_0x9e1b51+'\x0a'),Papa[_0x40dcbf('0x80')](fs[_0x40dcbf('0x63')](_0x4cd943),{'header':!![],'skipEmptyLines':!![],'step':function(_0x148e78,_0x151eee){const _0x15fafc=_0x40dcbf;let _0x4fbb4f=0x0,_0x3d5e32=[];try{for(_0x4fbb4f=0x0,_0x3d5e32=[];_0x4fbb4f<_0x148e78[_0x15fafc('0x14')]['length'];_0x4fbb4f++){stats[_0x15fafc('0x43')]+=0x1;if(_0x148e78[_0x15fafc('0x3e')][_0x4fbb4f])handleError(_0x148e78['data'][_0x4fbb4f],_0x148e78[_0x15fafc('0x3e')][_0x4fbb4f]);else{const _0x222822=[];for(const _0x4b15ef in _0x3544fc){if(typeof _0x3544fc[_0x4b15ef]!==_0x15fafc('0x77')){if(_0x148e78['data'][_0x4fbb4f][_0x3544fc[_0x4b15ef]]){let _0x2fa711=_[_0x15fafc('0x73')](_0x148e78[_0x15fafc('0x14')][_0x4fbb4f][_0x3544fc[_0x4b15ef]][_0x15fafc('0x70')](/"/g,'\x27'));switch(_0x4b15ef){case _0x15fafc('0x8'):{const _0x22be97=moment(_0x2fa711,_0x15fafc('0x15'),!![]);_0x22be97[_0x15fafc('0x41')]()?_0x3d5e32[_0x15fafc('0x5d')]('\x22'+_0x2fa711+'\x22'):_0x222822[_0x15fafc('0x5d')]('scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)');}break;case _0x15fafc('0x55'):{const _0x2f64df=parseInt(_0x2fa711);_0x2fa711=_[_0x15fafc('0x82')](_0x2f64df)||_0x2f64df<0x0||_0x2f64df>0x4?_0x3d926e?0x3:0x2:_0x2f64df,_0x3d5e32[_0x15fafc('0x5d')]('\x22'+_0x2fa711+'\x22');}break;case _0x15fafc('0x4'):{const _0x15a1e7=parseInt(_0x2fa711);_[_0x15fafc('0x24')](_0x15a1e7)&&_[_0x15fafc('0x59')](_0x128dce,_0x15a1e7)?_0x3d5e32['push']('\x22'+_0x15a1e7+'\x22'):_0x222822[_0x15fafc('0x5d')]('agent\x20not\x20exists');}break;case _0x15fafc('0x5a'):isEmail(_0x2fa711)?_0x3d5e32['push']('\x22'+_0x2fa711+'\x22'):_0x222822[_0x15fafc('0x5d')](_0x15fafc('0x66'));break;case _0x15fafc('0x22'):{const _0x187fda=moment(_0x2fa711,_0x15fafc('0x35'),!![]);_0x187fda[_0x15fafc('0x41')]()?_0x3d5e32['push']('\x22'+_0x2fa711+'\x22'):_0x222822[_0x15fafc('0x5d')](_0x15fafc('0x3'));}break;default:_0x3d5e32[_0x15fafc('0x5d')]('\x22'+_0x2fa711+'\x22');}}else switch(_0x4b15ef){case _0x15fafc('0x60'):case _0x15fafc('0xf'):_0x222822[_0x15fafc('0x5d')](_0x4b15ef+_0x15fafc('0x9'));break;default:_0x3d5e32['push']('\x22\x22');}}}_0x222822[_0x15fafc('0x4d')]?handleError(_0x148e78[_0x15fafc('0x14')][_0x4fbb4f],{'code':_0x15fafc('0x74'),'message':_0x222822[_0x15fafc('0x50')]()}):fs[_0x15fafc('0x6f')](_0x2e72ef,_0x3d5e32[_0x15fafc('0x50')]()+'\x0a'),_0x3d5e32=[];}!(stats[_0x15fafc('0x43')]%0xc8)&&(socket[_0x15fafc('0x67')](_0x15fafc('0x51')+_0x4091ce,stats),stats[_0x15fafc('0x3e')]=[]);}stopped&&(logger[_0x15fafc('0x6c')](_0x15fafc('0x53')),_0x151eee[_0x15fafc('0x2c')]());}catch(_0x43d78b){handleError(_0x148e78[_0x15fafc('0x14')][_0x4fbb4f],{'code':_0x15fafc('0x1f'),'message':_0x43d78b['message']}),logger['error'](_0x15fafc('0x81'),_0x43d78b[_0x15fafc('0x58')]);}},'complete':function(){const _0x157e19=_0x40dcbf;try{const _0x33e1be=[];let _0x60b468='cm_contacts';_0x4cea17[_0x157e19('0x6a')]&&_0x4cea17[_0x157e19('0x6a')][_0x157e19('0x4d')]&&(_0x60b468=_0x157e19('0x7')+_0x4091ce,_0x33e1be[_0x157e19('0x5d')](_0x157e19('0x46')+_0x60b468+_0x157e19('0x1')));let _0x182f90=util[_0x157e19('0x62')]('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',_0x2e72ef,_0x60b468,_0x9e1b51,_0x4cea17[_0x157e19('0x16')],_0x4cea17[_0x157e19('0x6e')]||null,NOW,NOW);_[_0x157e19('0x1e')](_0x3544fc[_0x157e19('0x8')])&&(_0x182f90+=',\x20scheduledAt\x20=\x20NOW()'),_0x33e1be['push'](_0x182f90),_0x4cea17[_0x157e19('0x6a')]&&_0x4cea17[_0x157e19('0x6a')]['length']&&(_0x33e1be[_0x157e19('0x5d')](insertCmContact(_0x4cea17[_0x157e19('0x16')],_0x60b468,_0x4cea17[_0x157e19('0x6a')],_0x3544fc)),_0x33e1be[_0x157e19('0x5d')](_0x157e19('0x57')+_0x60b468+';')),_0x33e1be[_0x157e19('0x5d')](insertCmHopper(_0x4cea17[_0x157e19('0x16')],'voice_queues_has_cm_lists',_0x157e19('0x34'),'VoiceQueueId',_0x3d926e)),_0x33e1be[_0x157e19('0x5d')](insertCmHopperBlack(_0x4cea17[_0x157e19('0x16')],_0x157e19('0x2a'),_0x157e19('0x34'),_0x157e19('0x25'))),_0x33e1be['push'](insertCmHopper(_0x4cea17[_0x157e19('0x16')],_0x157e19('0x68'),'campaigns','CampaignId',_0x3d926e)),_0x33e1be[_0x157e19('0x5d')](insertCmHopperBlack(_0x4cea17[_0x157e19('0x16')],'campaigns_has_cm_blacklists',_0x157e19('0x38'),_0x157e19('0x40'))),Promise[_0x157e19('0x42')](_0x33e1be,handleQuery)[_0x157e19('0x64')](function(){const _0x2ef7d8=_0x157e19;stats['finish']=!![],stats[_0x2ef7d8('0x18')]=_0x4cea17[_0x2ef7d8('0x6a')]&&_0x4cea17[_0x2ef7d8('0x6a')][_0x2ef7d8('0x4d')]?results[0x2][_0x2ef7d8('0x18')]:results[0x0]['affectedRows'],socket[_0x2ef7d8('0x67')](_0x2ef7d8('0x51')+_0x4091ce,stats),fs['unlink'](_0x4cd943),fs[_0x2ef7d8('0x7b')](_0x2e72ef),process['exit'](0x0);})[_0x157e19('0x4c')](function(_0x369f2e){const _0x5b9d74=_0x157e19;logger[_0x5b9d74('0x7f')](_0x369f2e[_0x5b9d74('0x58')]);throw _0x369f2e;});}catch(_0x4e5269){logger[_0x157e19('0x7f')]('complete',_0x4e5269[_0x157e19('0x58')]);throw _0x4e5269;}},'error':function(_0x95c11d,_0x422559,_0x18fdb3,_0x2aa481){throw new Error(_0x2aa481);}});}function validate(){const _0x241c54=_0x15264b;if(process[_0x241c54('0x61')]&&process[_0x241c54('0x61')][_0x241c54('0x4d')]<0x4)throw new Error(_0x241c54('0x13'));const _0x3a6c42=path[_0x241c54('0x50')](config['root'],_0x241c54('0xc'),process[_0x241c54('0x61')][0x2]);if(!fs[_0x241c54('0x2f')](_0x3a6c42))throw new Error(_0x241c54('0x75'));let _0x4e0838={},_0x20cc82=0x0;try{_0x4e0838=JSON[_0x241c54('0x80')](process[_0x241c54('0x61')][0x3]);if(!_0x4e0838[_0x241c54('0x6b')])throw new Error(_0x241c54('0x36'));if(!_0x4e0838[_0x241c54('0x6b')][_0x241c54('0xf')])throw new Error(_0x241c54('0x2d'));if(!_0x4e0838['binding'][_0x241c54('0x60')])throw new Error(_0x241c54('0x7a'));_0x4e0838['binding']['UserId']&&(_0x20cc82=0x1);if(!_0x4e0838[_0x241c54('0x16')])throw new Error(_0x241c54('0x6d'));}catch(_0x181f26){throw new Error(_0x241c54('0x5e'));}let _0x858100=[];try{_0x858100=_[_0x241c54('0x33')](JSON[_0x241c54('0x80')](process[_0x241c54('0x61')][0x4]),'id');}catch(_0x44f786){logger[_0x241c54('0x6c')](_0x241c54('0x3c'));}main(_0x3a6c42,_0x4e0838,_0x858100,_0x20cc82);}validate(),process['on'](_0x15264b('0x76'),handleStop),process['on'](_0x15264b('0x5'),handleUncaughtException);