98c88822deae1099d81f4022c22f645a4c3f3a18
[motion2.git] / server / components / import / cm / csv2.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 _0x5454=['concat','keys','CompanyId','id\x20IN\x20?','MIN(id)','min_id','group','info','Received\x20SIGINT','message','exit','push','code','sequelize','query','spread','main','stringify','start','binding','socket_timestamp','root','server/files/tmp','.csv','writeFileSync','createReadStream','data','length','rows','errors','trim','replace','scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)','priority','isNaN','includes','agent\x20not\x20exists','email','email\x20wrong\x20format','dateOfBirth','YYYY-MM-DD','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','firstName','\x20not\x20specified','SystemRow','appendFileSync','emit','contact:import:','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',',\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','unlink','error','complete','argv','existsSync','file\x20doesn\x27t\x20exists','binding\x20doesn\x27t\x20exists','ListId\x20is\x20mandatory','parse','No\x20agent\x20available','SIGINT','uncaughtException','util','ioredis','../../../mysqldb','../../../config/environment','../../../config/logger','redis','defaults','localhost','socket.io-emitter','format','YYYY-MM-DD\x20HH:mm:ss','test','select','field','phone','from','join','where','\x20=\x20','.dialCheckDuplicateType\x20IN\x20(','cm_contacts.scheduledat','cm_contacts.id','cm_contacts.ListId','cm_contacts.createdAt','cm_contacts.UserId','insert','into','cm_hopper','fromQuery','scheduledat','ContactId','updatedAt','UserId','recallme','fields','cm_contacts','cm_contacts.ListId\x20=\x20','.id\x20=\x20','.CmListId\x20=\x20?','cm_contacts.createdAt\x20=\x20?','\x22always\x22','\x22onlyIfOpen\x22','cm_contacts.phone\x20NOT\x20IN\x20(?)','toString','cm_contacts.phone','cm_contacts.updatedAt','cm_hopper_black','ListId','createdAt','.CmListId','ListId\x20=\x20?','\x20=\x20?'];(function(_0x15beab,_0x578076){var _0x5cb60a=function(_0x31fc90){while(--_0x31fc90){_0x15beab['push'](_0x15beab['shift']());}};_0x5cb60a(++_0x578076);}(_0x5454,0x15b));var _0x4545=function(_0x2afa20,_0x6a922e){_0x2afa20=_0x2afa20-0x0;var _0x5b82c8=_0x5454[_0x2afa20];return _0x5b82c8;};'use strict';var fs=require('fs');var path=require('path');var _=require('lodash');var Papa=require('papaparse');var util=require(_0x4545('0x0'));var BPromise=require('bluebird');var moment=require('moment');var squel=require('squel');var Redis=require(_0x4545('0x1'));var db=require(_0x4545('0x2'))['db'];var config=require(_0x4545('0x3'));var logger=require(_0x4545('0x4'))('import');config[_0x4545('0x5')]=_[_0x4545('0x6')](config[_0x4545('0x5')],{'host':_0x4545('0x7'),'port':0x18eb});var socket=require(_0x4545('0x8'))(new Redis(config[_0x4545('0x5')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()[_0x4545('0x9')](_0x4545('0xa'));function isEmail(_0xe576ac){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))*$/[_0x4545('0xb')](_0xe576ac);}function selectNotIn(_0x22447e,_0x40cd1c,_0xafaeb7,_0x491b7b,_0x869994){return squel[_0x4545('0xc')]()[_0x4545('0xd')](_0x4545('0xe'))[_0x4545('0xf')](_0x491b7b)[_0x4545('0x10')](_0x40cd1c,null,_0x40cd1c+'.id\x20=\x20'+_0x491b7b+'.'+_0xafaeb7)[_0x4545('0x11')](_0x491b7b+'.'+_0xafaeb7+_0x4545('0x12')+_0x22447e+'.'+_0xafaeb7)[_0x4545('0x11')](_0x40cd1c+_0x4545('0x13')+_0x869994[_0x4545('0x10')]()+')');}function insertCmHopper(_0xb9120a,_0x3e7088,_0x347072,_0x36bb8e,_0xab7856){var _0x40a079=['cm_contacts.phone',_0x4545('0x14'),_0x4545('0x15'),_0x4545('0x16'),_0x3e7088+'.'+_0x36bb8e,_0x4545('0x17'),'cm_contacts.updatedAt','cm_contacts.priority',_0x4545('0x18'),'\x22'+_0xab7856+'\x22'];return squel[_0x4545('0x19')]()[_0x4545('0x1a')](_0x4545('0x1b'))[_0x4545('0x1c')]([_0x4545('0xe'),_0x4545('0x1d'),_0x4545('0x1e'),'ListId',_0x36bb8e,'createdAt',_0x4545('0x1f'),'priority',_0x4545('0x20'),_0x4545('0x21')],squel[_0x4545('0xc')]()[_0x4545('0x22')](_0x40a079)[_0x4545('0xf')](_0x3e7088)[_0x4545('0x10')](_0x4545('0x23'),null,_0x4545('0x24')+_0x3e7088+'.CmListId')['join'](_0x347072,null,_0x347072+_0x4545('0x25')+_0x3e7088+'.'+_0x36bb8e)['where'](_0x3e7088+_0x4545('0x26'),_0xb9120a)['where'](_0x4545('0x27'),NOW)['where']('cm_contacts.phone\x20NOT\x20IN\x20(?)',selectNotIn(_0x3e7088,_0x347072,_0x36bb8e,_0x4545('0x1b'),[_0x4545('0x28'),_0x4545('0x29')]))[_0x4545('0x11')](_0x4545('0x2a'),selectNotIn(_0x3e7088,_0x347072,_0x36bb8e,_0x4545('0x1b'),[_0x4545('0x28')])))[_0x4545('0x2b')]();}function insertCmHopperBlack(_0xac3456,_0x2a6842,_0x3e5807,_0x43749b){var _0xab0b00=[_0x4545('0x2c'),_0x4545('0x15'),_0x4545('0x16'),_0x2a6842+'.'+_0x43749b,'cm_contacts.createdAt',_0x4545('0x2d')];return squel[_0x4545('0x19')]()[_0x4545('0x1a')](_0x4545('0x2e'))[_0x4545('0x1c')](['phone',_0x4545('0x1e'),_0x4545('0x2f'),_0x43749b,_0x4545('0x30'),_0x4545('0x1f')],squel[_0x4545('0xc')]()[_0x4545('0x22')](_0xab0b00)['from'](_0x2a6842)[_0x4545('0x10')](_0x4545('0x23'),null,_0x4545('0x24')+_0x2a6842+_0x4545('0x31'))[_0x4545('0x10')](_0x3e5807,null,_0x3e5807+_0x4545('0x25')+_0x2a6842+'.'+_0x43749b)[_0x4545('0x11')](_0x2a6842+_0x4545('0x26'),_0xac3456)[_0x4545('0x11')](_0x4545('0x27'),NOW)[_0x4545('0x11')]('cm_contacts.phone\x20NOT\x20IN\x20(?)',squel[_0x4545('0xc')]()['field'](_0x4545('0xe'))[_0x4545('0xf')]('cm_hopper_black')[_0x4545('0x11')](_0x4545('0x32'),_0xac3456)[_0x4545('0x11')](_0x43749b+_0x4545('0x33'),_0x2a6842+'.'+_0x43749b)))[_0x4545('0x2b')]();}function insertCmContact(_0x140d7a,_0x16acf4,_0x54ea62,_0x1abc06){var _0x386b38=_[_0x4545('0x34')](_[_0x4545('0x35')](_0x1abc06),[_0x4545('0x2f'),_0x4545('0x36'),'createdAt',_0x4545('0x1f')]);return squel[_0x4545('0x19')]()[_0x4545('0x1a')](_0x4545('0x23'))[_0x4545('0x1c')](_0x386b38,squel[_0x4545('0xc')]()[_0x4545('0x22')](_0x386b38)[_0x4545('0xf')](_0x16acf4)[_0x4545('0x11')]('('+_0x54ea62['join'](',')+')\x20NOT\x20IN\x20?',squel['select']()[_0x4545('0x22')](_0x54ea62)['from'](_0x4545('0x23'))[_0x4545('0x11')](_0x4545('0x32'),_0x140d7a)[_0x4545('0x11')]('deletedAt\x20IS\x20NULL'))['where'](_0x4545('0x37'),squel['select']()[_0x4545('0xd')](_0x4545('0x38'),_0x4545('0x39'))[_0x4545('0xf')](_0x16acf4)[_0x4545('0x3a')](_0x54ea62['join'](','))))['toString']();}function handleStop(){logger[_0x4545('0x3b')](_0x4545('0x3c'));stopped=!![];}function handleUncaughtException(_0x58d519){logger['error'](_0x58d519[_0x4545('0x3d')]);process[_0x4545('0x3e')](0x1);}function handleError(_0x481a43,_0x1aafdf){stats['errors'][_0x4545('0x3f')]({'name':_0x1aafdf[_0x4545('0x40')],'message':_0x1aafdf[_0x4545('0x3d')],'row':_0x481a43});}function handleQuery(_0x3070c4){logger['info'](_0x3070c4);return db[_0x4545('0x41')][_0x4545('0x42')](_0x3070c4)[_0x4545('0x43')](function(_0x299869){results['push'](_0x299869);})['catch'](function(_0x41456a){logger['error'](_0x41456a[_0x4545('0x3d')]);});}function main(_0x11e808,_0x3b01c2,_0x593569,_0x427554){logger[_0x4545('0x3b')](_0x4545('0x44'),_0x11e808,JSON[_0x4545('0x45')](_0x3b01c2));process['send'](_0x4545('0x46'));var _0x380463=_0x3b01c2[_0x4545('0x47')];var _0x523308=_0x3b01c2[_0x4545('0x48')];var _0x323769=path[_0x4545('0x10')](config[_0x4545('0x49')],_0x4545('0x4a'),_0x523308+_0x4545('0x4b'));var _0x223c37=_[_0x4545('0x35')](_0x380463)['join']();logger[_0x4545('0x3b')]('header',_0x223c37);fs[_0x4545('0x4c')](_0x323769,_0x223c37+'\x0a');Papa['parse'](fs[_0x4545('0x4d')](_0x11e808),{'header':!![],'skipEmptyLines':!![],'step':function(_0x1e653b,_0x162f29){try{for(var _0x582192=0x0,_0x20f054=[];_0x582192<_0x1e653b[_0x4545('0x4e')][_0x4545('0x4f')];_0x582192++){stats[_0x4545('0x50')]+=0x1;if(_0x1e653b[_0x4545('0x51')][_0x582192]){handleError(_0x1e653b[_0x4545('0x4e')][_0x582192],_0x1e653b['errors'][_0x582192]);}else{var _0x59d00c=[];for(var _0x499246 in _0x380463){if(_0x380463['hasOwnProperty'](_0x499246)){if(_0x1e653b[_0x4545('0x4e')][_0x582192][_0x380463[_0x499246]]){var _0x14153e=_[_0x4545('0x52')](_0x1e653b[_0x4545('0x4e')][_0x582192][_0x380463[_0x499246]][_0x4545('0x53')](/"/g,'\x27'));switch(_0x499246){case _0x4545('0x1d'):var _0x637b88=moment(_0x14153e,'YYYY-MM-DD\x20HH:mm:ss',!![]);if(_0x637b88['isValid']()){_0x20f054[_0x4545('0x3f')]('\x22'+_0x14153e+'\x22');}else{_0x59d00c[_0x4545('0x3f')](_0x4545('0x54'));}break;case _0x4545('0x55'):var _0x40df40=parseInt(_0x14153e);_0x14153e=_[_0x4545('0x56')](_0x40df40)||_0x40df40<0x0||_0x40df40>0x4?_0x427554?0x3:0x2:_0x40df40;_0x20f054['push']('\x22'+_0x14153e+'\x22');break;case _0x4545('0x20'):var _0xbf9ef4=parseInt(_0x14153e);if(_['isNumber'](_0xbf9ef4)&&_[_0x4545('0x57')](_0x593569,_0xbf9ef4)){_0x20f054['push']('\x22'+_0xbf9ef4+'\x22');}else{_0x59d00c[_0x4545('0x3f')](_0x4545('0x58'));}break;case _0x4545('0x59'):if(isEmail(_0x14153e)){_0x20f054[_0x4545('0x3f')]('\x22'+_0x14153e+'\x22');}else{_0x59d00c[_0x4545('0x3f')](_0x4545('0x5a'));}break;case _0x4545('0x5b'):var _0x569fa6=moment(_0x14153e,_0x4545('0x5c'),!![]);if(_0x569fa6['isValid']()){_0x20f054[_0x4545('0x3f')]('\x22'+_0x14153e+'\x22');}else{_0x59d00c['push'](_0x4545('0x5d'));}break;default:_0x20f054[_0x4545('0x3f')]('\x22'+_0x14153e+'\x22');}}else{switch(_0x499246){case _0x4545('0x5e'):case'phone':_0x59d00c[_0x4545('0x3f')](_0x499246+_0x4545('0x5f'));break;default:_0x20f054['push']('\x22\x22');}}}}if(_0x59d00c[_0x4545('0x4f')]){handleError(_0x1e653b['data'][_0x582192],{'code':_0x4545('0x60'),'message':_0x59d00c[_0x4545('0x10')]()});}else{fs[_0x4545('0x61')](_0x323769,_0x20f054[_0x4545('0x10')]()+'\x0a');}_0x20f054=[];}if(!(stats[_0x4545('0x50')]%0xc8)){socket[_0x4545('0x62')](_0x4545('0x63')+_0x523308,stats);stats[_0x4545('0x51')]=[];}}if(stopped){logger[_0x4545('0x3b')]('Abort\x20parser');_0x162f29[_0x4545('0x64')]();}}catch(_0x2496be){handleError(_0x1e653b[_0x4545('0x4e')][_0x582192],{'code':_0x4545('0x65'),'message':_0x2496be[_0x4545('0x3d')]});logger['error'](_0x4545('0x66'),_0x2496be[_0x4545('0x3d')]);}},'complete':function(){try{var _0x40e25a=[];var _0x7f4553=_0x4545('0x23');if(_0x3b01c2[_0x4545('0x67')]&&_0x3b01c2[_0x4545('0x67')][_0x4545('0x4f')]){_0x7f4553='cm_contacts_'+_0x523308;_0x40e25a[_0x4545('0x3f')](_0x4545('0x68')+_0x7f4553+_0x4545('0x69'));}var _0x237564=util[_0x4545('0x9')](_0x4545('0x6a'),_0x323769,_0x7f4553,_0x223c37,_0x3b01c2[_0x4545('0x2f')],_0x3b01c2[_0x4545('0x36')]||null,NOW,NOW);if(_['isNil'](_0x380463[_0x4545('0x1d')])){_0x237564+=_0x4545('0x6b');}_0x40e25a[_0x4545('0x3f')](_0x237564);if(_0x3b01c2[_0x4545('0x67')]&&_0x3b01c2[_0x4545('0x67')]['length']){_0x40e25a[_0x4545('0x3f')](insertCmContact(_0x3b01c2[_0x4545('0x2f')],_0x7f4553,_0x3b01c2[_0x4545('0x67')],_0x380463));_0x40e25a[_0x4545('0x3f')](_0x4545('0x6c')+_0x7f4553+';');}_0x40e25a[_0x4545('0x3f')](insertCmHopper(_0x3b01c2[_0x4545('0x2f')],_0x4545('0x6d'),_0x4545('0x6e'),_0x4545('0x6f'),_0x427554));_0x40e25a[_0x4545('0x3f')](insertCmHopperBlack(_0x3b01c2[_0x4545('0x2f')],_0x4545('0x70'),'voice_queues',_0x4545('0x6f')));_0x40e25a[_0x4545('0x3f')](insertCmHopper(_0x3b01c2[_0x4545('0x2f')],_0x4545('0x71'),_0x4545('0x72'),_0x4545('0x73'),_0x427554));_0x40e25a['push'](insertCmHopperBlack(_0x3b01c2['ListId'],_0x4545('0x74'),_0x4545('0x72'),_0x4545('0x73')));BPromise[_0x4545('0x75')](_0x40e25a,handleQuery)[_0x4545('0x76')](function(_0x1025d1){stats[_0x4545('0x77')]=!![];stats[_0x4545('0x78')]=_0x3b01c2[_0x4545('0x67')]&&_0x3b01c2[_0x4545('0x67')][_0x4545('0x4f')]?results[0x2][_0x4545('0x78')]:results[0x0]['affectedRows'];socket[_0x4545('0x62')]('contact:import:'+_0x523308,stats);fs[_0x4545('0x79')](_0x11e808);fs['unlink'](_0x323769);process[_0x4545('0x3e')](0x0);})['catch'](function(_0x34159a){logger[_0x4545('0x7a')](_0x34159a[_0x4545('0x3d')]);process['exit'](0x1);});}catch(_0x5a27c2){logger[_0x4545('0x7a')](_0x4545('0x7b'),_0x5a27c2['message']);process[_0x4545('0x3e')](0x1);}},'error':function(_0x8839ed,_0x52b8da,_0x5400a5,_0x59333c){throw new Error(_0x59333c);}});}function validate(){if(process[_0x4545('0x7c')]&&process[_0x4545('0x7c')]['length']<0x4){throw new Error('arguments\x20<filename>\x20<parameters>\x20<agents>');}var _0x5b8e76=path['join'](config['root'],_0x4545('0x4a'),process[_0x4545('0x7c')][0x2]);if(!fs[_0x4545('0x7d')](_0x5b8e76)){throw new Error(_0x4545('0x7e'));}var _0x3f9574={};try{_0x3f9574=JSON['parse'](process['argv'][0x3]);if(!_0x3f9574['binding']){throw new Error(_0x4545('0x7f'));}if(!_0x3f9574[_0x4545('0x47')][_0x4545('0xe')]){throw new Error('phone\x20is\x20mandatory');}if(!_0x3f9574[_0x4545('0x47')][_0x4545('0x5e')]){throw new Error('firstName\x20is\x20mandatory');}var _0x443147=0x0;if(_0x3f9574[_0x4545('0x47')]['UserId']){_0x443147=0x1;}if(!_0x3f9574['ListId']){throw new Error(_0x4545('0x80'));}}catch(_0x411d3c){throw new Error('body\x20id\x20mandatory');}var _0x44aa2c=[];try{_0x44aa2c=_['map'](JSON[_0x4545('0x81')](process['argv'][0x4]),'id');}catch(_0x58e3d9){logger['info'](_0x4545('0x82'));}main(_0x5b8e76,_0x3f9574,_0x44aa2c,_0x443147);}validate();process['on'](_0x4545('0x83'),handleStop);process['on'](_0x4545('0x84'),handleUncaughtException);