Built motion from commit 639a1a7f.|2.6.19
[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 _0x8dc1=['ContactId','VoiceQueueId','all','[CmContact][afterDestroy][Succesfull]','lodash','../../config/logger','api','moment','bluebird','path','rimraf','../../config/environment','../../config/environment/shared','./cmContact.attributes','exports','define','phone','fti_cm_contacts','firstName','lastName','mobile','fax','email','FULLTEXT','utf8mb4','utf8mb4_unicode_ci','models','get','error','cmContact','afterCreate','isNil','dialTimezone','VoiceQueue','findOne','timezones','utcOffset','scheduledat','minutes','then','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create','ListId','priority','format','callbackUniqueid','YYYY-MM-DD\x20HH:mm:ss','CmHopper','cmContact,\x20afterCreate\x20','\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','catch','inspect','Campaign','add','ret','\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','findAll','outbound','dialCheckDuplicateType','CmList','Lists','length','always','onlyIfOpen','cmContact,\x20%s,\x20%s','message','CmHopperBlack','destroy'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x8dc1,0xdc));var _0x18dc=function(_0x1045d4,_0x15a9cb){_0x1045d4=_0x1045d4-0x0;var _0x1c48d3=_0x8dc1[_0x1045d4];return _0x1c48d3;};'use strict';var _=require(_0x18dc('0x0'));var util=require('util');var logger=require(_0x18dc('0x1'))(_0x18dc('0x2'));var moment=require(_0x18dc('0x3'));var BPromise=require(_0x18dc('0x4'));var rp=require('request-promise');var fs=require('fs');var path=require(_0x18dc('0x5'));var rimraf=require(_0x18dc('0x6'));var config=require(_0x18dc('0x7'));var shared=require(_0x18dc('0x8'));var attributes=require(_0x18dc('0x9'));module[_0x18dc('0xa')]=function(_0x20abbf,_0x3718cd){return _0x20abbf[_0x18dc('0xb')]('CmContact',attributes,{'tableName':'cm_contacts','paranoid':!![],'indexes':[{'name':_0x18dc('0xc'),'fields':['phone']},{'name':_0x18dc('0xd'),'fields':[_0x18dc('0xe'),_0x18dc('0xf'),_0x18dc('0xc'),_0x18dc('0x10'),_0x18dc('0x11'),_0x18dc('0x12')],'type':_0x18dc('0x13')}],'timestamps':!![],'charset':_0x18dc('0x14'),'collate':_0x18dc('0x15'),'hooks':{'afterCreate':function(_0x54464e,_0x46e6d3,_0x582886){var _0xf24fef=_0x20abbf[_0x18dc('0x16')];var _0x298365=_0x54464e[_0x18dc('0x17')]({'plain':!![]});function _0x11de26(){return function(_0x369309){logger[_0x18dc('0x18')](_0x18dc('0x19'),_0x18dc('0x1a'),util['inspect'](_0x369309,{'showHidden':![],'depth':null}));};}function _0x89a018(_0x547362,_0x869471){var _0x1c891c=_0x869471['scheduledat']?_0x869471['scheduledat']:moment()['format']('YYYY-MM-DD\x20HH:mm:ss');return new BPromise(function(_0x5aab85,_0x2dbc4c){if(_[_0x18dc('0x1b')](_0x869471[_0x18dc('0x1c')])){_0x5aab85({'ret':_0x1c891c});}else{_0xf24fef[_0x18dc('0x1d')][_0x18dc('0x1e')]({'where':{'id':_0x547362['id']}})['then'](function(_0x26d7d7){if(!_[_0x18dc('0x1b')](_0x26d7d7)){var _0x58e980=_0x26d7d7[_0x18dc('0x1c')];if(!_[_0x18dc('0x1b')](_0x58e980)){var _0x5b4ba5=shared[_0x18dc('0x1f')];var _0xab6ec=_0x5b4ba5[_0x58e980];var _0x2c4635=_0x5b4ba5[_0x869471[_0x18dc('0x1c')]];if(!_[_0x18dc('0x1b')](_0xab6ec)&&!_[_0x18dc('0x1b')](_0x2c4635)){var _0x163f28=_0xab6ec[_0x18dc('0x20')]-_0x2c4635[_0x18dc('0x20')];_0x1c891c=_0x869471[_0x18dc('0x21')]?_0x869471[_0x18dc('0x21')]:moment();var _0x1f3677=moment(_0x1c891c)['add'](_0x163f28,_0x18dc('0x22'));_0x5aab85({'ret':_0x1f3677['format']('YYYY-MM-DD\x20HH:mm:ss')});}else _0x5aab85({'ret':_0x1c891c});}else _0x5aab85({'ret':_0x1c891c});}else{_0x5aab85({'ret':_0x1c891c});}});}});return 0xa;}var _0x23121b=function(_0x23383b){return new BPromise(function(_0x5d0968,_0x427d28){_0x89a018(_0x23383b,_0x298365)[_0x18dc('0x23')](function(_0x2cc8cd){_0x298365['scheduledat']=_0x2cc8cd['ret'];_0xf24fef['CmHopper']['findOne']({'where':{'phone':_0x298365[_0x18dc('0xc')],'VoiceQueueId':_0x23383b['id']}})[_0x18dc('0x23')](function(_0x174576){if(!_[_0x18dc('0x1b')](_0x174576)){throw new Error('cmContact,\x20afterCreate\x20'+_0x298365[_0x18dc('0xc')]+_0x18dc('0x24')+_0x23383b['id']);}return _0xf24fef['CmHopper'][_0x18dc('0x25')]({'phone':_0x298365[_0x18dc('0xc')],'ContactId':_0x298365['id'],'ListId':_0x298365[_0x18dc('0x26')],'priority':_0x298365[_0x18dc('0x27')]?_0x298365[_0x18dc('0x27')]:0x2,'scheduledat':_0x298365[_0x18dc('0x21')]?_0x298365[_0x18dc('0x21')]:moment()[_0x18dc('0x28')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x298365[_0x18dc('0x29')]?!![]:![],'callbackuniqueid':_0x298365[_0x18dc('0x29')]?_0x298365[_0x18dc('0x29')]:undefined,'callbackat':_0x298365[_0x18dc('0x29')]?moment()[_0x18dc('0x28')](_0x18dc('0x2a')):undefined,'VoiceQueueId':_0x23383b['id']});})['catch'](function(_0x316afb){_0x427d28(_0x316afb);});});});};var _0x2d49d9=function(_0x1567bb){return new BPromise(function(_0x59f21b,_0x5016f3){_0x89a018(_0x1567bb,_0x298365)[_0x18dc('0x23')](function(_0x1113fa){_0x298365[_0x18dc('0x21')]=_0x1113fa['ret'];_0xf24fef[_0x18dc('0x2b')]['findOne']({'where':{'phone':_0x298365[_0x18dc('0xc')],'VoiceQueueId':_0x1567bb['id']}})['then'](function(_0x1ad22f){if(!_[_0x18dc('0x1b')](_0x1ad22f)){throw new Error(_0x18dc('0x2c')+_0x298365[_0x18dc('0xc')]+_0x18dc('0x2d')+_0x1567bb['id']);}return _0xf24fef[_0x18dc('0x2e')][_0x18dc('0x1e')]({'where':{'calleridnum':_0x298365[_0x18dc('0xc')],'VoiceQueueId':_0x1567bb['id']}});})[_0x18dc('0x23')](function(_0x251392){if(!_[_0x18dc('0x1b')](_0x251392)){throw new Error(_0x18dc('0x2f')+_0x298365[_0x18dc('0xc')]+_0x18dc('0x30')+_0x1567bb['id']);}return _0xf24fef[_0x18dc('0x2b')][_0x18dc('0x25')]({'phone':_0x298365[_0x18dc('0xc')],'ContactId':_0x298365['id'],'ListId':_0x298365[_0x18dc('0x26')],'priority':_0x298365['priority']?_0x298365[_0x18dc('0x27')]:0x2,'scheduledat':_0x298365[_0x18dc('0x21')]?_0x298365[_0x18dc('0x21')]:moment()['format'](_0x18dc('0x2a')),'callback':_0x298365[_0x18dc('0x29')]?!![]:![],'callbackuniqueid':_0x298365[_0x18dc('0x29')]?_0x298365[_0x18dc('0x29')]:undefined,'callbackat':_0x298365[_0x18dc('0x29')]?moment()['format'](_0x18dc('0x2a')):undefined,'VoiceQueueId':_0x1567bb['id']});})[_0x18dc('0x31')](function(_0x56a181){_0x5016f3(_0x56a181);});});});};function _0x546f9a(){return function(_0x1b54f0){logger[_0x18dc('0x18')]('cmContact',_0x18dc('0x1a'),util[_0x18dc('0x32')](_0x1b54f0,{'showHidden':![],'depth':null}));};}function _0x2dea42(_0x5c958a,_0x2c3010){var _0x1f4150=_0x2c3010['scheduledat']?_0x2c3010[_0x18dc('0x21')]:moment()[_0x18dc('0x28')](_0x18dc('0x2a'));return new BPromise(function(_0x586f33,_0x4a561e){if(_[_0x18dc('0x1b')](_0x2c3010[_0x18dc('0x1c')])){_0x586f33({'ret':_0x1f4150});}else{_0xf24fef[_0x18dc('0x33')]['findOne']({'where':{'id':_0x5c958a['id']}})['then'](function(_0x4de482){if(!_[_0x18dc('0x1b')](_0x4de482)){var _0x11f286=_0x4de482['dialTimezone'];if(!_[_0x18dc('0x1b')](_0x11f286)){var _0x23e2a8=shared[_0x18dc('0x1f')];var _0x240f36=_0x23e2a8[_0x11f286];var _0x31ef1c=_0x23e2a8[_0x2c3010['dialTimezone']];if(!_['isNil'](_0x240f36)&&!_[_0x18dc('0x1b')](_0x31ef1c)){var _0x2bc94b=_0x240f36[_0x18dc('0x20')]-_0x31ef1c['utcOffset'];_0x1f4150=_0x2c3010[_0x18dc('0x21')]?_0x2c3010['scheduledat']:moment();var _0x29da0a=moment(_0x1f4150)[_0x18dc('0x34')](_0x2bc94b,_0x18dc('0x22'));_0x586f33({'ret':_0x29da0a[_0x18dc('0x28')]('YYYY-MM-DD\x20HH:mm:ss')});}else _0x586f33({'ret':_0x1f4150});}else _0x586f33({'ret':_0x1f4150});}else{_0x586f33({'ret':_0x1f4150});}});}});return 0xa;}var _0x390f40=function(_0x587006){return new BPromise(function(_0xc3bcf5,_0x58c127){_0x2dea42(_0x587006,_0x298365)[_0x18dc('0x23')](function(_0x18e911){_0x298365['scheduledat']=_0x18e911[_0x18dc('0x35')];_0xf24fef[_0x18dc('0x2b')][_0x18dc('0x1e')]({'where':{'phone':_0x298365['phone'],'CampaignId':_0x587006['id']}})[_0x18dc('0x23')](function(_0x2456ed){if(!_['isNil'](_0x2456ed)){throw new Error(_0x18dc('0x2c')+_0x298365['phone']+_0x18dc('0x36')+_0x587006['id']);}return _0xf24fef['CmHopper'][_0x18dc('0x25')]({'phone':_0x298365[_0x18dc('0xc')],'ContactId':_0x298365['id'],'ListId':_0x298365['ListId'],'priority':_0x298365[_0x18dc('0x27')]?_0x298365['priority']:0x2,'scheduledat':_0x298365['scheduledat']?_0x298365[_0x18dc('0x21')]:moment()[_0x18dc('0x28')](_0x18dc('0x2a')),'callback':_0x298365['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x298365[_0x18dc('0x29')]?_0x298365[_0x18dc('0x29')]:undefined,'callbackat':_0x298365[_0x18dc('0x29')]?moment()[_0x18dc('0x28')](_0x18dc('0x2a')):undefined,'CampaignId':_0x587006['id']});})['catch'](function(_0x424109){_0x58c127(_0x424109);});});});};var _0x21be35=function(_0x1d28b2){return new BPromise(function(_0x4c4a6f,_0x12f013){_0x2dea42(_0x1d28b2,_0x298365)['then'](function(_0x1bbcb8){_0x298365[_0x18dc('0x21')]=_0x1bbcb8['ret'];_0xf24fef[_0x18dc('0x2b')]['findOne']({'where':{'phone':_0x298365[_0x18dc('0xc')],'CampaignId':_0x1d28b2['id']}})[_0x18dc('0x23')](function(_0x2080b0){if(!_[_0x18dc('0x1b')](_0x2080b0)){throw new Error(_0x18dc('0x2c')+_0x298365['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0x1d28b2['id']);}return _0xf24fef[_0x18dc('0x2e')][_0x18dc('0x1e')]({'where':{'calleridnum':_0x298365['phone'],'CampaignId':_0x1d28b2['id']}});})[_0x18dc('0x23')](function(_0x211430){if(!_['isNil'](_0x211430)){throw new Error(_0x18dc('0x2f')+_0x298365[_0x18dc('0xc')]+_0x18dc('0x37')+_0x1d28b2['id']);}return _0xf24fef['CmHopper'][_0x18dc('0x25')]({'phone':_0x298365[_0x18dc('0xc')],'ContactId':_0x298365['id'],'ListId':_0x298365['ListId'],'priority':_0x298365[_0x18dc('0x27')]?_0x298365[_0x18dc('0x27')]:0x2,'scheduledat':_0x298365[_0x18dc('0x21')]?_0x298365[_0x18dc('0x21')]:moment()[_0x18dc('0x28')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x298365[_0x18dc('0x29')]?!![]:![],'callbackuniqueid':_0x298365[_0x18dc('0x29')]?_0x298365[_0x18dc('0x29')]:undefined,'callbackat':_0x298365[_0x18dc('0x29')]?moment()[_0x18dc('0x28')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x1d28b2['id']});})[_0x18dc('0x31')](function(_0x121805){_0x12f013(_0x121805);});});});};if(_0x298365[_0x18dc('0x26')]){_0xf24fef[_0x18dc('0x1d')][_0x18dc('0x38')]({'where':{'type':_0x18dc('0x39')},'attributes':['id',_0x18dc('0x3a'),_0x18dc('0x1c')],'include':[{'model':_0xf24fef[_0x18dc('0x3b')],'as':_0x18dc('0x3c'),'where':{'id':_0x298365[_0x18dc('0x26')]}}]})[_0x18dc('0x23')](function(_0xd2ecad){if(_0xd2ecad){var _0x4ea3b9=[];for(var _0x145224=0x0,_0x5e8d43;_0x145224<_0xd2ecad[_0x18dc('0x3d')];_0x145224+=0x1){_0x5e8d43=_0xd2ecad[_0x145224]['get']({'plain':!![]});switch(_0x5e8d43['dialCheckDuplicateType']){case _0x18dc('0x3e'):_0x2d49d9(_0x5e8d43)[_0x18dc('0x31')](_0x11de26());break;case _0x18dc('0x3f'):_0x23121b(_0x5e8d43)[_0x18dc('0x31')](_0x11de26());break;default:_0x2dea42(_0x5e8d43,_0x298365)[_0x18dc('0x23')](function(_0x346f41){_0x298365[_0x18dc('0x21')]=_0x346f41[_0x18dc('0x35')];_0xf24fef[_0x18dc('0x2b')]['create']({'phone':_0x298365[_0x18dc('0xc')],'ContactId':_0x298365['id'],'ListId':_0x298365[_0x18dc('0x26')],'scheduledat':_0x298365[_0x18dc('0x21')]?_0x298365[_0x18dc('0x21')]:moment()[_0x18dc('0x28')](_0x18dc('0x2a')),'priority':_0x298365['priority']?_0x298365[_0x18dc('0x27')]:0x2,'callback':_0x298365['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x298365['callbackUniqueid']?_0x298365['callbackUniqueid']:undefined,'callbackat':_0x298365['callbackUniqueid']?moment()[_0x18dc('0x28')](_0x18dc('0x2a')):undefined,'VoiceQueueId':_0x5e8d43['id']})[_0x18dc('0x31')](_0x11de26());});}}}})[_0x18dc('0x23')](function(){return _0xf24fef[_0x18dc('0x1d')][_0x18dc('0x38')]({'where':{'type':_0x18dc('0x39')},'attributes':['id'],'include':[{'model':_0xf24fef[_0x18dc('0x3b')],'as':'BlackLists','where':{'id':_0x298365['ListId']}}]})[_0x18dc('0x23')](function(_0x40b883){if(_0x40b883){var _0x2be09c=[];for(var _0x204be6=0x0,_0x3a194b;_0x204be6<_0x40b883[_0x18dc('0x3d')];_0x204be6+=0x1){_0x3a194b=_0x40b883[_0x204be6][_0x18dc('0x17')]({'plain':!![]});_0xf24fef['CmHopperBlack']['create']({'phone':_0x298365[_0x18dc('0xc')],'ContactId':_0x298365['id'],'ListId':_0x298365[_0x18dc('0x26')],'VoiceQueueId':_0x3a194b['id']})[_0x18dc('0x31')](function(_0x16203c){logger[_0x18dc('0x18')](_0x18dc('0x40'),_0x18dc('0x1a'),_0x16203c[_0x18dc('0x41')]);});}}});})[_0x18dc('0x23')](function(){return _0xf24fef[_0x18dc('0x33')][_0x18dc('0x38')]({'attributes':['id',_0x18dc('0x3a'),_0x18dc('0x1c')],'include':[{'model':_0xf24fef[_0x18dc('0x3b')],'as':_0x18dc('0x3c'),'where':{'id':_0x298365[_0x18dc('0x26')]}}]})[_0x18dc('0x23')](function(_0x5b8b38){if(_0x5b8b38){var _0x4d910f=[];for(var _0x5be296=0x0,_0x3d0b17;_0x5be296<_0x5b8b38[_0x18dc('0x3d')];_0x5be296+=0x1){_0x3d0b17=_0x5b8b38[_0x5be296][_0x18dc('0x17')]({'plain':!![]});switch(_0x3d0b17['dialCheckDuplicateType']){case _0x18dc('0x3e'):_0x21be35(_0x3d0b17)['catch'](_0x546f9a());break;case'onlyIfOpen':_0x390f40(_0x3d0b17)[_0x18dc('0x31')](_0x546f9a());break;default:_0xf24fef[_0x18dc('0x2b')][_0x18dc('0x25')]({'phone':_0x298365['phone'],'ContactId':_0x298365['id'],'ListId':_0x298365[_0x18dc('0x26')],'scheduledat':_0x298365[_0x18dc('0x21')]?_0x298365['scheduledat']:moment()[_0x18dc('0x28')](_0x18dc('0x2a')),'priority':_0x298365[_0x18dc('0x27')]?_0x298365[_0x18dc('0x27')]:0x2,'callback':_0x298365[_0x18dc('0x29')]?!![]:![],'callbackuniqueid':_0x298365[_0x18dc('0x29')]?_0x298365[_0x18dc('0x29')]:undefined,'callbackat':_0x298365[_0x18dc('0x29')]?moment()[_0x18dc('0x28')](_0x18dc('0x2a')):undefined,'CampaignId':_0x3d0b17['id']})['catch'](_0x546f9a());}}}});})['then'](function(){return _0xf24fef[_0x18dc('0x33')][_0x18dc('0x38')]({'attributes':['id'],'include':[{'model':_0xf24fef[_0x18dc('0x3b')],'as':'BlackLists','where':{'id':_0x298365['ListId']}}]})[_0x18dc('0x23')](function(_0x512cc2){if(_0x512cc2){var _0x12a64a=[];for(var _0x462a92=0x0,_0x3654b7;_0x462a92<_0x512cc2[_0x18dc('0x3d')];_0x462a92+=0x1){_0x3654b7=_0x512cc2[_0x462a92][_0x18dc('0x17')]({'plain':!![]});_0xf24fef[_0x18dc('0x42')][_0x18dc('0x25')]({'phone':_0x298365['phone'],'ContactId':_0x298365['id'],'ListId':_0x298365[_0x18dc('0x26')],'CampaignId':_0x3654b7['id']})[_0x18dc('0x31')](function(_0xc1007b){logger[_0x18dc('0x18')]('cmContact,\x20%s,\x20%s',_0x18dc('0x1a'),_0xc1007b[_0x18dc('0x41')]);});}}});})['then'](function(){_0x582886();})[_0x18dc('0x31')](function(_0x131018){logger[_0x18dc('0x18')]('cmContact,\x20%s,\x20%s','afterCreate',_0x131018['message']);_0x582886(_0x131018);});}else{_0x582886(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x46651a,_0x1ae446,_0xfa6ec0){var _0x2c568c=_0x20abbf[_0x18dc('0x16')];var _0x54b165=_0x46651a[_0x18dc('0x17')]({'plain':!![]});return _0x2c568c[_0x18dc('0x2b')][_0x18dc('0x38')]({'raw':!![],'where':{'ListId':_0x54b165[_0x18dc('0x26')],'ContactId':_0x54b165['id']}})[_0x18dc('0x23')](function(_0x440105){if(!_['isEmpty'](_0x440105)){var _0x35eb6c=[];for(var _0x54a888=0x0;_0x54a888<_0x440105[_0x18dc('0x3d')];_0x54a888+=0x1){var _0x349e36=_0x2c568c[_0x18dc('0x2b')][_0x18dc('0x43')]({'where':{'ListId':_0x440105[_0x54a888]['ListId'],'ContactId':_0x440105[_0x54a888][_0x18dc('0x44')],'VoiceQueueId':_0x440105[_0x54a888][_0x18dc('0x45')],'CampaignId':_0x440105[_0x54a888]['CampaignId']}});_0x35eb6c['push'](_0x349e36);}return BPromise[_0x18dc('0x46')](_0x35eb6c);}})['then'](function(_0x3bde25){logger['info'](_0x18dc('0x47'),util[_0x18dc('0x32')](_0x3bde25,{'showHidden':![],'depth':null}));_0xfa6ec0();})['catch'](function(_0x598e30){logger['error']('[CmContact][afterDestroy][Error]',util['inspect'](_0x598e30,{'showHidden':![],'depth':null}));_0xfa6ec0(_0x598e30);});}}});};