b11fc788bfde6e1d753ee2e49a22a601f50ac6ea
[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 _0x839e=['[CmContact][afterDestroy][Succesfull]','inspect','lodash','util','api','moment','path','rimraf','../../config/environment','../../config/environment/shared','./cmContact.attributes','exports','define','CmContact','cm_contacts','phone','firstName','lastName','mobile','email','utf8mb4','models','cmContact','scheduledat','YYYY-MM-DD\x20HH:mm:ss','isNil','dialTimezone','VoiceQueue','findOne','then','timezones','utcOffset','add','minutes','format','ret','CmHopper','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create','priority','callbackUniqueid','\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','ListId','catch','error','Campaign','\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','outbound','dialCheckDuplicateType','CmList','Lists','length','get','always','onlyIfOpen','findAll','BlackLists','CmHopperBlack','cmContact,\x20%s,\x20%s','afterCreate','message','List\x20not\x20found','destroy','ContactId','CampaignId','push','all','info'];(function(_0x4a9b61,_0x131a15){var _0x2c0c75=function(_0x51809b){while(--_0x51809b){_0x4a9b61['push'](_0x4a9b61['shift']());}};_0x2c0c75(++_0x131a15);}(_0x839e,0xdd));var _0xe839=function(_0x56dbe5,_0x57fa97){_0x56dbe5=_0x56dbe5-0x0;var _0x47009d=_0x839e[_0x56dbe5];return _0x47009d;};'use strict';var _=require(_0xe839('0x0'));var util=require(_0xe839('0x1'));var logger=require('../../config/logger')(_0xe839('0x2'));var moment=require(_0xe839('0x3'));var BPromise=require('bluebird');var rp=require('request-promise');var fs=require('fs');var path=require(_0xe839('0x4'));var rimraf=require(_0xe839('0x5'));var config=require(_0xe839('0x6'));var shared=require(_0xe839('0x7'));var attributes=require(_0xe839('0x8'));module[_0xe839('0x9')]=function(_0x51a6ea,_0x3163e4){return _0x51a6ea[_0xe839('0xa')](_0xe839('0xb'),attributes,{'tableName':_0xe839('0xc'),'paranoid':!![],'indexes':[{'name':_0xe839('0xd'),'fields':['phone']},{'name':'fti_cm_contacts','fields':[_0xe839('0xe'),_0xe839('0xf'),_0xe839('0xd'),_0xe839('0x10'),'fax',_0xe839('0x11')],'type':'FULLTEXT'}],'timestamps':!![],'charset':_0xe839('0x12'),'collate':'utf8mb4_unicode_ci','hooks':{'afterCreate':function(_0x5d50cb,_0x4b7ebb,_0x516853){var _0xf1a7bd=_0x51a6ea[_0xe839('0x13')];var _0x10e667=_0x5d50cb['get']({'plain':!![]});function _0x2608b9(){return function(_0x550a02){logger['error'](_0xe839('0x14'),'afterCreate',util['inspect'](_0x550a02,{'showHidden':![],'depth':null}));};}function _0x13a0bd(_0x2ca795,_0x42ee86){var _0x2251f5=_0x42ee86[_0xe839('0x15')]?_0x42ee86[_0xe839('0x15')]:moment()['format'](_0xe839('0x16'));return new BPromise(function(_0x58fa24,_0x482b28){if(_[_0xe839('0x17')](_0x42ee86[_0xe839('0x18')])){_0x58fa24({'ret':_0x2251f5});}else{_0xf1a7bd[_0xe839('0x19')][_0xe839('0x1a')]({'where':{'id':_0x2ca795['id']}})[_0xe839('0x1b')](function(_0x2270cd){if(!_[_0xe839('0x17')](_0x2270cd)){var _0x1232bd=_0x2270cd['dialTimezone'];if(!_[_0xe839('0x17')](_0x1232bd)){var _0x469729=shared[_0xe839('0x1c')];var _0x1468f6=_0x469729[_0x1232bd];var _0x2541b6=_0x469729[_0x42ee86[_0xe839('0x18')]];if(!_[_0xe839('0x17')](_0x1468f6)&&!_[_0xe839('0x17')](_0x2541b6)){var _0x5b2d28=_0x1468f6[_0xe839('0x1d')]-_0x2541b6[_0xe839('0x1d')];_0x2251f5=_0x42ee86[_0xe839('0x15')]?_0x42ee86[_0xe839('0x15')]:moment();var _0x5d2ce4=moment(_0x2251f5)[_0xe839('0x1e')](_0x5b2d28,_0xe839('0x1f'));_0x58fa24({'ret':_0x5d2ce4[_0xe839('0x20')](_0xe839('0x16'))});}else _0x58fa24({'ret':_0x2251f5});}else _0x58fa24({'ret':_0x2251f5});}else{_0x58fa24({'ret':_0x2251f5});}});}});return 0xa;}var _0x5b53ac=function(_0x11a0a0){return new BPromise(function(_0xae5cda,_0x21f9a5){_0x13a0bd(_0x11a0a0,_0x10e667)[_0xe839('0x1b')](function(_0x1a13f1){_0x10e667[_0xe839('0x15')]=_0x1a13f1[_0xe839('0x21')];_0xf1a7bd[_0xe839('0x22')][_0xe839('0x1a')]({'where':{'phone':_0x10e667[_0xe839('0xd')],'VoiceQueueId':_0x11a0a0['id']}})['then'](function(_0x2e8815){if(!_['isNil'](_0x2e8815)){throw new Error(_0xe839('0x23')+_0x10e667[_0xe839('0xd')]+_0xe839('0x24')+_0x11a0a0['id']);}return _0xf1a7bd[_0xe839('0x22')][_0xe839('0x25')]({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667['ListId'],'priority':_0x10e667[_0xe839('0x26')]?_0x10e667[_0xe839('0x26')]:0x2,'scheduledat':_0x10e667[_0xe839('0x15')]?_0x10e667[_0xe839('0x15')]:moment()[_0xe839('0x20')](_0xe839('0x16')),'callback':_0x10e667[_0xe839('0x27')]?!![]:![],'callbackuniqueid':_0x10e667[_0xe839('0x27')]?_0x10e667[_0xe839('0x27')]:undefined,'callbackat':_0x10e667[_0xe839('0x27')]?moment()[_0xe839('0x20')](_0xe839('0x16')):undefined,'VoiceQueueId':_0x11a0a0['id']});})['catch'](function(_0x3ec42d){_0x21f9a5(_0x3ec42d);});});});};var _0x33539e=function(_0x38053a){return new BPromise(function(_0x1fe995,_0x3d1925){_0x13a0bd(_0x38053a,_0x10e667)[_0xe839('0x1b')](function(_0x796608){_0x10e667[_0xe839('0x15')]=_0x796608[_0xe839('0x21')];_0xf1a7bd['CmHopper'][_0xe839('0x1a')]({'where':{'phone':_0x10e667['phone'],'VoiceQueueId':_0x38053a['id']}})[_0xe839('0x1b')](function(_0x850074){if(!_['isNil'](_0x850074)){throw new Error(_0xe839('0x23')+_0x10e667[_0xe839('0xd')]+_0xe839('0x28')+_0x38053a['id']);}return _0xf1a7bd['CmHopperFinal'][_0xe839('0x1a')]({'where':{'calleridnum':_0x10e667[_0xe839('0xd')],'VoiceQueueId':_0x38053a['id']}});})[_0xe839('0x1b')](function(_0xddc88c){if(!_[_0xe839('0x17')](_0xddc88c)){throw new Error(_0xe839('0x29')+_0x10e667[_0xe839('0xd')]+_0xe839('0x2a')+_0x38053a['id']);}return _0xf1a7bd[_0xe839('0x22')]['create']({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667[_0xe839('0x2b')],'priority':_0x10e667['priority']?_0x10e667['priority']:0x2,'scheduledat':_0x10e667['scheduledat']?_0x10e667[_0xe839('0x15')]:moment()[_0xe839('0x20')](_0xe839('0x16')),'callback':_0x10e667[_0xe839('0x27')]?!![]:![],'callbackuniqueid':_0x10e667['callbackUniqueid']?_0x10e667[_0xe839('0x27')]:undefined,'callbackat':_0x10e667[_0xe839('0x27')]?moment()[_0xe839('0x20')](_0xe839('0x16')):undefined,'VoiceQueueId':_0x38053a['id']});})[_0xe839('0x2c')](function(_0x3c6bb0){_0x3d1925(_0x3c6bb0);});});});};function _0x11c89e(){return function(_0x502398){logger[_0xe839('0x2d')](_0xe839('0x14'),'afterCreate',util['inspect'](_0x502398,{'showHidden':![],'depth':null}));};}function _0x3f6394(_0x947f8c,_0x4aa7d4){var _0x401f63=_0x4aa7d4[_0xe839('0x15')]?_0x4aa7d4['scheduledat']:moment()[_0xe839('0x20')]('YYYY-MM-DD\x20HH:mm:ss');return new BPromise(function(_0x5805e4,_0x541b23){if(_['isNil'](_0x4aa7d4['dialTimezone'])){_0x5805e4({'ret':_0x401f63});}else{_0xf1a7bd[_0xe839('0x2e')]['findOne']({'where':{'id':_0x947f8c['id']}})['then'](function(_0xbacfb4){if(!_[_0xe839('0x17')](_0xbacfb4)){var _0x240f24=_0xbacfb4[_0xe839('0x18')];if(!_[_0xe839('0x17')](_0x240f24)){var _0x140b7f=shared[_0xe839('0x1c')];var _0x282038=_0x140b7f[_0x240f24];var _0x5e5454=_0x140b7f[_0x4aa7d4[_0xe839('0x18')]];if(!_['isNil'](_0x282038)&&!_[_0xe839('0x17')](_0x5e5454)){var _0xe3a912=_0x282038[_0xe839('0x1d')]-_0x5e5454[_0xe839('0x1d')];_0x401f63=_0x4aa7d4[_0xe839('0x15')]?_0x4aa7d4[_0xe839('0x15')]:moment();var _0x4f9d7d=moment(_0x401f63)['add'](_0xe3a912,_0xe839('0x1f'));_0x5805e4({'ret':_0x4f9d7d[_0xe839('0x20')]('YYYY-MM-DD\x20HH:mm:ss')});}else _0x5805e4({'ret':_0x401f63});}else _0x5805e4({'ret':_0x401f63});}else{_0x5805e4({'ret':_0x401f63});}});}});return 0xa;}var _0x5cbcbc=function(_0x4f623c){return new BPromise(function(_0x5cd22d,_0xd2901c){_0x3f6394(_0x4f623c,_0x10e667)[_0xe839('0x1b')](function(_0x1e3867){_0x10e667[_0xe839('0x15')]=_0x1e3867[_0xe839('0x21')];_0xf1a7bd[_0xe839('0x22')]['findOne']({'where':{'phone':_0x10e667['phone'],'CampaignId':_0x4f623c['id']}})[_0xe839('0x1b')](function(_0x35ddc4){if(!_[_0xe839('0x17')](_0x35ddc4)){throw new Error(_0xe839('0x23')+_0x10e667[_0xe839('0xd')]+_0xe839('0x2f')+_0x4f623c['id']);}return _0xf1a7bd[_0xe839('0x22')]['create']({'phone':_0x10e667['phone'],'ContactId':_0x10e667['id'],'ListId':_0x10e667[_0xe839('0x2b')],'priority':_0x10e667[_0xe839('0x26')]?_0x10e667['priority']:0x2,'scheduledat':_0x10e667[_0xe839('0x15')]?_0x10e667[_0xe839('0x15')]:moment()[_0xe839('0x20')](_0xe839('0x16')),'callback':_0x10e667['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x10e667[_0xe839('0x27')]?_0x10e667[_0xe839('0x27')]:undefined,'callbackat':_0x10e667[_0xe839('0x27')]?moment()[_0xe839('0x20')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x4f623c['id']});})[_0xe839('0x2c')](function(_0x5369c8){_0xd2901c(_0x5369c8);});});});};var _0x2b70c0=function(_0x3520aa){return new BPromise(function(_0x1836e5,_0x36b585){_0x3f6394(_0x3520aa,_0x10e667)[_0xe839('0x1b')](function(_0x4fc89f){_0x10e667[_0xe839('0x15')]=_0x4fc89f['ret'];_0xf1a7bd['CmHopper'][_0xe839('0x1a')]({'where':{'phone':_0x10e667[_0xe839('0xd')],'CampaignId':_0x3520aa['id']}})[_0xe839('0x1b')](function(_0x25de1d){if(!_['isNil'](_0x25de1d)){throw new Error(_0xe839('0x23')+_0x10e667[_0xe839('0xd')]+_0xe839('0x30')+_0x3520aa['id']);}return _0xf1a7bd[_0xe839('0x31')]['findOne']({'where':{'calleridnum':_0x10e667[_0xe839('0xd')],'CampaignId':_0x3520aa['id']}});})[_0xe839('0x1b')](function(_0x1f0afd){if(!_[_0xe839('0x17')](_0x1f0afd)){throw new Error(_0xe839('0x29')+_0x10e667[_0xe839('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20'+_0x3520aa['id']);}return _0xf1a7bd[_0xe839('0x22')][_0xe839('0x25')]({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667[_0xe839('0x2b')],'priority':_0x10e667[_0xe839('0x26')]?_0x10e667[_0xe839('0x26')]:0x2,'scheduledat':_0x10e667[_0xe839('0x15')]?_0x10e667[_0xe839('0x15')]:moment()[_0xe839('0x20')](_0xe839('0x16')),'callback':_0x10e667['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x10e667['callbackUniqueid']?_0x10e667[_0xe839('0x27')]:undefined,'callbackat':_0x10e667[_0xe839('0x27')]?moment()[_0xe839('0x20')](_0xe839('0x16')):undefined,'CampaignId':_0x3520aa['id']});})['catch'](function(_0x564cc9){_0x36b585(_0x564cc9);});});});};if(_0x10e667[_0xe839('0x2b')]){_0xf1a7bd[_0xe839('0x19')]['findAll']({'where':{'type':_0xe839('0x32')},'attributes':['id',_0xe839('0x33'),_0xe839('0x18')],'include':[{'model':_0xf1a7bd[_0xe839('0x34')],'as':_0xe839('0x35'),'where':{'id':_0x10e667[_0xe839('0x2b')]}}]})[_0xe839('0x1b')](function(_0x2cb789){if(_0x2cb789){var _0x3ecd0d=[];for(var _0x225065=0x0,_0x587865;_0x225065<_0x2cb789[_0xe839('0x36')];_0x225065+=0x1){_0x587865=_0x2cb789[_0x225065][_0xe839('0x37')]({'plain':!![]});switch(_0x587865[_0xe839('0x33')]){case _0xe839('0x38'):_0x33539e(_0x587865)['catch'](_0x2608b9());break;case _0xe839('0x39'):_0x5b53ac(_0x587865)['catch'](_0x2608b9());break;default:_0x3f6394(_0x587865,_0x10e667)[_0xe839('0x1b')](function(_0x5578da){_0x10e667[_0xe839('0x15')]=_0x5578da[_0xe839('0x21')];_0xf1a7bd[_0xe839('0x22')][_0xe839('0x25')]({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667[_0xe839('0x2b')],'scheduledat':_0x10e667[_0xe839('0x15')]?_0x10e667[_0xe839('0x15')]:moment()[_0xe839('0x20')](_0xe839('0x16')),'priority':_0x10e667[_0xe839('0x26')]?_0x10e667[_0xe839('0x26')]:0x2,'callback':_0x10e667[_0xe839('0x27')]?!![]:![],'callbackuniqueid':_0x10e667['callbackUniqueid']?_0x10e667['callbackUniqueid']:undefined,'callbackat':_0x10e667['callbackUniqueid']?moment()['format'](_0xe839('0x16')):undefined,'VoiceQueueId':_0x587865['id']})[_0xe839('0x2c')](_0x2608b9());});}}}})[_0xe839('0x1b')](function(){return _0xf1a7bd[_0xe839('0x19')][_0xe839('0x3a')]({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0xf1a7bd[_0xe839('0x34')],'as':_0xe839('0x3b'),'where':{'id':_0x10e667[_0xe839('0x2b')]}}]})[_0xe839('0x1b')](function(_0x517c91){if(_0x517c91){var _0x141272=[];for(var _0x831bcd=0x0,_0x6e7666;_0x831bcd<_0x517c91[_0xe839('0x36')];_0x831bcd+=0x1){_0x6e7666=_0x517c91[_0x831bcd]['get']({'plain':!![]});_0xf1a7bd[_0xe839('0x3c')]['create']({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667['ListId'],'VoiceQueueId':_0x6e7666['id']})[_0xe839('0x2c')](function(_0xc02262){logger[_0xe839('0x2d')](_0xe839('0x3d'),_0xe839('0x3e'),_0xc02262[_0xe839('0x3f')]);});}}});})[_0xe839('0x1b')](function(){return _0xf1a7bd[_0xe839('0x2e')]['findAll']({'attributes':['id','dialCheckDuplicateType',_0xe839('0x18')],'include':[{'model':_0xf1a7bd[_0xe839('0x34')],'as':_0xe839('0x35'),'where':{'id':_0x10e667[_0xe839('0x2b')]}}]})[_0xe839('0x1b')](function(_0x267bb2){if(_0x267bb2){var _0x50459e=[];for(var _0x2c64a8=0x0,_0x45630c;_0x2c64a8<_0x267bb2[_0xe839('0x36')];_0x2c64a8+=0x1){_0x45630c=_0x267bb2[_0x2c64a8]['get']({'plain':!![]});switch(_0x45630c['dialCheckDuplicateType']){case _0xe839('0x38'):_0x2b70c0(_0x45630c)['catch'](_0x11c89e());break;case _0xe839('0x39'):_0x5cbcbc(_0x45630c)['catch'](_0x11c89e());break;default:_0xf1a7bd[_0xe839('0x22')][_0xe839('0x25')]({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667['ListId'],'scheduledat':_0x10e667[_0xe839('0x15')]?_0x10e667['scheduledat']:moment()[_0xe839('0x20')](_0xe839('0x16')),'priority':_0x10e667[_0xe839('0x26')]?_0x10e667[_0xe839('0x26')]:0x2,'callback':_0x10e667[_0xe839('0x27')]?!![]:![],'callbackuniqueid':_0x10e667[_0xe839('0x27')]?_0x10e667[_0xe839('0x27')]:undefined,'callbackat':_0x10e667[_0xe839('0x27')]?moment()[_0xe839('0x20')](_0xe839('0x16')):undefined,'CampaignId':_0x45630c['id']})[_0xe839('0x2c')](_0x11c89e());}}}});})['then'](function(){return _0xf1a7bd[_0xe839('0x2e')][_0xe839('0x3a')]({'attributes':['id'],'include':[{'model':_0xf1a7bd[_0xe839('0x34')],'as':_0xe839('0x3b'),'where':{'id':_0x10e667[_0xe839('0x2b')]}}]})['then'](function(_0x1231de){if(_0x1231de){var _0x45f88b=[];for(var _0x24a8c6=0x0,_0x56e0c2;_0x24a8c6<_0x1231de['length'];_0x24a8c6+=0x1){_0x56e0c2=_0x1231de[_0x24a8c6][_0xe839('0x37')]({'plain':!![]});_0xf1a7bd[_0xe839('0x3c')][_0xe839('0x25')]({'phone':_0x10e667[_0xe839('0xd')],'ContactId':_0x10e667['id'],'ListId':_0x10e667[_0xe839('0x2b')],'CampaignId':_0x56e0c2['id']})[_0xe839('0x2c')](function(_0x5d8a29){logger[_0xe839('0x2d')](_0xe839('0x3d'),'afterCreate',_0x5d8a29[_0xe839('0x3f')]);});}}});})[_0xe839('0x1b')](function(){_0x516853();})[_0xe839('0x2c')](function(_0x29f012){logger['error'](_0xe839('0x3d'),_0xe839('0x3e'),_0x29f012[_0xe839('0x3f')]);_0x516853(_0x29f012);});}else{_0x516853(new Error(_0xe839('0x40')));}},'afterDestroy':function(_0x8c8a5,_0x14d02f,_0x419858){var _0x65fd04=_0x51a6ea[_0xe839('0x13')];var _0x55f8fe=_0x8c8a5[_0xe839('0x37')]({'plain':!![]});return _0x65fd04['CmHopper'][_0xe839('0x3a')]({'raw':!![],'where':{'ListId':_0x55f8fe['ListId'],'ContactId':_0x55f8fe['id']}})['then'](function(_0x3f457a){if(!_['isEmpty'](_0x3f457a)){var _0x1be4b2=[];for(var _0x12e291=0x0;_0x12e291<_0x3f457a[_0xe839('0x36')];_0x12e291+=0x1){var _0x3549cc=_0x65fd04['CmHopper'][_0xe839('0x41')]({'where':{'ListId':_0x3f457a[_0x12e291][_0xe839('0x2b')],'ContactId':_0x3f457a[_0x12e291][_0xe839('0x42')],'VoiceQueueId':_0x3f457a[_0x12e291]['VoiceQueueId'],'CampaignId':_0x3f457a[_0x12e291][_0xe839('0x43')]}});_0x1be4b2[_0xe839('0x44')](_0x3549cc);}return BPromise[_0xe839('0x45')](_0x1be4b2);}})[_0xe839('0x1b')](function(_0x55b906){logger[_0xe839('0x46')](_0xe839('0x47'),util['inspect'](_0x55b906,{'showHidden':![],'depth':null}));_0x419858();})[_0xe839('0x2c')](function(_0x19780e){logger[_0xe839('0x2d')]('[CmContact][afterDestroy][Error]',util[_0xe839('0x48')](_0x19780e,{'showHidden':![],'depth':null}));_0x419858(_0x19780e);});}}});};