Built motion from commit 19a84241.|2.5.34
[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 _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);