Built motion from commit (unavailable).|2.5.10
[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 _0xbbfd=['.id\x20=\x20','where','\x20=\x20','join','cm_contacts.phone','cm_contacts.updatedAt','insert','fromQuery','scheduledat','ListId','createdAt','priority','UserId','recallme','select','from','cm_contacts','cm_contacts.ListId\x20=\x20','.CmListId\x20=\x20?','cm_contacts.createdAt\x20=\x20?','cm_contacts.phone\x20NOT\x20IN\x20(?)','cm_hopper','\x22always\x22','\x22onlyIfOpen\x22','cm_contacts.ListId','cm_contacts.createdAt','into','cm_hopper_black','updatedAt','.CmListId','ListId\x20=\x20?','toString','concat','keys',')\x20NOT\x20IN\x20?','fields','deletedAt\x20IS\x20NULL','id\x20IN\x20?','MIN(id)','min_id','group','info','Received\x20SIGINT','message','exit','errors','push','code','sequelize','spread','error','main','stringify','send','start','binding','socket_timestamp','root','server/files/tmp','.csv','writeFileSync','parse','length','rows','data','hasOwnProperty','trim','replace','isValid','isNaN','includes','agent\x20not\x20exists','email\x20wrong\x20format','YYYY-MM-DD','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','firstName','\x20not\x20specified','contact:import:','abort','step','duplicates','cm_contacts_','CREATE\x20TABLE\x20','CompanyId','isNil',',\x20scheduledAt\x20=\x20NOW()','DROP\x20TABLE\x20','voice_queues_has_cm_lists','voice_queues','VoiceQueueId','campaigns_has_cm_lists','campaigns','CampaignId','campaigns_has_cm_blacklists','each','then','finish','affectedRows','emit','unlink','complete','argv','arguments\x20<filename>\x20<parameters>\x20<agents>','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','lodash','papaparse','util','moment','squel','ioredis','../../../mysqldb','../../../config/environment','../../../config/logger','redis','defaults','socket.io-emitter','format','YYYY-MM-DD\x20HH:mm:ss','test','field','phone'];(function(_0x5b417b,_0x251759){var _0x9aa893=function(_0x529de6){while(--_0x529de6){_0x5b417b['push'](_0x5b417b['shift']());}};_0x9aa893(++_0x251759);}(_0xbbfd,0x72));var _0xdbbf=function(_0x7e2eb0,_0x17cd58){_0x7e2eb0=_0x7e2eb0-0x0;var _0xd92de4=_0xbbfd[_0x7e2eb0];return _0xd92de4;};'use strict';var fs=require('fs');var path=require(_0xdbbf('0x0'));var _=require(_0xdbbf('0x1'));var Papa=require(_0xdbbf('0x2'));var util=require(_0xdbbf('0x3'));var BPromise=require('bluebird');var moment=require(_0xdbbf('0x4'));var squel=require(_0xdbbf('0x5'));var Redis=require(_0xdbbf('0x6'));var db=require(_0xdbbf('0x7'))['db'];var config=require(_0xdbbf('0x8'));var logger=require(_0xdbbf('0x9'))('import');config[_0xdbbf('0xa')]=_[_0xdbbf('0xb')](config[_0xdbbf('0xa')],{'host':'localhost','port':0x18eb});var socket=require(_0xdbbf('0xc'))(new Redis(config[_0xdbbf('0xa')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()[_0xdbbf('0xd')](_0xdbbf('0xe'));function isEmail(_0x1fddeb){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))*$/[_0xdbbf('0xf')](_0x1fddeb);}function selectNotIn(_0x575a50,_0x772b74,_0x57ed05,_0x186e80,_0x15863a){return squel['select']()[_0xdbbf('0x10')](_0xdbbf('0x11'))['from'](_0x186e80)['join'](_0x772b74,null,_0x772b74+_0xdbbf('0x12')+_0x186e80+'.'+_0x57ed05)[_0xdbbf('0x13')](_0x186e80+'.'+_0x57ed05+_0xdbbf('0x14')+_0x575a50+'.'+_0x57ed05)['where'](_0x772b74+'.dialCheckDuplicateType\x20IN\x20('+_0x15863a[_0xdbbf('0x15')]()+')');}function insertCmHopper(_0x218109,_0x5423ee,_0x529b1f,_0x5544fe,_0x4adc45){var _0x43e237=[_0xdbbf('0x16'),'cm_contacts.scheduledat','cm_contacts.id','cm_contacts.ListId',_0x5423ee+'.'+_0x5544fe,'cm_contacts.createdAt',_0xdbbf('0x17'),'cm_contacts.priority','cm_contacts.UserId','\x22'+_0x4adc45+'\x22'];return squel[_0xdbbf('0x18')]()['into']('cm_hopper')[_0xdbbf('0x19')]([_0xdbbf('0x11'),_0xdbbf('0x1a'),'ContactId',_0xdbbf('0x1b'),_0x5544fe,_0xdbbf('0x1c'),'updatedAt',_0xdbbf('0x1d'),_0xdbbf('0x1e'),_0xdbbf('0x1f')],squel[_0xdbbf('0x20')]()['fields'](_0x43e237)[_0xdbbf('0x21')](_0x5423ee)[_0xdbbf('0x15')](_0xdbbf('0x22'),null,_0xdbbf('0x23')+_0x5423ee+'.CmListId')[_0xdbbf('0x15')](_0x529b1f,null,_0x529b1f+_0xdbbf('0x12')+_0x5423ee+'.'+_0x5544fe)[_0xdbbf('0x13')](_0x5423ee+_0xdbbf('0x24'),_0x218109)[_0xdbbf('0x13')](_0xdbbf('0x25'),NOW)[_0xdbbf('0x13')](_0xdbbf('0x26'),selectNotIn(_0x5423ee,_0x529b1f,_0x5544fe,_0xdbbf('0x27'),[_0xdbbf('0x28'),_0xdbbf('0x29')]))[_0xdbbf('0x13')](_0xdbbf('0x26'),selectNotIn(_0x5423ee,_0x529b1f,_0x5544fe,_0xdbbf('0x27'),['\x22always\x22'])))['toString']();}function insertCmHopperBlack(_0x420ff6,_0x103faa,_0x55966c,_0x5e73c0){var _0x2194ed=[_0xdbbf('0x16'),'cm_contacts.id',_0xdbbf('0x2a'),_0x103faa+'.'+_0x5e73c0,_0xdbbf('0x2b'),_0xdbbf('0x17')];return squel['insert']()[_0xdbbf('0x2c')](_0xdbbf('0x2d'))[_0xdbbf('0x19')]([_0xdbbf('0x11'),'ContactId','ListId',_0x5e73c0,_0xdbbf('0x1c'),_0xdbbf('0x2e')],squel[_0xdbbf('0x20')]()['fields'](_0x2194ed)[_0xdbbf('0x21')](_0x103faa)[_0xdbbf('0x15')](_0xdbbf('0x22'),null,_0xdbbf('0x23')+_0x103faa+_0xdbbf('0x2f'))[_0xdbbf('0x15')](_0x55966c,null,_0x55966c+'.id\x20=\x20'+_0x103faa+'.'+_0x5e73c0)[_0xdbbf('0x13')](_0x103faa+'.CmListId\x20=\x20?',_0x420ff6)[_0xdbbf('0x13')]('cm_contacts.createdAt\x20=\x20?',NOW)[_0xdbbf('0x13')]('cm_contacts.phone\x20NOT\x20IN\x20(?)',squel['select']()[_0xdbbf('0x10')](_0xdbbf('0x11'))[_0xdbbf('0x21')](_0xdbbf('0x2d'))['where'](_0xdbbf('0x30'),_0x420ff6)[_0xdbbf('0x13')](_0x5e73c0+'\x20=\x20?',_0x103faa+'.'+_0x5e73c0)))[_0xdbbf('0x31')]();}function insertCmContact(_0x2c3ae,_0x293184,_0x51a92f,_0x413b83){var _0x459fd8=_[_0xdbbf('0x32')](_[_0xdbbf('0x33')](_0x413b83),[_0xdbbf('0x1b'),'CompanyId','createdAt','updatedAt']);return squel['insert']()[_0xdbbf('0x2c')](_0xdbbf('0x22'))[_0xdbbf('0x19')](_0x459fd8,squel[_0xdbbf('0x20')]()['fields'](_0x459fd8)[_0xdbbf('0x21')](_0x293184)[_0xdbbf('0x13')]('('+_0x51a92f[_0xdbbf('0x15')](',')+_0xdbbf('0x34'),squel[_0xdbbf('0x20')]()[_0xdbbf('0x35')](_0x51a92f)[_0xdbbf('0x21')](_0xdbbf('0x22'))['where'](_0xdbbf('0x30'),_0x2c3ae)[_0xdbbf('0x13')](_0xdbbf('0x36')))[_0xdbbf('0x13')](_0xdbbf('0x37'),squel[_0xdbbf('0x20')]()[_0xdbbf('0x10')](_0xdbbf('0x38'),_0xdbbf('0x39'))[_0xdbbf('0x21')](_0x293184)[_0xdbbf('0x3a')](_0x51a92f[_0xdbbf('0x15')](','))))['toString']();}function handleStop(){logger[_0xdbbf('0x3b')](_0xdbbf('0x3c'));stopped=!![];}function handleUncaughtException(_0x3c94cb){logger['error'](_0x3c94cb[_0xdbbf('0x3d')]);process[_0xdbbf('0x3e')](0x1);}function handleError(_0x4e7ea2,_0x32cdb1){stats[_0xdbbf('0x3f')][_0xdbbf('0x40')]({'name':_0x32cdb1[_0xdbbf('0x41')],'message':_0x32cdb1[_0xdbbf('0x3d')],'row':_0x4e7ea2});}function handleQuery(_0x189573){logger['info'](_0x189573);return db[_0xdbbf('0x42')]['query'](_0x189573)[_0xdbbf('0x43')](function(_0x1af7de){results[_0xdbbf('0x40')](_0x1af7de);})['catch'](function(_0x480841){logger[_0xdbbf('0x44')](_0x480841[_0xdbbf('0x3d')]);});}function main(_0x42b47e,_0x573b74,_0x316f1d,_0x2a399c){logger[_0xdbbf('0x3b')](_0xdbbf('0x45'),_0x42b47e,JSON[_0xdbbf('0x46')](_0x573b74));process[_0xdbbf('0x47')](_0xdbbf('0x48'));var _0x12892e=_0x573b74[_0xdbbf('0x49')];var _0x70eab3=_0x573b74[_0xdbbf('0x4a')];var _0x1b0198=path[_0xdbbf('0x15')](config[_0xdbbf('0x4b')],_0xdbbf('0x4c'),_0x70eab3+_0xdbbf('0x4d'));var _0x3a3f06=_[_0xdbbf('0x33')](_0x12892e)[_0xdbbf('0x15')]();logger[_0xdbbf('0x3b')]('header',_0x3a3f06);fs[_0xdbbf('0x4e')](_0x1b0198,_0x3a3f06+'\x0a');Papa[_0xdbbf('0x4f')](fs['createReadStream'](_0x42b47e),{'header':!![],'skipEmptyLines':!![],'step':function(_0x5a0ebd,_0x40fd03){try{for(var _0xaa2954=0x0,_0x40ca4a=[];_0xaa2954<_0x5a0ebd['data'][_0xdbbf('0x50')];_0xaa2954++){stats[_0xdbbf('0x51')]+=0x1;if(_0x5a0ebd['errors'][_0xaa2954]){handleError(_0x5a0ebd[_0xdbbf('0x52')][_0xaa2954],_0x5a0ebd[_0xdbbf('0x3f')][_0xaa2954]);}else{var _0x461136=[];for(var _0x5dbc0c in _0x12892e){if(_0x12892e[_0xdbbf('0x53')](_0x5dbc0c)){if(_0x5a0ebd[_0xdbbf('0x52')][_0xaa2954][_0x12892e[_0x5dbc0c]]){var _0x138a00=_[_0xdbbf('0x54')](_0x5a0ebd[_0xdbbf('0x52')][_0xaa2954][_0x12892e[_0x5dbc0c]][_0xdbbf('0x55')](/"/g,'\x27'));switch(_0x5dbc0c){case _0xdbbf('0x1a'):var _0x3263d1=moment(_0x138a00,_0xdbbf('0xe'),!![]);if(_0x3263d1[_0xdbbf('0x56')]()){_0x40ca4a[_0xdbbf('0x40')]('\x22'+_0x138a00+'\x22');}else{_0x461136[_0xdbbf('0x40')]('scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)');}break;case _0xdbbf('0x1d'):var _0x4d9805=parseInt(_0x138a00);_0x138a00=_[_0xdbbf('0x57')](_0x4d9805)||_0x4d9805<0x0||_0x4d9805>0x4?_0x2a399c?0x3:0x2:_0x4d9805;_0x40ca4a[_0xdbbf('0x40')]('\x22'+_0x138a00+'\x22');break;case'UserId':var _0x384b24=parseInt(_0x138a00);if(_['isNumber'](_0x384b24)&&_[_0xdbbf('0x58')](_0x316f1d,_0x384b24)){_0x40ca4a['push']('\x22'+_0x384b24+'\x22');}else{_0x461136[_0xdbbf('0x40')](_0xdbbf('0x59'));}break;case'email':if(isEmail(_0x138a00)){_0x40ca4a[_0xdbbf('0x40')]('\x22'+_0x138a00+'\x22');}else{_0x461136[_0xdbbf('0x40')](_0xdbbf('0x5a'));}break;case'dateOfBirth':var _0x5edb5c=moment(_0x138a00,_0xdbbf('0x5b'),!![]);if(_0x5edb5c[_0xdbbf('0x56')]()){_0x40ca4a['push']('\x22'+_0x138a00+'\x22');}else{_0x461136[_0xdbbf('0x40')](_0xdbbf('0x5c'));}break;default:_0x40ca4a[_0xdbbf('0x40')]('\x22'+_0x138a00+'\x22');}}else{switch(_0x5dbc0c){case _0xdbbf('0x5d'):case _0xdbbf('0x11'):_0x461136[_0xdbbf('0x40')](_0x5dbc0c+_0xdbbf('0x5e'));break;default:_0x40ca4a[_0xdbbf('0x40')]('\x22\x22');}}}}if(_0x461136[_0xdbbf('0x50')]){handleError(_0x5a0ebd[_0xdbbf('0x52')][_0xaa2954],{'code':'SystemRow','message':_0x461136['join']()});}else{fs['appendFileSync'](_0x1b0198,_0x40ca4a[_0xdbbf('0x15')]()+'\x0a');}_0x40ca4a=[];}if(!(stats[_0xdbbf('0x51')]%0xc8)){socket['emit'](_0xdbbf('0x5f')+_0x70eab3,stats);stats['errors']=[];}}if(stopped){logger['info']('Abort\x20parser');_0x40fd03[_0xdbbf('0x60')]();}}catch(_0x357186){handleError(_0x5a0ebd[_0xdbbf('0x52')][_0xaa2954],{'code':'SystemError','message':_0x357186[_0xdbbf('0x3d')]});logger[_0xdbbf('0x44')](_0xdbbf('0x61'),_0x357186[_0xdbbf('0x3d')]);}},'complete':function(){try{var _0x490674=[];var _0x11295f=_0xdbbf('0x22');if(_0x573b74['duplicates']&&_0x573b74[_0xdbbf('0x62')]['length']){_0x11295f=_0xdbbf('0x63')+_0x70eab3;_0x490674[_0xdbbf('0x40')](_0xdbbf('0x64')+_0x11295f+'\x20LIKE\x20cm_contacts;');}var _0x5653eb=util[_0xdbbf('0xd')]('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',_0x1b0198,_0x11295f,_0x3a3f06,_0x573b74[_0xdbbf('0x1b')],_0x573b74[_0xdbbf('0x65')]||null,NOW,NOW);if(_[_0xdbbf('0x66')](_0x12892e[_0xdbbf('0x1a')])){_0x5653eb+=_0xdbbf('0x67');}_0x490674[_0xdbbf('0x40')](_0x5653eb);if(_0x573b74[_0xdbbf('0x62')]&&_0x573b74[_0xdbbf('0x62')]['length']){_0x490674[_0xdbbf('0x40')](insertCmContact(_0x573b74[_0xdbbf('0x1b')],_0x11295f,_0x573b74[_0xdbbf('0x62')],_0x12892e));_0x490674[_0xdbbf('0x40')](_0xdbbf('0x68')+_0x11295f+';');}_0x490674[_0xdbbf('0x40')](insertCmHopper(_0x573b74[_0xdbbf('0x1b')],_0xdbbf('0x69'),_0xdbbf('0x6a'),_0xdbbf('0x6b'),_0x2a399c));_0x490674[_0xdbbf('0x40')](insertCmHopperBlack(_0x573b74[_0xdbbf('0x1b')],'voice_queues_has_cm_blacklists',_0xdbbf('0x6a'),'VoiceQueueId'));_0x490674[_0xdbbf('0x40')](insertCmHopper(_0x573b74[_0xdbbf('0x1b')],_0xdbbf('0x6c'),_0xdbbf('0x6d'),_0xdbbf('0x6e'),_0x2a399c));_0x490674[_0xdbbf('0x40')](insertCmHopperBlack(_0x573b74['ListId'],_0xdbbf('0x6f'),_0xdbbf('0x6d'),_0xdbbf('0x6e')));BPromise[_0xdbbf('0x70')](_0x490674,handleQuery)[_0xdbbf('0x71')](function(_0x524252){stats[_0xdbbf('0x72')]=!![];stats[_0xdbbf('0x73')]=_0x573b74[_0xdbbf('0x62')]&&_0x573b74[_0xdbbf('0x62')][_0xdbbf('0x50')]?results[0x2]['affectedRows']:results[0x0][_0xdbbf('0x73')];socket[_0xdbbf('0x74')]('contact:import:'+_0x70eab3,stats);fs[_0xdbbf('0x75')](_0x42b47e);fs[_0xdbbf('0x75')](_0x1b0198);process[_0xdbbf('0x3e')](0x0);})['catch'](function(_0x142552){logger[_0xdbbf('0x44')](_0x142552['message']);process[_0xdbbf('0x3e')](0x1);});}catch(_0x5cbe32){logger[_0xdbbf('0x44')](_0xdbbf('0x76'),_0x5cbe32[_0xdbbf('0x3d')]);process['exit'](0x1);}},'error':function(_0x17cb3f,_0x4cf097,_0x3b18fb,_0x4db598){throw new Error(_0x4db598);}});}function validate(){if(process['argv']&&process[_0xdbbf('0x77')][_0xdbbf('0x50')]<0x4){throw new Error(_0xdbbf('0x78'));}var _0x3ca791=path[_0xdbbf('0x15')](config[_0xdbbf('0x4b')],_0xdbbf('0x4c'),process[_0xdbbf('0x77')][0x2]);if(!fs[_0xdbbf('0x79')](_0x3ca791)){throw new Error(_0xdbbf('0x7a'));}var _0x445a85={};try{_0x445a85=JSON[_0xdbbf('0x4f')](process['argv'][0x3]);if(!_0x445a85[_0xdbbf('0x49')]){throw new Error(_0xdbbf('0x7b'));}if(!_0x445a85[_0xdbbf('0x49')]['phone']){throw new Error(_0xdbbf('0x7c'));}if(!_0x445a85[_0xdbbf('0x49')][_0xdbbf('0x5d')]){throw new Error(_0xdbbf('0x7d'));}var _0x4d20e4=0x0;if(_0x445a85[_0xdbbf('0x49')][_0xdbbf('0x1e')]){_0x4d20e4=0x1;}if(!_0x445a85['ListId']){throw new Error(_0xdbbf('0x7e'));}}catch(_0x44f943){throw new Error(_0xdbbf('0x7f'));}var _0x8046d7=[];try{_0x8046d7=_[_0xdbbf('0x80')](JSON[_0xdbbf('0x4f')](process[_0xdbbf('0x77')][0x4]),'id');}catch(_0xd83574){logger['info'](_0xdbbf('0x81'));}main(_0x3ca791,_0x445a85,_0x8046d7,_0x4d20e4);}validate();process['on'](_0xdbbf('0x82'),handleStop);process['on'](_0xdbbf('0x83'),handleUncaughtException);