5485941f6e716d44125c2770002cd552133ab650
[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 _0x6f7a=['List\x20not\x20found','models','isEmpty','destroy','ContactId','CampaignId','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','api','bluebird','request-promise','path','rimraf','../../config/environment','./cmContact.attributes','exports','define','CmContact','phone','get','error','afterCreate','inspect','findOne','then','isNil','CmHopper','create','priority','scheduledat','callbackUniqueid','YYYY-MM-DD\x20HH:mm:ss','catch','cmContact,\x20afterCreate\x20','cmContact,\x20afterCreate,\x20','ListId','format','cmContact','\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','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','findAll','outbound','dialCheckDuplicateType','CmList','Lists','length','always','onlyIfOpen','BlackLists','CmHopperBlack','cmContact,\x20%s,\x20%s','Campaign','message'];(function(_0x177334,_0x3a4eec){var _0x3874b7=function(_0x513bdf){while(--_0x513bdf){_0x177334['push'](_0x177334['shift']());}};_0x3874b7(++_0x3a4eec);}(_0x6f7a,0x1ae));var _0xa6f7=function(_0x2e6620,_0x5326b2){_0x2e6620=_0x2e6620-0x0;var _0x239ff8=_0x6f7a[_0x2e6620];return _0x239ff8;};'use strict';var _=require(_0xa6f7('0x0'));var util=require(_0xa6f7('0x1'));var logger=require(_0xa6f7('0x2'))(_0xa6f7('0x3'));var moment=require('moment');var BPromise=require(_0xa6f7('0x4'));var rp=require(_0xa6f7('0x5'));var fs=require('fs');var path=require(_0xa6f7('0x6'));var rimraf=require(_0xa6f7('0x7'));var config=require(_0xa6f7('0x8'));var attributes=require(_0xa6f7('0x9'));module[_0xa6f7('0xa')]=function(_0x20cccd,_0x1fdd5f){return _0x20cccd[_0xa6f7('0xb')](_0xa6f7('0xc'),attributes,{'tableName':'cm_contacts','paranoid':!![],'indexes':[{'name':'phone','fields':[_0xa6f7('0xd')]}],'timestamps':!![],'hooks':{'afterCreate':function(_0x2674c9,_0x397020,_0x362404){var _0x23bb05=_0x20cccd['models'];var _0x5bdc9e=_0x2674c9[_0xa6f7('0xe')]({'plain':!![]});function _0x225da4(){return function(_0x4e30bc){logger[_0xa6f7('0xf')]('cmContact',_0xa6f7('0x10'),util[_0xa6f7('0x11')](_0x4e30bc,{'showHidden':![],'depth':null}));};}var _0x13425e=function(_0xd5558b){return new BPromise(function(_0x5a3035,_0x3fc7f1){_0x23bb05['CmHopper'][_0xa6f7('0x12')]({'where':{'phone':_0x5bdc9e[_0xa6f7('0xd')],'VoiceQueueId':_0xd5558b['id']}})[_0xa6f7('0x13')](function(_0x4d3d59){if(!_[_0xa6f7('0x14')](_0x4d3d59)){throw new Error('cmContact,\x20afterCreate\x20'+_0x5bdc9e[_0xa6f7('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0xd5558b['id']);}return _0x23bb05[_0xa6f7('0x15')][_0xa6f7('0x16')]({'phone':_0x5bdc9e[_0xa6f7('0xd')],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e['ListId'],'priority':_0x5bdc9e[_0xa6f7('0x17')]?_0x5bdc9e[_0xa6f7('0x17')]:0x2,'scheduledat':_0x5bdc9e[_0xa6f7('0x18')]?_0x5bdc9e[_0xa6f7('0x18')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x5bdc9e[_0xa6f7('0x19')]?!![]:![],'callbackuniqueid':_0x5bdc9e['callbackUniqueid']?_0x5bdc9e[_0xa6f7('0x19')]:undefined,'callbackat':_0x5bdc9e[_0xa6f7('0x19')]?moment()['format'](_0xa6f7('0x1a')):undefined,'VoiceQueueId':_0xd5558b['id']});})[_0xa6f7('0x1b')](function(_0x322e29){_0x3fc7f1(_0x322e29);});});};var _0x35891a=function(_0x1f9d20){return new BPromise(function(_0x1aa8dc,_0x4887ad){_0x23bb05[_0xa6f7('0x15')][_0xa6f7('0x12')]({'where':{'phone':_0x5bdc9e['phone'],'VoiceQueueId':_0x1f9d20['id']}})['then'](function(_0x5ddc8d){if(!_[_0xa6f7('0x14')](_0x5ddc8d)){throw new Error(_0xa6f7('0x1c')+_0x5bdc9e['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0x1f9d20['id']);}return _0x23bb05['CmHopperFinal'][_0xa6f7('0x12')]({'where':{'calleridnum':_0x5bdc9e[_0xa6f7('0xd')],'VoiceQueueId':_0x1f9d20['id']}});})[_0xa6f7('0x13')](function(_0x5baaa9){if(!_['isNil'](_0x5baaa9)){throw new Error(_0xa6f7('0x1d')+_0x5bdc9e['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20'+_0x1f9d20['id']);}return _0x23bb05[_0xa6f7('0x15')]['create']({'phone':_0x5bdc9e[_0xa6f7('0xd')],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e[_0xa6f7('0x1e')],'priority':_0x5bdc9e[_0xa6f7('0x17')]?_0x5bdc9e[_0xa6f7('0x17')]:0x2,'scheduledat':_0x5bdc9e[_0xa6f7('0x18')]?_0x5bdc9e['scheduledat']:moment()['format'](_0xa6f7('0x1a')),'callback':_0x5bdc9e[_0xa6f7('0x19')]?!![]:![],'callbackuniqueid':_0x5bdc9e['callbackUniqueid']?_0x5bdc9e[_0xa6f7('0x19')]:undefined,'callbackat':_0x5bdc9e[_0xa6f7('0x19')]?moment()[_0xa6f7('0x1f')](_0xa6f7('0x1a')):undefined,'VoiceQueueId':_0x1f9d20['id']});})[_0xa6f7('0x1b')](function(_0x10bf77){_0x4887ad(_0x10bf77);});});};function _0x353de8(){return function(_0x54f72c){logger[_0xa6f7('0xf')](_0xa6f7('0x20'),_0xa6f7('0x10'),util[_0xa6f7('0x11')](_0x54f72c,{'showHidden':![],'depth':null}));};}var _0x52f9c1=function(_0x442ccf){return new BPromise(function(_0x42dc49,_0x5bf8e3){_0x23bb05[_0xa6f7('0x15')][_0xa6f7('0x12')]({'where':{'phone':_0x5bdc9e['phone'],'CampaignId':_0x442ccf['id']}})['then'](function(_0x176378){if(!_[_0xa6f7('0x14')](_0x176378)){throw new Error(_0xa6f7('0x1c')+_0x5bdc9e[_0xa6f7('0xd')]+_0xa6f7('0x21')+_0x442ccf['id']);}return _0x23bb05['CmHopper']['create']({'phone':_0x5bdc9e[_0xa6f7('0xd')],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e['ListId'],'priority':_0x5bdc9e[_0xa6f7('0x17')]?_0x5bdc9e[_0xa6f7('0x17')]:0x2,'scheduledat':_0x5bdc9e[_0xa6f7('0x18')]?_0x5bdc9e[_0xa6f7('0x18')]:moment()[_0xa6f7('0x1f')](_0xa6f7('0x1a')),'callback':_0x5bdc9e['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5bdc9e[_0xa6f7('0x19')]?_0x5bdc9e[_0xa6f7('0x19')]:undefined,'callbackat':_0x5bdc9e[_0xa6f7('0x19')]?moment()['format'](_0xa6f7('0x1a')):undefined,'CampaignId':_0x442ccf['id']});})['catch'](function(_0x1170c4){_0x5bf8e3(_0x1170c4);});});};var _0x2f5632=function(_0x45837c){return new BPromise(function(_0x4ccbd3,_0x44a5f2){_0x23bb05[_0xa6f7('0x15')][_0xa6f7('0x12')]({'where':{'phone':_0x5bdc9e[_0xa6f7('0xd')],'CampaignId':_0x45837c['id']}})[_0xa6f7('0x13')](function(_0x28490e){if(!_['isNil'](_0x28490e)){throw new Error(_0xa6f7('0x1c')+_0x5bdc9e[_0xa6f7('0xd')]+_0xa6f7('0x22')+_0x45837c['id']);}return _0x23bb05[_0xa6f7('0x23')][_0xa6f7('0x12')]({'where':{'calleridnum':_0x5bdc9e[_0xa6f7('0xd')],'CampaignId':_0x45837c['id']}});})[_0xa6f7('0x13')](function(_0x12d966){if(!_['isNil'](_0x12d966)){throw new Error(_0xa6f7('0x1d')+_0x5bdc9e[_0xa6f7('0xd')]+_0xa6f7('0x24')+_0x45837c['id']);}return _0x23bb05['CmHopper'][_0xa6f7('0x16')]({'phone':_0x5bdc9e[_0xa6f7('0xd')],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e['ListId'],'priority':_0x5bdc9e[_0xa6f7('0x17')]?_0x5bdc9e[_0xa6f7('0x17')]:0x2,'scheduledat':_0x5bdc9e[_0xa6f7('0x18')]?_0x5bdc9e[_0xa6f7('0x18')]:moment()[_0xa6f7('0x1f')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x5bdc9e[_0xa6f7('0x19')]?!![]:![],'callbackuniqueid':_0x5bdc9e[_0xa6f7('0x19')]?_0x5bdc9e[_0xa6f7('0x19')]:undefined,'callbackat':_0x5bdc9e[_0xa6f7('0x19')]?moment()[_0xa6f7('0x1f')](_0xa6f7('0x1a')):undefined,'CampaignId':_0x45837c['id']});})[_0xa6f7('0x1b')](function(_0x3bd87c){_0x44a5f2(_0x3bd87c);});});};if(_0x5bdc9e['ListId']){_0x23bb05['VoiceQueue'][_0xa6f7('0x25')]({'where':{'type':_0xa6f7('0x26')},'attributes':['id',_0xa6f7('0x27')],'include':[{'model':_0x23bb05[_0xa6f7('0x28')],'as':_0xa6f7('0x29'),'where':{'id':_0x5bdc9e[_0xa6f7('0x1e')]}}]})[_0xa6f7('0x13')](function(_0x404552){if(_0x404552){var _0x40bd08=[];for(var _0x395ad7=0x0,_0x55baa7;_0x395ad7<_0x404552[_0xa6f7('0x2a')];_0x395ad7+=0x1){_0x55baa7=_0x404552[_0x395ad7][_0xa6f7('0xe')]({'plain':!![]});switch(_0x55baa7['dialCheckDuplicateType']){case _0xa6f7('0x2b'):_0x35891a(_0x55baa7)[_0xa6f7('0x1b')](_0x225da4());break;case _0xa6f7('0x2c'):_0x13425e(_0x55baa7)[_0xa6f7('0x1b')](_0x225da4());break;default:_0x23bb05['CmHopper'][_0xa6f7('0x16')]({'phone':_0x5bdc9e['phone'],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e['ListId'],'scheduledat':_0x5bdc9e[_0xa6f7('0x18')]?_0x5bdc9e[_0xa6f7('0x18')]:moment()[_0xa6f7('0x1f')](_0xa6f7('0x1a')),'priority':_0x5bdc9e[_0xa6f7('0x17')]?_0x5bdc9e[_0xa6f7('0x17')]:0x2,'callback':_0x5bdc9e[_0xa6f7('0x19')]?!![]:![],'callbackuniqueid':_0x5bdc9e['callbackUniqueid']?_0x5bdc9e[_0xa6f7('0x19')]:undefined,'callbackat':_0x5bdc9e[_0xa6f7('0x19')]?moment()['format'](_0xa6f7('0x1a')):undefined,'VoiceQueueId':_0x55baa7['id']})[_0xa6f7('0x1b')](_0x225da4());}}}})[_0xa6f7('0x13')](function(){return _0x23bb05['VoiceQueue'][_0xa6f7('0x25')]({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0x23bb05[_0xa6f7('0x28')],'as':_0xa6f7('0x2d'),'where':{'id':_0x5bdc9e[_0xa6f7('0x1e')]}}]})['then'](function(_0x5df564){if(_0x5df564){var _0x2b06b5=[];for(var _0x5928e4=0x0,_0x472e39;_0x5928e4<_0x5df564['length'];_0x5928e4+=0x1){_0x472e39=_0x5df564[_0x5928e4]['get']({'plain':!![]});_0x23bb05[_0xa6f7('0x2e')][_0xa6f7('0x16')]({'phone':_0x5bdc9e[_0xa6f7('0xd')],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e[_0xa6f7('0x1e')],'VoiceQueueId':_0x472e39['id']})[_0xa6f7('0x1b')](function(_0x3bd4f6){logger['error'](_0xa6f7('0x2f'),_0xa6f7('0x10'),_0x3bd4f6['message']);});}}});})[_0xa6f7('0x13')](function(){return _0x23bb05['Campaign'][_0xa6f7('0x25')]({'attributes':['id','dialCheckDuplicateType'],'include':[{'model':_0x23bb05['CmList'],'as':_0xa6f7('0x29'),'where':{'id':_0x5bdc9e[_0xa6f7('0x1e')]}}]})[_0xa6f7('0x13')](function(_0x2b3de2){if(_0x2b3de2){var _0x16d6b1=[];for(var _0x50d9a9=0x0,_0x340d3f;_0x50d9a9<_0x2b3de2[_0xa6f7('0x2a')];_0x50d9a9+=0x1){_0x340d3f=_0x2b3de2[_0x50d9a9]['get']({'plain':!![]});switch(_0x340d3f[_0xa6f7('0x27')]){case'always':_0x2f5632(_0x340d3f)[_0xa6f7('0x1b')](_0x353de8());break;case _0xa6f7('0x2c'):_0x52f9c1(_0x340d3f)[_0xa6f7('0x1b')](_0x353de8());break;default:_0x23bb05['CmHopper'][_0xa6f7('0x16')]({'phone':_0x5bdc9e['phone'],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e[_0xa6f7('0x1e')],'scheduledat':_0x5bdc9e[_0xa6f7('0x18')]?_0x5bdc9e[_0xa6f7('0x18')]:moment()[_0xa6f7('0x1f')](_0xa6f7('0x1a')),'priority':_0x5bdc9e['priority']?_0x5bdc9e['priority']:0x2,'callback':_0x5bdc9e[_0xa6f7('0x19')]?!![]:![],'callbackuniqueid':_0x5bdc9e[_0xa6f7('0x19')]?_0x5bdc9e[_0xa6f7('0x19')]:undefined,'callbackat':_0x5bdc9e[_0xa6f7('0x19')]?moment()['format'](_0xa6f7('0x1a')):undefined,'CampaignId':_0x340d3f['id']})[_0xa6f7('0x1b')](_0x353de8());}}}});})[_0xa6f7('0x13')](function(){return _0x23bb05[_0xa6f7('0x30')][_0xa6f7('0x25')]({'attributes':['id'],'include':[{'model':_0x23bb05[_0xa6f7('0x28')],'as':'BlackLists','where':{'id':_0x5bdc9e[_0xa6f7('0x1e')]}}]})[_0xa6f7('0x13')](function(_0x454252){if(_0x454252){var _0x5eac88=[];for(var _0xb049d7=0x0,_0x16a2a4;_0xb049d7<_0x454252[_0xa6f7('0x2a')];_0xb049d7+=0x1){_0x16a2a4=_0x454252[_0xb049d7][_0xa6f7('0xe')]({'plain':!![]});_0x23bb05['CmHopperBlack']['create']({'phone':_0x5bdc9e[_0xa6f7('0xd')],'ContactId':_0x5bdc9e['id'],'ListId':_0x5bdc9e[_0xa6f7('0x1e')],'CampaignId':_0x16a2a4['id']})[_0xa6f7('0x1b')](function(_0x52e647){logger[_0xa6f7('0xf')](_0xa6f7('0x2f'),_0xa6f7('0x10'),_0x52e647['message']);});}}});})[_0xa6f7('0x13')](function(){_0x362404();})[_0xa6f7('0x1b')](function(_0x2f131c){logger[_0xa6f7('0xf')](_0xa6f7('0x2f'),_0xa6f7('0x10'),_0x2f131c[_0xa6f7('0x31')]);_0x362404(_0x2f131c);});}else{_0x362404(new Error(_0xa6f7('0x32')));}},'afterDestroy':function(_0x444177,_0x186956,_0x3ade42){var _0x5719fb=_0x20cccd[_0xa6f7('0x33')];var _0x55c5ce=_0x444177[_0xa6f7('0xe')]({'plain':!![]});return _0x5719fb['CmHopper'][_0xa6f7('0x25')]({'raw':!![],'where':{'ListId':_0x55c5ce[_0xa6f7('0x1e')],'ContactId':_0x55c5ce['id']}})['then'](function(_0x288618){if(!_[_0xa6f7('0x34')](_0x288618)){var _0x13dd9b=[];for(var _0x501503=0x0;_0x501503<_0x288618[_0xa6f7('0x2a')];_0x501503+=0x1){var _0x59f12b=_0x5719fb[_0xa6f7('0x15')][_0xa6f7('0x35')]({'where':{'ListId':_0x288618[_0x501503]['ListId'],'ContactId':_0x288618[_0x501503][_0xa6f7('0x36')],'VoiceQueueId':_0x288618[_0x501503]['VoiceQueueId'],'CampaignId':_0x288618[_0x501503][_0xa6f7('0x37')]}});_0x13dd9b['push'](_0x59f12b);}return BPromise[_0xa6f7('0x38')](_0x13dd9b);}})[_0xa6f7('0x13')](function(_0x2bf525){logger[_0xa6f7('0x39')](_0xa6f7('0x3a'),util[_0xa6f7('0x11')](_0x2bf525,{'showHidden':![],'depth':null}));_0x3ade42();})[_0xa6f7('0x1b')](function(_0x2ebb08){logger[_0xa6f7('0xf')](_0xa6f7('0x3b'),util[_0xa6f7('0x11')](_0x2ebb08,{'showHidden':![],'depth':null}));_0x3ade42(_0x2ebb08);});}}});};