1babecd60ff3671d48f6c5fdf3c23ca09f961644
[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 _0x3d4b=['phone','lastName','mobile','fax','FULLTEXT','models','get','cmContact','afterCreate','inspect','CmHopper','findOne','then','cmContact,\x20afterCreate\x20','\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','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','isNil','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','error','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','catch','VoiceQueue','findAll','dialCheckDuplicateType','Lists','length','always','onlyIfOpen','outbound','CmList','BlackLists','message','CmHopperBlack','cmContact,\x20%s,\x20%s','isEmpty','destroy','ContactId','VoiceQueueId','CampaignId','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','api','moment','bluebird','path','rimraf','../../config/environment','./cmContact.attributes','exports','define','CmContact','cm_contacts'];(function(_0x6b25ac,_0xe9fa01){var _0x4c84e9=function(_0x2bf6ec){while(--_0x2bf6ec){_0x6b25ac['push'](_0x6b25ac['shift']());}};_0x4c84e9(++_0xe9fa01);}(_0x3d4b,0xf3));var _0xb3d4=function(_0x2a6ea8,_0x150a79){_0x2a6ea8=_0x2a6ea8-0x0;var _0x4ca660=_0x3d4b[_0x2a6ea8];return _0x4ca660;};'use strict';var _=require(_0xb3d4('0x0'));var util=require(_0xb3d4('0x1'));var logger=require('../../config/logger')(_0xb3d4('0x2'));var moment=require(_0xb3d4('0x3'));var BPromise=require(_0xb3d4('0x4'));var rp=require('request-promise');var fs=require('fs');var path=require(_0xb3d4('0x5'));var rimraf=require(_0xb3d4('0x6'));var config=require(_0xb3d4('0x7'));var attributes=require(_0xb3d4('0x8'));module[_0xb3d4('0x9')]=function(_0x1a7434,_0x22525d){return _0x1a7434[_0xb3d4('0xa')](_0xb3d4('0xb'),attributes,{'tableName':_0xb3d4('0xc'),'paranoid':!![],'indexes':[{'name':_0xb3d4('0xd'),'fields':[_0xb3d4('0xd')]},{'name':'fti_cm_contacts','fields':['firstName',_0xb3d4('0xe'),_0xb3d4('0xd'),_0xb3d4('0xf'),_0xb3d4('0x10'),'email'],'type':_0xb3d4('0x11')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x11bb63,_0x318362,_0x2550c5){var _0x2fd962=_0x1a7434[_0xb3d4('0x12')];var _0x315847=_0x11bb63[_0xb3d4('0x13')]({'plain':!![]});function _0x199f95(){return function(_0x49c596){logger['error'](_0xb3d4('0x14'),_0xb3d4('0x15'),util[_0xb3d4('0x16')](_0x49c596,{'showHidden':![],'depth':null}));};}var _0x59c9bf=function(_0x30bc93){return new BPromise(function(_0x445ee2,_0x4f2c22){_0x2fd962[_0xb3d4('0x17')][_0xb3d4('0x18')]({'where':{'phone':_0x315847['phone'],'VoiceQueueId':_0x30bc93['id']}})[_0xb3d4('0x19')](function(_0x485f59){if(!_['isNil'](_0x485f59)){throw new Error(_0xb3d4('0x1a')+_0x315847['phone']+_0xb3d4('0x1b')+_0x30bc93['id']);}return _0x2fd962[_0xb3d4('0x17')][_0xb3d4('0x1c')]({'phone':_0x315847[_0xb3d4('0xd')],'ContactId':_0x315847['id'],'ListId':_0x315847[_0xb3d4('0x1d')],'priority':_0x315847['priority']?_0x315847[_0xb3d4('0x1e')]:0x2,'scheduledat':_0x315847[_0xb3d4('0x1f')]?_0x315847[_0xb3d4('0x1f')]:moment()[_0xb3d4('0x20')](_0xb3d4('0x21')),'callback':_0x315847[_0xb3d4('0x22')]?!![]:![],'callbackuniqueid':_0x315847[_0xb3d4('0x22')]?_0x315847['callbackUniqueid']:undefined,'callbackat':_0x315847[_0xb3d4('0x22')]?moment()[_0xb3d4('0x20')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x30bc93['id']});})['catch'](function(_0x5d62b9){_0x4f2c22(_0x5d62b9);});});};var _0x22eeb1=function(_0x42caac){return new BPromise(function(_0x7b8413,_0x17e6e0){_0x2fd962['CmHopper']['findOne']({'where':{'phone':_0x315847[_0xb3d4('0xd')],'VoiceQueueId':_0x42caac['id']}})['then'](function(_0x1abfd4){if(!_['isNil'](_0x1abfd4)){throw new Error('cmContact,\x20afterCreate\x20'+_0x315847[_0xb3d4('0xd')]+_0xb3d4('0x23')+_0x42caac['id']);}return _0x2fd962[_0xb3d4('0x24')]['findOne']({'where':{'calleridnum':_0x315847[_0xb3d4('0xd')],'VoiceQueueId':_0x42caac['id']}});})['then'](function(_0x56fa52){if(!_[_0xb3d4('0x25')](_0x56fa52)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x315847[_0xb3d4('0xd')]+_0xb3d4('0x26')+_0x42caac['id']);}return _0x2fd962[_0xb3d4('0x17')]['create']({'phone':_0x315847['phone'],'ContactId':_0x315847['id'],'ListId':_0x315847[_0xb3d4('0x1d')],'priority':_0x315847[_0xb3d4('0x1e')]?_0x315847[_0xb3d4('0x1e')]:0x2,'scheduledat':_0x315847['scheduledat']?_0x315847['scheduledat']:moment()['format'](_0xb3d4('0x21')),'callback':_0x315847[_0xb3d4('0x22')]?!![]:![],'callbackuniqueid':_0x315847[_0xb3d4('0x22')]?_0x315847['callbackUniqueid']:undefined,'callbackat':_0x315847[_0xb3d4('0x22')]?moment()[_0xb3d4('0x20')](_0xb3d4('0x21')):undefined,'VoiceQueueId':_0x42caac['id']});})['catch'](function(_0x447200){_0x17e6e0(_0x447200);});});};function _0x98a5ff(){return function(_0x59d504){logger[_0xb3d4('0x27')]('cmContact','afterCreate',util[_0xb3d4('0x16')](_0x59d504,{'showHidden':![],'depth':null}));};}var _0x28eede=function(_0x2cd469){return new BPromise(function(_0x3204f1,_0x34fc98){_0x2fd962[_0xb3d4('0x17')][_0xb3d4('0x18')]({'where':{'phone':_0x315847[_0xb3d4('0xd')],'CampaignId':_0x2cd469['id']}})[_0xb3d4('0x19')](function(_0x5e256a){if(!_[_0xb3d4('0x25')](_0x5e256a)){throw new Error(_0xb3d4('0x1a')+_0x315847[_0xb3d4('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x2cd469['id']);}return _0x2fd962[_0xb3d4('0x17')]['create']({'phone':_0x315847['phone'],'ContactId':_0x315847['id'],'ListId':_0x315847['ListId'],'priority':_0x315847[_0xb3d4('0x1e')]?_0x315847[_0xb3d4('0x1e')]:0x2,'scheduledat':_0x315847['scheduledat']?_0x315847[_0xb3d4('0x1f')]:moment()['format'](_0xb3d4('0x21')),'callback':_0x315847[_0xb3d4('0x22')]?!![]:![],'callbackuniqueid':_0x315847['callbackUniqueid']?_0x315847[_0xb3d4('0x22')]:undefined,'callbackat':_0x315847[_0xb3d4('0x22')]?moment()[_0xb3d4('0x20')](_0xb3d4('0x21')):undefined,'CampaignId':_0x2cd469['id']});})['catch'](function(_0x2e3800){_0x34fc98(_0x2e3800);});});};var _0xa166d=function(_0x3ee718){return new BPromise(function(_0x96e271,_0x2c5333){_0x2fd962[_0xb3d4('0x17')][_0xb3d4('0x18')]({'where':{'phone':_0x315847['phone'],'CampaignId':_0x3ee718['id']}})['then'](function(_0xb9cc25){if(!_['isNil'](_0xb9cc25)){throw new Error(_0xb3d4('0x1a')+_0x315847[_0xb3d4('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x3ee718['id']);}return _0x2fd962[_0xb3d4('0x24')][_0xb3d4('0x18')]({'where':{'calleridnum':_0x315847['phone'],'CampaignId':_0x3ee718['id']}});})[_0xb3d4('0x19')](function(_0x3cded0){if(!_[_0xb3d4('0x25')](_0x3cded0)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x315847[_0xb3d4('0xd')]+_0xb3d4('0x28')+_0x3ee718['id']);}return _0x2fd962[_0xb3d4('0x17')]['create']({'phone':_0x315847[_0xb3d4('0xd')],'ContactId':_0x315847['id'],'ListId':_0x315847['ListId'],'priority':_0x315847[_0xb3d4('0x1e')]?_0x315847[_0xb3d4('0x1e')]:0x2,'scheduledat':_0x315847[_0xb3d4('0x1f')]?_0x315847[_0xb3d4('0x1f')]:moment()[_0xb3d4('0x20')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x315847[_0xb3d4('0x22')]?!![]:![],'callbackuniqueid':_0x315847[_0xb3d4('0x22')]?_0x315847[_0xb3d4('0x22')]:undefined,'callbackat':_0x315847['callbackUniqueid']?moment()['format'](_0xb3d4('0x21')):undefined,'CampaignId':_0x3ee718['id']});})[_0xb3d4('0x29')](function(_0xe02641){_0x2c5333(_0xe02641);});});};if(_0x315847[_0xb3d4('0x1d')]){_0x2fd962[_0xb3d4('0x2a')][_0xb3d4('0x2b')]({'where':{'type':'outbound'},'attributes':['id',_0xb3d4('0x2c')],'include':[{'model':_0x2fd962['CmList'],'as':_0xb3d4('0x2d'),'where':{'id':_0x315847[_0xb3d4('0x1d')]}}]})[_0xb3d4('0x19')](function(_0x3ad97e){if(_0x3ad97e){var _0xeede50=[];for(var _0xb81e85=0x0,_0x11b2d2;_0xb81e85<_0x3ad97e[_0xb3d4('0x2e')];_0xb81e85+=0x1){_0x11b2d2=_0x3ad97e[_0xb81e85][_0xb3d4('0x13')]({'plain':!![]});switch(_0x11b2d2[_0xb3d4('0x2c')]){case _0xb3d4('0x2f'):_0x22eeb1(_0x11b2d2)[_0xb3d4('0x29')](_0x199f95());break;case _0xb3d4('0x30'):_0x59c9bf(_0x11b2d2)[_0xb3d4('0x29')](_0x199f95());break;default:_0x2fd962[_0xb3d4('0x17')]['create']({'phone':_0x315847['phone'],'ContactId':_0x315847['id'],'ListId':_0x315847['ListId'],'scheduledat':_0x315847[_0xb3d4('0x1f')]?_0x315847[_0xb3d4('0x1f')]:moment()[_0xb3d4('0x20')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x315847[_0xb3d4('0x1e')]?_0x315847[_0xb3d4('0x1e')]:0x2,'callback':_0x315847['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x315847[_0xb3d4('0x22')]?_0x315847['callbackUniqueid']:undefined,'callbackat':_0x315847[_0xb3d4('0x22')]?moment()[_0xb3d4('0x20')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x11b2d2['id']})[_0xb3d4('0x29')](_0x199f95());}}}})[_0xb3d4('0x19')](function(){return _0x2fd962[_0xb3d4('0x2a')]['findAll']({'where':{'type':_0xb3d4('0x31')},'attributes':['id'],'include':[{'model':_0x2fd962[_0xb3d4('0x32')],'as':_0xb3d4('0x33'),'where':{'id':_0x315847[_0xb3d4('0x1d')]}}]})[_0xb3d4('0x19')](function(_0x2851c5){if(_0x2851c5){var _0x2eab8e=[];for(var _0x899ea6=0x0,_0x371613;_0x899ea6<_0x2851c5['length'];_0x899ea6+=0x1){_0x371613=_0x2851c5[_0x899ea6]['get']({'plain':!![]});_0x2fd962['CmHopperBlack'][_0xb3d4('0x1c')]({'phone':_0x315847[_0xb3d4('0xd')],'ContactId':_0x315847['id'],'ListId':_0x315847['ListId'],'VoiceQueueId':_0x371613['id']})['catch'](function(_0x3834eb){logger[_0xb3d4('0x27')]('cmContact,\x20%s,\x20%s','afterCreate',_0x3834eb[_0xb3d4('0x34')]);});}}});})[_0xb3d4('0x19')](function(){return _0x2fd962['Campaign']['findAll']({'attributes':['id','dialCheckDuplicateType'],'include':[{'model':_0x2fd962[_0xb3d4('0x32')],'as':_0xb3d4('0x2d'),'where':{'id':_0x315847['ListId']}}]})[_0xb3d4('0x19')](function(_0x119ae6){if(_0x119ae6){var _0x5e0eb2=[];for(var _0x277486=0x0,_0x2742f2;_0x277486<_0x119ae6[_0xb3d4('0x2e')];_0x277486+=0x1){_0x2742f2=_0x119ae6[_0x277486][_0xb3d4('0x13')]({'plain':!![]});switch(_0x2742f2[_0xb3d4('0x2c')]){case _0xb3d4('0x2f'):_0xa166d(_0x2742f2)[_0xb3d4('0x29')](_0x98a5ff());break;case'onlyIfOpen':_0x28eede(_0x2742f2)[_0xb3d4('0x29')](_0x98a5ff());break;default:_0x2fd962['CmHopper']['create']({'phone':_0x315847[_0xb3d4('0xd')],'ContactId':_0x315847['id'],'ListId':_0x315847['ListId'],'scheduledat':_0x315847[_0xb3d4('0x1f')]?_0x315847[_0xb3d4('0x1f')]:moment()[_0xb3d4('0x20')](_0xb3d4('0x21')),'priority':_0x315847[_0xb3d4('0x1e')]?_0x315847['priority']:0x2,'callback':_0x315847[_0xb3d4('0x22')]?!![]:![],'callbackuniqueid':_0x315847[_0xb3d4('0x22')]?_0x315847[_0xb3d4('0x22')]:undefined,'callbackat':_0x315847[_0xb3d4('0x22')]?moment()['format'](_0xb3d4('0x21')):undefined,'CampaignId':_0x2742f2['id']})[_0xb3d4('0x29')](_0x98a5ff());}}}});})[_0xb3d4('0x19')](function(){return _0x2fd962['Campaign']['findAll']({'attributes':['id'],'include':[{'model':_0x2fd962[_0xb3d4('0x32')],'as':'BlackLists','where':{'id':_0x315847[_0xb3d4('0x1d')]}}]})[_0xb3d4('0x19')](function(_0x3739c4){if(_0x3739c4){var _0x1a2447=[];for(var _0x29fafe=0x0,_0x10972c;_0x29fafe<_0x3739c4[_0xb3d4('0x2e')];_0x29fafe+=0x1){_0x10972c=_0x3739c4[_0x29fafe]['get']({'plain':!![]});_0x2fd962[_0xb3d4('0x35')][_0xb3d4('0x1c')]({'phone':_0x315847[_0xb3d4('0xd')],'ContactId':_0x315847['id'],'ListId':_0x315847[_0xb3d4('0x1d')],'CampaignId':_0x10972c['id']})['catch'](function(_0x4e2520){logger['error'](_0xb3d4('0x36'),_0xb3d4('0x15'),_0x4e2520[_0xb3d4('0x34')]);});}}});})[_0xb3d4('0x19')](function(){_0x2550c5();})[_0xb3d4('0x29')](function(_0x1bf0a4){logger[_0xb3d4('0x27')](_0xb3d4('0x36'),_0xb3d4('0x15'),_0x1bf0a4[_0xb3d4('0x34')]);_0x2550c5(_0x1bf0a4);});}else{_0x2550c5(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x470574,_0x241345,_0x4496da){var _0xea42bc=_0x1a7434['models'];var _0x3e7aca=_0x470574[_0xb3d4('0x13')]({'plain':!![]});return _0xea42bc[_0xb3d4('0x17')][_0xb3d4('0x2b')]({'raw':!![],'where':{'ListId':_0x3e7aca[_0xb3d4('0x1d')],'ContactId':_0x3e7aca['id']}})[_0xb3d4('0x19')](function(_0x2fc983){if(!_[_0xb3d4('0x37')](_0x2fc983)){var _0x5da1ff=[];for(var _0x44e69e=0x0;_0x44e69e<_0x2fc983[_0xb3d4('0x2e')];_0x44e69e+=0x1){var _0x50ea3b=_0xea42bc[_0xb3d4('0x17')][_0xb3d4('0x38')]({'where':{'ListId':_0x2fc983[_0x44e69e]['ListId'],'ContactId':_0x2fc983[_0x44e69e][_0xb3d4('0x39')],'VoiceQueueId':_0x2fc983[_0x44e69e][_0xb3d4('0x3a')],'CampaignId':_0x2fc983[_0x44e69e][_0xb3d4('0x3b')]}});_0x5da1ff['push'](_0x50ea3b);}return BPromise[_0xb3d4('0x3c')](_0x5da1ff);}})['then'](function(_0x53134b){logger[_0xb3d4('0x3d')](_0xb3d4('0x3e'),util['inspect'](_0x53134b,{'showHidden':![],'depth':null}));_0x4496da();})[_0xb3d4('0x29')](function(_0x1dbd2e){logger[_0xb3d4('0x27')](_0xb3d4('0x3f'),util['inspect'](_0x1dbd2e,{'showHidden':![],'depth':null}));_0x4496da(_0x1dbd2e);});}}});};