Built motion from commit (unavailable).|2.4.4
[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 _0xf3e1=['models','get','error','cmContact','afterCreate','inspect','CmHopper','then','cmContact,\x20afterCreate\x20','create','ListId','priority','scheduledat','format','callbackUniqueid','YYYY-MM-DD\x20HH:mm:ss','catch','findOne','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','isNil','\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','always','onlyIfOpen','VoiceQueue','BlackLists','length','message','Campaign','CmHopperBlack','cmContact,\x20%s,\x20%s','List\x20not\x20found','isEmpty','ContactId','VoiceQueueId','CampaignId','push','all','info','[CmContact][afterDestroy][Error]','lodash','../../config/logger','api','moment','../../config/environment','./cmContact.attributes','exports','define','CmContact','cm_contacts','phone'];(function(_0x29ad21,_0x54c47c){var _0x5e60f3=function(_0x20be3d){while(--_0x20be3d){_0x29ad21['push'](_0x29ad21['shift']());}};_0x5e60f3(++_0x54c47c);}(_0xf3e1,0xa9));var _0x1f3e=function(_0x172ed7,_0x3d08fd){_0x172ed7=_0x172ed7-0x0;var _0xfa8f61=_0xf3e1[_0x172ed7];return _0xfa8f61;};'use strict';var _=require(_0x1f3e('0x0'));var util=require('util');var logger=require(_0x1f3e('0x1'))(_0x1f3e('0x2'));var moment=require(_0x1f3e('0x3'));var BPromise=require('bluebird');var rp=require('request-promise');var fs=require('fs');var path=require('path');var rimraf=require('rimraf');var config=require(_0x1f3e('0x4'));var attributes=require(_0x1f3e('0x5'));module[_0x1f3e('0x6')]=function(_0x41c1ca,_0x1dd5f8){return _0x41c1ca[_0x1f3e('0x7')](_0x1f3e('0x8'),attributes,{'tableName':_0x1f3e('0x9'),'paranoid':!![],'indexes':[{'name':_0x1f3e('0xa'),'fields':[_0x1f3e('0xa')]}],'timestamps':!![],'hooks':{'afterCreate':function(_0x46bc3a,_0x70a939,_0x563660){var _0x3ed134=_0x41c1ca[_0x1f3e('0xb')];var _0x2e6ec3=_0x46bc3a[_0x1f3e('0xc')]({'plain':!![]});function _0x28c72c(){return function(_0x1d1f7e){logger[_0x1f3e('0xd')](_0x1f3e('0xe'),_0x1f3e('0xf'),util[_0x1f3e('0x10')](_0x1d1f7e,{'showHidden':![],'depth':null}));};}var _0x123cba=function(_0x11c55e){return new BPromise(function(_0x6f66b5,_0x4cf8d0){_0x3ed134[_0x1f3e('0x11')]['findOne']({'where':{'phone':_0x2e6ec3['phone'],'VoiceQueueId':_0x11c55e['id']}})[_0x1f3e('0x12')](function(_0x36c05c){if(!_['isNil'](_0x36c05c)){throw new Error(_0x1f3e('0x13')+_0x2e6ec3[_0x1f3e('0xa')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0x11c55e['id']);}return _0x3ed134[_0x1f3e('0x11')][_0x1f3e('0x14')]({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'priority':_0x2e6ec3['priority']?_0x2e6ec3[_0x1f3e('0x16')]:0x2,'scheduledat':_0x2e6ec3['scheduledat']?_0x2e6ec3[_0x1f3e('0x17')]:moment()[_0x1f3e('0x18')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x2e6ec3[_0x1f3e('0x19')]?!![]:![],'callbackuniqueid':_0x2e6ec3['callbackUniqueid']?_0x2e6ec3[_0x1f3e('0x19')]:undefined,'callbackat':_0x2e6ec3[_0x1f3e('0x19')]?moment()[_0x1f3e('0x18')](_0x1f3e('0x1a')):undefined,'VoiceQueueId':_0x11c55e['id']});})[_0x1f3e('0x1b')](function(_0x27d5a8){_0x4cf8d0(_0x27d5a8);});});};var _0x32fe8e=function(_0x4c30d0){return new BPromise(function(_0x29cf2b,_0x527fdd){_0x3ed134[_0x1f3e('0x11')][_0x1f3e('0x1c')]({'where':{'phone':_0x2e6ec3[_0x1f3e('0xa')],'VoiceQueueId':_0x4c30d0['id']}})[_0x1f3e('0x12')](function(_0x42ebc9){if(!_['isNil'](_0x42ebc9)){throw new Error(_0x1f3e('0x13')+_0x2e6ec3[_0x1f3e('0xa')]+_0x1f3e('0x1d')+_0x4c30d0['id']);}return _0x3ed134['CmHopperFinal'][_0x1f3e('0x1c')]({'where':{'calleridnum':_0x2e6ec3['phone'],'VoiceQueueId':_0x4c30d0['id']}});})['then'](function(_0x2bbb70){if(!_['isNil'](_0x2bbb70)){throw new Error(_0x1f3e('0x1e')+_0x2e6ec3[_0x1f3e('0xa')]+_0x1f3e('0x1f')+_0x4c30d0['id']);}return _0x3ed134[_0x1f3e('0x11')][_0x1f3e('0x14')]({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'priority':_0x2e6ec3['priority']?_0x2e6ec3[_0x1f3e('0x16')]:0x2,'scheduledat':_0x2e6ec3[_0x1f3e('0x17')]?_0x2e6ec3['scheduledat']:moment()[_0x1f3e('0x18')](_0x1f3e('0x1a')),'callback':_0x2e6ec3[_0x1f3e('0x19')]?!![]:![],'callbackuniqueid':_0x2e6ec3[_0x1f3e('0x19')]?_0x2e6ec3['callbackUniqueid']:undefined,'callbackat':_0x2e6ec3[_0x1f3e('0x19')]?moment()[_0x1f3e('0x18')](_0x1f3e('0x1a')):undefined,'VoiceQueueId':_0x4c30d0['id']});})[_0x1f3e('0x1b')](function(_0x5e7a98){_0x527fdd(_0x5e7a98);});});};function _0x4018ba(){return function(_0x24a999){logger[_0x1f3e('0xd')](_0x1f3e('0xe'),_0x1f3e('0xf'),util[_0x1f3e('0x10')](_0x24a999,{'showHidden':![],'depth':null}));};}var _0x4aa82d=function(_0x21a974){return new BPromise(function(_0x5880b0,_0x2850e2){_0x3ed134[_0x1f3e('0x11')][_0x1f3e('0x1c')]({'where':{'phone':_0x2e6ec3[_0x1f3e('0xa')],'CampaignId':_0x21a974['id']}})[_0x1f3e('0x12')](function(_0x595250){if(!_[_0x1f3e('0x20')](_0x595250)){throw new Error('cmContact,\x20afterCreate\x20'+_0x2e6ec3[_0x1f3e('0xa')]+_0x1f3e('0x21')+_0x21a974['id']);}return _0x3ed134[_0x1f3e('0x11')]['create']({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'priority':_0x2e6ec3[_0x1f3e('0x16')]?_0x2e6ec3[_0x1f3e('0x16')]:0x2,'scheduledat':_0x2e6ec3[_0x1f3e('0x17')]?_0x2e6ec3[_0x1f3e('0x17')]:moment()['format'](_0x1f3e('0x1a')),'callback':_0x2e6ec3[_0x1f3e('0x19')]?!![]:![],'callbackuniqueid':_0x2e6ec3[_0x1f3e('0x19')]?_0x2e6ec3['callbackUniqueid']:undefined,'callbackat':_0x2e6ec3[_0x1f3e('0x19')]?moment()[_0x1f3e('0x18')](_0x1f3e('0x1a')):undefined,'CampaignId':_0x21a974['id']});})[_0x1f3e('0x1b')](function(_0x290787){_0x2850e2(_0x290787);});});};var _0x2dd3b=function(_0x227785){return new BPromise(function(_0x2e6baf,_0x3ba547){_0x3ed134['CmHopper'][_0x1f3e('0x1c')]({'where':{'phone':_0x2e6ec3['phone'],'CampaignId':_0x227785['id']}})[_0x1f3e('0x12')](function(_0x594079){if(!_[_0x1f3e('0x20')](_0x594079)){throw new Error(_0x1f3e('0x13')+_0x2e6ec3['phone']+_0x1f3e('0x22')+_0x227785['id']);}return _0x3ed134[_0x1f3e('0x23')][_0x1f3e('0x1c')]({'where':{'calleridnum':_0x2e6ec3[_0x1f3e('0xa')],'CampaignId':_0x227785['id']}});})[_0x1f3e('0x12')](function(_0x3728a3){if(!_[_0x1f3e('0x20')](_0x3728a3)){throw new Error(_0x1f3e('0x1e')+_0x2e6ec3['phone']+_0x1f3e('0x24')+_0x227785['id']);}return _0x3ed134[_0x1f3e('0x11')]['create']({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3['ListId'],'priority':_0x2e6ec3[_0x1f3e('0x16')]?_0x2e6ec3[_0x1f3e('0x16')]:0x2,'scheduledat':_0x2e6ec3[_0x1f3e('0x17')]?_0x2e6ec3[_0x1f3e('0x17')]:moment()[_0x1f3e('0x18')](_0x1f3e('0x1a')),'callback':_0x2e6ec3[_0x1f3e('0x19')]?!![]:![],'callbackuniqueid':_0x2e6ec3[_0x1f3e('0x19')]?_0x2e6ec3['callbackUniqueid']:undefined,'callbackat':_0x2e6ec3[_0x1f3e('0x19')]?moment()['format'](_0x1f3e('0x1a')):undefined,'CampaignId':_0x227785['id']});})[_0x1f3e('0x1b')](function(_0x32790b){_0x3ba547(_0x32790b);});});};if(_0x2e6ec3[_0x1f3e('0x15')]){_0x3ed134['VoiceQueue'][_0x1f3e('0x25')]({'where':{'type':_0x1f3e('0x26')},'attributes':['id',_0x1f3e('0x27')],'include':[{'model':_0x3ed134[_0x1f3e('0x28')],'as':_0x1f3e('0x29'),'where':{'id':_0x2e6ec3[_0x1f3e('0x15')]}}]})[_0x1f3e('0x12')](function(_0x230450){if(_0x230450){var _0x34711c=[];for(var _0x1d16a2=0x0,_0xfa08e7;_0x1d16a2<_0x230450['length'];_0x1d16a2+=0x1){_0xfa08e7=_0x230450[_0x1d16a2]['get']({'plain':!![]});switch(_0xfa08e7[_0x1f3e('0x27')]){case _0x1f3e('0x2a'):_0x32fe8e(_0xfa08e7)[_0x1f3e('0x1b')](_0x28c72c());break;case _0x1f3e('0x2b'):_0x123cba(_0xfa08e7)[_0x1f3e('0x1b')](_0x28c72c());break;default:_0x3ed134[_0x1f3e('0x11')][_0x1f3e('0x14')]({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'scheduledat':_0x2e6ec3['scheduledat']?_0x2e6ec3[_0x1f3e('0x17')]:moment()['format'](_0x1f3e('0x1a')),'priority':_0x2e6ec3['priority']?_0x2e6ec3['priority']:0x2,'callback':_0x2e6ec3[_0x1f3e('0x19')]?!![]:![],'callbackuniqueid':_0x2e6ec3['callbackUniqueid']?_0x2e6ec3[_0x1f3e('0x19')]:undefined,'callbackat':_0x2e6ec3['callbackUniqueid']?moment()[_0x1f3e('0x18')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0xfa08e7['id']})['catch'](_0x28c72c());}}}})[_0x1f3e('0x12')](function(){return _0x3ed134[_0x1f3e('0x2c')][_0x1f3e('0x25')]({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0x3ed134['CmList'],'as':_0x1f3e('0x2d'),'where':{'id':_0x2e6ec3['ListId']}}]})['then'](function(_0x3188e7){if(_0x3188e7){var _0x209fdc=[];for(var _0x4b2fc9=0x0,_0x24916c;_0x4b2fc9<_0x3188e7[_0x1f3e('0x2e')];_0x4b2fc9+=0x1){_0x24916c=_0x3188e7[_0x4b2fc9][_0x1f3e('0xc')]({'plain':!![]});_0x3ed134['CmHopperBlack'][_0x1f3e('0x14')]({'phone':_0x2e6ec3['phone'],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'VoiceQueueId':_0x24916c['id']})['catch'](function(_0x52adb5){logger[_0x1f3e('0xd')]('cmContact,\x20%s,\x20%s',_0x1f3e('0xf'),_0x52adb5[_0x1f3e('0x2f')]);});}}});})[_0x1f3e('0x12')](function(){return _0x3ed134['Campaign']['findAll']({'attributes':['id',_0x1f3e('0x27')],'include':[{'model':_0x3ed134[_0x1f3e('0x28')],'as':'Lists','where':{'id':_0x2e6ec3['ListId']}}]})['then'](function(_0x5c7bed){if(_0x5c7bed){var _0x200718=[];for(var _0x30af74=0x0,_0x2cac7b;_0x30af74<_0x5c7bed[_0x1f3e('0x2e')];_0x30af74+=0x1){_0x2cac7b=_0x5c7bed[_0x30af74][_0x1f3e('0xc')]({'plain':!![]});switch(_0x2cac7b[_0x1f3e('0x27')]){case'always':_0x2dd3b(_0x2cac7b)[_0x1f3e('0x1b')](_0x4018ba());break;case _0x1f3e('0x2b'):_0x4aa82d(_0x2cac7b)[_0x1f3e('0x1b')](_0x4018ba());break;default:_0x3ed134[_0x1f3e('0x11')]['create']({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'scheduledat':_0x2e6ec3['scheduledat']?_0x2e6ec3[_0x1f3e('0x17')]:moment()[_0x1f3e('0x18')](_0x1f3e('0x1a')),'priority':_0x2e6ec3[_0x1f3e('0x16')]?_0x2e6ec3[_0x1f3e('0x16')]:0x2,'callback':_0x2e6ec3[_0x1f3e('0x19')]?!![]:![],'callbackuniqueid':_0x2e6ec3[_0x1f3e('0x19')]?_0x2e6ec3[_0x1f3e('0x19')]:undefined,'callbackat':_0x2e6ec3[_0x1f3e('0x19')]?moment()['format']('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x2cac7b['id']})[_0x1f3e('0x1b')](_0x4018ba());}}}});})[_0x1f3e('0x12')](function(){return _0x3ed134[_0x1f3e('0x30')]['findAll']({'attributes':['id'],'include':[{'model':_0x3ed134[_0x1f3e('0x28')],'as':_0x1f3e('0x2d'),'where':{'id':_0x2e6ec3[_0x1f3e('0x15')]}}]})['then'](function(_0x54e897){if(_0x54e897){var _0x53cfe5=[];for(var _0x51e831=0x0,_0x1bdf02;_0x51e831<_0x54e897[_0x1f3e('0x2e')];_0x51e831+=0x1){_0x1bdf02=_0x54e897[_0x51e831][_0x1f3e('0xc')]({'plain':!![]});_0x3ed134[_0x1f3e('0x31')][_0x1f3e('0x14')]({'phone':_0x2e6ec3[_0x1f3e('0xa')],'ContactId':_0x2e6ec3['id'],'ListId':_0x2e6ec3[_0x1f3e('0x15')],'CampaignId':_0x1bdf02['id']})[_0x1f3e('0x1b')](function(_0x35c27d){logger[_0x1f3e('0xd')]('cmContact,\x20%s,\x20%s',_0x1f3e('0xf'),_0x35c27d[_0x1f3e('0x2f')]);});}}});})[_0x1f3e('0x12')](function(){_0x563660();})[_0x1f3e('0x1b')](function(_0x173f14){logger[_0x1f3e('0xd')](_0x1f3e('0x32'),_0x1f3e('0xf'),_0x173f14[_0x1f3e('0x2f')]);_0x563660(_0x173f14);});}else{_0x563660(new Error(_0x1f3e('0x33')));}},'afterDestroy':function(_0x298e60,_0x52c0de,_0x271ae9){var _0x4e6f47=_0x41c1ca['models'];var _0x2ba4b8=_0x298e60[_0x1f3e('0xc')]({'plain':!![]});return _0x4e6f47[_0x1f3e('0x11')][_0x1f3e('0x25')]({'raw':!![],'where':{'ListId':_0x2ba4b8[_0x1f3e('0x15')],'ContactId':_0x2ba4b8['id']}})['then'](function(_0x24a90e){if(!_[_0x1f3e('0x34')](_0x24a90e)){var _0x4fe8ca=[];for(var _0x4c9056=0x0;_0x4c9056<_0x24a90e['length'];_0x4c9056+=0x1){var _0x474b26=_0x4e6f47[_0x1f3e('0x11')]['destroy']({'where':{'ListId':_0x24a90e[_0x4c9056][_0x1f3e('0x15')],'ContactId':_0x24a90e[_0x4c9056][_0x1f3e('0x35')],'VoiceQueueId':_0x24a90e[_0x4c9056][_0x1f3e('0x36')],'CampaignId':_0x24a90e[_0x4c9056][_0x1f3e('0x37')]}});_0x4fe8ca[_0x1f3e('0x38')](_0x474b26);}return BPromise[_0x1f3e('0x39')](_0x4fe8ca);}})[_0x1f3e('0x12')](function(_0x1a1b8e){logger[_0x1f3e('0x3a')]('[CmContact][afterDestroy][Succesfull]',util['inspect'](_0x1a1b8e,{'showHidden':![],'depth':null}));_0x271ae9();})[_0x1f3e('0x1b')](function(_0x61fc94){logger['error'](_0x1f3e('0x3b'),util['inspect'](_0x61fc94,{'showHidden':![],'depth':null}));_0x271ae9(_0x61fc94);});}}});};