d4055141c291273e9f95b4688155ba6c3e3cb4ce
[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 _0xf943=['cmContact','afterCreate','inspect','CmHopper','findOne','then','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create','ListId','priority','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','isNil','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20','CmHopperFinal','VoiceQueue','findAll','dialCheckDuplicateType','CmList','length','BlackLists','CmHopperBlack','error','cmContact,\x20%s,\x20%s','Campaign','Lists','always','onlyIfOpen','message','List\x20not\x20found','ContactId','VoiceQueueId','CampaignId','push','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','api','moment','bluebird','request-promise','path','rimraf','exports','CmContact','cm_contacts','phone','get'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xf943,0x115));var _0x3f94=function(_0x458806,_0x2fe9df){_0x458806=_0x458806-0x0;var _0x111d60=_0xf943[_0x458806];return _0x111d60;};'use strict';var _=require(_0x3f94('0x0'));var util=require(_0x3f94('0x1'));var logger=require('../../config/logger')(_0x3f94('0x2'));var moment=require(_0x3f94('0x3'));var BPromise=require(_0x3f94('0x4'));var rp=require(_0x3f94('0x5'));var fs=require('fs');var path=require(_0x3f94('0x6'));var rimraf=require(_0x3f94('0x7'));var config=require('../../config/environment');var attributes=require('./cmContact.attributes');module[_0x3f94('0x8')]=function(_0x25ce99,_0x134a60){return _0x25ce99['define'](_0x3f94('0x9'),attributes,{'tableName':_0x3f94('0xa'),'paranoid':!![],'indexes':[{'name':_0x3f94('0xb'),'fields':['phone']}],'timestamps':!![],'hooks':{'afterCreate':function(_0x46ca1a,_0x37cf4b,_0x4a1dc8){var _0x1da970=_0x25ce99['models'];var _0x4f4d69=_0x46ca1a[_0x3f94('0xc')]({'plain':!![]});function _0x1084e5(){return function(_0x4e685a){logger['error'](_0x3f94('0xd'),_0x3f94('0xe'),util[_0x3f94('0xf')](_0x4e685a,{'showHidden':![],'depth':null}));};}var _0x4c34ce=function(_0x58fb4a){return new BPromise(function(_0x2ba8d9,_0x421c0c){_0x1da970[_0x3f94('0x10')][_0x3f94('0x11')]({'where':{'phone':_0x4f4d69[_0x3f94('0xb')],'VoiceQueueId':_0x58fb4a['id']}})[_0x3f94('0x12')](function(_0x56be44){if(!_['isNil'](_0x56be44)){throw new Error('cmContact,\x20afterCreate\x20'+_0x4f4d69[_0x3f94('0xb')]+_0x3f94('0x13')+_0x58fb4a['id']);}return _0x1da970[_0x3f94('0x10')][_0x3f94('0x14')]({'phone':_0x4f4d69[_0x3f94('0xb')],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'priority':_0x4f4d69[_0x3f94('0x16')]?_0x4f4d69[_0x3f94('0x16')]:0x2,'scheduledat':_0x4f4d69[_0x3f94('0x17')]?_0x4f4d69[_0x3f94('0x17')]:moment()[_0x3f94('0x18')](_0x3f94('0x19')),'callback':_0x4f4d69[_0x3f94('0x1a')]?!![]:![],'callbackuniqueid':_0x4f4d69[_0x3f94('0x1a')]?_0x4f4d69[_0x3f94('0x1a')]:undefined,'callbackat':_0x4f4d69[_0x3f94('0x1a')]?moment()[_0x3f94('0x18')](_0x3f94('0x19')):undefined,'VoiceQueueId':_0x58fb4a['id']});})[_0x3f94('0x1b')](function(_0x3510a1){_0x421c0c(_0x3510a1);});});};var _0x6de26f=function(_0x3b852f){return new BPromise(function(_0x3340ed,_0x37e9f6){_0x1da970[_0x3f94('0x10')][_0x3f94('0x11')]({'where':{'phone':_0x4f4d69[_0x3f94('0xb')],'VoiceQueueId':_0x3b852f['id']}})[_0x3f94('0x12')](function(_0xd4d376){if(!_[_0x3f94('0x1c')](_0xd4d376)){throw new Error(_0x3f94('0x1d')+_0x4f4d69[_0x3f94('0xb')]+_0x3f94('0x1e')+_0x3b852f['id']);}return _0x1da970['CmHopperFinal'][_0x3f94('0x11')]({'where':{'calleridnum':_0x4f4d69['phone'],'VoiceQueueId':_0x3b852f['id']}});})[_0x3f94('0x12')](function(_0x145409){if(!_[_0x3f94('0x1c')](_0x145409)){throw new Error(_0x3f94('0x1f')+_0x4f4d69['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20'+_0x3b852f['id']);}return _0x1da970[_0x3f94('0x10')][_0x3f94('0x14')]({'phone':_0x4f4d69[_0x3f94('0xb')],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'priority':_0x4f4d69[_0x3f94('0x16')]?_0x4f4d69[_0x3f94('0x16')]:0x2,'scheduledat':_0x4f4d69[_0x3f94('0x17')]?_0x4f4d69[_0x3f94('0x17')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x4f4d69[_0x3f94('0x1a')]?!![]:![],'callbackuniqueid':_0x4f4d69[_0x3f94('0x1a')]?_0x4f4d69[_0x3f94('0x1a')]:undefined,'callbackat':_0x4f4d69[_0x3f94('0x1a')]?moment()[_0x3f94('0x18')](_0x3f94('0x19')):undefined,'VoiceQueueId':_0x3b852f['id']});})['catch'](function(_0x6b2fc9){_0x37e9f6(_0x6b2fc9);});});};function _0x2f51a8(){return function(_0x3c7f83){logger['error'](_0x3f94('0xd'),_0x3f94('0xe'),util['inspect'](_0x3c7f83,{'showHidden':![],'depth':null}));};}var _0x2fdef4=function(_0x57101a){return new BPromise(function(_0x362117,_0x27dc86){_0x1da970[_0x3f94('0x10')]['findOne']({'where':{'phone':_0x4f4d69[_0x3f94('0xb')],'CampaignId':_0x57101a['id']}})[_0x3f94('0x12')](function(_0x3b1bea){if(!_[_0x3f94('0x1c')](_0x3b1bea)){throw new Error(_0x3f94('0x1d')+_0x4f4d69['phone']+_0x3f94('0x20')+_0x57101a['id']);}return _0x1da970['CmHopper']['create']({'phone':_0x4f4d69[_0x3f94('0xb')],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69['ListId'],'priority':_0x4f4d69['priority']?_0x4f4d69[_0x3f94('0x16')]:0x2,'scheduledat':_0x4f4d69[_0x3f94('0x17')]?_0x4f4d69[_0x3f94('0x17')]:moment()[_0x3f94('0x18')](_0x3f94('0x19')),'callback':_0x4f4d69[_0x3f94('0x1a')]?!![]:![],'callbackuniqueid':_0x4f4d69['callbackUniqueid']?_0x4f4d69[_0x3f94('0x1a')]:undefined,'callbackat':_0x4f4d69[_0x3f94('0x1a')]?moment()['format'](_0x3f94('0x19')):undefined,'CampaignId':_0x57101a['id']});})[_0x3f94('0x1b')](function(_0x2afa8d){_0x27dc86(_0x2afa8d);});});};var _0x575ed1=function(_0x51735b){return new BPromise(function(_0x5d3e41,_0x293aa3){_0x1da970['CmHopper'][_0x3f94('0x11')]({'where':{'phone':_0x4f4d69[_0x3f94('0xb')],'CampaignId':_0x51735b['id']}})['then'](function(_0x5bab31){if(!_['isNil'](_0x5bab31)){throw new Error('cmContact,\x20afterCreate\x20'+_0x4f4d69[_0x3f94('0xb')]+_0x3f94('0x21')+_0x51735b['id']);}return _0x1da970[_0x3f94('0x22')][_0x3f94('0x11')]({'where':{'calleridnum':_0x4f4d69[_0x3f94('0xb')],'CampaignId':_0x51735b['id']}});})[_0x3f94('0x12')](function(_0x2f8e17){if(!_[_0x3f94('0x1c')](_0x2f8e17)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x4f4d69[_0x3f94('0xb')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20'+_0x51735b['id']);}return _0x1da970[_0x3f94('0x10')][_0x3f94('0x14')]({'phone':_0x4f4d69[_0x3f94('0xb')],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'priority':_0x4f4d69[_0x3f94('0x16')]?_0x4f4d69[_0x3f94('0x16')]:0x2,'scheduledat':_0x4f4d69['scheduledat']?_0x4f4d69[_0x3f94('0x17')]:moment()[_0x3f94('0x18')](_0x3f94('0x19')),'callback':_0x4f4d69[_0x3f94('0x1a')]?!![]:![],'callbackuniqueid':_0x4f4d69[_0x3f94('0x1a')]?_0x4f4d69[_0x3f94('0x1a')]:undefined,'callbackat':_0x4f4d69[_0x3f94('0x1a')]?moment()[_0x3f94('0x18')](_0x3f94('0x19')):undefined,'CampaignId':_0x51735b['id']});})[_0x3f94('0x1b')](function(_0x5b45e8){_0x293aa3(_0x5b45e8);});});};if(_0x4f4d69[_0x3f94('0x15')]){_0x1da970[_0x3f94('0x23')][_0x3f94('0x24')]({'where':{'type':'outbound'},'attributes':['id',_0x3f94('0x25')],'include':[{'model':_0x1da970[_0x3f94('0x26')],'as':'Lists','where':{'id':_0x4f4d69['ListId']}}]})[_0x3f94('0x12')](function(_0x5aa129){if(_0x5aa129){var _0x15a467=[];for(var _0x304078=0x0,_0x3aafc5;_0x304078<_0x5aa129[_0x3f94('0x27')];_0x304078+=0x1){_0x3aafc5=_0x5aa129[_0x304078][_0x3f94('0xc')]({'plain':!![]});switch(_0x3aafc5['dialCheckDuplicateType']){case'always':_0x6de26f(_0x3aafc5)['catch'](_0x1084e5());break;case'onlyIfOpen':_0x4c34ce(_0x3aafc5)['catch'](_0x1084e5());break;default:_0x1da970[_0x3f94('0x10')][_0x3f94('0x14')]({'phone':_0x4f4d69['phone'],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'scheduledat':_0x4f4d69['scheduledat']?_0x4f4d69[_0x3f94('0x17')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x4f4d69['priority']?_0x4f4d69['priority']:0x2,'callback':_0x4f4d69[_0x3f94('0x1a')]?!![]:![],'callbackuniqueid':_0x4f4d69['callbackUniqueid']?_0x4f4d69[_0x3f94('0x1a')]:undefined,'callbackat':_0x4f4d69[_0x3f94('0x1a')]?moment()[_0x3f94('0x18')](_0x3f94('0x19')):undefined,'VoiceQueueId':_0x3aafc5['id']})['catch'](_0x1084e5());}}}})['then'](function(){return _0x1da970[_0x3f94('0x23')][_0x3f94('0x24')]({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0x1da970['CmList'],'as':_0x3f94('0x28'),'where':{'id':_0x4f4d69['ListId']}}]})[_0x3f94('0x12')](function(_0x15d8f3){if(_0x15d8f3){var _0x1da434=[];for(var _0x1a898e=0x0,_0x251e7d;_0x1a898e<_0x15d8f3[_0x3f94('0x27')];_0x1a898e+=0x1){_0x251e7d=_0x15d8f3[_0x1a898e][_0x3f94('0xc')]({'plain':!![]});_0x1da970[_0x3f94('0x29')]['create']({'phone':_0x4f4d69[_0x3f94('0xb')],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'VoiceQueueId':_0x251e7d['id']})['catch'](function(_0x4c5606){logger[_0x3f94('0x2a')](_0x3f94('0x2b'),'afterCreate',_0x4c5606['message']);});}}});})[_0x3f94('0x12')](function(){return _0x1da970[_0x3f94('0x2c')][_0x3f94('0x24')]({'attributes':['id','dialCheckDuplicateType'],'include':[{'model':_0x1da970[_0x3f94('0x26')],'as':_0x3f94('0x2d'),'where':{'id':_0x4f4d69[_0x3f94('0x15')]}}]})[_0x3f94('0x12')](function(_0x39496d){if(_0x39496d){var _0x3c2fbf=[];for(var _0x246d01=0x0,_0xeda1d3;_0x246d01<_0x39496d[_0x3f94('0x27')];_0x246d01+=0x1){_0xeda1d3=_0x39496d[_0x246d01][_0x3f94('0xc')]({'plain':!![]});switch(_0xeda1d3[_0x3f94('0x25')]){case _0x3f94('0x2e'):_0x575ed1(_0xeda1d3)['catch'](_0x2f51a8());break;case _0x3f94('0x2f'):_0x2fdef4(_0xeda1d3)['catch'](_0x2f51a8());break;default:_0x1da970['CmHopper'][_0x3f94('0x14')]({'phone':_0x4f4d69[_0x3f94('0xb')],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'scheduledat':_0x4f4d69[_0x3f94('0x17')]?_0x4f4d69[_0x3f94('0x17')]:moment()[_0x3f94('0x18')](_0x3f94('0x19')),'priority':_0x4f4d69['priority']?_0x4f4d69[_0x3f94('0x16')]:0x2,'callback':_0x4f4d69[_0x3f94('0x1a')]?!![]:![],'callbackuniqueid':_0x4f4d69[_0x3f94('0x1a')]?_0x4f4d69[_0x3f94('0x1a')]:undefined,'callbackat':_0x4f4d69[_0x3f94('0x1a')]?moment()[_0x3f94('0x18')](_0x3f94('0x19')):undefined,'CampaignId':_0xeda1d3['id']})[_0x3f94('0x1b')](_0x2f51a8());}}}});})[_0x3f94('0x12')](function(){return _0x1da970[_0x3f94('0x2c')][_0x3f94('0x24')]({'attributes':['id'],'include':[{'model':_0x1da970['CmList'],'as':_0x3f94('0x28'),'where':{'id':_0x4f4d69[_0x3f94('0x15')]}}]})[_0x3f94('0x12')](function(_0x19fc3f){if(_0x19fc3f){var _0x259fc6=[];for(var _0x5663a2=0x0,_0x28039d;_0x5663a2<_0x19fc3f[_0x3f94('0x27')];_0x5663a2+=0x1){_0x28039d=_0x19fc3f[_0x5663a2][_0x3f94('0xc')]({'plain':!![]});_0x1da970['CmHopperBlack'][_0x3f94('0x14')]({'phone':_0x4f4d69['phone'],'ContactId':_0x4f4d69['id'],'ListId':_0x4f4d69[_0x3f94('0x15')],'CampaignId':_0x28039d['id']})[_0x3f94('0x1b')](function(_0x3e6de8){logger[_0x3f94('0x2a')]('cmContact,\x20%s,\x20%s',_0x3f94('0xe'),_0x3e6de8[_0x3f94('0x30')]);});}}});})[_0x3f94('0x12')](function(){_0x4a1dc8();})[_0x3f94('0x1b')](function(_0xa76107){logger[_0x3f94('0x2a')](_0x3f94('0x2b'),_0x3f94('0xe'),_0xa76107['message']);_0x4a1dc8(_0xa76107);});}else{_0x4a1dc8(new Error(_0x3f94('0x31')));}},'afterDestroy':function(_0x521f45,_0x4db83b,_0xe2b383){var _0x5b2f5d=_0x25ce99['models'];var _0x4f0118=_0x521f45['get']({'plain':!![]});return _0x5b2f5d[_0x3f94('0x10')][_0x3f94('0x24')]({'raw':!![],'where':{'ListId':_0x4f0118[_0x3f94('0x15')],'ContactId':_0x4f0118['id']}})['then'](function(_0x224da4){if(!_['isEmpty'](_0x224da4)){var _0x22cf92=[];for(var _0x17a7ef=0x0;_0x17a7ef<_0x224da4[_0x3f94('0x27')];_0x17a7ef+=0x1){var _0x3578ca=_0x5b2f5d[_0x3f94('0x10')]['destroy']({'where':{'ListId':_0x224da4[_0x17a7ef][_0x3f94('0x15')],'ContactId':_0x224da4[_0x17a7ef][_0x3f94('0x32')],'VoiceQueueId':_0x224da4[_0x17a7ef][_0x3f94('0x33')],'CampaignId':_0x224da4[_0x17a7ef][_0x3f94('0x34')]}});_0x22cf92[_0x3f94('0x35')](_0x3578ca);}return BPromise[_0x3f94('0x36')](_0x22cf92);}})[_0x3f94('0x12')](function(_0x509c34){logger[_0x3f94('0x37')](_0x3f94('0x38'),util[_0x3f94('0xf')](_0x509c34,{'showHidden':![],'depth':null}));_0xe2b383();})[_0x3f94('0x1b')](function(_0x537476){logger['error'](_0x3f94('0x39'),util[_0x3f94('0xf')](_0x537476,{'showHidden':![],'depth':null}));_0xe2b383(_0x537476);});}}});};