Built motion from commit (unavailable).|2.5.9
[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 _0x5a99=['BlackLists','cmContact,\x20%s,\x20%s','message','List\x20not\x20found','models','ContactId','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','firstName','lastName','mobile','fax','get','error','CmHopper','isNil','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','ListId','priority','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','findOne','then','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','create','cmContact','afterCreate','inspect','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','VoiceQueue','findAll','outbound','dialCheckDuplicateType','CmList','Lists','length','always','CmHopperBlack','Campaign','onlyIfOpen'];(function(_0x42cf89,_0x2bd0dd){var _0x5a918f=function(_0x27591a){while(--_0x27591a){_0x42cf89['push'](_0x42cf89['shift']());}};_0x5a918f(++_0x2bd0dd);}(_0x5a99,0xc9));var _0x95a9=function(_0x2b2699,_0xb59742){_0x2b2699=_0x2b2699-0x0;var _0x358b0c=_0x5a99[_0x2b2699];return _0x358b0c;};'use strict';var _=require(_0x95a9('0x0'));var util=require(_0x95a9('0x1'));var logger=require(_0x95a9('0x2'))('api');var moment=require(_0x95a9('0x3'));var BPromise=require(_0x95a9('0x4'));var rp=require(_0x95a9('0x5'));var fs=require('fs');var path=require(_0x95a9('0x6'));var rimraf=require('rimraf');var config=require(_0x95a9('0x7'));var attributes=require(_0x95a9('0x8'));module[_0x95a9('0x9')]=function(_0x5195cd,_0x4aecd8){return _0x5195cd[_0x95a9('0xa')](_0x95a9('0xb'),attributes,{'tableName':_0x95a9('0xc'),'paranoid':!![],'indexes':[{'name':'phone','fields':[_0x95a9('0xd')]},{'name':'fti_cm_contacts','fields':[_0x95a9('0xe'),_0x95a9('0xf'),_0x95a9('0xd'),_0x95a9('0x10'),_0x95a9('0x11'),'email'],'type':'FULLTEXT'}],'timestamps':!![],'hooks':{'afterCreate':function(_0x1f617c,_0x1e1d8a,_0x4de6ba){var _0x140a3a=_0x5195cd['models'];var _0x2e9142=_0x1f617c[_0x95a9('0x12')]({'plain':!![]});function _0x789d86(){return function(_0x1c7256){logger[_0x95a9('0x13')]('cmContact','afterCreate',util['inspect'](_0x1c7256,{'showHidden':![],'depth':null}));};}var _0x39126b=function(_0x537c35){return new BPromise(function(_0xccaf69,_0x45f48e){_0x140a3a[_0x95a9('0x14')]['findOne']({'where':{'phone':_0x2e9142[_0x95a9('0xd')],'VoiceQueueId':_0x537c35['id']}})['then'](function(_0x37ccf1){if(!_[_0x95a9('0x15')](_0x37ccf1)){throw new Error(_0x95a9('0x16')+_0x2e9142['phone']+_0x95a9('0x17')+_0x537c35['id']);}return _0x140a3a['CmHopper']['create']({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142[_0x95a9('0x18')],'priority':_0x2e9142[_0x95a9('0x19')]?_0x2e9142[_0x95a9('0x19')]:0x2,'scheduledat':_0x2e9142[_0x95a9('0x1a')]?_0x2e9142[_0x95a9('0x1a')]:moment()[_0x95a9('0x1b')](_0x95a9('0x1c')),'callback':_0x2e9142[_0x95a9('0x1d')]?!![]:![],'callbackuniqueid':_0x2e9142[_0x95a9('0x1d')]?_0x2e9142[_0x95a9('0x1d')]:undefined,'callbackat':_0x2e9142['callbackUniqueid']?moment()[_0x95a9('0x1b')](_0x95a9('0x1c')):undefined,'VoiceQueueId':_0x537c35['id']});})[_0x95a9('0x1e')](function(_0x2e61d1){_0x45f48e(_0x2e61d1);});});};var _0x5a6034=function(_0x2f05a7){return new BPromise(function(_0x21b556,_0xc4520e){_0x140a3a[_0x95a9('0x14')][_0x95a9('0x1f')]({'where':{'phone':_0x2e9142[_0x95a9('0xd')],'VoiceQueueId':_0x2f05a7['id']}})[_0x95a9('0x20')](function(_0x48de16){if(!_[_0x95a9('0x15')](_0x48de16)){throw new Error(_0x95a9('0x16')+_0x2e9142[_0x95a9('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0x2f05a7['id']);}return _0x140a3a['CmHopperFinal'][_0x95a9('0x1f')]({'where':{'calleridnum':_0x2e9142['phone'],'VoiceQueueId':_0x2f05a7['id']}});})['then'](function(_0xda089a){if(!_['isNil'](_0xda089a)){throw new Error(_0x95a9('0x21')+_0x2e9142[_0x95a9('0xd')]+_0x95a9('0x22')+_0x2f05a7['id']);}return _0x140a3a[_0x95a9('0x14')][_0x95a9('0x23')]({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142[_0x95a9('0x18')],'priority':_0x2e9142[_0x95a9('0x19')]?_0x2e9142[_0x95a9('0x19')]:0x2,'scheduledat':_0x2e9142[_0x95a9('0x1a')]?_0x2e9142[_0x95a9('0x1a')]:moment()[_0x95a9('0x1b')](_0x95a9('0x1c')),'callback':_0x2e9142[_0x95a9('0x1d')]?!![]:![],'callbackuniqueid':_0x2e9142[_0x95a9('0x1d')]?_0x2e9142[_0x95a9('0x1d')]:undefined,'callbackat':_0x2e9142[_0x95a9('0x1d')]?moment()['format']('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x2f05a7['id']});})['catch'](function(_0x462993){_0xc4520e(_0x462993);});});};function _0x1d4ab3(){return function(_0x8ff6ef){logger['error'](_0x95a9('0x24'),_0x95a9('0x25'),util[_0x95a9('0x26')](_0x8ff6ef,{'showHidden':![],'depth':null}));};}var _0x3df461=function(_0x2877d){return new BPromise(function(_0x3a7e75,_0x213d19){_0x140a3a[_0x95a9('0x14')]['findOne']({'where':{'phone':_0x2e9142['phone'],'CampaignId':_0x2877d['id']}})['then'](function(_0x37072a){if(!_[_0x95a9('0x15')](_0x37072a)){throw new Error(_0x95a9('0x16')+_0x2e9142[_0x95a9('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x2877d['id']);}return _0x140a3a[_0x95a9('0x14')][_0x95a9('0x23')]({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142[_0x95a9('0x18')],'priority':_0x2e9142[_0x95a9('0x19')]?_0x2e9142[_0x95a9('0x19')]:0x2,'scheduledat':_0x2e9142[_0x95a9('0x1a')]?_0x2e9142[_0x95a9('0x1a')]:moment()[_0x95a9('0x1b')](_0x95a9('0x1c')),'callback':_0x2e9142[_0x95a9('0x1d')]?!![]:![],'callbackuniqueid':_0x2e9142['callbackUniqueid']?_0x2e9142[_0x95a9('0x1d')]:undefined,'callbackat':_0x2e9142[_0x95a9('0x1d')]?moment()[_0x95a9('0x1b')](_0x95a9('0x1c')):undefined,'CampaignId':_0x2877d['id']});})[_0x95a9('0x1e')](function(_0x3a3caf){_0x213d19(_0x3a3caf);});});};var _0x5b415a=function(_0x365d2a){return new BPromise(function(_0x14a56a,_0x38dc69){_0x140a3a[_0x95a9('0x14')][_0x95a9('0x1f')]({'where':{'phone':_0x2e9142['phone'],'CampaignId':_0x365d2a['id']}})[_0x95a9('0x20')](function(_0x2f4f3f){if(!_['isNil'](_0x2f4f3f)){throw new Error(_0x95a9('0x16')+_0x2e9142[_0x95a9('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x365d2a['id']);}return _0x140a3a['CmHopperFinal'][_0x95a9('0x1f')]({'where':{'calleridnum':_0x2e9142[_0x95a9('0xd')],'CampaignId':_0x365d2a['id']}});})[_0x95a9('0x20')](function(_0x336cbf){if(!_[_0x95a9('0x15')](_0x336cbf)){throw new Error(_0x95a9('0x21')+_0x2e9142[_0x95a9('0xd')]+_0x95a9('0x27')+_0x365d2a['id']);}return _0x140a3a['CmHopper'][_0x95a9('0x23')]({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142[_0x95a9('0x18')],'priority':_0x2e9142[_0x95a9('0x19')]?_0x2e9142[_0x95a9('0x19')]:0x2,'scheduledat':_0x2e9142[_0x95a9('0x1a')]?_0x2e9142[_0x95a9('0x1a')]:moment()[_0x95a9('0x1b')](_0x95a9('0x1c')),'callback':_0x2e9142[_0x95a9('0x1d')]?!![]:![],'callbackuniqueid':_0x2e9142[_0x95a9('0x1d')]?_0x2e9142[_0x95a9('0x1d')]:undefined,'callbackat':_0x2e9142['callbackUniqueid']?moment()[_0x95a9('0x1b')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x365d2a['id']});})[_0x95a9('0x1e')](function(_0xc05ea1){_0x38dc69(_0xc05ea1);});});};if(_0x2e9142['ListId']){_0x140a3a[_0x95a9('0x28')][_0x95a9('0x29')]({'where':{'type':_0x95a9('0x2a')},'attributes':['id',_0x95a9('0x2b')],'include':[{'model':_0x140a3a[_0x95a9('0x2c')],'as':_0x95a9('0x2d'),'where':{'id':_0x2e9142[_0x95a9('0x18')]}}]})[_0x95a9('0x20')](function(_0x2d7bfb){if(_0x2d7bfb){var _0x4a204d=[];for(var _0x3e5976=0x0,_0x3ff3f6;_0x3e5976<_0x2d7bfb[_0x95a9('0x2e')];_0x3e5976+=0x1){_0x3ff3f6=_0x2d7bfb[_0x3e5976][_0x95a9('0x12')]({'plain':!![]});switch(_0x3ff3f6[_0x95a9('0x2b')]){case _0x95a9('0x2f'):_0x5a6034(_0x3ff3f6)['catch'](_0x789d86());break;case'onlyIfOpen':_0x39126b(_0x3ff3f6)[_0x95a9('0x1e')](_0x789d86());break;default:_0x140a3a[_0x95a9('0x14')][_0x95a9('0x23')]({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142[_0x95a9('0x18')],'scheduledat':_0x2e9142[_0x95a9('0x1a')]?_0x2e9142[_0x95a9('0x1a')]:moment()[_0x95a9('0x1b')](_0x95a9('0x1c')),'priority':_0x2e9142[_0x95a9('0x19')]?_0x2e9142[_0x95a9('0x19')]:0x2,'callback':_0x2e9142['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x2e9142[_0x95a9('0x1d')]?_0x2e9142[_0x95a9('0x1d')]:undefined,'callbackat':_0x2e9142['callbackUniqueid']?moment()[_0x95a9('0x1b')](_0x95a9('0x1c')):undefined,'VoiceQueueId':_0x3ff3f6['id']})[_0x95a9('0x1e')](_0x789d86());}}}})[_0x95a9('0x20')](function(){return _0x140a3a[_0x95a9('0x28')][_0x95a9('0x29')]({'where':{'type':_0x95a9('0x2a')},'attributes':['id'],'include':[{'model':_0x140a3a[_0x95a9('0x2c')],'as':'BlackLists','where':{'id':_0x2e9142[_0x95a9('0x18')]}}]})[_0x95a9('0x20')](function(_0x20f7b0){if(_0x20f7b0){var _0x30184b=[];for(var _0x3e510b=0x0,_0x5960c5;_0x3e510b<_0x20f7b0[_0x95a9('0x2e')];_0x3e510b+=0x1){_0x5960c5=_0x20f7b0[_0x3e510b][_0x95a9('0x12')]({'plain':!![]});_0x140a3a[_0x95a9('0x30')][_0x95a9('0x23')]({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142['ListId'],'VoiceQueueId':_0x5960c5['id']})['catch'](function(_0x195a8e){logger[_0x95a9('0x13')]('cmContact,\x20%s,\x20%s',_0x95a9('0x25'),_0x195a8e['message']);});}}});})[_0x95a9('0x20')](function(){return _0x140a3a[_0x95a9('0x31')][_0x95a9('0x29')]({'attributes':['id','dialCheckDuplicateType'],'include':[{'model':_0x140a3a[_0x95a9('0x2c')],'as':_0x95a9('0x2d'),'where':{'id':_0x2e9142[_0x95a9('0x18')]}}]})['then'](function(_0x29764d){if(_0x29764d){var _0x366837=[];for(var _0x139630=0x0,_0x352ec9;_0x139630<_0x29764d[_0x95a9('0x2e')];_0x139630+=0x1){_0x352ec9=_0x29764d[_0x139630][_0x95a9('0x12')]({'plain':!![]});switch(_0x352ec9[_0x95a9('0x2b')]){case _0x95a9('0x2f'):_0x5b415a(_0x352ec9)[_0x95a9('0x1e')](_0x1d4ab3());break;case _0x95a9('0x32'):_0x3df461(_0x352ec9)[_0x95a9('0x1e')](_0x1d4ab3());break;default:_0x140a3a[_0x95a9('0x14')][_0x95a9('0x23')]({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142[_0x95a9('0x18')],'scheduledat':_0x2e9142['scheduledat']?_0x2e9142[_0x95a9('0x1a')]:moment()[_0x95a9('0x1b')](_0x95a9('0x1c')),'priority':_0x2e9142[_0x95a9('0x19')]?_0x2e9142[_0x95a9('0x19')]:0x2,'callback':_0x2e9142[_0x95a9('0x1d')]?!![]:![],'callbackuniqueid':_0x2e9142[_0x95a9('0x1d')]?_0x2e9142[_0x95a9('0x1d')]:undefined,'callbackat':_0x2e9142[_0x95a9('0x1d')]?moment()[_0x95a9('0x1b')](_0x95a9('0x1c')):undefined,'CampaignId':_0x352ec9['id']})[_0x95a9('0x1e')](_0x1d4ab3());}}}});})[_0x95a9('0x20')](function(){return _0x140a3a[_0x95a9('0x31')][_0x95a9('0x29')]({'attributes':['id'],'include':[{'model':_0x140a3a[_0x95a9('0x2c')],'as':_0x95a9('0x33'),'where':{'id':_0x2e9142[_0x95a9('0x18')]}}]})[_0x95a9('0x20')](function(_0x29436f){if(_0x29436f){var _0x3131b8=[];for(var _0x3d7c7f=0x0,_0x5655e2;_0x3d7c7f<_0x29436f['length'];_0x3d7c7f+=0x1){_0x5655e2=_0x29436f[_0x3d7c7f][_0x95a9('0x12')]({'plain':!![]});_0x140a3a[_0x95a9('0x30')]['create']({'phone':_0x2e9142[_0x95a9('0xd')],'ContactId':_0x2e9142['id'],'ListId':_0x2e9142['ListId'],'CampaignId':_0x5655e2['id']})['catch'](function(_0x24b8ed){logger['error'](_0x95a9('0x34'),_0x95a9('0x25'),_0x24b8ed['message']);});}}});})[_0x95a9('0x20')](function(){_0x4de6ba();})[_0x95a9('0x1e')](function(_0x56f37c){logger[_0x95a9('0x13')](_0x95a9('0x34'),'afterCreate',_0x56f37c[_0x95a9('0x35')]);_0x4de6ba(_0x56f37c);});}else{_0x4de6ba(new Error(_0x95a9('0x36')));}},'afterDestroy':function(_0x2e21c2,_0x26069f,_0x5375dd){var _0x5e75ef=_0x5195cd[_0x95a9('0x37')];var _0x281d6c=_0x2e21c2[_0x95a9('0x12')]({'plain':!![]});return _0x5e75ef[_0x95a9('0x14')][_0x95a9('0x29')]({'raw':!![],'where':{'ListId':_0x281d6c[_0x95a9('0x18')],'ContactId':_0x281d6c['id']}})[_0x95a9('0x20')](function(_0x55657e){if(!_['isEmpty'](_0x55657e)){var _0x58abbb=[];for(var _0xde306=0x0;_0xde306<_0x55657e[_0x95a9('0x2e')];_0xde306+=0x1){var _0x10490a=_0x5e75ef[_0x95a9('0x14')]['destroy']({'where':{'ListId':_0x55657e[_0xde306][_0x95a9('0x18')],'ContactId':_0x55657e[_0xde306][_0x95a9('0x38')],'VoiceQueueId':_0x55657e[_0xde306]['VoiceQueueId'],'CampaignId':_0x55657e[_0xde306][_0x95a9('0x39')]}});_0x58abbb[_0x95a9('0x3a')](_0x10490a);}return BPromise[_0x95a9('0x3b')](_0x58abbb);}})[_0x95a9('0x20')](function(_0x3a9787){logger[_0x95a9('0x3c')](_0x95a9('0x3d'),util[_0x95a9('0x26')](_0x3a9787,{'showHidden':![],'depth':null}));_0x5375dd();})[_0x95a9('0x1e')](function(_0x3cb345){logger['error'](_0x95a9('0x3e'),util[_0x95a9('0x26')](_0x3cb345,{'showHidden':![],'depth':null}));_0x5375dd(_0x3cb345);});}}});};