a79b5cae71ddbe0296a82478e81179ee5d4842de
[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 _0x9ffd=['cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','priority','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','create','ListId','\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','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','VoiceQueue','outbound','dialCheckDuplicateType','CmList','Lists','always','onlyIfOpen','findAll','BlackLists','CmHopperBlack','cmContact,\x20%s,\x20%s','length','Campaign','message','models','isEmpty','destroy','ContactId','VoiceQueueId','CampaignId','push','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','moment','bluebird','request-promise','path','../../config/environment','./cmContact.attributes','define','CmContact','cm_contacts','phone','get','error','cmContact','afterCreate','inspect','CmHopper','findOne','then','isNil'];(function(_0x43b965,_0x20a22b){var _0x271ed1=function(_0x1027a6){while(--_0x1027a6){_0x43b965['push'](_0x43b965['shift']());}};_0x271ed1(++_0x20a22b);}(_0x9ffd,0xaa));var _0xd9ff=function(_0x18eab4,_0x240ee4){_0x18eab4=_0x18eab4-0x0;var _0x31ca6d=_0x9ffd[_0x18eab4];return _0x31ca6d;};'use strict';var _=require(_0xd9ff('0x0'));var util=require(_0xd9ff('0x1'));var logger=require(_0xd9ff('0x2'))('api');var moment=require(_0xd9ff('0x3'));var BPromise=require(_0xd9ff('0x4'));var rp=require(_0xd9ff('0x5'));var fs=require('fs');var path=require(_0xd9ff('0x6'));var rimraf=require('rimraf');var config=require(_0xd9ff('0x7'));var attributes=require(_0xd9ff('0x8'));module['exports']=function(_0x3aa26b,_0x2982c3){return _0x3aa26b[_0xd9ff('0x9')](_0xd9ff('0xa'),attributes,{'tableName':_0xd9ff('0xb'),'paranoid':!![],'indexes':[{'name':_0xd9ff('0xc'),'fields':[_0xd9ff('0xc')]}],'timestamps':!![],'hooks':{'afterCreate':function(_0x41ae79,_0x977487,_0x58e728){var _0x54a555=_0x3aa26b['models'];var _0x5aa954=_0x41ae79[_0xd9ff('0xd')]({'plain':!![]});function _0x2fd77a(){return function(_0x193537){logger[_0xd9ff('0xe')](_0xd9ff('0xf'),_0xd9ff('0x10'),util[_0xd9ff('0x11')](_0x193537,{'showHidden':![],'depth':null}));};}var _0x1c2a16=function(_0x47d842){return new BPromise(function(_0x47b0d7,_0xaaf7af){_0x54a555[_0xd9ff('0x12')][_0xd9ff('0x13')]({'where':{'phone':_0x5aa954[_0xd9ff('0xc')],'VoiceQueueId':_0x47d842['id']}})[_0xd9ff('0x14')](function(_0x556aa4){if(!_[_0xd9ff('0x15')](_0x556aa4)){throw new Error(_0xd9ff('0x16')+_0x5aa954['phone']+_0xd9ff('0x17')+_0x47d842['id']);}return _0x54a555['CmHopper']['create']({'phone':_0x5aa954['phone'],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954['ListId'],'priority':_0x5aa954[_0xd9ff('0x18')]?_0x5aa954['priority']:0x2,'scheduledat':_0x5aa954[_0xd9ff('0x19')]?_0x5aa954[_0xd9ff('0x19')]:moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')),'callback':_0x5aa954[_0xd9ff('0x1c')]?!![]:![],'callbackuniqueid':_0x5aa954[_0xd9ff('0x1c')]?_0x5aa954[_0xd9ff('0x1c')]:undefined,'callbackat':_0x5aa954[_0xd9ff('0x1c')]?moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')):undefined,'VoiceQueueId':_0x47d842['id']});})[_0xd9ff('0x1d')](function(_0x39ef37){_0xaaf7af(_0x39ef37);});});};var _0x46a6ab=function(_0x4c8740){return new BPromise(function(_0x4cf8ec,_0x264a3d){_0x54a555[_0xd9ff('0x12')]['findOne']({'where':{'phone':_0x5aa954['phone'],'VoiceQueueId':_0x4c8740['id']}})['then'](function(_0x4b1477){if(!_[_0xd9ff('0x15')](_0x4b1477)){throw new Error('cmContact,\x20afterCreate\x20'+_0x5aa954[_0xd9ff('0xc')]+_0xd9ff('0x1e')+_0x4c8740['id']);}return _0x54a555[_0xd9ff('0x1f')][_0xd9ff('0x13')]({'where':{'calleridnum':_0x5aa954[_0xd9ff('0xc')],'VoiceQueueId':_0x4c8740['id']}});})[_0xd9ff('0x14')](function(_0x4b058a){if(!_['isNil'](_0x4b058a)){throw new Error(_0xd9ff('0x20')+_0x5aa954['phone']+_0xd9ff('0x21')+_0x4c8740['id']);}return _0x54a555['CmHopper'][_0xd9ff('0x22')]({'phone':_0x5aa954['phone'],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954[_0xd9ff('0x23')],'priority':_0x5aa954[_0xd9ff('0x18')]?_0x5aa954[_0xd9ff('0x18')]:0x2,'scheduledat':_0x5aa954[_0xd9ff('0x19')]?_0x5aa954['scheduledat']:moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')),'callback':_0x5aa954[_0xd9ff('0x1c')]?!![]:![],'callbackuniqueid':_0x5aa954[_0xd9ff('0x1c')]?_0x5aa954['callbackUniqueid']:undefined,'callbackat':_0x5aa954[_0xd9ff('0x1c')]?moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')):undefined,'VoiceQueueId':_0x4c8740['id']});})[_0xd9ff('0x1d')](function(_0x263d3a){_0x264a3d(_0x263d3a);});});};function _0xe516e2(){return function(_0x420ffa){logger[_0xd9ff('0xe')](_0xd9ff('0xf'),'afterCreate',util[_0xd9ff('0x11')](_0x420ffa,{'showHidden':![],'depth':null}));};}var _0x4ba2be=function(_0x1d55fd){return new BPromise(function(_0x29c532,_0xbdd352){_0x54a555['CmHopper'][_0xd9ff('0x13')]({'where':{'phone':_0x5aa954[_0xd9ff('0xc')],'CampaignId':_0x1d55fd['id']}})[_0xd9ff('0x14')](function(_0x436a55){if(!_['isNil'](_0x436a55)){throw new Error(_0xd9ff('0x16')+_0x5aa954['phone']+_0xd9ff('0x24')+_0x1d55fd['id']);}return _0x54a555[_0xd9ff('0x12')][_0xd9ff('0x22')]({'phone':_0x5aa954[_0xd9ff('0xc')],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954[_0xd9ff('0x23')],'priority':_0x5aa954[_0xd9ff('0x18')]?_0x5aa954[_0xd9ff('0x18')]:0x2,'scheduledat':_0x5aa954[_0xd9ff('0x19')]?_0x5aa954[_0xd9ff('0x19')]:moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')),'callback':_0x5aa954[_0xd9ff('0x1c')]?!![]:![],'callbackuniqueid':_0x5aa954[_0xd9ff('0x1c')]?_0x5aa954[_0xd9ff('0x1c')]:undefined,'callbackat':_0x5aa954['callbackUniqueid']?moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')):undefined,'CampaignId':_0x1d55fd['id']});})[_0xd9ff('0x1d')](function(_0x4ab10f){_0xbdd352(_0x4ab10f);});});};var _0x3d449d=function(_0x5e3ff9){return new BPromise(function(_0x3ae761,_0x4ae56b){_0x54a555[_0xd9ff('0x12')]['findOne']({'where':{'phone':_0x5aa954[_0xd9ff('0xc')],'CampaignId':_0x5e3ff9['id']}})[_0xd9ff('0x14')](function(_0x11566b){if(!_['isNil'](_0x11566b)){throw new Error('cmContact,\x20afterCreate\x20'+_0x5aa954['phone']+_0xd9ff('0x25')+_0x5e3ff9['id']);}return _0x54a555[_0xd9ff('0x1f')][_0xd9ff('0x13')]({'where':{'calleridnum':_0x5aa954[_0xd9ff('0xc')],'CampaignId':_0x5e3ff9['id']}});})[_0xd9ff('0x14')](function(_0x34daf6){if(!_['isNil'](_0x34daf6)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x5aa954['phone']+_0xd9ff('0x26')+_0x5e3ff9['id']);}return _0x54a555[_0xd9ff('0x12')][_0xd9ff('0x22')]({'phone':_0x5aa954[_0xd9ff('0xc')],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954['ListId'],'priority':_0x5aa954[_0xd9ff('0x18')]?_0x5aa954['priority']:0x2,'scheduledat':_0x5aa954[_0xd9ff('0x19')]?_0x5aa954[_0xd9ff('0x19')]:moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')),'callback':_0x5aa954['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5aa954[_0xd9ff('0x1c')]?_0x5aa954[_0xd9ff('0x1c')]:undefined,'callbackat':_0x5aa954['callbackUniqueid']?moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')):undefined,'CampaignId':_0x5e3ff9['id']});})[_0xd9ff('0x1d')](function(_0x3ba5ff){_0x4ae56b(_0x3ba5ff);});});};if(_0x5aa954[_0xd9ff('0x23')]){_0x54a555[_0xd9ff('0x27')]['findAll']({'where':{'type':_0xd9ff('0x28')},'attributes':['id',_0xd9ff('0x29')],'include':[{'model':_0x54a555[_0xd9ff('0x2a')],'as':_0xd9ff('0x2b'),'where':{'id':_0x5aa954[_0xd9ff('0x23')]}}]})['then'](function(_0x3178cd){if(_0x3178cd){var _0x463d6e=[];for(var _0xe0bc49=0x0,_0x5082ad;_0xe0bc49<_0x3178cd['length'];_0xe0bc49+=0x1){_0x5082ad=_0x3178cd[_0xe0bc49][_0xd9ff('0xd')]({'plain':!![]});switch(_0x5082ad[_0xd9ff('0x29')]){case _0xd9ff('0x2c'):_0x46a6ab(_0x5082ad)[_0xd9ff('0x1d')](_0x2fd77a());break;case _0xd9ff('0x2d'):_0x1c2a16(_0x5082ad)[_0xd9ff('0x1d')](_0x2fd77a());break;default:_0x54a555['CmHopper'][_0xd9ff('0x22')]({'phone':_0x5aa954[_0xd9ff('0xc')],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954['ListId'],'scheduledat':_0x5aa954[_0xd9ff('0x19')]?_0x5aa954[_0xd9ff('0x19')]:moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')),'priority':_0x5aa954[_0xd9ff('0x18')]?_0x5aa954['priority']:0x2,'callback':_0x5aa954['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5aa954['callbackUniqueid']?_0x5aa954[_0xd9ff('0x1c')]:undefined,'callbackat':_0x5aa954['callbackUniqueid']?moment()[_0xd9ff('0x1a')](_0xd9ff('0x1b')):undefined,'VoiceQueueId':_0x5082ad['id']})[_0xd9ff('0x1d')](_0x2fd77a());}}}})['then'](function(){return _0x54a555[_0xd9ff('0x27')][_0xd9ff('0x2e')]({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0x54a555[_0xd9ff('0x2a')],'as':_0xd9ff('0x2f'),'where':{'id':_0x5aa954[_0xd9ff('0x23')]}}]})[_0xd9ff('0x14')](function(_0x559101){if(_0x559101){var _0x38b5b6=[];for(var _0x5313e8=0x0,_0x309189;_0x5313e8<_0x559101['length'];_0x5313e8+=0x1){_0x309189=_0x559101[_0x5313e8][_0xd9ff('0xd')]({'plain':!![]});_0x54a555[_0xd9ff('0x30')][_0xd9ff('0x22')]({'phone':_0x5aa954[_0xd9ff('0xc')],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954[_0xd9ff('0x23')],'VoiceQueueId':_0x309189['id']})['catch'](function(_0x2d5237){logger[_0xd9ff('0xe')](_0xd9ff('0x31'),_0xd9ff('0x10'),_0x2d5237['message']);});}}});})['then'](function(){return _0x54a555['Campaign'][_0xd9ff('0x2e')]({'attributes':['id',_0xd9ff('0x29')],'include':[{'model':_0x54a555[_0xd9ff('0x2a')],'as':'Lists','where':{'id':_0x5aa954[_0xd9ff('0x23')]}}]})[_0xd9ff('0x14')](function(_0x6d1082){if(_0x6d1082){var _0x25268e=[];for(var _0xfede73=0x0,_0x40d391;_0xfede73<_0x6d1082[_0xd9ff('0x32')];_0xfede73+=0x1){_0x40d391=_0x6d1082[_0xfede73][_0xd9ff('0xd')]({'plain':!![]});switch(_0x40d391[_0xd9ff('0x29')]){case _0xd9ff('0x2c'):_0x3d449d(_0x40d391)[_0xd9ff('0x1d')](_0xe516e2());break;case _0xd9ff('0x2d'):_0x4ba2be(_0x40d391)[_0xd9ff('0x1d')](_0xe516e2());break;default:_0x54a555['CmHopper'][_0xd9ff('0x22')]({'phone':_0x5aa954['phone'],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954[_0xd9ff('0x23')],'scheduledat':_0x5aa954['scheduledat']?_0x5aa954[_0xd9ff('0x19')]:moment()['format'](_0xd9ff('0x1b')),'priority':_0x5aa954['priority']?_0x5aa954[_0xd9ff('0x18')]:0x2,'callback':_0x5aa954['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5aa954[_0xd9ff('0x1c')]?_0x5aa954['callbackUniqueid']:undefined,'callbackat':_0x5aa954[_0xd9ff('0x1c')]?moment()['format'](_0xd9ff('0x1b')):undefined,'CampaignId':_0x40d391['id']})[_0xd9ff('0x1d')](_0xe516e2());}}}});})[_0xd9ff('0x14')](function(){return _0x54a555[_0xd9ff('0x33')]['findAll']({'attributes':['id'],'include':[{'model':_0x54a555[_0xd9ff('0x2a')],'as':_0xd9ff('0x2f'),'where':{'id':_0x5aa954[_0xd9ff('0x23')]}}]})['then'](function(_0x2d6c3){if(_0x2d6c3){var _0x32e1b9=[];for(var _0x19b033=0x0,_0x2e29aa;_0x19b033<_0x2d6c3[_0xd9ff('0x32')];_0x19b033+=0x1){_0x2e29aa=_0x2d6c3[_0x19b033]['get']({'plain':!![]});_0x54a555[_0xd9ff('0x30')][_0xd9ff('0x22')]({'phone':_0x5aa954[_0xd9ff('0xc')],'ContactId':_0x5aa954['id'],'ListId':_0x5aa954[_0xd9ff('0x23')],'CampaignId':_0x2e29aa['id']})[_0xd9ff('0x1d')](function(_0x2b36e8){logger[_0xd9ff('0xe')]('cmContact,\x20%s,\x20%s',_0xd9ff('0x10'),_0x2b36e8[_0xd9ff('0x34')]);});}}});})['then'](function(){_0x58e728();})[_0xd9ff('0x1d')](function(_0x17c1b2){logger['error'](_0xd9ff('0x31'),_0xd9ff('0x10'),_0x17c1b2[_0xd9ff('0x34')]);_0x58e728(_0x17c1b2);});}else{_0x58e728(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x4ea9c6,_0x39473e,_0x4f0c33){var _0x34dc83=_0x3aa26b[_0xd9ff('0x35')];var _0x31907e=_0x4ea9c6[_0xd9ff('0xd')]({'plain':!![]});return _0x34dc83['CmHopper']['findAll']({'raw':!![],'where':{'ListId':_0x31907e['ListId'],'ContactId':_0x31907e['id']}})[_0xd9ff('0x14')](function(_0x3a8b6a){if(!_[_0xd9ff('0x36')](_0x3a8b6a)){var _0x5073f5=[];for(var _0x27dad3=0x0;_0x27dad3<_0x3a8b6a['length'];_0x27dad3+=0x1){var _0x19be5b=_0x34dc83['CmHopper'][_0xd9ff('0x37')]({'where':{'ListId':_0x3a8b6a[_0x27dad3][_0xd9ff('0x23')],'ContactId':_0x3a8b6a[_0x27dad3][_0xd9ff('0x38')],'VoiceQueueId':_0x3a8b6a[_0x27dad3][_0xd9ff('0x39')],'CampaignId':_0x3a8b6a[_0x27dad3][_0xd9ff('0x3a')]}});_0x5073f5[_0xd9ff('0x3b')](_0x19be5b);}return BPromise[_0xd9ff('0x3c')](_0x5073f5);}})[_0xd9ff('0x14')](function(_0x5d633b){logger[_0xd9ff('0x3d')](_0xd9ff('0x3e'),util['inspect'](_0x5d633b,{'showHidden':![],'depth':null}));_0x4f0c33();})['catch'](function(_0x3e8766){logger['error'](_0xd9ff('0x3f'),util[_0xd9ff('0x11')](_0x3e8766,{'showHidden':![],'depth':null}));_0x4f0c33(_0x3e8766);});}}});};