Built motion from commit ecdc8e66766c75b1b08288b0ea13eb12d42606b4. Version 3.0.0...
[motion-next.git] / server / components / import / cm / csv2.js
1 const a831_0x40f4=['updatedAt','phone\x20is\x20mandatory','message','ListId\x20=\x20?','CompanyId','complete','cm_contacts.scheduledat','trim','main','from','send','errors','.id\x20=\x20','min_id','keys','cm_contacts.UserId','parse','isNaN','affectedRows','appendFileSync','priority',')\x20NOT\x20IN\x20?','cm_hopper_black','createdAt','import','data','finish','\x22always\x22','cm_contacts.phone\x20NOT\x20IN\x20(?)','socket_timestamp','cm_contacts.createdAt\x20=\x20?','binding','header','id\x20IN\x20?','firstName','createReadStream','papaparse','cm_contacts.id','ContactId','Received\x20SIGINT','voice_queues_has_cm_lists','../../../config/logger','includes','../../../mysqldb','squel','error','cm_contacts.priority','fromQuery','into','query','emit','files/tmp','unlink','DROP\x20TABLE\x20','../../../rediscache','duplicates','join','format','SystemRow','path','start','each','existsSync','VoiceQueueId','group','.dialCheckDuplicateType\x20IN\x20(','insert','ListId\x20is\x20mandatory','cm_contacts.phone','.CmListId\x20=\x20?','cm_contacts','uncaughtException','field','catch','server/files/tmp','toString','.CmListId','scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)','agent\x20not\x20exists','then','select','exit','undefined','replace','CampaignId','cm_hopper','stringify','cm_contacts.createdAt','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','isValid','scheduledat','argv','voice_queues','campaigns_has_cm_blacklists','fields','info','recallme','rows','MIN(id)','phone','cm_contacts_','YYYY-MM-DD\x20HH:mm:ss','code','UserId','isNil','campaigns','cm_contacts.ListId\x20=\x20','push','ListId','../../../config/environment','.csv','voice_queues_has_cm_blacklists','\x20=\x20?','length','SIGINT','cm_contacts.updatedAt','SystemError','cm_contacts.ListId','file\x20doesn\x27t\x20exists','firstName\x20is\x20mandatory','where','No\x20agent\x20available'];(function(_0x49e5f5,_0x40f408){const _0x44d6af=function(_0x2a71bb){while(--_0x2a71bb){_0x49e5f5['push'](_0x49e5f5['shift']());}};_0x44d6af(++_0x40f408);}(a831_0x40f4,0x1c3));const a831_0x44d6=function(_0x49e5f5,_0x40f408){_0x49e5f5=_0x49e5f5-0x0;let _0x44d6af=a831_0x40f4[_0x49e5f5];return _0x44d6af;};const _0x3ed061=a831_0x44d6;'use strict';const fs=require('fs'),path=require(_0x3ed061('0x60')),_=require('lodash'),Papa=require(_0x3ed061('0x49')),util=require('util'),moment=require('moment'),squel=require(_0x3ed061('0x51')),db=require(_0x3ed061('0x50'))['db'],config=require(_0x3ed061('0x18')),logger=require(_0x3ed061('0x4e'))(_0x3ed061('0x3d')),{getSocket}=require(_0x3ed061('0x5b')),socket=getSocket(),stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0},results=[];let stopped=![];const NOW=moment()[_0x3ed061('0x5e')](_0x3ed061('0x10'));function isEmail(_0x2a71bb){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))*$/['test'](_0x2a71bb);}function selectNotIn(_0x3815eb,_0xdb6be7,_0x4e3a80,_0x56fe03,_0x4edf59){const _0x229081=_0x3ed061;return squel[_0x229081('0x75')]()[_0x229081('0x6d')](_0x229081('0xe'))[_0x229081('0x2e')](_0x56fe03)['join'](_0xdb6be7,null,_0xdb6be7+_0x229081('0x31')+_0x56fe03+'.'+_0x4e3a80)['where'](_0x56fe03+'.'+_0x4e3a80+'\x20=\x20'+_0x3815eb+'.'+_0x4e3a80)[_0x229081('0x23')](_0xdb6be7+_0x229081('0x66')+_0x4edf59[_0x229081('0x5d')]()+')');}function insertCmHopper(_0x2378e4,_0x123928,_0x18ef3e,_0x29e465,_0x36afd7){const _0x3af6d6=_0x3ed061,_0x33f4e5=[_0x3af6d6('0x69'),_0x3af6d6('0x2b'),_0x3af6d6('0x4a'),_0x3af6d6('0x20'),_0x123928+'.'+_0x29e465,_0x3af6d6('0x2'),_0x3af6d6('0x1e'),_0x3af6d6('0x53'),_0x3af6d6('0x34'),'\x22'+_0x36afd7+'\x22'];return squel['insert']()[_0x3af6d6('0x55')](_0x3af6d6('0x0'))[_0x3af6d6('0x54')]([_0x3af6d6('0xe'),_0x3af6d6('0x5'),_0x3af6d6('0x4b'),_0x3af6d6('0x17'),_0x29e465,'createdAt',_0x3af6d6('0x25'),'priority','UserId',_0x3af6d6('0xb')],squel[_0x3af6d6('0x75')]()[_0x3af6d6('0x9')](_0x33f4e5)[_0x3af6d6('0x2e')](_0x123928)[_0x3af6d6('0x5d')](_0x3af6d6('0x6b'),null,'cm_contacts.ListId\x20=\x20'+_0x123928+_0x3af6d6('0x71'))[_0x3af6d6('0x5d')](_0x18ef3e,null,_0x18ef3e+_0x3af6d6('0x31')+_0x123928+'.'+_0x29e465)['where'](_0x123928+_0x3af6d6('0x6a'),_0x2378e4)['where'](_0x3af6d6('0x43'),NOW)[_0x3af6d6('0x23')](_0x3af6d6('0x41'),selectNotIn(_0x123928,_0x18ef3e,_0x29e465,_0x3af6d6('0x0'),['\x22always\x22','\x22onlyIfOpen\x22']))[_0x3af6d6('0x23')](_0x3af6d6('0x41'),selectNotIn(_0x123928,_0x18ef3e,_0x29e465,_0x3af6d6('0x0'),[_0x3af6d6('0x40')])))[_0x3af6d6('0x70')]();}function insertCmHopperBlack(_0x351d96,_0x42363e,_0x2be77b,_0x2647ad){const _0x5e8f03=_0x3ed061,_0x23a132=[_0x5e8f03('0x69'),'cm_contacts.id',_0x5e8f03('0x20'),_0x42363e+'.'+_0x2647ad,_0x5e8f03('0x2'),_0x5e8f03('0x1e')];return squel[_0x5e8f03('0x67')]()[_0x5e8f03('0x55')](_0x5e8f03('0x3b'))['fromQuery'](['phone',_0x5e8f03('0x4b'),_0x5e8f03('0x17'),_0x2647ad,_0x5e8f03('0x3c'),_0x5e8f03('0x25')],squel[_0x5e8f03('0x75')]()[_0x5e8f03('0x9')](_0x23a132)[_0x5e8f03('0x2e')](_0x42363e)[_0x5e8f03('0x5d')](_0x5e8f03('0x6b'),null,_0x5e8f03('0x15')+_0x42363e+'.CmListId')['join'](_0x2be77b,null,_0x2be77b+_0x5e8f03('0x31')+_0x42363e+'.'+_0x2647ad)[_0x5e8f03('0x23')](_0x42363e+'.CmListId\x20=\x20?',_0x351d96)[_0x5e8f03('0x23')](_0x5e8f03('0x43'),NOW)[_0x5e8f03('0x23')](_0x5e8f03('0x41'),squel[_0x5e8f03('0x75')]()[_0x5e8f03('0x6d')](_0x5e8f03('0xe'))[_0x5e8f03('0x2e')]('cm_hopper_black')[_0x5e8f03('0x23')](_0x5e8f03('0x28'),_0x351d96)[_0x5e8f03('0x23')](_0x2647ad+_0x5e8f03('0x1b'),_0x42363e+'.'+_0x2647ad)))['toString']();}function insertCmContact(_0x1bb8fb,_0x26052e,_0x574efa,_0x417068){const _0x1422a6=_0x3ed061,_0x1a45c3=_['concat'](_[_0x1422a6('0x33')](_0x417068),[_0x1422a6('0x17'),_0x1422a6('0x29'),_0x1422a6('0x3c'),_0x1422a6('0x25')]);return squel[_0x1422a6('0x67')]()[_0x1422a6('0x55')]('cm_contacts')[_0x1422a6('0x54')](_0x1a45c3,squel[_0x1422a6('0x75')]()[_0x1422a6('0x9')](_0x1a45c3)[_0x1422a6('0x2e')](_0x26052e)[_0x1422a6('0x23')]('('+_0x574efa['join'](',')+_0x1422a6('0x3a'),squel[_0x1422a6('0x75')]()[_0x1422a6('0x9')](_0x574efa)['from'](_0x1422a6('0x6b'))['where'](_0x1422a6('0x28'),_0x1bb8fb)['where']('deletedAt\x20IS\x20NULL'))[_0x1422a6('0x23')](_0x1422a6('0x46'),squel[_0x1422a6('0x75')]()['field'](_0x1422a6('0xd'),_0x1422a6('0x32'))[_0x1422a6('0x2e')](_0x26052e)[_0x1422a6('0x65')](_0x574efa['join'](','))))[_0x1422a6('0x70')]();}function handleStop(){const _0x597f08=_0x3ed061;logger['info'](_0x597f08('0x4c')),stopped=!![];}function handleUncaughtException(_0x2e173d){const _0x559eb4=_0x3ed061;logger['error'](_0x2e173d[_0x559eb4('0x27')]);throw _0x2e173d;}function handleError(_0x7cd938,_0xf42f74){const _0x451df6=_0x3ed061;stats[_0x451df6('0x30')][_0x451df6('0x16')]({'name':_0xf42f74[_0x451df6('0x11')],'message':_0xf42f74[_0x451df6('0x27')],'row':_0x7cd938});}function handleQuery(_0x31c4f5){const _0x53c38e=_0x3ed061;return logger[_0x53c38e('0xa')](_0x31c4f5),db['sequelize'][_0x53c38e('0x56')](_0x31c4f5)['then'](function([_0x3e7d5c]){const _0x24fc36=_0x53c38e;results[_0x24fc36('0x16')](_0x3e7d5c);})['catch'](function(_0x264b6a){const _0x63f347=_0x53c38e;logger['error'](_0x264b6a[_0x63f347('0x27')]);});}function main(_0x5b3130,_0x5a5dde,_0x4c6985,_0xb6a240){const _0x5cbc00=_0x3ed061;logger[_0x5cbc00('0xa')](_0x5cbc00('0x2d'),_0x5b3130,JSON[_0x5cbc00('0x1')](_0x5a5dde)),process[_0x5cbc00('0x2f')](_0x5cbc00('0x61'));const _0x1cd452=_0x5a5dde[_0x5cbc00('0x44')],_0x3ec524=_0x5a5dde[_0x5cbc00('0x42')],_0x1d1b1d=path[_0x5cbc00('0x5d')](config['root'],_0x5cbc00('0x6f'),_0x3ec524+_0x5cbc00('0x19')),_0x115351=_[_0x5cbc00('0x33')](_0x1cd452)[_0x5cbc00('0x5d')]();logger[_0x5cbc00('0xa')](_0x5cbc00('0x45'),_0x115351),fs['writeFileSync'](_0x1d1b1d,_0x115351+'\x0a'),Papa[_0x5cbc00('0x35')](fs[_0x5cbc00('0x48')](_0x5b3130),{'header':!![],'skipEmptyLines':!![],'step':function(_0x35d4e0,_0x4ce398){const _0x5f2558=_0x5cbc00;let _0x157d9f=0x0,_0x5c50f3=[];try{for(_0x157d9f=0x0,_0x5c50f3=[];_0x157d9f<_0x35d4e0[_0x5f2558('0x3e')][_0x5f2558('0x1c')];_0x157d9f++){stats[_0x5f2558('0xc')]+=0x1;if(_0x35d4e0[_0x5f2558('0x30')][_0x157d9f])handleError(_0x35d4e0[_0x5f2558('0x3e')][_0x157d9f],_0x35d4e0[_0x5f2558('0x30')][_0x157d9f]);else{const _0x2e3f5c=[];for(const _0x2b29d6 in _0x1cd452){if(typeof _0x1cd452[_0x2b29d6]!==_0x5f2558('0x77')){if(_0x35d4e0[_0x5f2558('0x3e')][_0x157d9f][_0x1cd452[_0x2b29d6]]){let _0x417c73=_[_0x5f2558('0x2c')](_0x35d4e0[_0x5f2558('0x3e')][_0x157d9f][_0x1cd452[_0x2b29d6]][_0x5f2558('0x78')](/"/g,'\x27'));switch(_0x2b29d6){case _0x5f2558('0x5'):{const _0x3de253=moment(_0x417c73,'YYYY-MM-DD\x20HH:mm:ss',!![]);_0x3de253[_0x5f2558('0x4')]()?_0x5c50f3['push']('\x22'+_0x417c73+'\x22'):_0x2e3f5c[_0x5f2558('0x16')](_0x5f2558('0x72'));}break;case _0x5f2558('0x39'):{const _0x40d62f=parseInt(_0x417c73);_0x417c73=_[_0x5f2558('0x36')](_0x40d62f)||_0x40d62f<0x0||_0x40d62f>0x4?_0xb6a240?0x3:0x2:_0x40d62f,_0x5c50f3[_0x5f2558('0x16')]('\x22'+_0x417c73+'\x22');}break;case _0x5f2558('0x12'):{const _0x56b12a=parseInt(_0x417c73);_['isNumber'](_0x56b12a)&&_[_0x5f2558('0x4f')](_0x4c6985,_0x56b12a)?_0x5c50f3[_0x5f2558('0x16')]('\x22'+_0x56b12a+'\x22'):_0x2e3f5c['push'](_0x5f2558('0x73'));}break;case'email':isEmail(_0x417c73)?_0x5c50f3['push']('\x22'+_0x417c73+'\x22'):_0x2e3f5c[_0x5f2558('0x16')]('email\x20wrong\x20format');break;case'dateOfBirth':{const _0x51b132=moment(_0x417c73,'YYYY-MM-DD',!![]);_0x51b132[_0x5f2558('0x4')]()?_0x5c50f3[_0x5f2558('0x16')]('\x22'+_0x417c73+'\x22'):_0x2e3f5c[_0x5f2558('0x16')]('dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)');}break;default:_0x5c50f3[_0x5f2558('0x16')]('\x22'+_0x417c73+'\x22');}}else switch(_0x2b29d6){case _0x5f2558('0x47'):case _0x5f2558('0xe'):_0x2e3f5c[_0x5f2558('0x16')](_0x2b29d6+'\x20not\x20specified');break;default:_0x5c50f3[_0x5f2558('0x16')]('\x22\x22');}}}_0x2e3f5c[_0x5f2558('0x1c')]?handleError(_0x35d4e0[_0x5f2558('0x3e')][_0x157d9f],{'code':_0x5f2558('0x5f'),'message':_0x2e3f5c['join']()}):fs[_0x5f2558('0x38')](_0x1d1b1d,_0x5c50f3[_0x5f2558('0x5d')]()+'\x0a'),_0x5c50f3=[];}!(stats[_0x5f2558('0xc')]%0xc8)&&(socket[_0x5f2558('0x57')]('contact:import:'+_0x3ec524,stats),stats[_0x5f2558('0x30')]=[]);}stopped&&(logger[_0x5f2558('0xa')]('Abort\x20parser'),_0x4ce398['abort']());}catch(_0x414407){handleError(_0x35d4e0[_0x5f2558('0x3e')][_0x157d9f],{'code':_0x5f2558('0x1f'),'message':_0x414407[_0x5f2558('0x27')]}),logger['error']('step',_0x414407['message']);}},'complete':function(){const _0x46e1be=_0x5cbc00;try{const _0x1dc0d4=[];let _0x2198e6=_0x46e1be('0x6b');_0x5a5dde[_0x46e1be('0x5c')]&&_0x5a5dde['duplicates'][_0x46e1be('0x1c')]&&(_0x2198e6=_0x46e1be('0xf')+_0x3ec524,_0x1dc0d4['push']('CREATE\x20TABLE\x20'+_0x2198e6+'\x20LIKE\x20cm_contacts;'));let _0x37e441=util[_0x46e1be('0x5e')](_0x46e1be('0x3'),_0x1d1b1d,_0x2198e6,_0x115351,_0x5a5dde['ListId'],_0x5a5dde['CompanyId']||null,NOW,NOW);_[_0x46e1be('0x13')](_0x1cd452[_0x46e1be('0x5')])&&(_0x37e441+=',\x20scheduledAt\x20=\x20NOW()'),_0x1dc0d4[_0x46e1be('0x16')](_0x37e441),_0x5a5dde['duplicates']&&_0x5a5dde[_0x46e1be('0x5c')]['length']&&(_0x1dc0d4[_0x46e1be('0x16')](insertCmContact(_0x5a5dde[_0x46e1be('0x17')],_0x2198e6,_0x5a5dde[_0x46e1be('0x5c')],_0x1cd452)),_0x1dc0d4[_0x46e1be('0x16')](_0x46e1be('0x5a')+_0x2198e6+';')),_0x1dc0d4['push'](insertCmHopper(_0x5a5dde['ListId'],_0x46e1be('0x4d'),_0x46e1be('0x7'),_0x46e1be('0x64'),_0xb6a240)),_0x1dc0d4[_0x46e1be('0x16')](insertCmHopperBlack(_0x5a5dde[_0x46e1be('0x17')],_0x46e1be('0x1a'),_0x46e1be('0x7'),_0x46e1be('0x64'))),_0x1dc0d4[_0x46e1be('0x16')](insertCmHopper(_0x5a5dde['ListId'],'campaigns_has_cm_lists','campaigns',_0x46e1be('0x79'),_0xb6a240)),_0x1dc0d4[_0x46e1be('0x16')](insertCmHopperBlack(_0x5a5dde[_0x46e1be('0x17')],_0x46e1be('0x8'),_0x46e1be('0x14'),_0x46e1be('0x79'))),Promise[_0x46e1be('0x62')](_0x1dc0d4,handleQuery)[_0x46e1be('0x74')](function(){const _0x5cc030=_0x46e1be;stats[_0x5cc030('0x3f')]=!![],stats[_0x5cc030('0x37')]=_0x5a5dde[_0x5cc030('0x5c')]&&_0x5a5dde['duplicates']['length']?results[0x2][_0x5cc030('0x37')]:results[0x0]['affectedRows'],socket['emit']('contact:import:'+_0x3ec524,stats),fs[_0x5cc030('0x59')](_0x5b3130),fs[_0x5cc030('0x59')](_0x1d1b1d),process[_0x5cc030('0x76')](0x0);})[_0x46e1be('0x6e')](function(_0x58ca0c){const _0x2ba771=_0x46e1be;logger[_0x2ba771('0x52')](_0x58ca0c['message']);throw _0x58ca0c;});}catch(_0x5a5e40){logger[_0x46e1be('0x52')](_0x46e1be('0x2a'),_0x5a5e40[_0x46e1be('0x27')]);throw _0x5a5e40;}},'error':function(_0x1b2cb7,_0x35bc68,_0x22dc21,_0x2ce64a){throw new Error(_0x2ce64a);}});}function validate(){const _0x485721=_0x3ed061;if(process[_0x485721('0x6')]&&process['argv']['length']<0x4)throw new Error('arguments\x20<filename>\x20<parameters>\x20<agents>');const _0x35a347=path[_0x485721('0x5d')](config['root'],_0x485721('0x58'),process[_0x485721('0x6')][0x2]);if(!fs[_0x485721('0x63')](_0x35a347))throw new Error(_0x485721('0x21'));let _0x3d98b9={},_0x1a7139=0x0;try{_0x3d98b9=JSON[_0x485721('0x35')](process[_0x485721('0x6')][0x3]);if(!_0x3d98b9[_0x485721('0x44')])throw new Error('binding\x20doesn\x27t\x20exists');if(!_0x3d98b9[_0x485721('0x44')][_0x485721('0xe')])throw new Error(_0x485721('0x26'));if(!_0x3d98b9[_0x485721('0x44')]['firstName'])throw new Error(_0x485721('0x22'));_0x3d98b9[_0x485721('0x44')]['UserId']&&(_0x1a7139=0x1);if(!_0x3d98b9[_0x485721('0x17')])throw new Error(_0x485721('0x68'));}catch(_0xdc90fa){throw new Error('body\x20id\x20mandatory');}let _0x526487=[];try{_0x526487=_['map'](JSON[_0x485721('0x35')](process[_0x485721('0x6')][0x4]),'id');}catch(_0x16bc29){logger[_0x485721('0xa')](_0x485721('0x24'));}main(_0x35a347,_0x3d98b9,_0x526487,_0x1a7139);}validate(),process['on'](_0x3ed061('0x1d'),handleStop),process['on'](_0x3ed061('0x6c'),handleUncaughtException);