Built motion from commit 14588770.|2.5.38
[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 _0x9f7b=['fti_cm_contacts','firstName','lastName','mobile','fax','email','FULLTEXT','utf8mb4','utf8mb4_unicode_ci','models','get','error','afterCreate','inspect','CmHopper','findOne','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create','ListId','priority','scheduledat','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','format','then','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','isNil','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','catch','cmContact','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','VoiceQueue','findAll','outbound','dialCheckDuplicateType','CmList','Lists','length','always','onlyIfOpen','CmHopperBlack','cmContact,\x20%s,\x20%s','Campaign','BlackLists','message','isEmpty','destroy','CampaignId','push','all','info','[CmContact][afterDestroy][Succesfull]','../../config/logger','api','moment','bluebird','request-promise','path','../../config/environment','./cmContact.attributes','define','CmContact','cm_contacts','phone'];(function(_0x3cacb7,_0x4eeda1){var _0x2125c5=function(_0x49c04e){while(--_0x49c04e){_0x3cacb7['push'](_0x3cacb7['shift']());}};_0x2125c5(++_0x4eeda1);}(_0x9f7b,0x104));var _0xb9f7=function(_0x594c4f,_0x574f78){_0x594c4f=_0x594c4f-0x0;var _0x48dca3=_0x9f7b[_0x594c4f];return _0x48dca3;};'use strict';var _=require('lodash');var util=require('util');var logger=require(_0xb9f7('0x0'))(_0xb9f7('0x1'));var moment=require(_0xb9f7('0x2'));var BPromise=require(_0xb9f7('0x3'));var rp=require(_0xb9f7('0x4'));var fs=require('fs');var path=require(_0xb9f7('0x5'));var rimraf=require('rimraf');var config=require(_0xb9f7('0x6'));var attributes=require(_0xb9f7('0x7'));module['exports']=function(_0x3138b5,_0x3d3e0d){return _0x3138b5[_0xb9f7('0x8')](_0xb9f7('0x9'),attributes,{'tableName':_0xb9f7('0xa'),'paranoid':!![],'indexes':[{'name':_0xb9f7('0xb'),'fields':['phone']},{'name':_0xb9f7('0xc'),'fields':[_0xb9f7('0xd'),_0xb9f7('0xe'),_0xb9f7('0xb'),_0xb9f7('0xf'),_0xb9f7('0x10'),_0xb9f7('0x11')],'type':_0xb9f7('0x12')}],'timestamps':!![],'charset':_0xb9f7('0x13'),'collate':_0xb9f7('0x14'),'hooks':{'afterCreate':function(_0x1de3c4,_0x579636,_0x36f896){var _0x51b1ef=_0x3138b5[_0xb9f7('0x15')];var _0xdc1a9d=_0x1de3c4[_0xb9f7('0x16')]({'plain':!![]});function _0x172043(){return function(_0x4c8e1f){logger[_0xb9f7('0x17')]('cmContact',_0xb9f7('0x18'),util[_0xb9f7('0x19')](_0x4c8e1f,{'showHidden':![],'depth':null}));};}var _0x22b886=function(_0x341ce1){return new BPromise(function(_0x393633,_0x1908ea){_0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1b')]({'where':{'phone':_0xdc1a9d['phone'],'VoiceQueueId':_0x341ce1['id']}})['then'](function(_0x420e47){if(!_['isNil'](_0x420e47)){throw new Error(_0xb9f7('0x1c')+_0xdc1a9d[_0xb9f7('0xb')]+_0xb9f7('0x1d')+_0x341ce1['id']);}return _0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d[_0xb9f7('0xb')],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d[_0xb9f7('0x1f')],'priority':_0xdc1a9d[_0xb9f7('0x20')]?_0xdc1a9d[_0xb9f7('0x20')]:0x2,'scheduledat':_0xdc1a9d[_0xb9f7('0x21')]?_0xdc1a9d[_0xb9f7('0x21')]:moment()['format'](_0xb9f7('0x22')),'callback':_0xdc1a9d['callbackUniqueid']?!![]:![],'callbackuniqueid':_0xdc1a9d['callbackUniqueid']?_0xdc1a9d[_0xb9f7('0x23')]:undefined,'callbackat':_0xdc1a9d[_0xb9f7('0x23')]?moment()[_0xb9f7('0x24')](_0xb9f7('0x22')):undefined,'VoiceQueueId':_0x341ce1['id']});})['catch'](function(_0x16f988){_0x1908ea(_0x16f988);});});};var _0x53094a=function(_0x49fa5b){return new BPromise(function(_0x1d2d49,_0x49e798){_0x51b1ef['CmHopper'][_0xb9f7('0x1b')]({'where':{'phone':_0xdc1a9d[_0xb9f7('0xb')],'VoiceQueueId':_0x49fa5b['id']}})[_0xb9f7('0x25')](function(_0x2ee735){if(!_['isNil'](_0x2ee735)){throw new Error('cmContact,\x20afterCreate\x20'+_0xdc1a9d[_0xb9f7('0xb')]+_0xb9f7('0x26')+_0x49fa5b['id']);}return _0x51b1ef[_0xb9f7('0x27')][_0xb9f7('0x1b')]({'where':{'calleridnum':_0xdc1a9d[_0xb9f7('0xb')],'VoiceQueueId':_0x49fa5b['id']}});})[_0xb9f7('0x25')](function(_0x1c31d7){if(!_[_0xb9f7('0x28')](_0x1c31d7)){throw new Error(_0xb9f7('0x29')+_0xdc1a9d[_0xb9f7('0xb')]+_0xb9f7('0x2a')+_0x49fa5b['id']);}return _0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d[_0xb9f7('0xb')],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d[_0xb9f7('0x1f')],'priority':_0xdc1a9d[_0xb9f7('0x20')]?_0xdc1a9d['priority']:0x2,'scheduledat':_0xdc1a9d[_0xb9f7('0x21')]?_0xdc1a9d[_0xb9f7('0x21')]:moment()[_0xb9f7('0x24')](_0xb9f7('0x22')),'callback':_0xdc1a9d['callbackUniqueid']?!![]:![],'callbackuniqueid':_0xdc1a9d[_0xb9f7('0x23')]?_0xdc1a9d['callbackUniqueid']:undefined,'callbackat':_0xdc1a9d[_0xb9f7('0x23')]?moment()[_0xb9f7('0x24')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x49fa5b['id']});})[_0xb9f7('0x2b')](function(_0xef593a){_0x49e798(_0xef593a);});});};function _0x5edb37(){return function(_0x421756){logger[_0xb9f7('0x17')](_0xb9f7('0x2c'),'afterCreate',util[_0xb9f7('0x19')](_0x421756,{'showHidden':![],'depth':null}));};}var _0x21c93f=function(_0x45d851){return new BPromise(function(_0x247878,_0x13afe5){_0x51b1ef[_0xb9f7('0x1a')]['findOne']({'where':{'phone':_0xdc1a9d[_0xb9f7('0xb')],'CampaignId':_0x45d851['id']}})['then'](function(_0xaddbae){if(!_[_0xb9f7('0x28')](_0xaddbae)){throw new Error(_0xb9f7('0x1c')+_0xdc1a9d[_0xb9f7('0xb')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x45d851['id']);}return _0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d[_0xb9f7('0xb')],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d[_0xb9f7('0x1f')],'priority':_0xdc1a9d[_0xb9f7('0x20')]?_0xdc1a9d[_0xb9f7('0x20')]:0x2,'scheduledat':_0xdc1a9d['scheduledat']?_0xdc1a9d[_0xb9f7('0x21')]:moment()['format'](_0xb9f7('0x22')),'callback':_0xdc1a9d[_0xb9f7('0x23')]?!![]:![],'callbackuniqueid':_0xdc1a9d[_0xb9f7('0x23')]?_0xdc1a9d[_0xb9f7('0x23')]:undefined,'callbackat':_0xdc1a9d[_0xb9f7('0x23')]?moment()[_0xb9f7('0x24')](_0xb9f7('0x22')):undefined,'CampaignId':_0x45d851['id']});})['catch'](function(_0x17c601){_0x13afe5(_0x17c601);});});};var _0x3c4fc2=function(_0x4fcf3a){return new BPromise(function(_0x1caf7b,_0x4af571){_0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1b')]({'where':{'phone':_0xdc1a9d[_0xb9f7('0xb')],'CampaignId':_0x4fcf3a['id']}})[_0xb9f7('0x25')](function(_0x4abcfd){if(!_[_0xb9f7('0x28')](_0x4abcfd)){throw new Error(_0xb9f7('0x1c')+_0xdc1a9d[_0xb9f7('0xb')]+_0xb9f7('0x2d')+_0x4fcf3a['id']);}return _0x51b1ef[_0xb9f7('0x27')][_0xb9f7('0x1b')]({'where':{'calleridnum':_0xdc1a9d['phone'],'CampaignId':_0x4fcf3a['id']}});})[_0xb9f7('0x25')](function(_0x1dd2ea){if(!_[_0xb9f7('0x28')](_0x1dd2ea)){throw new Error(_0xb9f7('0x29')+_0xdc1a9d[_0xb9f7('0xb')]+_0xb9f7('0x2e')+_0x4fcf3a['id']);}return _0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d[_0xb9f7('0xb')],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d['ListId'],'priority':_0xdc1a9d[_0xb9f7('0x20')]?_0xdc1a9d[_0xb9f7('0x20')]:0x2,'scheduledat':_0xdc1a9d[_0xb9f7('0x21')]?_0xdc1a9d[_0xb9f7('0x21')]:moment()[_0xb9f7('0x24')](_0xb9f7('0x22')),'callback':_0xdc1a9d[_0xb9f7('0x23')]?!![]:![],'callbackuniqueid':_0xdc1a9d[_0xb9f7('0x23')]?_0xdc1a9d[_0xb9f7('0x23')]:undefined,'callbackat':_0xdc1a9d[_0xb9f7('0x23')]?moment()[_0xb9f7('0x24')](_0xb9f7('0x22')):undefined,'CampaignId':_0x4fcf3a['id']});})['catch'](function(_0x3fb3cc){_0x4af571(_0x3fb3cc);});});};if(_0xdc1a9d['ListId']){_0x51b1ef[_0xb9f7('0x2f')][_0xb9f7('0x30')]({'where':{'type':_0xb9f7('0x31')},'attributes':['id',_0xb9f7('0x32')],'include':[{'model':_0x51b1ef[_0xb9f7('0x33')],'as':_0xb9f7('0x34'),'where':{'id':_0xdc1a9d['ListId']}}]})['then'](function(_0x19e2f3){if(_0x19e2f3){var _0x51b344=[];for(var _0x99c441=0x0,_0x466bf6;_0x99c441<_0x19e2f3[_0xb9f7('0x35')];_0x99c441+=0x1){_0x466bf6=_0x19e2f3[_0x99c441]['get']({'plain':!![]});switch(_0x466bf6['dialCheckDuplicateType']){case _0xb9f7('0x36'):_0x53094a(_0x466bf6)[_0xb9f7('0x2b')](_0x172043());break;case _0xb9f7('0x37'):_0x22b886(_0x466bf6)[_0xb9f7('0x2b')](_0x172043());break;default:_0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d[_0xb9f7('0xb')],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d[_0xb9f7('0x1f')],'scheduledat':_0xdc1a9d[_0xb9f7('0x21')]?_0xdc1a9d[_0xb9f7('0x21')]:moment()[_0xb9f7('0x24')](_0xb9f7('0x22')),'priority':_0xdc1a9d[_0xb9f7('0x20')]?_0xdc1a9d[_0xb9f7('0x20')]:0x2,'callback':_0xdc1a9d[_0xb9f7('0x23')]?!![]:![],'callbackuniqueid':_0xdc1a9d['callbackUniqueid']?_0xdc1a9d[_0xb9f7('0x23')]:undefined,'callbackat':_0xdc1a9d[_0xb9f7('0x23')]?moment()['format'](_0xb9f7('0x22')):undefined,'VoiceQueueId':_0x466bf6['id']})[_0xb9f7('0x2b')](_0x172043());}}}})[_0xb9f7('0x25')](function(){return _0x51b1ef[_0xb9f7('0x2f')]['findAll']({'where':{'type':_0xb9f7('0x31')},'attributes':['id'],'include':[{'model':_0x51b1ef['CmList'],'as':'BlackLists','where':{'id':_0xdc1a9d[_0xb9f7('0x1f')]}}]})[_0xb9f7('0x25')](function(_0x1c5f07){if(_0x1c5f07){var _0x56b0c3=[];for(var _0xef1b7=0x0,_0x188f2e;_0xef1b7<_0x1c5f07[_0xb9f7('0x35')];_0xef1b7+=0x1){_0x188f2e=_0x1c5f07[_0xef1b7][_0xb9f7('0x16')]({'plain':!![]});_0x51b1ef[_0xb9f7('0x38')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d['phone'],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d['ListId'],'VoiceQueueId':_0x188f2e['id']})[_0xb9f7('0x2b')](function(_0x40ff95){logger[_0xb9f7('0x17')](_0xb9f7('0x39'),_0xb9f7('0x18'),_0x40ff95['message']);});}}});})[_0xb9f7('0x25')](function(){return _0x51b1ef['Campaign']['findAll']({'attributes':['id',_0xb9f7('0x32')],'include':[{'model':_0x51b1ef[_0xb9f7('0x33')],'as':_0xb9f7('0x34'),'where':{'id':_0xdc1a9d['ListId']}}]})[_0xb9f7('0x25')](function(_0x4e14f6){if(_0x4e14f6){var _0x283737=[];for(var _0x4363c1=0x0,_0x4b5e70;_0x4363c1<_0x4e14f6[_0xb9f7('0x35')];_0x4363c1+=0x1){_0x4b5e70=_0x4e14f6[_0x4363c1][_0xb9f7('0x16')]({'plain':!![]});switch(_0x4b5e70['dialCheckDuplicateType']){case _0xb9f7('0x36'):_0x3c4fc2(_0x4b5e70)[_0xb9f7('0x2b')](_0x5edb37());break;case _0xb9f7('0x37'):_0x21c93f(_0x4b5e70)[_0xb9f7('0x2b')](_0x5edb37());break;default:_0x51b1ef[_0xb9f7('0x1a')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d['phone'],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d[_0xb9f7('0x1f')],'scheduledat':_0xdc1a9d[_0xb9f7('0x21')]?_0xdc1a9d['scheduledat']:moment()[_0xb9f7('0x24')](_0xb9f7('0x22')),'priority':_0xdc1a9d[_0xb9f7('0x20')]?_0xdc1a9d[_0xb9f7('0x20')]:0x2,'callback':_0xdc1a9d[_0xb9f7('0x23')]?!![]:![],'callbackuniqueid':_0xdc1a9d[_0xb9f7('0x23')]?_0xdc1a9d['callbackUniqueid']:undefined,'callbackat':_0xdc1a9d[_0xb9f7('0x23')]?moment()[_0xb9f7('0x24')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x4b5e70['id']})[_0xb9f7('0x2b')](_0x5edb37());}}}});})['then'](function(){return _0x51b1ef[_0xb9f7('0x3a')][_0xb9f7('0x30')]({'attributes':['id'],'include':[{'model':_0x51b1ef[_0xb9f7('0x33')],'as':_0xb9f7('0x3b'),'where':{'id':_0xdc1a9d[_0xb9f7('0x1f')]}}]})[_0xb9f7('0x25')](function(_0x372569){if(_0x372569){var _0x3104f6=[];for(var _0x479c4d=0x0,_0x511905;_0x479c4d<_0x372569[_0xb9f7('0x35')];_0x479c4d+=0x1){_0x511905=_0x372569[_0x479c4d][_0xb9f7('0x16')]({'plain':!![]});_0x51b1ef[_0xb9f7('0x38')][_0xb9f7('0x1e')]({'phone':_0xdc1a9d[_0xb9f7('0xb')],'ContactId':_0xdc1a9d['id'],'ListId':_0xdc1a9d['ListId'],'CampaignId':_0x511905['id']})['catch'](function(_0x15e40c){logger['error'](_0xb9f7('0x39'),_0xb9f7('0x18'),_0x15e40c['message']);});}}});})[_0xb9f7('0x25')](function(){_0x36f896();})[_0xb9f7('0x2b')](function(_0x5587df){logger[_0xb9f7('0x17')](_0xb9f7('0x39'),_0xb9f7('0x18'),_0x5587df[_0xb9f7('0x3c')]);_0x36f896(_0x5587df);});}else{_0x36f896(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x3fd8e6,_0x55177c,_0x268284){var _0x52c727=_0x3138b5[_0xb9f7('0x15')];var _0x40e4dc=_0x3fd8e6[_0xb9f7('0x16')]({'plain':!![]});return _0x52c727[_0xb9f7('0x1a')]['findAll']({'raw':!![],'where':{'ListId':_0x40e4dc[_0xb9f7('0x1f')],'ContactId':_0x40e4dc['id']}})['then'](function(_0xadabed){if(!_[_0xb9f7('0x3d')](_0xadabed)){var _0x2558d8=[];for(var _0x23ef29=0x0;_0x23ef29<_0xadabed[_0xb9f7('0x35')];_0x23ef29+=0x1){var _0x11366a=_0x52c727[_0xb9f7('0x1a')][_0xb9f7('0x3e')]({'where':{'ListId':_0xadabed[_0x23ef29][_0xb9f7('0x1f')],'ContactId':_0xadabed[_0x23ef29]['ContactId'],'VoiceQueueId':_0xadabed[_0x23ef29]['VoiceQueueId'],'CampaignId':_0xadabed[_0x23ef29][_0xb9f7('0x3f')]}});_0x2558d8[_0xb9f7('0x40')](_0x11366a);}return BPromise[_0xb9f7('0x41')](_0x2558d8);}})[_0xb9f7('0x25')](function(_0x3d85fa){logger[_0xb9f7('0x42')](_0xb9f7('0x43'),util[_0xb9f7('0x19')](_0x3d85fa,{'showHidden':![],'depth':null}));_0x268284();})[_0xb9f7('0x2b')](function(_0x5eec31){logger[_0xb9f7('0x17')]('[CmContact][afterDestroy][Error]',util[_0xb9f7('0x19')](_0x5eec31,{'showHidden':![],'depth':null}));_0x268284(_0x5eec31);});}}});};