7cf6c085c49764ec3c5d92a6ef0716ae38eef3d3
[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 _0xeb57=['writeFileSync','parse','createReadStream','data','errors','find','isNil','hasOwnProperty','trim','isNaN','isNumber','agent\x20not\x20exists','email','dateOfBirth','dateOfBirth\x20wrong\x20format\x20(YYYY-MM-DD)','firstName','\x20not\x20specified','replace','itemTypeId','description','SystemRow','rows','emit','Abort\x20parser','abort','SystemError','duplicates','cm_contacts_','CREATE\x20TABLE\x20','\x20LIKE\x20cm_contacts;','voice_queues_','each','dialTimezone,utcOffset','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','contactId','contactTypeId','orderBy','VoiceQueueId','CampaignId','voice_queues_has_cm_blacklists','campaigns_has_cm_blacklists','finish','affectedRows','unlink','complete','argv','arguments\x20<filename>\x20<parameters>\x20<agents>','server/files/tmp','existsSync','file\x20doesn\x27t\x20exists','binding\x20doesn\x27t\x20exists','phone\x20is\x20mandatory','firstName\x20is\x20mandatory','ListId\x20is\x20mandatory','SIGINT','uncaughtException','path','papaparse','util','bluebird','squel','ioredis','../../../mysqldb','../../../config/environment','../../../config/environment/shared','../../../config/logger','import','redis','defaults','localhost','socket.io-emitter','YYYY-MM-DD\x20HH:mm:ss','format','test','select','from','join','where','.dialCheckDuplicateType\x20IN\x20(','CmContact','then','length','push','all','AdditionalPhones','split','filter','CmContactHasItem','findOne','update','updated','create','PHONE','inserted','cm_contacts.phone','cm_contacts.scheduledat','cm_contacts.id','cm_contacts.ListId','cm_contacts.updatedAt','cm_contacts.UserId','insert','into','fromQuery','phone','scheduledat','ContactId','ListId','createdAt','updatedAt','priority','UserId','fields','cm_contacts','.CmListId','.CmListId\x20=\x20?','cm_contacts.createdAt\x20=\x20?','cm_hopper','\x22always\x22','cm_contacts.phone\x20NOT\x20IN\x20(?)','toString','cm_contacts.createdAt','cm_hopper_black','cm_contacts.ListId\x20=\x20','.id\x20=\x20','field','\x20=\x20','date_add(cm_contacts.scheduledat,interval\x20','cm_contacts.priority','recallme','\x22onlyIfOpen\x22','ListId\x20=\x20?','cm_hopper.id','cm_contact_has_items.item','cm_contact_has_items.OrderBy','cm_contact_has_items.createdAt','cm_contact_has_items.updatedAt','cm_hopper_additional_phones','CmHopperId','cm_contacts.id\x20=\x20cm_hopper.ContactId','cm_contact_has_items','cm_contacts.id\x20=\x20cm_contact_has_items.CmContactId','cm_hopper.ListId\x20=\x20?','concat','keys','CompanyId','scheduledAt','deletedAt\x20IS\x20NULL','id\x20IN\x20?','MIN(id)','info','Received\x20SIGINT','error','message','exit','code','sequelize','spread','campaigns.id','campaigns.dialTimezone','campaigns','campaigns_has_cm_lists','campaigns.id\x20=\x20campaigns_has_cm_lists.CampaignId','campaigns_has_cm_lists.CmListId=','voice_queues.id','voice_queues.dialTimezone','voice_queues','voice_queues_has_cm_lists','voice_queues_has_cm_lists.CmListId=','catch','timezones','utcOffset','set','offset','dialTimezone','query','main','stringify','send','start','binding','clone','socket_timestamp','root','.csv','includes','ContactItemType','findAll','name','_description','header'];(function(_0x10a688,_0x4ae439){var _0x3098e8=function(_0x305f9d){while(--_0x305f9d){_0x10a688['push'](_0x10a688['shift']());}};_0x3098e8(++_0x4ae439);}(_0xeb57,0x1ba));var _0x7eb5=function(_0x437ad4,_0x572b4e){_0x437ad4=_0x437ad4-0x0;var _0x274b8d=_0xeb57[_0x437ad4];return _0x274b8d;};'use strict';var fs=require('fs');var path=require(_0x7eb5('0x0'));var _=require('lodash');var Papa=require(_0x7eb5('0x1'));var util=require(_0x7eb5('0x2'));var BPromise=require(_0x7eb5('0x3'));var moment=require('moment');var squel=require(_0x7eb5('0x4'));var Redis=require(_0x7eb5('0x5'));var db=require(_0x7eb5('0x6'))['db'];var config=require(_0x7eb5('0x7'));var shared=require(_0x7eb5('0x8'));var logger=require(_0x7eb5('0x9'))(_0x7eb5('0xa'));config[_0x7eb5('0xb')]=_[_0x7eb5('0xc')](config[_0x7eb5('0xb')],{'host':_0x7eb5('0xd'),'port':0x18eb});var socket=require(_0x7eb5('0xe'))(new Redis(config[_0x7eb5('0xb')]));var stats={'finish':![],'errors':[],'rows':0x0,'affectedRows':0x0};var results=[];var promisesAdditionalPhones=[];var orderBy=0x0;var stopped=![];var NOW=moment()['format'](_0x7eb5('0xf'));var contactItemTypes=[];var phoneDescriptions=[];var hasAdditionalPhones=![];var tmp_voice_queues;var tmp_campaigns;var tzOffset=0x0;var hasTZ;var NOW=moment()[_0x7eb5('0x10')](_0x7eb5('0xf'));function isEmail(_0x1ea69a){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))*$/[_0x7eb5('0x11')](_0x1ea69a);}function selectNotIn(_0x3c948e,_0x52e2ad,_0x10ec5a,_0x399d0a,_0x3463f6){return squel[_0x7eb5('0x12')]()['field']('phone')[_0x7eb5('0x13')](_0x399d0a)[_0x7eb5('0x14')](_0x52e2ad,null,_0x52e2ad+'.id\x20=\x20'+_0x399d0a+'.'+_0x10ec5a)[_0x7eb5('0x15')](_0x399d0a+'.'+_0x10ec5a+'\x20=\x20'+_0x3c948e+'.'+_0x10ec5a)[_0x7eb5('0x15')](_0x52e2ad+_0x7eb5('0x16')+_0x3463f6[_0x7eb5('0x14')]()+')');}function handleAdditionalPhones(){return new Promise(function(_0xfaaa9f,_0x5062e0){promisesAdditionalPhones=[];return db[_0x7eb5('0x17')]['findAll']({'raw':!![],'attributes':['id','AdditionalPhones'],'where':{'createdAt':NOW,'AdditionalPhones':{'$ne':null}}})[_0x7eb5('0x18')](function(_0x5d3a7a){if(_0x5d3a7a!==null){var _0x59bdbf=[];for(var _0x44c3e1=0x0;_0x44c3e1<_0x5d3a7a[_0x7eb5('0x19')];_0x44c3e1++){var _0x2b1806=_0x5d3a7a[_0x44c3e1];_0x59bdbf[_0x7eb5('0x1a')](insertAdditionalPhones(_0x2b1806));}}Promise[_0x7eb5('0x1b')](_0x59bdbf)[_0x7eb5('0x18')](function(){_0xfaaa9f({'val':0x1});});});});}function insertAdditionalPhones(_0x43e3a6){return new Promise(function(_0x3e8462,_0x55f6a8){var _0xfc50d5=_0x43e3a6[_0x7eb5('0x1c')];var _0x33d2ce=_0xfc50d5[_0x7eb5('0x1d')](';');var _0x3a0e87=[];orderBy=0x0;for(var _0x15c6b6=0x0;_0x15c6b6<_0x33d2ce[_0x7eb5('0x19')];_0x15c6b6++){_0x3a0e87[_0x7eb5('0x1a')](splitAdditionalPhone(_0x43e3a6['id'],_0x33d2ce[_0x15c6b6]));}Promise['all'](_0x3a0e87)[_0x7eb5('0x18')](function(){_0x3e8462({'val':_0x33d2ce[_0x7eb5('0x19')]});});});}function splitAdditionalPhone(_0x10a65a,_0x29b44a){return new Promise(function(_0x5f4169,_0x4d05f2){var _0x5a3f2e=_0x29b44a[_0x7eb5('0x1d')](':');var _0x1b4b5b=_0x5a3f2e[0x0];var _0x1e4bc6=_0x5a3f2e[0x1];if(!_['isNil'](_0x1e4bc6)){var _0x2b52c4=_0x1e4bc6[_0x7eb5('0x1d')]('|');var _0x35c733=[];for(var _0x4c96a5=0x0;_0x4c96a5<_0x2b52c4[_0x7eb5('0x19')];_0x4c96a5++){if(_0x2b52c4[_0x4c96a5]!==''){var _0x1eed24=_[_0x7eb5('0x1e')](promisesAdditionalPhones,{'contactId':_0x10a65a,'phone':_0x2b52c4[_0x4c96a5]});if(_0x1eed24['length']==0x0){orderBy++;promisesAdditionalPhones['push']({'contactId':_0x10a65a,'contactTypeId':_0x1b4b5b,'phone':_0x2b52c4[_0x4c96a5],'orderBy':orderBy});}}}_0x5f4169({'ok':0x1});}else _0x5f4169({'res':0x0});});}function createAdditionalPhone(_0x1fd28f,_0xa364bc,_0x2adcba,_0x2d9578){var _0x15651e='';var _0x5361b8='';var _0x19968=_0x2adcba[_0x7eb5('0x1d')]('§');_0x15651e=_0x19968[0x0];if(_0x19968[_0x7eb5('0x19')]==0x2)_0x5361b8=_0x19968[0x1];return new Promise(function(_0x311544,_0xceaa90){return db[_0x7eb5('0x1f')][_0x7eb5('0x20')]({'where':{'CmContactId':_0x1fd28f,'item':_0x15651e}})[_0x7eb5('0x18')](function(_0x3cf1de){if(_0x3cf1de){return db[_0x7eb5('0x1f')][_0x7eb5('0x21')]({'ItemTypeId':_0xa364bc,'description':_0x5361b8,'OrderBy':_0x2d9578},{'where':{'CmContactId':_0x1fd28f,'item':_0x15651e}})['then'](function(_0x14ad04){_0x311544({'res':_0x7eb5('0x22')});});}else{return db[_0x7eb5('0x1f')][_0x7eb5('0x23')]({'CmContactId':_0x1fd28f,'item':_0x15651e,'description':_0x5361b8,'ItemTypeId':_0xa364bc,'OrderBy':_0x2d9578,'ItemClass':_0x7eb5('0x24')})[_0x7eb5('0x18')](function(_0x23fc79){_0x311544({'res':_0x7eb5('0x25')});});}});});}function insertCmHopper(_0x901abc,_0x3289ac,_0x34cd98,_0x12076f,_0x178d16){var _0x10c72a=[_0x7eb5('0x26'),_0x7eb5('0x27'),_0x7eb5('0x28'),_0x7eb5('0x29'),_0x3289ac+'.'+_0x12076f,'cm_contacts.createdAt',_0x7eb5('0x2a'),'cm_contacts.priority',_0x7eb5('0x2b'),'\x22'+_0x178d16+'\x22'];return squel[_0x7eb5('0x2c')]()[_0x7eb5('0x2d')]('cm_hopper')[_0x7eb5('0x2e')]([_0x7eb5('0x2f'),_0x7eb5('0x30'),_0x7eb5('0x31'),_0x7eb5('0x32'),_0x12076f,_0x7eb5('0x33'),_0x7eb5('0x34'),_0x7eb5('0x35'),_0x7eb5('0x36'),'recallme'],squel['select']()[_0x7eb5('0x37')](_0x10c72a)['from'](_0x3289ac)['join'](_0x7eb5('0x38'),null,'cm_contacts.ListId\x20=\x20'+_0x3289ac+_0x7eb5('0x39'))[_0x7eb5('0x14')](_0x34cd98,null,_0x34cd98+'.id\x20=\x20'+_0x3289ac+'.'+_0x12076f)['where'](_0x3289ac+_0x7eb5('0x3a'),_0x901abc)['where'](_0x7eb5('0x3b'),NOW)[_0x7eb5('0x15')]('cm_contacts.phone\x20NOT\x20IN\x20(?)',selectNotIn(_0x3289ac,_0x34cd98,_0x12076f,_0x7eb5('0x3c'),[_0x7eb5('0x3d'),'\x22onlyIfOpen\x22']))[_0x7eb5('0x15')](_0x7eb5('0x3e'),selectNotIn(_0x3289ac,_0x34cd98,_0x12076f,_0x7eb5('0x3c'),[_0x7eb5('0x3d')])))[_0x7eb5('0x3f')]();}function insertCmHopperBlack(_0x5cc29d,_0x4173f2,_0x24ca75,_0x3d6cc9){var _0x4c9183=[_0x7eb5('0x26'),_0x7eb5('0x28'),_0x7eb5('0x29'),_0x4173f2+'.'+_0x3d6cc9,_0x7eb5('0x40'),_0x7eb5('0x2a')];return squel[_0x7eb5('0x2c')]()[_0x7eb5('0x2d')](_0x7eb5('0x41'))[_0x7eb5('0x2e')](['phone',_0x7eb5('0x31'),'ListId',_0x3d6cc9,'createdAt',_0x7eb5('0x34')],squel['select']()[_0x7eb5('0x37')](_0x4c9183)[_0x7eb5('0x13')](_0x4173f2)[_0x7eb5('0x14')](_0x7eb5('0x38'),null,_0x7eb5('0x42')+_0x4173f2+_0x7eb5('0x39'))[_0x7eb5('0x14')](_0x24ca75,null,_0x24ca75+_0x7eb5('0x43')+_0x4173f2+'.'+_0x3d6cc9)[_0x7eb5('0x15')](_0x4173f2+'.CmListId\x20=\x20?',_0x5cc29d)[_0x7eb5('0x15')]('cm_contacts.createdAt\x20=\x20?',NOW)[_0x7eb5('0x15')](_0x7eb5('0x3e'),squel[_0x7eb5('0x12')]()[_0x7eb5('0x44')](_0x7eb5('0x2f'))[_0x7eb5('0x13')](_0x7eb5('0x41'))['where']('ListId\x20=\x20?',_0x5cc29d)[_0x7eb5('0x15')](_0x3d6cc9+_0x7eb5('0x45')+_0x4173f2+'.'+_0x3d6cc9)))[_0x7eb5('0x3f')]();}function insertTzCmHopper(_0x46fa62,_0x5c9fb0,_0x1212fa,_0x44e6b1,_0x186e3b,_0x502ef2){var _0xeb8075=['cm_contacts.phone',_0x7eb5('0x46')+_0x502ef2+'.offset-cm_contacts.utcOffset\x20minute)',_0x7eb5('0x28'),_0x7eb5('0x29'),_0x5c9fb0+'.'+_0x44e6b1,_0x7eb5('0x40'),_0x7eb5('0x2a'),_0x7eb5('0x47'),'cm_contacts.UserId','\x22'+_0x186e3b+'\x22'];var _0x2068f1=squel[_0x7eb5('0x2c')]()[_0x7eb5('0x2d')](_0x7eb5('0x3c'))['fromQuery']([_0x7eb5('0x2f'),_0x7eb5('0x30'),_0x7eb5('0x31'),'ListId',_0x44e6b1,_0x7eb5('0x33'),'updatedAt',_0x7eb5('0x35'),_0x7eb5('0x36'),_0x7eb5('0x48')],squel['select']()[_0x7eb5('0x37')](_0xeb8075)['from'](_0x5c9fb0)[_0x7eb5('0x14')](_0x7eb5('0x38'),null,_0x7eb5('0x42')+_0x5c9fb0+_0x7eb5('0x39'))['join'](_0x1212fa,null,_0x1212fa+_0x7eb5('0x43')+_0x5c9fb0+'.'+_0x44e6b1)[_0x7eb5('0x14')](_0x502ef2,null,_0x502ef2+'.id\x20=\x20'+_0x5c9fb0+'.'+_0x44e6b1)['where'](_0x5c9fb0+_0x7eb5('0x3a'),_0x46fa62)[_0x7eb5('0x15')]('cm_contacts.createdAt\x20=\x20?',NOW)['where']('cm_contacts.phone\x20NOT\x20IN\x20(?)',selectNotIn(_0x5c9fb0,_0x1212fa,_0x44e6b1,_0x7eb5('0x3c'),[_0x7eb5('0x3d'),_0x7eb5('0x49')]))[_0x7eb5('0x15')]('cm_contacts.phone\x20NOT\x20IN\x20(?)',selectNotIn(_0x5c9fb0,_0x1212fa,_0x44e6b1,'cm_hopper',[_0x7eb5('0x3d')])))[_0x7eb5('0x3f')]();return _0x2068f1;}function insertTzCmHopperBlack(_0xd57c5f,_0x124c9f,_0xac188e,_0x53d407){var _0x5f527f=[_0x7eb5('0x26'),_0x7eb5('0x28'),'cm_contacts.ListId',_0x124c9f+'.'+_0x53d407,_0x7eb5('0x40'),_0x7eb5('0x2a')];return squel['insert']()[_0x7eb5('0x2d')](_0x7eb5('0x41'))[_0x7eb5('0x2e')]([_0x7eb5('0x2f'),_0x7eb5('0x31'),_0x7eb5('0x32'),_0x53d407,_0x7eb5('0x33'),_0x7eb5('0x34')],squel['select']()[_0x7eb5('0x37')](_0x5f527f)[_0x7eb5('0x13')](_0x124c9f)[_0x7eb5('0x14')](_0x7eb5('0x38'),null,_0x7eb5('0x42')+_0x124c9f+_0x7eb5('0x39'))[_0x7eb5('0x14')](_0xac188e,null,_0xac188e+_0x7eb5('0x43')+_0x124c9f+'.'+_0x53d407)[_0x7eb5('0x15')](_0x124c9f+'.CmListId\x20=\x20?',_0xd57c5f)[_0x7eb5('0x15')](_0x7eb5('0x3b'),NOW)['where'](_0x7eb5('0x3e'),squel[_0x7eb5('0x12')]()[_0x7eb5('0x44')]('phone')[_0x7eb5('0x13')](_0x7eb5('0x41'))[_0x7eb5('0x15')](_0x7eb5('0x4a'),_0xd57c5f)[_0x7eb5('0x15')](_0x53d407+_0x7eb5('0x45')+_0x124c9f+'.'+_0x53d407)))[_0x7eb5('0x3f')]();}function insertCmHopperAdditionalPhones(_0x315257){var _0x5202e4=[_0x7eb5('0x4b'),_0x7eb5('0x4c'),_0x7eb5('0x4d'),_0x7eb5('0x4e'),_0x7eb5('0x4f')];return squel[_0x7eb5('0x2c')]()[_0x7eb5('0x2d')](_0x7eb5('0x50'))[_0x7eb5('0x2e')]([_0x7eb5('0x51'),_0x7eb5('0x2f'),'OrderBy',_0x7eb5('0x33'),_0x7eb5('0x34')],squel[_0x7eb5('0x12')]()[_0x7eb5('0x37')](_0x5202e4)['from'](_0x7eb5('0x3c'))[_0x7eb5('0x14')](_0x7eb5('0x38'),null,_0x7eb5('0x52'))[_0x7eb5('0x14')](_0x7eb5('0x53'),null,_0x7eb5('0x54'))[_0x7eb5('0x15')](_0x7eb5('0x55'),_0x315257)[_0x7eb5('0x15')](_0x7eb5('0x3b'),NOW))[_0x7eb5('0x3f')]();}function insertCmContact(_0x10718f,_0x4df4e3,_0x4ecdc1,_0xe4367c){var _0x29bd30=_[_0x7eb5('0x56')](_[_0x7eb5('0x57')](_0xe4367c),[_0x7eb5('0x32'),_0x7eb5('0x58'),_0x7eb5('0x33'),_0x7eb5('0x34'),_0x7eb5('0x59')]);return squel[_0x7eb5('0x2c')]()[_0x7eb5('0x2d')](_0x7eb5('0x38'))[_0x7eb5('0x2e')](_0x29bd30,squel[_0x7eb5('0x12')]()[_0x7eb5('0x37')](_0x29bd30)[_0x7eb5('0x13')](_0x4df4e3)['where']('('+_0x4ecdc1[_0x7eb5('0x14')](',')+')\x20NOT\x20IN\x20?',squel['select']()[_0x7eb5('0x37')](_0x4ecdc1)[_0x7eb5('0x13')](_0x7eb5('0x38'))['where']('ListId\x20=\x20?',_0x10718f)['where'](_0x7eb5('0x5a')))['where'](_0x7eb5('0x5b'),squel[_0x7eb5('0x12')]()[_0x7eb5('0x44')](_0x7eb5('0x5c'),'min_id')[_0x7eb5('0x13')](_0x4df4e3)['group'](_0x4ecdc1['join'](','))))[_0x7eb5('0x3f')]();}function handleStop(){logger[_0x7eb5('0x5d')](_0x7eb5('0x5e'));stopped=!![];}function handleUncaughtException(_0x417236){logger[_0x7eb5('0x5f')](_0x417236[_0x7eb5('0x60')]);process[_0x7eb5('0x61')](0x1);}function handleError(_0x5aa861,_0x447bdd){stats['errors'][_0x7eb5('0x1a')]({'name':_0x447bdd[_0x7eb5('0x62')],'message':_0x447bdd['message'],'row':_0x5aa861});}function handleQuery(_0x4d2bfd){logger['info'](_0x4d2bfd);return db[_0x7eb5('0x63')]['query'](_0x4d2bfd)[_0x7eb5('0x64')](function(_0x250ec8){results[_0x7eb5('0x1a')](_0x250ec8);})['catch'](function(_0x3cc9cb){logger[_0x7eb5('0x5f')](_0x3cc9cb['message']);});}function updateTzCampaigns(_0x4a9215){return new Promise(function(_0x4ff2e6,_0x52692b){var _0x220647=squel['select']()[_0x7eb5('0x37')]([_0x7eb5('0x65'),_0x7eb5('0x66')])['from'](_0x7eb5('0x67'))['join'](_0x7eb5('0x68'),null,_0x7eb5('0x69'))[_0x7eb5('0x15')](_0x7eb5('0x6a')+_0x4a9215);db['sequelize']['query'](_0x220647[_0x7eb5('0x3f')]())[_0x7eb5('0x64')](function(_0x15d744){var _0x1a34ae=[];for(var _0x5e7958=0x0;_0x5e7958<_0x15d744[_0x7eb5('0x19')];_0x5e7958++){_0x1a34ae[_0x7eb5('0x1a')](getTimezoneData(_0x15d744[_0x5e7958],tmp_campaigns));}BPromise[_0x7eb5('0x1b')](_0x1a34ae)['then'](function(){_0x4ff2e6({'val':0x1});});})['catch'](function(_0x1bf0b5){logger[_0x7eb5('0x5f')](_0x1bf0b5[_0x7eb5('0x60')]);_0x4ff2e6({'val':0x0});});});}function updateTzVoiceQueues(_0x53d0e0){return new Promise(function(_0x23bb1a,_0x24a097){var _0x4e13ec=squel[_0x7eb5('0x12')]()[_0x7eb5('0x37')]([_0x7eb5('0x6b'),_0x7eb5('0x6c')])['from'](_0x7eb5('0x6d'))['join'](_0x7eb5('0x6e'),null,'voice_queues.id\x20=\x20voice_queues_has_cm_lists.VoiceQueueId')[_0x7eb5('0x15')](_0x7eb5('0x6f')+_0x53d0e0);db[_0x7eb5('0x63')]['query'](_0x4e13ec['toString']())[_0x7eb5('0x64')](function(_0x23e9c1){var _0x4be807=[];for(var _0x3359a0=0x0;_0x3359a0<_0x23e9c1[_0x7eb5('0x19')];_0x3359a0++){_0x4be807['push'](getTimezoneData(_0x23e9c1[_0x3359a0],tmp_voice_queues));}BPromise[_0x7eb5('0x1b')](_0x4be807)[_0x7eb5('0x18')](function(){_0x23bb1a({'val':0x1});});})[_0x7eb5('0x70')](function(_0x5674af){logger[_0x7eb5('0x5f')](_0x5674af[_0x7eb5('0x60')]);_0x23bb1a({'val':0x0});});});}function getTimezoneData(_0x5ec46e,_0x33827c){return new Promise(function(_0x2bc681,_0xfde646){var _0x450de3=shared[_0x7eb5('0x71')];var _0x1126f5=_0x450de3[_0x5ec46e['dialTimezone']];if(_['isNil'](_0x1126f5)){_0x2bc681({'ret':0x1});}else{var _0x1ed2ca=_0x1126f5[_0x7eb5('0x72')];var _0x301f19=squel[_0x7eb5('0x2c')]()[_0x7eb5('0x2d')](_0x33827c)[_0x7eb5('0x73')]('id',_0x5ec46e['id'])[_0x7eb5('0x73')](_0x7eb5('0x74'),_0x1ed2ca)[_0x7eb5('0x73')](_0x7eb5('0x75'),_0x5ec46e[_0x7eb5('0x75')])['toString']();return db[_0x7eb5('0x63')][_0x7eb5('0x76')](_0x301f19)['spread'](function(_0xa097c8){_0x2bc681({'ret':0x1});})[_0x7eb5('0x70')](function(_0x539717){logger['error'](_0x539717[_0x7eb5('0x60')]);_0xfde646({'ret':0x1});});}});}function handleTZQuery(_0x28b358){logger[_0x7eb5('0x5d')](_0x28b358);return db['sequelize']['query'](_0x28b358)[_0x7eb5('0x64')](function(_0x2fd2a0){})[_0x7eb5('0x70')](function(_0x37d78f){logger[_0x7eb5('0x5f')](_0x37d78f[_0x7eb5('0x60')]);});}function main(_0x304387,_0x3ff23c,_0x268832,_0x3d3c03){logger[_0x7eb5('0x5d')](_0x7eb5('0x77'),_0x304387,JSON[_0x7eb5('0x78')](_0x3ff23c));process[_0x7eb5('0x79')](_0x7eb5('0x7a'));var _0x200955=_0x3ff23c[_0x7eb5('0x7b')];var _0x261558=_[_0x7eb5('0x7c')](_0x200955);var _0x2674e8=_0x3ff23c[_0x7eb5('0x7d')];var _0x57ed0c=path[_0x7eb5('0x14')](config[_0x7eb5('0x7e')],'server/files/tmp',_0x2674e8+_0x7eb5('0x7f'));hasTZ=_['keys'](_0x200955)[_0x7eb5('0x80')]('dialTimezone');var _0x2aa85e=_[_0x7eb5('0x57')](_0x200955)[_0x7eb5('0x14')]();logger[_0x7eb5('0x5d')]('header',_0x2aa85e);db[_0x7eb5('0x81')][_0x7eb5('0x82')]({'raw':!![],'attributes':['id',_0x7eb5('0x83')]})[_0x7eb5('0x18')](function(_0x11c770){for(var _0x195f1e=0x0;_0x195f1e<_0x11c770[_0x7eb5('0x19')];_0x195f1e++){if(_0x11c770[_0x195f1e][_0x7eb5('0x83')]in _0x200955){contactItemTypes[_0x7eb5('0x1a')](_0x11c770[_0x195f1e]);delete _0x261558[_0x11c770[_0x195f1e]['name']];hasAdditionalPhones=!![];}for(var _0x12bb3a in _0x200955){if(_0x11c770[_0x195f1e]['name']+_0x7eb5('0x84')===_0x12bb3a){phoneDescriptions[_0x7eb5('0x1a')]({'itemTypeId':_0x11c770[_0x195f1e]['id'],'description':_0x12bb3a});delete _0x261558[_0x12bb3a];}}}if(hasAdditionalPhones)_0x261558['AdditionalPhones']='';var _0x57b4fc=_[_0x7eb5('0x57')](_0x261558)[_0x7eb5('0x14')]();logger[_0x7eb5('0x5d')](_0x7eb5('0x85'),_0x57b4fc);fs[_0x7eb5('0x86')](_0x57ed0c,_0x57b4fc+'\x0a');Papa[_0x7eb5('0x87')](fs[_0x7eb5('0x88')](_0x304387),{'header':!![],'skipEmptyLines':!![],'step':function(_0x1e3d4f,_0x3ceeab){try{for(var _0x424a6b=0x0,_0x5bee56=[];_0x424a6b<_0x1e3d4f[_0x7eb5('0x89')][_0x7eb5('0x19')];_0x424a6b++){stats['rows']+=0x1;if(_0x1e3d4f[_0x7eb5('0x8a')][_0x424a6b]){handleError(_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b],_0x1e3d4f['errors'][_0x424a6b]);}else{var _0x44657e=[];var _0xcc4c8f=[];var _0x1491e5={};for(var _0x4df4b6 in _0x200955){let _0x39b3f9=contactItemTypes[_0x7eb5('0x8b')](_0x2f5ace=>_0x2f5ace[_0x7eb5('0x83')]===_0x4df4b6);let _0x3f75e1=contactItemTypes[_0x7eb5('0x8b')](_0x4dc200=>_0x4dc200[_0x7eb5('0x83')]+_0x7eb5('0x84')===_0x4df4b6);if(_[_0x7eb5('0x8c')](_0x39b3f9)&&_[_0x7eb5('0x8c')](_0x3f75e1)){if(_0x200955[_0x7eb5('0x8d')](_0x4df4b6)){if(_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b][_0x200955[_0x4df4b6]]){var _0x5a6753=_[_0x7eb5('0x8e')](_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b][_0x200955[_0x4df4b6]]['replace'](/"/g,'\x27'));switch(_0x4df4b6){case'scheduledat':var _0x2e7b56=moment(_0x5a6753,_0x7eb5('0xf'),!![]);if(_0x2e7b56['isValid']()){_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0x5a6753+'\x22');}else{_0x44657e[_0x7eb5('0x1a')]('scheduledat\x20wrong\x20format\x20(YYYY-MM-DD\x20HH:mm:ss)');}break;case _0x7eb5('0x35'):var _0x24a29f=parseInt(_0x5a6753);_0x5a6753=_[_0x7eb5('0x8f')](_0x24a29f)||_0x24a29f<0x0||_0x24a29f>0x4?_0x3d3c03?0x3:0x2:_0x24a29f;_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0x5a6753+'\x22');break;case'UserId':var _0xbe1dbf=parseInt(_0x5a6753);if(_[_0x7eb5('0x90')](_0xbe1dbf)&&_['includes'](_0x268832,_0xbe1dbf)){_0x5bee56['push']('\x22'+_0xbe1dbf+'\x22');}else{_0x44657e[_0x7eb5('0x1a')](_0x7eb5('0x91'));}break;case _0x7eb5('0x92'):if(isEmail(_0x5a6753)){_0x5bee56['push']('\x22'+_0x5a6753+'\x22');}else{_0x44657e['push']('email\x20wrong\x20format');}break;case _0x7eb5('0x93'):var _0x3b0880=moment(_0x5a6753,'YYYY-MM-DD',!![]);if(_0x3b0880['isValid']()){_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0x5a6753+'\x22');}else{_0x44657e[_0x7eb5('0x1a')](_0x7eb5('0x94'));}break;case _0x7eb5('0x75'):var _0x5a3966=shared[_0x7eb5('0x71')];var _0x2e5a05=_0x5a3966[_0x5a6753];if(!_['isNil'](_0x2e5a05)){var _0x2ef576=_0x2e5a05[_0x7eb5('0x72')];_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0x5a6753+'\x22');_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0x2ef576+'\x22');}break;default:_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0x5a6753+'\x22');}}else{switch(_0x4df4b6){case _0x7eb5('0x95'):case _0x7eb5('0x2f'):_0x44657e[_0x7eb5('0x1a')](_0x4df4b6+_0x7eb5('0x96'));break;default:_0x5bee56[_0x7eb5('0x1a')]('\x22\x22');}}}}else{var _0x25404b='';if(!_[_0x7eb5('0x8c')](_0x39b3f9)){if(!_0x1491e5['hasOwnProperty'](_0x39b3f9['id'])){if(!_[_0x7eb5('0x8c')](_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b][_0x200955[_0x4df4b6]])){var _0x5a6753=_[_0x7eb5('0x8e')](_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b][_0x200955[_0x4df4b6]][_0x7eb5('0x97')](/"/g,'\x27'));var _0x24f54f=phoneDescriptions[_0x7eb5('0x8b')](_0x509e7d=>_0x509e7d[_0x7eb5('0x98')]===_0x39b3f9['id']);if(!_[_0x7eb5('0x8c')](_0x24f54f)){var _0x55e691=_[_0x7eb5('0x8e')](_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b][_0x200955[_0x24f54f[_0x7eb5('0x99')]]][_0x7eb5('0x97')](/"/g,'\x27'));if(!_[_0x7eb5('0x8c')](_0x55e691))_0x5a6753=_0x5a6753+'§'+_0x55e691;}if(_0x25404b=='')_0x25404b=_0x5a6753;else _0x25404b+='|'+_0x5a6753;}_0xcc4c8f[_0x7eb5('0x1a')](_0x39b3f9['id']+':'+_0x25404b);_0x1491e5[_0x39b3f9['id']]=0x1;}}}}if(hasAdditionalPhones){_0x5bee56[_0x7eb5('0x1a')]('\x22'+_0xcc4c8f[_0x7eb5('0x14')](';')+'\x22');}if(_0x44657e[_0x7eb5('0x19')]){handleError(_0x1e3d4f['data'][_0x424a6b],{'code':_0x7eb5('0x9a'),'message':_0x44657e[_0x7eb5('0x14')]()});}else{fs['appendFileSync'](_0x57ed0c,_0x5bee56[_0x7eb5('0x14')]()+'\x0a');}_0x5bee56=[];}if(!(stats[_0x7eb5('0x9b')]%0xc8)){socket[_0x7eb5('0x9c')]('contact:import:'+_0x2674e8,stats);stats['errors']=[];}}if(stopped){logger[_0x7eb5('0x5d')](_0x7eb5('0x9d'));_0x3ceeab[_0x7eb5('0x9e')]();}}catch(_0x371750){handleError(_0x1e3d4f[_0x7eb5('0x89')][_0x424a6b],{'code':_0x7eb5('0x9f'),'message':_0x371750[_0x7eb5('0x60')]});logger[_0x7eb5('0x5f')]('step',_0x371750[_0x7eb5('0x60')]);}},'complete':function(){try{var _0x23e119=[];var _0x3e5b28=[];var _0x693fc2='cm_contacts';if(_0x3ff23c[_0x7eb5('0xa0')]&&_0x3ff23c[_0x7eb5('0xa0')][_0x7eb5('0x19')]){_0x693fc2=_0x7eb5('0xa1')+_0x2674e8;_0x23e119['push'](_0x7eb5('0xa2')+_0x693fc2+_0x7eb5('0xa3'));}tmp_campaigns='campaigns_'+_0x2674e8;tmp_voice_queues=_0x7eb5('0xa4')+_0x2674e8;_0x3e5b28[_0x7eb5('0x1a')](_0x7eb5('0xa2')+tmp_campaigns+'(id\x20int,dialTimezone\x20varchar(255),offset\x20int);');_0x3e5b28['push'](_0x7eb5('0xa2')+tmp_voice_queues+'(id\x20int,dialTimezone\x20varchar(255),offset\x20int);');BPromise[_0x7eb5('0xa5')](_0x3e5b28,handleTZQuery)[_0x7eb5('0x18')](function(_0x1b39d1){updateTzCampaigns(_0x3ff23c[_0x7eb5('0x32')])[_0x7eb5('0x18')](function(_0x22f338){updateTzVoiceQueues(_0x3ff23c['ListId'])[_0x7eb5('0x18')](function(_0x459452){if(hasTZ)_0x57b4fc=_0x57b4fc[_0x7eb5('0x97')](_0x7eb5('0x75'),_0x7eb5('0xa6'));var _0x3c5763=util[_0x7eb5('0x10')](_0x7eb5('0xa7'),_0x57ed0c,_0x693fc2,_0x57b4fc,_0x3ff23c['ListId'],_0x3ff23c['CompanyId']||null,NOW,NOW);if(_[_0x7eb5('0x8c')](_0x200955[_0x7eb5('0x30')])){_0x3c5763+=_0x7eb5('0xa8');}_0x23e119[_0x7eb5('0x1a')](_0x3c5763);if(_0x3ff23c[_0x7eb5('0xa0')]&&_0x3ff23c['duplicates'][_0x7eb5('0x19')]){_0x23e119[_0x7eb5('0x1a')](insertCmContact(_0x3ff23c[_0x7eb5('0x32')],_0x693fc2,_0x3ff23c[_0x7eb5('0xa0')],_0x200955));_0x23e119['push'](_0x7eb5('0xa9')+_0x693fc2+';');}BPromise[_0x7eb5('0xa5')](_0x23e119,handleQuery)[_0x7eb5('0x18')](function(_0x520537){BPromise[_0x7eb5('0x1b')]([handleAdditionalPhones()])['then'](function(){_0x23e119=[];for(var _0x3afe64=0x0;_0x3afe64<promisesAdditionalPhones[_0x7eb5('0x19')];_0x3afe64++){if(promisesAdditionalPhones[_0x3afe64][_0x7eb5('0x2f')]!='§'){_0x23e119['push'](createAdditionalPhone(promisesAdditionalPhones[_0x3afe64][_0x7eb5('0xaa')],promisesAdditionalPhones[_0x3afe64][_0x7eb5('0xab')],promisesAdditionalPhones[_0x3afe64][_0x7eb5('0x2f')],promisesAdditionalPhones[_0x3afe64][_0x7eb5('0xac')]));}}BPromise['all'](_0x23e119)[_0x7eb5('0x18')](function(){_0x23e119=[];if(hasTZ){_0x23e119[_0x7eb5('0x1a')](insertTzCmHopper(_0x3ff23c['ListId'],_0x7eb5('0x6e'),'voice_queues',_0x7eb5('0xad'),_0x3d3c03,tmp_voice_queues));_0x23e119[_0x7eb5('0x1a')](insertTzCmHopperBlack(_0x3ff23c[_0x7eb5('0x32')],'voice_queues_has_cm_blacklists','voice_queues',_0x7eb5('0xad')));_0x23e119[_0x7eb5('0x1a')](insertTzCmHopper(_0x3ff23c[_0x7eb5('0x32')],_0x7eb5('0x68'),_0x7eb5('0x67'),_0x7eb5('0xae'),_0x3d3c03,tmp_campaigns));_0x23e119['push'](insertTzCmHopperBlack(_0x3ff23c[_0x7eb5('0x32')],'campaigns_has_cm_blacklists',_0x7eb5('0x67'),_0x7eb5('0xae')));_0x23e119[_0x7eb5('0x1a')](insertCmHopperAdditionalPhones(_0x3ff23c[_0x7eb5('0x32')]));}else{_0x23e119[_0x7eb5('0x1a')](insertCmHopper(_0x3ff23c[_0x7eb5('0x32')],_0x7eb5('0x6e'),_0x7eb5('0x6d'),'VoiceQueueId',_0x3d3c03));_0x23e119[_0x7eb5('0x1a')](insertCmHopperBlack(_0x3ff23c[_0x7eb5('0x32')],_0x7eb5('0xaf'),_0x7eb5('0x6d'),_0x7eb5('0xad')));_0x23e119['push'](insertCmHopper(_0x3ff23c[_0x7eb5('0x32')],'campaigns_has_cm_lists',_0x7eb5('0x67'),_0x7eb5('0xae'),_0x3d3c03));_0x23e119[_0x7eb5('0x1a')](insertCmHopperBlack(_0x3ff23c[_0x7eb5('0x32')],_0x7eb5('0xb0'),_0x7eb5('0x67'),'CampaignId'));_0x23e119[_0x7eb5('0x1a')](insertCmHopperAdditionalPhones(_0x3ff23c[_0x7eb5('0x32')]));}_0x23e119[_0x7eb5('0x1a')]('DROP\x20TABLE\x20'+tmp_campaigns+';');_0x23e119['push'](_0x7eb5('0xa9')+tmp_voice_queues+';');BPromise[_0x7eb5('0xa5')](_0x23e119,handleQuery)[_0x7eb5('0x18')](function(_0x2a56ca){stats[_0x7eb5('0xb1')]=!![];stats[_0x7eb5('0xb2')]=_0x3ff23c[_0x7eb5('0xa0')]&&_0x3ff23c[_0x7eb5('0xa0')][_0x7eb5('0x19')]?results[0x2]['affectedRows']:results[0x0]['affectedRows'];socket[_0x7eb5('0x9c')]('contact:import:'+_0x2674e8,stats);fs[_0x7eb5('0xb3')](_0x304387);fs[_0x7eb5('0xb3')](_0x57ed0c);process[_0x7eb5('0x61')](0x0);})['catch'](function(_0x227a98){logger[_0x7eb5('0x5f')](_0x227a98['message']);process['exit'](0x1);});});});})['catch'](function(_0x473444){logger[_0x7eb5('0x5f')](_0x473444[_0x7eb5('0x60')]);process[_0x7eb5('0x61')](0x1);});});});});}catch(_0x3a1f56){logger['error'](_0x7eb5('0xb4'),_0x3a1f56[_0x7eb5('0x60')]);process[_0x7eb5('0x61')](0x1);}},'error':function(_0x3f79e4,_0x5efeea,_0x5482b0,_0x18fadc){throw new Error(_0x18fadc);}});});}function validate(){if(process[_0x7eb5('0xb5')]&&process[_0x7eb5('0xb5')][_0x7eb5('0x19')]<0x4){throw new Error(_0x7eb5('0xb6'));}var _0x3dd2b8=path[_0x7eb5('0x14')](config[_0x7eb5('0x7e')],_0x7eb5('0xb7'),process['argv'][0x2]);if(!fs[_0x7eb5('0xb8')](_0x3dd2b8)){throw new Error(_0x7eb5('0xb9'));}var _0x3cbfbc={};try{_0x3cbfbc=JSON['parse'](process[_0x7eb5('0xb5')][0x3]);if(!_0x3cbfbc[_0x7eb5('0x7b')]){throw new Error(_0x7eb5('0xba'));}if(!_0x3cbfbc['binding'][_0x7eb5('0x2f')]){throw new Error(_0x7eb5('0xbb'));}if(!_0x3cbfbc[_0x7eb5('0x7b')]['firstName']){throw new Error(_0x7eb5('0xbc'));}var _0x38f231=0x0;if(_0x3cbfbc[_0x7eb5('0x7b')][_0x7eb5('0x36')]){_0x38f231=0x1;}if(!_0x3cbfbc[_0x7eb5('0x32')]){throw new Error(_0x7eb5('0xbd'));}}catch(_0x469f5f){throw new Error('body\x20id\x20mandatory');}var _0x2b632f=[];try{_0x2b632f=_['map'](JSON[_0x7eb5('0x87')](process[_0x7eb5('0xb5')][0x4]),'id');}catch(_0x142593){logger[_0x7eb5('0x5d')]('No\x20agent\x20available');}main(_0x3dd2b8,_0x3cbfbc,_0x2b632f,_0x38f231);}validate();process['on'](_0x7eb5('0xbe'),handleStop);process['on'](_0x7eb5('0xbf'),handleUncaughtException);