66b34c391f2cab8b35a2fb985c62528b4dafd142
[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 _0x8efe=['destroy','ContactId','VoiceQueueId','CampaignId','push','all','info','[CmContact][afterDestroy][Error]','lodash','util','api','moment','bluebird','request-promise','path','rimraf','../../config/environment','./cmContact.attributes','exports','CmContact','cm_contacts','phone','models','get','error','cmContact','afterCreate','inspect','findOne','then','isNil','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopper','create','ListId','priority','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','CmHopperFinal','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','VoiceQueue','findAll','dialCheckDuplicateType','CmList','Lists','length','always','BlackLists','CmHopperBlack','message','Campaign','cmContact,\x20%s,\x20%s','isEmpty'];(function(_0x4e1211,_0x1c0e26){var _0x4ae887=function(_0x6e55ff){while(--_0x6e55ff){_0x4e1211['push'](_0x4e1211['shift']());}};_0x4ae887(++_0x1c0e26);}(_0x8efe,0xf8));var _0xe8ef=function(_0x532502,_0x26dbb7){_0x532502=_0x532502-0x0;var _0x29de08=_0x8efe[_0x532502];return _0x29de08;};'use strict';var _=require(_0xe8ef('0x0'));var util=require(_0xe8ef('0x1'));var logger=require('../../config/logger')(_0xe8ef('0x2'));var moment=require(_0xe8ef('0x3'));var BPromise=require(_0xe8ef('0x4'));var rp=require(_0xe8ef('0x5'));var fs=require('fs');var path=require(_0xe8ef('0x6'));var rimraf=require(_0xe8ef('0x7'));var config=require(_0xe8ef('0x8'));var attributes=require(_0xe8ef('0x9'));module[_0xe8ef('0xa')]=function(_0x1c4552,_0x3b1636){return _0x1c4552['define'](_0xe8ef('0xb'),attributes,{'tableName':_0xe8ef('0xc'),'paranoid':!![],'indexes':[{'name':_0xe8ef('0xd'),'fields':[_0xe8ef('0xd')]}],'timestamps':!![],'hooks':{'afterCreate':function(_0x3f6e86,_0x4b079c,_0x4810a5){var _0x3e6a0f=_0x1c4552[_0xe8ef('0xe')];var _0x544784=_0x3f6e86[_0xe8ef('0xf')]({'plain':!![]});function _0x186342(){return function(_0x28ecdc){logger[_0xe8ef('0x10')](_0xe8ef('0x11'),_0xe8ef('0x12'),util[_0xe8ef('0x13')](_0x28ecdc,{'showHidden':![],'depth':null}));};}var _0x162118=function(_0x4d8301){return new BPromise(function(_0x118781,_0x354b46){_0x3e6a0f['CmHopper'][_0xe8ef('0x14')]({'where':{'phone':_0x544784['phone'],'VoiceQueueId':_0x4d8301['id']}})[_0xe8ef('0x15')](function(_0x53120f){if(!_[_0xe8ef('0x16')](_0x53120f)){throw new Error(_0xe8ef('0x17')+_0x544784[_0xe8ef('0xd')]+_0xe8ef('0x18')+_0x4d8301['id']);}return _0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x1a')]({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784[_0xe8ef('0x1b')],'priority':_0x544784[_0xe8ef('0x1c')]?_0x544784[_0xe8ef('0x1c')]:0x2,'scheduledat':_0x544784[_0xe8ef('0x1d')]?_0x544784[_0xe8ef('0x1d')]:moment()[_0xe8ef('0x1e')](_0xe8ef('0x1f')),'callback':_0x544784[_0xe8ef('0x20')]?!![]:![],'callbackuniqueid':_0x544784[_0xe8ef('0x20')]?_0x544784[_0xe8ef('0x20')]:undefined,'callbackat':_0x544784[_0xe8ef('0x20')]?moment()['format'](_0xe8ef('0x1f')):undefined,'VoiceQueueId':_0x4d8301['id']});})[_0xe8ef('0x21')](function(_0x3335aa){_0x354b46(_0x3335aa);});});};var _0x4f13f2=function(_0x54f5d3){return new BPromise(function(_0x4bc5e0,_0x117545){_0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x14')]({'where':{'phone':_0x544784[_0xe8ef('0xd')],'VoiceQueueId':_0x54f5d3['id']}})['then'](function(_0x4279ec){if(!_[_0xe8ef('0x16')](_0x4279ec)){throw new Error(_0xe8ef('0x17')+_0x544784[_0xe8ef('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0x54f5d3['id']);}return _0x3e6a0f[_0xe8ef('0x22')]['findOne']({'where':{'calleridnum':_0x544784[_0xe8ef('0xd')],'VoiceQueueId':_0x54f5d3['id']}});})[_0xe8ef('0x15')](function(_0x4549b1){if(!_['isNil'](_0x4549b1)){throw new Error(_0xe8ef('0x23')+_0x544784['phone']+_0xe8ef('0x24')+_0x54f5d3['id']);}return _0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x1a')]({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784[_0xe8ef('0x1b')],'priority':_0x544784['priority']?_0x544784[_0xe8ef('0x1c')]:0x2,'scheduledat':_0x544784[_0xe8ef('0x1d')]?_0x544784['scheduledat']:moment()[_0xe8ef('0x1e')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x544784[_0xe8ef('0x20')]?!![]:![],'callbackuniqueid':_0x544784['callbackUniqueid']?_0x544784[_0xe8ef('0x20')]:undefined,'callbackat':_0x544784[_0xe8ef('0x20')]?moment()[_0xe8ef('0x1e')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x54f5d3['id']});})['catch'](function(_0x20167c){_0x117545(_0x20167c);});});};function _0x11ee41(){return function(_0x2f2a52){logger['error'](_0xe8ef('0x11'),'afterCreate',util['inspect'](_0x2f2a52,{'showHidden':![],'depth':null}));};}var _0x46d956=function(_0x1c758a){return new BPromise(function(_0x106f06,_0x3973e6){_0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x14')]({'where':{'phone':_0x544784['phone'],'CampaignId':_0x1c758a['id']}})['then'](function(_0xc4dbeb){if(!_[_0xe8ef('0x16')](_0xc4dbeb)){throw new Error('cmContact,\x20afterCreate\x20'+_0x544784[_0xe8ef('0xd')]+_0xe8ef('0x25')+_0x1c758a['id']);}return _0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x1a')]({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784[_0xe8ef('0x1b')],'priority':_0x544784[_0xe8ef('0x1c')]?_0x544784[_0xe8ef('0x1c')]:0x2,'scheduledat':_0x544784[_0xe8ef('0x1d')]?_0x544784[_0xe8ef('0x1d')]:moment()[_0xe8ef('0x1e')](_0xe8ef('0x1f')),'callback':_0x544784[_0xe8ef('0x20')]?!![]:![],'callbackuniqueid':_0x544784[_0xe8ef('0x20')]?_0x544784[_0xe8ef('0x20')]:undefined,'callbackat':_0x544784[_0xe8ef('0x20')]?moment()[_0xe8ef('0x1e')](_0xe8ef('0x1f')):undefined,'CampaignId':_0x1c758a['id']});})[_0xe8ef('0x21')](function(_0x4aef85){_0x3973e6(_0x4aef85);});});};var _0x3e8b44=function(_0x15b550){return new BPromise(function(_0x1de181,_0x1fe32f){_0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x14')]({'where':{'phone':_0x544784[_0xe8ef('0xd')],'CampaignId':_0x15b550['id']}})[_0xe8ef('0x15')](function(_0x40a78f){if(!_['isNil'](_0x40a78f)){throw new Error(_0xe8ef('0x17')+_0x544784[_0xe8ef('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x15b550['id']);}return _0x3e6a0f[_0xe8ef('0x22')][_0xe8ef('0x14')]({'where':{'calleridnum':_0x544784[_0xe8ef('0xd')],'CampaignId':_0x15b550['id']}});})[_0xe8ef('0x15')](function(_0x3b6d88){if(!_[_0xe8ef('0x16')](_0x3b6d88)){throw new Error(_0xe8ef('0x23')+_0x544784[_0xe8ef('0xd')]+_0xe8ef('0x26')+_0x15b550['id']);}return _0x3e6a0f[_0xe8ef('0x19')][_0xe8ef('0x1a')]({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784['ListId'],'priority':_0x544784[_0xe8ef('0x1c')]?_0x544784['priority']:0x2,'scheduledat':_0x544784[_0xe8ef('0x1d')]?_0x544784[_0xe8ef('0x1d')]:moment()[_0xe8ef('0x1e')](_0xe8ef('0x1f')),'callback':_0x544784[_0xe8ef('0x20')]?!![]:![],'callbackuniqueid':_0x544784[_0xe8ef('0x20')]?_0x544784[_0xe8ef('0x20')]:undefined,'callbackat':_0x544784['callbackUniqueid']?moment()[_0xe8ef('0x1e')](_0xe8ef('0x1f')):undefined,'CampaignId':_0x15b550['id']});})['catch'](function(_0x5471bf){_0x1fe32f(_0x5471bf);});});};if(_0x544784[_0xe8ef('0x1b')]){_0x3e6a0f[_0xe8ef('0x27')][_0xe8ef('0x28')]({'where':{'type':'outbound'},'attributes':['id',_0xe8ef('0x29')],'include':[{'model':_0x3e6a0f[_0xe8ef('0x2a')],'as':_0xe8ef('0x2b'),'where':{'id':_0x544784[_0xe8ef('0x1b')]}}]})['then'](function(_0xc101da){if(_0xc101da){var _0x4186fb=[];for(var _0x49f3f5=0x0,_0x5e0898;_0x49f3f5<_0xc101da[_0xe8ef('0x2c')];_0x49f3f5+=0x1){_0x5e0898=_0xc101da[_0x49f3f5][_0xe8ef('0xf')]({'plain':!![]});switch(_0x5e0898[_0xe8ef('0x29')]){case _0xe8ef('0x2d'):_0x4f13f2(_0x5e0898)[_0xe8ef('0x21')](_0x186342());break;case'onlyIfOpen':_0x162118(_0x5e0898)[_0xe8ef('0x21')](_0x186342());break;default:_0x3e6a0f[_0xe8ef('0x19')]['create']({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784[_0xe8ef('0x1b')],'scheduledat':_0x544784[_0xe8ef('0x1d')]?_0x544784[_0xe8ef('0x1d')]:moment()['format'](_0xe8ef('0x1f')),'priority':_0x544784['priority']?_0x544784[_0xe8ef('0x1c')]:0x2,'callback':_0x544784[_0xe8ef('0x20')]?!![]:![],'callbackuniqueid':_0x544784[_0xe8ef('0x20')]?_0x544784[_0xe8ef('0x20')]:undefined,'callbackat':_0x544784[_0xe8ef('0x20')]?moment()[_0xe8ef('0x1e')](_0xe8ef('0x1f')):undefined,'VoiceQueueId':_0x5e0898['id']})['catch'](_0x186342());}}}})[_0xe8ef('0x15')](function(){return _0x3e6a0f[_0xe8ef('0x27')][_0xe8ef('0x28')]({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0x3e6a0f[_0xe8ef('0x2a')],'as':_0xe8ef('0x2e'),'where':{'id':_0x544784[_0xe8ef('0x1b')]}}]})['then'](function(_0x213515){if(_0x213515){var _0x66b9b0=[];for(var _0x37027f=0x0,_0x31f825;_0x37027f<_0x213515[_0xe8ef('0x2c')];_0x37027f+=0x1){_0x31f825=_0x213515[_0x37027f][_0xe8ef('0xf')]({'plain':!![]});_0x3e6a0f[_0xe8ef('0x2f')][_0xe8ef('0x1a')]({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784['ListId'],'VoiceQueueId':_0x31f825['id']})[_0xe8ef('0x21')](function(_0x2697d6){logger['error']('cmContact,\x20%s,\x20%s',_0xe8ef('0x12'),_0x2697d6[_0xe8ef('0x30')]);});}}});})[_0xe8ef('0x15')](function(){return _0x3e6a0f[_0xe8ef('0x31')][_0xe8ef('0x28')]({'attributes':['id',_0xe8ef('0x29')],'include':[{'model':_0x3e6a0f[_0xe8ef('0x2a')],'as':_0xe8ef('0x2b'),'where':{'id':_0x544784[_0xe8ef('0x1b')]}}]})[_0xe8ef('0x15')](function(_0x403715){if(_0x403715){var _0x552406=[];for(var _0x218f16=0x0,_0x152cc1;_0x218f16<_0x403715[_0xe8ef('0x2c')];_0x218f16+=0x1){_0x152cc1=_0x403715[_0x218f16][_0xe8ef('0xf')]({'plain':!![]});switch(_0x152cc1[_0xe8ef('0x29')]){case _0xe8ef('0x2d'):_0x3e8b44(_0x152cc1)[_0xe8ef('0x21')](_0x11ee41());break;case'onlyIfOpen':_0x46d956(_0x152cc1)['catch'](_0x11ee41());break;default:_0x3e6a0f[_0xe8ef('0x19')]['create']({'phone':_0x544784[_0xe8ef('0xd')],'ContactId':_0x544784['id'],'ListId':_0x544784[_0xe8ef('0x1b')],'scheduledat':_0x544784[_0xe8ef('0x1d')]?_0x544784[_0xe8ef('0x1d')]:moment()[_0xe8ef('0x1e')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x544784[_0xe8ef('0x1c')]?_0x544784['priority']:0x2,'callback':_0x544784['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x544784[_0xe8ef('0x20')]?_0x544784[_0xe8ef('0x20')]:undefined,'callbackat':_0x544784[_0xe8ef('0x20')]?moment()[_0xe8ef('0x1e')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x152cc1['id']})[_0xe8ef('0x21')](_0x11ee41());}}}});})[_0xe8ef('0x15')](function(){return _0x3e6a0f['Campaign'][_0xe8ef('0x28')]({'attributes':['id'],'include':[{'model':_0x3e6a0f['CmList'],'as':'BlackLists','where':{'id':_0x544784['ListId']}}]})['then'](function(_0xe4b8a5){if(_0xe4b8a5){var _0x21c07a=[];for(var _0x2bdc1f=0x0,_0xb20e5c;_0x2bdc1f<_0xe4b8a5['length'];_0x2bdc1f+=0x1){_0xb20e5c=_0xe4b8a5[_0x2bdc1f][_0xe8ef('0xf')]({'plain':!![]});_0x3e6a0f[_0xe8ef('0x2f')]['create']({'phone':_0x544784['phone'],'ContactId':_0x544784['id'],'ListId':_0x544784[_0xe8ef('0x1b')],'CampaignId':_0xb20e5c['id']})[_0xe8ef('0x21')](function(_0xff1e7b){logger[_0xe8ef('0x10')](_0xe8ef('0x32'),_0xe8ef('0x12'),_0xff1e7b[_0xe8ef('0x30')]);});}}});})[_0xe8ef('0x15')](function(){_0x4810a5();})[_0xe8ef('0x21')](function(_0x18ff30){logger[_0xe8ef('0x10')](_0xe8ef('0x32'),'afterCreate',_0x18ff30[_0xe8ef('0x30')]);_0x4810a5(_0x18ff30);});}else{_0x4810a5(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x58261a,_0x154bf0,_0x2d1c28){var _0x281995=_0x1c4552[_0xe8ef('0xe')];var _0x283a65=_0x58261a[_0xe8ef('0xf')]({'plain':!![]});return _0x281995['CmHopper']['findAll']({'raw':!![],'where':{'ListId':_0x283a65[_0xe8ef('0x1b')],'ContactId':_0x283a65['id']}})[_0xe8ef('0x15')](function(_0x3ded9b){if(!_[_0xe8ef('0x33')](_0x3ded9b)){var _0x496402=[];for(var _0x164a65=0x0;_0x164a65<_0x3ded9b[_0xe8ef('0x2c')];_0x164a65+=0x1){var _0x1f04df=_0x281995[_0xe8ef('0x19')][_0xe8ef('0x34')]({'where':{'ListId':_0x3ded9b[_0x164a65][_0xe8ef('0x1b')],'ContactId':_0x3ded9b[_0x164a65][_0xe8ef('0x35')],'VoiceQueueId':_0x3ded9b[_0x164a65][_0xe8ef('0x36')],'CampaignId':_0x3ded9b[_0x164a65][_0xe8ef('0x37')]}});_0x496402[_0xe8ef('0x38')](_0x1f04df);}return BPromise[_0xe8ef('0x39')](_0x496402);}})['then'](function(_0x20e6c9){logger[_0xe8ef('0x3a')]('[CmContact][afterDestroy][Succesfull]',util[_0xe8ef('0x13')](_0x20e6c9,{'showHidden':![],'depth':null}));_0x2d1c28();})['catch'](function(_0x22cc42){logger['error'](_0xe8ef('0x3b'),util['inspect'](_0x22cc42,{'showHidden':![],'depth':null}));_0x2d1c28(_0x22cc42);});}}});};