Built motion from commit d9028345.|2.5.43
[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 _0xfadb=['campaigns','CampaignId','campaigns_has_cm_blacklists','each','then','finish','affectedRows','unlink','catch','complete','argv','arguments\x20<filename>\x20<parameters>\x20<agents>','file\x20doesn\x27t\x20exists','parse','binding\x20doesn\x27t\x20exists','phone\x20is\x20mandatory','binding','firstName\x20is\x20mandatory','ListId\x20is\x20mandatory','map','SIGINT','uncaughtException','path','lodash','papaparse','util','bluebird','moment','../../../mysqldb','../../../config/logger','import','redis','defaults','localhost','socket.io-emitter','format','YYYY-MM-DD\x20HH:mm:ss','test','select','field','phone','from','join','.id\x20=\x20','where','.dialCheckDuplicateType\x20IN\x20(','cm_contacts.phone','cm_contacts.scheduledat','cm_contacts.ListId','cm_contacts.updatedAt','cm_contacts.priority','cm_contacts.UserId','insert','cm_hopper','fromQuery','scheduledat','ContactId','updatedAt','priority','UserId','recallme','fields','cm_contacts','cm_contacts.ListId\x20=\x20','.CmListId','.CmListId\x20=\x20?','cm_contacts.phone\x20NOT\x20IN\x20(?)','\x22onlyIfOpen\x22','toString','cm_contacts.createdAt','into','cm_hopper_black','ListId','createdAt','cm_contacts.createdAt\x20=\x20?','ListId\x20=\x20?','keys','CompanyId',')\x20NOT\x20IN\x20?','deletedAt\x20IS\x20NULL','id\x20IN\x20?','MIN(id)','min_id','group','info','Received\x20SIGINT','error','exit','errors','push','code','message','query','spread','main','stringify','send','start','root','server/files/tmp','header','writeFileSync','createReadStream','data','rows','trim','replace','scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)','isNumber','email','email\x20wrong\x20format','dateOfBirth','isValid','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','\x20not\x20specified','length','SystemRow','appendFileSync','emit','contact:import:','Abort\x20parser','abort','SystemError','step','duplicates','cm_contacts_','CREATE\x20TABLE\x20','\x20LIKE\x20cm_contacts;','isNil',',\x20scheduledAt\x20=\x20NOW()','DROP\x20TABLE\x20','VoiceQueueId','voice_queues_has_cm_blacklists','voice_queues','campaigns_has_cm_lists'];(function(_0x332c10,_0x2a101c){var _0x2ce0b7=function(_0x1ad22a){while(--_0x1ad22a){_0x332c10['push'](_0x332c10['shift']());}};_0x2ce0b7(++_0x2a101c);}(_0xfadb,0x124));var _0xbfad=function(_0x20e7e3,_0x1529be){_0x20e7e3=_0x20e7e3-0x0;var _0x14b9de=_0xfadb[_0x20e7e3];return _0x14b9de;};'use strict';var fs=require('fs');var path=require(_0xbfad('0x0'));var _=require(_0xbfad('0x1'));var Papa=require(_0xbfad('0x2'));var util=require(_0xbfad('0x3'));var BPromise=require(_0xbfad('0x4'));var moment=require(_0xbfad('0x5'));var squel=require('squel');var Redis=require('ioredis');var db=require(_0xbfad('0x6'))['db'];var config=require('../../../config/environment');var logger=require(_0xbfad('0x7'))(_0xbfad('0x8'));config[_0xbfad('0x9')]=_[_0xbfad('0xa')](config[_0xbfad('0x9')],{'host':_0xbfad('0xb'),'port':0x18eb});var socket=require(_0xbfad('0xc'))(new Redis(config[_0xbfad('0x9')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var stopped=![];var NOW=moment()[_0xbfad('0xd')](_0xbfad('0xe'));function isEmail(_0x4076dd){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))*$/[_0xbfad('0xf')](_0x4076dd);}function selectNotIn(_0x130a61,_0x3c2a17,_0xa9bb7d,_0x25256a,_0x320017){return squel[_0xbfad('0x10')]()[_0xbfad('0x11')](_0xbfad('0x12'))[_0xbfad('0x13')](_0x25256a)[_0xbfad('0x14')](_0x3c2a17,null,_0x3c2a17+_0xbfad('0x15')+_0x25256a+'.'+_0xa9bb7d)['where'](_0x25256a+'.'+_0xa9bb7d+'\x20=\x20'+_0x130a61+'.'+_0xa9bb7d)[_0xbfad('0x16')](_0x3c2a17+_0xbfad('0x17')+_0x320017[_0xbfad('0x14')]()+')');}function insertCmHopper(_0x1a6a52,_0x35133f,_0x5e2823,_0x4d4076,_0x2c7d02){var _0x1c6b89=[_0xbfad('0x18'),_0xbfad('0x19'),'cm_contacts.id',_0xbfad('0x1a'),_0x35133f+'.'+_0x4d4076,'cm_contacts.createdAt',_0xbfad('0x1b'),_0xbfad('0x1c'),_0xbfad('0x1d'),'\x22'+_0x2c7d02+'\x22'];return squel[_0xbfad('0x1e')]()['into'](_0xbfad('0x1f'))[_0xbfad('0x20')]([_0xbfad('0x12'),_0xbfad('0x21'),_0xbfad('0x22'),'ListId',_0x4d4076,'createdAt',_0xbfad('0x23'),_0xbfad('0x24'),_0xbfad('0x25'),_0xbfad('0x26')],squel['select']()[_0xbfad('0x27')](_0x1c6b89)[_0xbfad('0x13')](_0x35133f)[_0xbfad('0x14')](_0xbfad('0x28'),null,_0xbfad('0x29')+_0x35133f+_0xbfad('0x2a'))[_0xbfad('0x14')](_0x5e2823,null,_0x5e2823+_0xbfad('0x15')+_0x35133f+'.'+_0x4d4076)[_0xbfad('0x16')](_0x35133f+_0xbfad('0x2b'),_0x1a6a52)['where']('cm_contacts.createdAt\x20=\x20?',NOW)[_0xbfad('0x16')](_0xbfad('0x2c'),selectNotIn(_0x35133f,_0x5e2823,_0x4d4076,_0xbfad('0x1f'),['\x22always\x22',_0xbfad('0x2d')]))[_0xbfad('0x16')](_0xbfad('0x2c'),selectNotIn(_0x35133f,_0x5e2823,_0x4d4076,_0xbfad('0x1f'),['\x22always\x22'])))[_0xbfad('0x2e')]();}function insertCmHopperBlack(_0x45c136,_0x19b5ea,_0x3b8a37,_0x206e47){var _0x2dd342=[_0xbfad('0x18'),'cm_contacts.id',_0xbfad('0x1a'),_0x19b5ea+'.'+_0x206e47,_0xbfad('0x2f'),_0xbfad('0x1b')];return squel['insert']()[_0xbfad('0x30')](_0xbfad('0x31'))[_0xbfad('0x20')]([_0xbfad('0x12'),_0xbfad('0x22'),_0xbfad('0x32'),_0x206e47,_0xbfad('0x33'),_0xbfad('0x23')],squel[_0xbfad('0x10')]()[_0xbfad('0x27')](_0x2dd342)[_0xbfad('0x13')](_0x19b5ea)[_0xbfad('0x14')](_0xbfad('0x28'),null,_0xbfad('0x29')+_0x19b5ea+'.CmListId')[_0xbfad('0x14')](_0x3b8a37,null,_0x3b8a37+'.id\x20=\x20'+_0x19b5ea+'.'+_0x206e47)[_0xbfad('0x16')](_0x19b5ea+'.CmListId\x20=\x20?',_0x45c136)['where'](_0xbfad('0x34'),NOW)[_0xbfad('0x16')](_0xbfad('0x2c'),squel[_0xbfad('0x10')]()[_0xbfad('0x11')](_0xbfad('0x12'))['from']('cm_hopper_black')[_0xbfad('0x16')](_0xbfad('0x35'),_0x45c136)[_0xbfad('0x16')](_0x206e47+'\x20=\x20?',_0x19b5ea+'.'+_0x206e47)))[_0xbfad('0x2e')]();}function insertCmContact(_0x18ea44,_0x2362d7,_0x5624ec,_0xf548b2){var _0x193a94=_['concat'](_[_0xbfad('0x36')](_0xf548b2),[_0xbfad('0x32'),_0xbfad('0x37'),_0xbfad('0x33'),_0xbfad('0x23')]);return squel[_0xbfad('0x1e')]()['into'](_0xbfad('0x28'))[_0xbfad('0x20')](_0x193a94,squel[_0xbfad('0x10')]()[_0xbfad('0x27')](_0x193a94)[_0xbfad('0x13')](_0x2362d7)['where']('('+_0x5624ec[_0xbfad('0x14')](',')+_0xbfad('0x38'),squel[_0xbfad('0x10')]()[_0xbfad('0x27')](_0x5624ec)[_0xbfad('0x13')]('cm_contacts')[_0xbfad('0x16')](_0xbfad('0x35'),_0x18ea44)[_0xbfad('0x16')](_0xbfad('0x39')))[_0xbfad('0x16')](_0xbfad('0x3a'),squel[_0xbfad('0x10')]()[_0xbfad('0x11')](_0xbfad('0x3b'),_0xbfad('0x3c'))[_0xbfad('0x13')](_0x2362d7)[_0xbfad('0x3d')](_0x5624ec[_0xbfad('0x14')](','))))[_0xbfad('0x2e')]();}function handleStop(){logger[_0xbfad('0x3e')](_0xbfad('0x3f'));stopped=!![];}function handleUncaughtException(_0x5c50c1){logger[_0xbfad('0x40')](_0x5c50c1['message']);process[_0xbfad('0x41')](0x1);}function handleError(_0x4e0ec8,_0x5df51f){stats[_0xbfad('0x42')][_0xbfad('0x43')]({'name':_0x5df51f[_0xbfad('0x44')],'message':_0x5df51f[_0xbfad('0x45')],'row':_0x4e0ec8});}function handleQuery(_0xea0d4e){logger[_0xbfad('0x3e')](_0xea0d4e);return db['sequelize'][_0xbfad('0x46')](_0xea0d4e)[_0xbfad('0x47')](function(_0x41248e){results[_0xbfad('0x43')](_0x41248e);})['catch'](function(_0x37363c){logger['error'](_0x37363c[_0xbfad('0x45')]);});}function main(_0x4d92f2,_0x537e9f,_0x4f4486,_0x56d6f2){logger[_0xbfad('0x3e')](_0xbfad('0x48'),_0x4d92f2,JSON[_0xbfad('0x49')](_0x537e9f));process[_0xbfad('0x4a')](_0xbfad('0x4b'));var _0x4fe05f=_0x537e9f['binding'];var _0x412c73=_0x537e9f['socket_timestamp'];var _0x3bb36c=path[_0xbfad('0x14')](config[_0xbfad('0x4c')],_0xbfad('0x4d'),_0x412c73+'.csv');var _0x45789c=_[_0xbfad('0x36')](_0x4fe05f)[_0xbfad('0x14')]();logger[_0xbfad('0x3e')](_0xbfad('0x4e'),_0x45789c);fs[_0xbfad('0x4f')](_0x3bb36c,_0x45789c+'\x0a');Papa['parse'](fs[_0xbfad('0x50')](_0x4d92f2),{'header':!![],'skipEmptyLines':!![],'step':function(_0x45a47c,_0x1c607b){try{for(var _0x462c5a=0x0,_0x2415d4=[];_0x462c5a<_0x45a47c[_0xbfad('0x51')]['length'];_0x462c5a++){stats[_0xbfad('0x52')]+=0x1;if(_0x45a47c['errors'][_0x462c5a]){handleError(_0x45a47c['data'][_0x462c5a],_0x45a47c[_0xbfad('0x42')][_0x462c5a]);}else{var _0x137c11=[];for(var _0x1fd1bd in _0x4fe05f){if(_0x4fe05f['hasOwnProperty'](_0x1fd1bd)){if(_0x45a47c[_0xbfad('0x51')][_0x462c5a][_0x4fe05f[_0x1fd1bd]]){var _0x3fe74d=_[_0xbfad('0x53')](_0x45a47c[_0xbfad('0x51')][_0x462c5a][_0x4fe05f[_0x1fd1bd]][_0xbfad('0x54')](/"/g,'\x27'));switch(_0x1fd1bd){case _0xbfad('0x21'):var _0x5292c0=moment(_0x3fe74d,_0xbfad('0xe'),!![]);if(_0x5292c0['isValid']()){_0x2415d4[_0xbfad('0x43')]('\x22'+_0x3fe74d+'\x22');}else{_0x137c11[_0xbfad('0x43')](_0xbfad('0x55'));}break;case _0xbfad('0x24'):var _0x53484f=parseInt(_0x3fe74d);_0x3fe74d=_['isNaN'](_0x53484f)||_0x53484f<0x0||_0x53484f>0x4?_0x56d6f2?0x3:0x2:_0x53484f;_0x2415d4[_0xbfad('0x43')]('\x22'+_0x3fe74d+'\x22');break;case _0xbfad('0x25'):var _0x36bc3d=parseInt(_0x3fe74d);if(_[_0xbfad('0x56')](_0x36bc3d)&&_['includes'](_0x4f4486,_0x36bc3d)){_0x2415d4['push']('\x22'+_0x36bc3d+'\x22');}else{_0x137c11[_0xbfad('0x43')]('agent\x20not\x20exists');}break;case _0xbfad('0x57'):if(isEmail(_0x3fe74d)){_0x2415d4['push']('\x22'+_0x3fe74d+'\x22');}else{_0x137c11[_0xbfad('0x43')](_0xbfad('0x58'));}break;case _0xbfad('0x59'):var _0x3716e=moment(_0x3fe74d,'YYYY-MM-DD',!![]);if(_0x3716e[_0xbfad('0x5a')]()){_0x2415d4['push']('\x22'+_0x3fe74d+'\x22');}else{_0x137c11[_0xbfad('0x43')](_0xbfad('0x5b'));}break;default:_0x2415d4['push']('\x22'+_0x3fe74d+'\x22');}}else{switch(_0x1fd1bd){case'firstName':case'phone':_0x137c11[_0xbfad('0x43')](_0x1fd1bd+_0xbfad('0x5c'));break;default:_0x2415d4[_0xbfad('0x43')]('\x22\x22');}}}}if(_0x137c11[_0xbfad('0x5d')]){handleError(_0x45a47c[_0xbfad('0x51')][_0x462c5a],{'code':_0xbfad('0x5e'),'message':_0x137c11['join']()});}else{fs[_0xbfad('0x5f')](_0x3bb36c,_0x2415d4[_0xbfad('0x14')]()+'\x0a');}_0x2415d4=[];}if(!(stats[_0xbfad('0x52')]%0xc8)){socket[_0xbfad('0x60')](_0xbfad('0x61')+_0x412c73,stats);stats['errors']=[];}}if(stopped){logger['info'](_0xbfad('0x62'));_0x1c607b[_0xbfad('0x63')]();}}catch(_0x2b3d5){handleError(_0x45a47c[_0xbfad('0x51')][_0x462c5a],{'code':_0xbfad('0x64'),'message':_0x2b3d5[_0xbfad('0x45')]});logger[_0xbfad('0x40')](_0xbfad('0x65'),_0x2b3d5[_0xbfad('0x45')]);}},'complete':function(){try{var _0x55fdbf=[];var _0x4c0809=_0xbfad('0x28');if(_0x537e9f[_0xbfad('0x66')]&&_0x537e9f[_0xbfad('0x66')][_0xbfad('0x5d')]){_0x4c0809=_0xbfad('0x67')+_0x412c73;_0x55fdbf['push'](_0xbfad('0x68')+_0x4c0809+_0xbfad('0x69'));}var _0x123402=util[_0xbfad('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',_0x3bb36c,_0x4c0809,_0x45789c,_0x537e9f[_0xbfad('0x32')],_0x537e9f['CompanyId']||null,NOW,NOW);if(_[_0xbfad('0x6a')](_0x4fe05f['scheduledat'])){_0x123402+=_0xbfad('0x6b');}_0x55fdbf[_0xbfad('0x43')](_0x123402);if(_0x537e9f[_0xbfad('0x66')]&&_0x537e9f[_0xbfad('0x66')]['length']){_0x55fdbf[_0xbfad('0x43')](insertCmContact(_0x537e9f[_0xbfad('0x32')],_0x4c0809,_0x537e9f['duplicates'],_0x4fe05f));_0x55fdbf[_0xbfad('0x43')](_0xbfad('0x6c')+_0x4c0809+';');}_0x55fdbf[_0xbfad('0x43')](insertCmHopper(_0x537e9f['ListId'],'voice_queues_has_cm_lists','voice_queues',_0xbfad('0x6d'),_0x56d6f2));_0x55fdbf[_0xbfad('0x43')](insertCmHopperBlack(_0x537e9f['ListId'],_0xbfad('0x6e'),_0xbfad('0x6f'),_0xbfad('0x6d')));_0x55fdbf[_0xbfad('0x43')](insertCmHopper(_0x537e9f[_0xbfad('0x32')],_0xbfad('0x70'),_0xbfad('0x71'),_0xbfad('0x72'),_0x56d6f2));_0x55fdbf[_0xbfad('0x43')](insertCmHopperBlack(_0x537e9f[_0xbfad('0x32')],_0xbfad('0x73'),_0xbfad('0x71'),_0xbfad('0x72')));BPromise[_0xbfad('0x74')](_0x55fdbf,handleQuery)[_0xbfad('0x75')](function(_0x351f10){stats[_0xbfad('0x76')]=!![];stats[_0xbfad('0x77')]=_0x537e9f['duplicates']&&_0x537e9f['duplicates']['length']?results[0x2][_0xbfad('0x77')]:results[0x0][_0xbfad('0x77')];socket[_0xbfad('0x60')](_0xbfad('0x61')+_0x412c73,stats);fs['unlink'](_0x4d92f2);fs[_0xbfad('0x78')](_0x3bb36c);process['exit'](0x0);})[_0xbfad('0x79')](function(_0x5f1446){logger[_0xbfad('0x40')](_0x5f1446['message']);process[_0xbfad('0x41')](0x1);});}catch(_0x4204cd){logger['error'](_0xbfad('0x7a'),_0x4204cd[_0xbfad('0x45')]);process['exit'](0x1);}},'error':function(_0x2bd162,_0x4a6119,_0xece89a,_0x34c558){throw new Error(_0x34c558);}});}function validate(){if(process[_0xbfad('0x7b')]&&process[_0xbfad('0x7b')][_0xbfad('0x5d')]<0x4){throw new Error(_0xbfad('0x7c'));}var _0x18b0c2=path['join'](config['root'],'server/files/tmp',process[_0xbfad('0x7b')][0x2]);if(!fs['existsSync'](_0x18b0c2)){throw new Error(_0xbfad('0x7d'));}var _0x3e9f7d={};try{_0x3e9f7d=JSON[_0xbfad('0x7e')](process[_0xbfad('0x7b')][0x3]);if(!_0x3e9f7d['binding']){throw new Error(_0xbfad('0x7f'));}if(!_0x3e9f7d['binding'][_0xbfad('0x12')]){throw new Error(_0xbfad('0x80'));}if(!_0x3e9f7d[_0xbfad('0x81')]['firstName']){throw new Error(_0xbfad('0x82'));}var _0x599f4c=0x0;if(_0x3e9f7d[_0xbfad('0x81')]['UserId']){_0x599f4c=0x1;}if(!_0x3e9f7d[_0xbfad('0x32')]){throw new Error(_0xbfad('0x83'));}}catch(_0x553b95){throw new Error('body\x20id\x20mandatory');}var _0x3f7c48=[];try{_0x3f7c48=_[_0xbfad('0x84')](JSON[_0xbfad('0x7e')](process[_0xbfad('0x7b')][0x4]),'id');}catch(_0x1840f5){logger[_0xbfad('0x3e')]('No\x20agent\x20available');}main(_0x18b0c2,_0x3e9f7d,_0x3f7c48,_0x599f4c);}validate();process['on'](_0xbfad('0x85'),handleStop);process['on'](_0xbfad('0x86'),handleUncaughtException);