765ba3f20c9985adf41a1adf092bd37e8ba511ed
[motion2.git] / server / api / cmContact / cmContact.model.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 _0x231f=['cmContact,\x20%s,\x20%s','List\x20not\x20found','models','isEmpty','destroy','ContactId','VoiceQueueId','CampaignId','push','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','moment','bluebird','request-promise','path','../../config/environment','./cmContact.attributes','exports','define','CmContact','cm_contacts','phone','fti_cm_contacts','firstName','lastName','mobile','fax','email','FULLTEXT','utf8mb4','utf8mb4_unicode_ci','error','cmContact','afterCreate','inspect','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','isNil','dialTimezone','VoiceQueue','then','utcOffset','add','ret','CmHopper','findOne','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create','ListId','priority','callbackUniqueid','catch','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','Campaign','timezones','minutes','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','findAll','CmList','Lists','length','get','dialCheckDuplicateType','always','onlyIfOpen','outbound','CmHopperBlack','message','BlackLists'];(function(_0x2847ec,_0x40ce19){var _0x507988=function(_0x3545a1){while(--_0x3545a1){_0x2847ec['push'](_0x2847ec['shift']());}};_0x507988(++_0x40ce19);}(_0x231f,0xad));var _0xf231=function(_0x57d7b3,_0x5407a4){_0x57d7b3=_0x57d7b3-0x0;var _0x3fb442=_0x231f[_0x57d7b3];return _0x3fb442;};'use strict';var _=require(_0xf231('0x0'));var util=require(_0xf231('0x1'));var logger=require(_0xf231('0x2'))('api');var moment=require(_0xf231('0x3'));var BPromise=require(_0xf231('0x4'));var rp=require(_0xf231('0x5'));var fs=require('fs');var path=require(_0xf231('0x6'));var rimraf=require('rimraf');var config=require(_0xf231('0x7'));var shared=require('../../config/environment/shared');var attributes=require(_0xf231('0x8'));module[_0xf231('0x9')]=function(_0x384a22,_0x3ef48f){return _0x384a22[_0xf231('0xa')](_0xf231('0xb'),attributes,{'tableName':_0xf231('0xc'),'paranoid':!![],'indexes':[{'name':_0xf231('0xd'),'fields':[_0xf231('0xd')]},{'name':_0xf231('0xe'),'fields':[_0xf231('0xf'),_0xf231('0x10'),_0xf231('0xd'),_0xf231('0x11'),_0xf231('0x12'),_0xf231('0x13')],'type':_0xf231('0x14')}],'timestamps':!![],'charset':_0xf231('0x15'),'collate':_0xf231('0x16'),'hooks':{'afterCreate':function(_0x461a37,_0x520c53,_0x22449b){var _0x3d826c=_0x384a22['models'];var _0x512972=_0x461a37['get']({'plain':!![]});function _0x376c5a(){return function(_0x3e26f8){logger[_0xf231('0x17')](_0xf231('0x18'),_0xf231('0x19'),util[_0xf231('0x1a')](_0x3e26f8,{'showHidden':![],'depth':null}));};}function _0x226020(_0x1bcc79,_0x105ffb){var _0x35c9f2=_0x105ffb[_0xf231('0x1b')]?_0x105ffb['scheduledat']:moment()[_0xf231('0x1c')](_0xf231('0x1d'));return new BPromise(function(_0x4a242a,_0x5487e5){if(_[_0xf231('0x1e')](_0x105ffb[_0xf231('0x1f')])){_0x4a242a({'ret':_0x35c9f2});}else{_0x3d826c[_0xf231('0x20')]['findOne']({'where':{'id':_0x1bcc79['id']}})[_0xf231('0x21')](function(_0x261cec){if(!_[_0xf231('0x1e')](_0x261cec)){var _0x698ecf=_0x261cec[_0xf231('0x1f')];if(!_[_0xf231('0x1e')](_0x698ecf)){var _0x5cdcf6=shared['timezones'];var _0x24f7c5=_0x5cdcf6[_0x698ecf];var _0x404b52=_0x5cdcf6[_0x105ffb['dialTimezone']];if(!_['isNil'](_0x24f7c5)&&!_[_0xf231('0x1e')](_0x404b52)){var _0x38933f=_0x24f7c5[_0xf231('0x22')]-_0x404b52['utcOffset'];_0x35c9f2=_0x105ffb[_0xf231('0x1b')]?_0x105ffb['scheduledat']:moment();var _0x242907=moment(_0x35c9f2)[_0xf231('0x23')](_0x38933f,'minutes');_0x4a242a({'ret':_0x242907[_0xf231('0x1c')](_0xf231('0x1d'))});}else _0x4a242a({'ret':_0x35c9f2});}else _0x4a242a({'ret':_0x35c9f2});}else{_0x4a242a({'ret':_0x35c9f2});}});}});return 0xa;}var _0x189660=function(_0xdb4398){return new BPromise(function(_0x2e1ae8,_0x1d615f){_0x226020(_0xdb4398,_0x512972)['then'](function(_0x5a6fd8){_0x512972[_0xf231('0x1b')]=_0x5a6fd8[_0xf231('0x24')];_0x3d826c[_0xf231('0x25')][_0xf231('0x26')]({'where':{'phone':_0x512972[_0xf231('0xd')],'VoiceQueueId':_0xdb4398['id']}})[_0xf231('0x21')](function(_0x3b3f0c){if(!_[_0xf231('0x1e')](_0x3b3f0c)){throw new Error(_0xf231('0x27')+_0x512972['phone']+_0xf231('0x28')+_0xdb4398['id']);}return _0x3d826c['CmHopper'][_0xf231('0x29')]({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972[_0xf231('0x2a')],'priority':_0x512972['priority']?_0x512972[_0xf231('0x2b')]:0x2,'scheduledat':_0x512972[_0xf231('0x1b')]?_0x512972[_0xf231('0x1b')]:moment()[_0xf231('0x1c')](_0xf231('0x1d')),'callback':_0x512972[_0xf231('0x2c')]?!![]:![],'callbackuniqueid':_0x512972[_0xf231('0x2c')]?_0x512972[_0xf231('0x2c')]:undefined,'callbackat':_0x512972[_0xf231('0x2c')]?moment()['format'](_0xf231('0x1d')):undefined,'VoiceQueueId':_0xdb4398['id']});})[_0xf231('0x2d')](function(_0x5a2e18){_0x1d615f(_0x5a2e18);});});});};var _0x29a224=function(_0x5e99ba){return new BPromise(function(_0x247c5e,_0x49ede1){_0x226020(_0x5e99ba,_0x512972)['then'](function(_0x5251be){_0x512972['scheduledat']=_0x5251be[_0xf231('0x24')];_0x3d826c['CmHopper'][_0xf231('0x26')]({'where':{'phone':_0x512972[_0xf231('0xd')],'VoiceQueueId':_0x5e99ba['id']}})['then'](function(_0x1ebdc2){if(!_[_0xf231('0x1e')](_0x1ebdc2)){throw new Error(_0xf231('0x27')+_0x512972[_0xf231('0xd')]+_0xf231('0x2e')+_0x5e99ba['id']);}return _0x3d826c[_0xf231('0x2f')][_0xf231('0x26')]({'where':{'calleridnum':_0x512972[_0xf231('0xd')],'VoiceQueueId':_0x5e99ba['id']}});})['then'](function(_0x29e16a){if(!_[_0xf231('0x1e')](_0x29e16a)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x512972['phone']+_0xf231('0x30')+_0x5e99ba['id']);}return _0x3d826c[_0xf231('0x25')][_0xf231('0x29')]({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972[_0xf231('0x2a')],'priority':_0x512972['priority']?_0x512972[_0xf231('0x2b')]:0x2,'scheduledat':_0x512972['scheduledat']?_0x512972[_0xf231('0x1b')]:moment()[_0xf231('0x1c')](_0xf231('0x1d')),'callback':_0x512972[_0xf231('0x2c')]?!![]:![],'callbackuniqueid':_0x512972[_0xf231('0x2c')]?_0x512972[_0xf231('0x2c')]:undefined,'callbackat':_0x512972[_0xf231('0x2c')]?moment()[_0xf231('0x1c')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x5e99ba['id']});})[_0xf231('0x2d')](function(_0x14605c){_0x49ede1(_0x14605c);});});});};function _0x305cf7(){return function(_0x3003cd){logger[_0xf231('0x17')](_0xf231('0x18'),_0xf231('0x19'),util[_0xf231('0x1a')](_0x3003cd,{'showHidden':![],'depth':null}));};}function _0x38ad37(_0x5eb1c3,_0x55c940){var _0x247bff=_0x55c940[_0xf231('0x1b')]?_0x55c940[_0xf231('0x1b')]:moment()['format'](_0xf231('0x1d'));return new BPromise(function(_0x4e4ba4,_0x20b4b9){if(_[_0xf231('0x1e')](_0x55c940[_0xf231('0x1f')])){_0x4e4ba4({'ret':_0x247bff});}else{_0x3d826c[_0xf231('0x31')][_0xf231('0x26')]({'where':{'id':_0x5eb1c3['id']}})['then'](function(_0x344836){if(!_[_0xf231('0x1e')](_0x344836)){var _0x4cd665=_0x344836['dialTimezone'];if(!_[_0xf231('0x1e')](_0x4cd665)){var _0x2d811a=shared[_0xf231('0x32')];var _0x4a8b4a=_0x2d811a[_0x4cd665];var _0x1e375b=_0x2d811a[_0x55c940[_0xf231('0x1f')]];if(!_[_0xf231('0x1e')](_0x4a8b4a)&&!_[_0xf231('0x1e')](_0x1e375b)){var _0x4fae4e=_0x4a8b4a[_0xf231('0x22')]-_0x1e375b[_0xf231('0x22')];_0x247bff=_0x55c940[_0xf231('0x1b')]?_0x55c940[_0xf231('0x1b')]:moment();var _0x3fc96f=moment(_0x247bff)[_0xf231('0x23')](_0x4fae4e,_0xf231('0x33'));_0x4e4ba4({'ret':_0x3fc96f[_0xf231('0x1c')]('YYYY-MM-DD\x20HH:mm:ss')});}else _0x4e4ba4({'ret':_0x247bff});}else _0x4e4ba4({'ret':_0x247bff});}else{_0x4e4ba4({'ret':_0x247bff});}});}});return 0xa;}var _0xb9c748=function(_0x10ef4b){return new BPromise(function(_0x56582b,_0x27fa38){_0x38ad37(_0x10ef4b,_0x512972)['then'](function(_0xb0bdcd){_0x512972[_0xf231('0x1b')]=_0xb0bdcd[_0xf231('0x24')];_0x3d826c[_0xf231('0x25')][_0xf231('0x26')]({'where':{'phone':_0x512972[_0xf231('0xd')],'CampaignId':_0x10ef4b['id']}})[_0xf231('0x21')](function(_0x55237c){if(!_[_0xf231('0x1e')](_0x55237c)){throw new Error(_0xf231('0x27')+_0x512972['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x10ef4b['id']);}return _0x3d826c[_0xf231('0x25')][_0xf231('0x29')]({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972['ListId'],'priority':_0x512972[_0xf231('0x2b')]?_0x512972[_0xf231('0x2b')]:0x2,'scheduledat':_0x512972[_0xf231('0x1b')]?_0x512972[_0xf231('0x1b')]:moment()[_0xf231('0x1c')](_0xf231('0x1d')),'callback':_0x512972[_0xf231('0x2c')]?!![]:![],'callbackuniqueid':_0x512972[_0xf231('0x2c')]?_0x512972[_0xf231('0x2c')]:undefined,'callbackat':_0x512972[_0xf231('0x2c')]?moment()[_0xf231('0x1c')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x10ef4b['id']});})['catch'](function(_0x2cebb2){_0x27fa38(_0x2cebb2);});});});};var _0x3a3dcd=function(_0x33357b){return new BPromise(function(_0x1b235a,_0x3e7f3a){_0x38ad37(_0x33357b,_0x512972)[_0xf231('0x21')](function(_0x4d6c63){_0x512972['scheduledat']=_0x4d6c63[_0xf231('0x24')];_0x3d826c[_0xf231('0x25')][_0xf231('0x26')]({'where':{'phone':_0x512972[_0xf231('0xd')],'CampaignId':_0x33357b['id']}})['then'](function(_0x52880f){if(!_[_0xf231('0x1e')](_0x52880f)){throw new Error('cmContact,\x20afterCreate\x20'+_0x512972[_0xf231('0xd')]+_0xf231('0x34')+_0x33357b['id']);}return _0x3d826c[_0xf231('0x2f')][_0xf231('0x26')]({'where':{'calleridnum':_0x512972[_0xf231('0xd')],'CampaignId':_0x33357b['id']}});})[_0xf231('0x21')](function(_0x254971){if(!_[_0xf231('0x1e')](_0x254971)){throw new Error(_0xf231('0x35')+_0x512972[_0xf231('0xd')]+_0xf231('0x36')+_0x33357b['id']);}return _0x3d826c[_0xf231('0x25')][_0xf231('0x29')]({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972['ListId'],'priority':_0x512972['priority']?_0x512972[_0xf231('0x2b')]:0x2,'scheduledat':_0x512972['scheduledat']?_0x512972['scheduledat']:moment()['format'](_0xf231('0x1d')),'callback':_0x512972[_0xf231('0x2c')]?!![]:![],'callbackuniqueid':_0x512972[_0xf231('0x2c')]?_0x512972[_0xf231('0x2c')]:undefined,'callbackat':_0x512972[_0xf231('0x2c')]?moment()[_0xf231('0x1c')](_0xf231('0x1d')):undefined,'CampaignId':_0x33357b['id']});})[_0xf231('0x2d')](function(_0x1e0488){_0x3e7f3a(_0x1e0488);});});});};if(_0x512972['ListId']){_0x3d826c['VoiceQueue'][_0xf231('0x37')]({'where':{'type':'outbound'},'attributes':['id','dialCheckDuplicateType',_0xf231('0x1f')],'include':[{'model':_0x3d826c[_0xf231('0x38')],'as':_0xf231('0x39'),'where':{'id':_0x512972[_0xf231('0x2a')]}}]})[_0xf231('0x21')](function(_0x251100){if(_0x251100){var _0x173908=[];for(var _0x1fc08a=0x0,_0x4a76aa;_0x1fc08a<_0x251100[_0xf231('0x3a')];_0x1fc08a+=0x1){_0x4a76aa=_0x251100[_0x1fc08a][_0xf231('0x3b')]({'plain':!![]});switch(_0x4a76aa[_0xf231('0x3c')]){case _0xf231('0x3d'):_0x29a224(_0x4a76aa)['catch'](_0x376c5a());break;case _0xf231('0x3e'):_0x189660(_0x4a76aa)[_0xf231('0x2d')](_0x376c5a());break;default:_0x38ad37(_0x4a76aa,_0x512972)[_0xf231('0x21')](function(_0x337e8c){_0x512972[_0xf231('0x1b')]=_0x337e8c['ret'];_0x3d826c[_0xf231('0x25')]['create']({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972[_0xf231('0x2a')],'scheduledat':_0x512972[_0xf231('0x1b')]?_0x512972[_0xf231('0x1b')]:moment()[_0xf231('0x1c')](_0xf231('0x1d')),'priority':_0x512972[_0xf231('0x2b')]?_0x512972[_0xf231('0x2b')]:0x2,'callback':_0x512972[_0xf231('0x2c')]?!![]:![],'callbackuniqueid':_0x512972[_0xf231('0x2c')]?_0x512972['callbackUniqueid']:undefined,'callbackat':_0x512972[_0xf231('0x2c')]?moment()[_0xf231('0x1c')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x4a76aa['id']})[_0xf231('0x2d')](_0x376c5a());});}}}})[_0xf231('0x21')](function(){return _0x3d826c['VoiceQueue'][_0xf231('0x37')]({'where':{'type':_0xf231('0x3f')},'attributes':['id'],'include':[{'model':_0x3d826c['CmList'],'as':'BlackLists','where':{'id':_0x512972[_0xf231('0x2a')]}}]})['then'](function(_0x41749e){if(_0x41749e){var _0x3fd0a7=[];for(var _0x5b3cb0=0x0,_0x46aa2c;_0x5b3cb0<_0x41749e['length'];_0x5b3cb0+=0x1){_0x46aa2c=_0x41749e[_0x5b3cb0][_0xf231('0x3b')]({'plain':!![]});_0x3d826c[_0xf231('0x40')]['create']({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972['ListId'],'VoiceQueueId':_0x46aa2c['id']})['catch'](function(_0x5350fa){logger[_0xf231('0x17')]('cmContact,\x20%s,\x20%s',_0xf231('0x19'),_0x5350fa[_0xf231('0x41')]);});}}});})[_0xf231('0x21')](function(){return _0x3d826c[_0xf231('0x31')][_0xf231('0x37')]({'attributes':['id','dialCheckDuplicateType',_0xf231('0x1f')],'include':[{'model':_0x3d826c['CmList'],'as':_0xf231('0x39'),'where':{'id':_0x512972[_0xf231('0x2a')]}}]})[_0xf231('0x21')](function(_0x3df03c){if(_0x3df03c){var _0x419c2b=[];for(var _0x549c67=0x0,_0x5448bf;_0x549c67<_0x3df03c[_0xf231('0x3a')];_0x549c67+=0x1){_0x5448bf=_0x3df03c[_0x549c67][_0xf231('0x3b')]({'plain':!![]});switch(_0x5448bf[_0xf231('0x3c')]){case _0xf231('0x3d'):_0x3a3dcd(_0x5448bf)[_0xf231('0x2d')](_0x305cf7());break;case'onlyIfOpen':_0xb9c748(_0x5448bf)[_0xf231('0x2d')](_0x305cf7());break;default:_0x3d826c['CmHopper'][_0xf231('0x29')]({'phone':_0x512972['phone'],'ContactId':_0x512972['id'],'ListId':_0x512972[_0xf231('0x2a')],'scheduledat':_0x512972[_0xf231('0x1b')]?_0x512972[_0xf231('0x1b')]:moment()[_0xf231('0x1c')](_0xf231('0x1d')),'priority':_0x512972[_0xf231('0x2b')]?_0x512972[_0xf231('0x2b')]:0x2,'callback':_0x512972[_0xf231('0x2c')]?!![]:![],'callbackuniqueid':_0x512972['callbackUniqueid']?_0x512972[_0xf231('0x2c')]:undefined,'callbackat':_0x512972[_0xf231('0x2c')]?moment()[_0xf231('0x1c')](_0xf231('0x1d')):undefined,'CampaignId':_0x5448bf['id']})[_0xf231('0x2d')](_0x305cf7());}}}});})[_0xf231('0x21')](function(){return _0x3d826c['Campaign']['findAll']({'attributes':['id'],'include':[{'model':_0x3d826c[_0xf231('0x38')],'as':_0xf231('0x42'),'where':{'id':_0x512972[_0xf231('0x2a')]}}]})[_0xf231('0x21')](function(_0x911f8f){if(_0x911f8f){var _0x4b24fe=[];for(var _0x373148=0x0,_0x5e671d;_0x373148<_0x911f8f[_0xf231('0x3a')];_0x373148+=0x1){_0x5e671d=_0x911f8f[_0x373148]['get']({'plain':!![]});_0x3d826c[_0xf231('0x40')][_0xf231('0x29')]({'phone':_0x512972[_0xf231('0xd')],'ContactId':_0x512972['id'],'ListId':_0x512972['ListId'],'CampaignId':_0x5e671d['id']})['catch'](function(_0x2a822f){logger[_0xf231('0x17')](_0xf231('0x43'),'afterCreate',_0x2a822f[_0xf231('0x41')]);});}}});})['then'](function(){_0x22449b();})[_0xf231('0x2d')](function(_0x14ec29){logger[_0xf231('0x17')](_0xf231('0x43'),_0xf231('0x19'),_0x14ec29[_0xf231('0x41')]);_0x22449b(_0x14ec29);});}else{_0x22449b(new Error(_0xf231('0x44')));}},'afterDestroy':function(_0x16668a,_0x20f8da,_0x489395){var _0x43419f=_0x384a22[_0xf231('0x45')];var _0xdf1efd=_0x16668a['get']({'plain':!![]});return _0x43419f[_0xf231('0x25')]['findAll']({'raw':!![],'where':{'ListId':_0xdf1efd[_0xf231('0x2a')],'ContactId':_0xdf1efd['id']}})[_0xf231('0x21')](function(_0xf4525b){if(!_[_0xf231('0x46')](_0xf4525b)){var _0x5ddfe0=[];for(var _0xfddd2e=0x0;_0xfddd2e<_0xf4525b[_0xf231('0x3a')];_0xfddd2e+=0x1){var _0xcbe686=_0x43419f[_0xf231('0x25')][_0xf231('0x47')]({'where':{'ListId':_0xf4525b[_0xfddd2e][_0xf231('0x2a')],'ContactId':_0xf4525b[_0xfddd2e][_0xf231('0x48')],'VoiceQueueId':_0xf4525b[_0xfddd2e][_0xf231('0x49')],'CampaignId':_0xf4525b[_0xfddd2e][_0xf231('0x4a')]}});_0x5ddfe0[_0xf231('0x4b')](_0xcbe686);}return BPromise[_0xf231('0x4c')](_0x5ddfe0);}})['then'](function(_0x2f8424){logger[_0xf231('0x4d')](_0xf231('0x4e'),util[_0xf231('0x1a')](_0x2f8424,{'showHidden':![],'depth':null}));_0x489395();})[_0xf231('0x2d')](function(_0x45b91f){logger[_0xf231('0x17')](_0xf231('0x4f'),util[_0xf231('0x1a')](_0x45b91f,{'showHidden':![],'depth':null}));_0x489395(_0x45b91f);});}}});};