Built motion from commit 936e8062.|2.5.33
[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 _0x7593=['ListId','priority','scheduledat','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','format','error','afterCreate','cmContact,\x20afterCreate\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','CmList','Lists','length','always','findAll','BlackLists','CmHopperBlack','cmContact,\x20%s,\x20%s','dialCheckDuplicateType','Campaign','message','isEmpty','destroy','CampaignId','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','api','moment','bluebird','request-promise','path','rimraf','./cmContact.attributes','define','CmContact','cm_contacts','phone','lastName','mobile','email','FULLTEXT','utf8mb4','get','cmContact','inspect','CmHopper','findOne','then','isNil','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create'];(function(_0x107ede,_0x5b5dc8){var _0x53be8a=function(_0x48774a){while(--_0x48774a){_0x107ede['push'](_0x107ede['shift']());}};_0x53be8a(++_0x5b5dc8);}(_0x7593,0x196));var _0x3759=function(_0xe33eca,_0x332ee5){_0xe33eca=_0xe33eca-0x0;var _0x2184d6=_0x7593[_0xe33eca];return _0x2184d6;};'use strict';var _=require(_0x3759('0x0'));var util=require(_0x3759('0x1'));var logger=require(_0x3759('0x2'))(_0x3759('0x3'));var moment=require(_0x3759('0x4'));var BPromise=require(_0x3759('0x5'));var rp=require(_0x3759('0x6'));var fs=require('fs');var path=require(_0x3759('0x7'));var rimraf=require(_0x3759('0x8'));var config=require('../../config/environment');var attributes=require(_0x3759('0x9'));module['exports']=function(_0x3510a7,_0x3a89f0){return _0x3510a7[_0x3759('0xa')](_0x3759('0xb'),attributes,{'tableName':_0x3759('0xc'),'paranoid':!![],'indexes':[{'name':_0x3759('0xd'),'fields':[_0x3759('0xd')]},{'name':'fti_cm_contacts','fields':['firstName',_0x3759('0xe'),_0x3759('0xd'),_0x3759('0xf'),'fax',_0x3759('0x10')],'type':_0x3759('0x11')}],'timestamps':!![],'charset':_0x3759('0x12'),'collate':'utf8mb4_unicode_ci','hooks':{'afterCreate':function(_0x3d3b14,_0x37caad,_0x2d69a1){var _0x4d4919=_0x3510a7['models'];var _0x126591=_0x3d3b14[_0x3759('0x13')]({'plain':!![]});function _0x293eef(){return function(_0x2dd40a){logger['error'](_0x3759('0x14'),'afterCreate',util[_0x3759('0x15')](_0x2dd40a,{'showHidden':![],'depth':null}));};}var _0x5c67a1=function(_0xf0cb3){return new BPromise(function(_0x20acfc,_0x4032aa){_0x4d4919[_0x3759('0x16')][_0x3759('0x17')]({'where':{'phone':_0x126591[_0x3759('0xd')],'VoiceQueueId':_0xf0cb3['id']}})[_0x3759('0x18')](function(_0x1e7d02){if(!_[_0x3759('0x19')](_0x1e7d02)){throw new Error('cmContact,\x20afterCreate\x20'+_0x126591[_0x3759('0xd')]+_0x3759('0x1a')+_0xf0cb3['id']);}return _0x4d4919[_0x3759('0x16')][_0x3759('0x1b')]({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'priority':_0x126591['priority']?_0x126591[_0x3759('0x1d')]:0x2,'scheduledat':_0x126591['scheduledat']?_0x126591[_0x3759('0x1e')]:moment()['format'](_0x3759('0x1f')),'callback':_0x126591['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x126591[_0x3759('0x20')]?_0x126591['callbackUniqueid']:undefined,'callbackat':_0x126591[_0x3759('0x20')]?moment()['format'](_0x3759('0x1f')):undefined,'VoiceQueueId':_0xf0cb3['id']});})[_0x3759('0x21')](function(_0x50f6aa){_0x4032aa(_0x50f6aa);});});};var _0x57caac=function(_0x18f528){return new BPromise(function(_0x55e385,_0xa31fe8){_0x4d4919['CmHopper']['findOne']({'where':{'phone':_0x126591[_0x3759('0xd')],'VoiceQueueId':_0x18f528['id']}})['then'](function(_0x131cf3){if(!_['isNil'](_0x131cf3)){throw new Error('cmContact,\x20afterCreate\x20'+_0x126591[_0x3759('0xd')]+_0x3759('0x22')+_0x18f528['id']);}return _0x4d4919[_0x3759('0x23')][_0x3759('0x17')]({'where':{'calleridnum':_0x126591[_0x3759('0xd')],'VoiceQueueId':_0x18f528['id']}});})['then'](function(_0x5337fa){if(!_['isNil'](_0x5337fa)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x126591[_0x3759('0xd')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20'+_0x18f528['id']);}return _0x4d4919[_0x3759('0x16')]['create']({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'priority':_0x126591[_0x3759('0x1d')]?_0x126591['priority']:0x2,'scheduledat':_0x126591['scheduledat']?_0x126591['scheduledat']:moment()[_0x3759('0x24')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x126591[_0x3759('0x20')]?!![]:![],'callbackuniqueid':_0x126591['callbackUniqueid']?_0x126591[_0x3759('0x20')]:undefined,'callbackat':_0x126591[_0x3759('0x20')]?moment()[_0x3759('0x24')](_0x3759('0x1f')):undefined,'VoiceQueueId':_0x18f528['id']});})[_0x3759('0x21')](function(_0x1dbe55){_0xa31fe8(_0x1dbe55);});});};function _0x3198ec(){return function(_0x209325){logger[_0x3759('0x25')](_0x3759('0x14'),_0x3759('0x26'),util[_0x3759('0x15')](_0x209325,{'showHidden':![],'depth':null}));};}var _0x4a7e32=function(_0xbefbec){return new BPromise(function(_0x47e94f,_0x161cb3){_0x4d4919[_0x3759('0x16')][_0x3759('0x17')]({'where':{'phone':_0x126591['phone'],'CampaignId':_0xbefbec['id']}})['then'](function(_0x4fcc76){if(!_[_0x3759('0x19')](_0x4fcc76)){throw new Error('cmContact,\x20afterCreate\x20'+_0x126591['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20'+_0xbefbec['id']);}return _0x4d4919[_0x3759('0x16')]['create']({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591['ListId'],'priority':_0x126591[_0x3759('0x1d')]?_0x126591[_0x3759('0x1d')]:0x2,'scheduledat':_0x126591[_0x3759('0x1e')]?_0x126591[_0x3759('0x1e')]:moment()[_0x3759('0x24')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x126591['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x126591[_0x3759('0x20')]?_0x126591[_0x3759('0x20')]:undefined,'callbackat':_0x126591[_0x3759('0x20')]?moment()[_0x3759('0x24')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0xbefbec['id']});})['catch'](function(_0x2bef23){_0x161cb3(_0x2bef23);});});};var _0x25ab45=function(_0x2683c5){return new BPromise(function(_0x4892cf,_0x50b149){_0x4d4919[_0x3759('0x16')]['findOne']({'where':{'phone':_0x126591[_0x3759('0xd')],'CampaignId':_0x2683c5['id']}})[_0x3759('0x18')](function(_0x550789){if(!_[_0x3759('0x19')](_0x550789)){throw new Error(_0x3759('0x27')+_0x126591[_0x3759('0xd')]+_0x3759('0x28')+_0x2683c5['id']);}return _0x4d4919[_0x3759('0x23')][_0x3759('0x17')]({'where':{'calleridnum':_0x126591['phone'],'CampaignId':_0x2683c5['id']}});})[_0x3759('0x18')](function(_0x41ba69){if(!_[_0x3759('0x19')](_0x41ba69)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x126591[_0x3759('0xd')]+_0x3759('0x29')+_0x2683c5['id']);}return _0x4d4919[_0x3759('0x16')][_0x3759('0x1b')]({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'priority':_0x126591[_0x3759('0x1d')]?_0x126591['priority']:0x2,'scheduledat':_0x126591[_0x3759('0x1e')]?_0x126591[_0x3759('0x1e')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x126591[_0x3759('0x20')]?!![]:![],'callbackuniqueid':_0x126591[_0x3759('0x20')]?_0x126591['callbackUniqueid']:undefined,'callbackat':_0x126591[_0x3759('0x20')]?moment()[_0x3759('0x24')](_0x3759('0x1f')):undefined,'CampaignId':_0x2683c5['id']});})[_0x3759('0x21')](function(_0x3e00f5){_0x50b149(_0x3e00f5);});});};if(_0x126591['ListId']){_0x4d4919[_0x3759('0x2a')]['findAll']({'where':{'type':_0x3759('0x2b')},'attributes':['id','dialCheckDuplicateType'],'include':[{'model':_0x4d4919[_0x3759('0x2c')],'as':_0x3759('0x2d'),'where':{'id':_0x126591[_0x3759('0x1c')]}}]})[_0x3759('0x18')](function(_0x41599a){if(_0x41599a){var _0x4c2633=[];for(var _0xb7ae26=0x0,_0x239487;_0xb7ae26<_0x41599a[_0x3759('0x2e')];_0xb7ae26+=0x1){_0x239487=_0x41599a[_0xb7ae26][_0x3759('0x13')]({'plain':!![]});switch(_0x239487['dialCheckDuplicateType']){case _0x3759('0x2f'):_0x57caac(_0x239487)['catch'](_0x293eef());break;case'onlyIfOpen':_0x5c67a1(_0x239487)['catch'](_0x293eef());break;default:_0x4d4919[_0x3759('0x16')][_0x3759('0x1b')]({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'scheduledat':_0x126591[_0x3759('0x1e')]?_0x126591[_0x3759('0x1e')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x126591[_0x3759('0x1d')]?_0x126591[_0x3759('0x1d')]:0x2,'callback':_0x126591[_0x3759('0x20')]?!![]:![],'callbackuniqueid':_0x126591[_0x3759('0x20')]?_0x126591[_0x3759('0x20')]:undefined,'callbackat':_0x126591[_0x3759('0x20')]?moment()[_0x3759('0x24')](_0x3759('0x1f')):undefined,'VoiceQueueId':_0x239487['id']})[_0x3759('0x21')](_0x293eef());}}}})[_0x3759('0x18')](function(){return _0x4d4919[_0x3759('0x2a')][_0x3759('0x30')]({'where':{'type':_0x3759('0x2b')},'attributes':['id'],'include':[{'model':_0x4d4919[_0x3759('0x2c')],'as':_0x3759('0x31'),'where':{'id':_0x126591[_0x3759('0x1c')]}}]})[_0x3759('0x18')](function(_0x506a2e){if(_0x506a2e){var _0x14b913=[];for(var _0x4f44d4=0x0,_0x2104c2;_0x4f44d4<_0x506a2e[_0x3759('0x2e')];_0x4f44d4+=0x1){_0x2104c2=_0x506a2e[_0x4f44d4]['get']({'plain':!![]});_0x4d4919[_0x3759('0x32')][_0x3759('0x1b')]({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'VoiceQueueId':_0x2104c2['id']})['catch'](function(_0x26e751){logger['error'](_0x3759('0x33'),_0x3759('0x26'),_0x26e751['message']);});}}});})[_0x3759('0x18')](function(){return _0x4d4919['Campaign']['findAll']({'attributes':['id',_0x3759('0x34')],'include':[{'model':_0x4d4919['CmList'],'as':'Lists','where':{'id':_0x126591[_0x3759('0x1c')]}}]})['then'](function(_0xb6cd8c){if(_0xb6cd8c){var _0x1610cb=[];for(var _0x23661c=0x0,_0xac2e2a;_0x23661c<_0xb6cd8c['length'];_0x23661c+=0x1){_0xac2e2a=_0xb6cd8c[_0x23661c][_0x3759('0x13')]({'plain':!![]});switch(_0xac2e2a[_0x3759('0x34')]){case _0x3759('0x2f'):_0x25ab45(_0xac2e2a)['catch'](_0x3198ec());break;case'onlyIfOpen':_0x4a7e32(_0xac2e2a)[_0x3759('0x21')](_0x3198ec());break;default:_0x4d4919[_0x3759('0x16')][_0x3759('0x1b')]({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'scheduledat':_0x126591['scheduledat']?_0x126591[_0x3759('0x1e')]:moment()['format'](_0x3759('0x1f')),'priority':_0x126591[_0x3759('0x1d')]?_0x126591[_0x3759('0x1d')]:0x2,'callback':_0x126591['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x126591[_0x3759('0x20')]?_0x126591[_0x3759('0x20')]:undefined,'callbackat':_0x126591['callbackUniqueid']?moment()[_0x3759('0x24')](_0x3759('0x1f')):undefined,'CampaignId':_0xac2e2a['id']})['catch'](_0x3198ec());}}}});})['then'](function(){return _0x4d4919[_0x3759('0x35')][_0x3759('0x30')]({'attributes':['id'],'include':[{'model':_0x4d4919[_0x3759('0x2c')],'as':_0x3759('0x31'),'where':{'id':_0x126591[_0x3759('0x1c')]}}]})[_0x3759('0x18')](function(_0x5a3886){if(_0x5a3886){var _0x496242=[];for(var _0x1cc8d0=0x0,_0x459b3b;_0x1cc8d0<_0x5a3886['length'];_0x1cc8d0+=0x1){_0x459b3b=_0x5a3886[_0x1cc8d0][_0x3759('0x13')]({'plain':!![]});_0x4d4919[_0x3759('0x32')][_0x3759('0x1b')]({'phone':_0x126591[_0x3759('0xd')],'ContactId':_0x126591['id'],'ListId':_0x126591[_0x3759('0x1c')],'CampaignId':_0x459b3b['id']})['catch'](function(_0x4e8868){logger[_0x3759('0x25')](_0x3759('0x33'),_0x3759('0x26'),_0x4e8868['message']);});}}});})[_0x3759('0x18')](function(){_0x2d69a1();})[_0x3759('0x21')](function(_0x355608){logger[_0x3759('0x25')]('cmContact,\x20%s,\x20%s','afterCreate',_0x355608[_0x3759('0x36')]);_0x2d69a1(_0x355608);});}else{_0x2d69a1(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x88e8e1,_0x6fe59b,_0x185259){var _0x4a1834=_0x3510a7['models'];var _0x2f4e77=_0x88e8e1['get']({'plain':!![]});return _0x4a1834[_0x3759('0x16')]['findAll']({'raw':!![],'where':{'ListId':_0x2f4e77['ListId'],'ContactId':_0x2f4e77['id']}})[_0x3759('0x18')](function(_0xfca382){if(!_[_0x3759('0x37')](_0xfca382)){var _0x144328=[];for(var _0x43dfb7=0x0;_0x43dfb7<_0xfca382[_0x3759('0x2e')];_0x43dfb7+=0x1){var _0x36073b=_0x4a1834['CmHopper'][_0x3759('0x38')]({'where':{'ListId':_0xfca382[_0x43dfb7]['ListId'],'ContactId':_0xfca382[_0x43dfb7]['ContactId'],'VoiceQueueId':_0xfca382[_0x43dfb7]['VoiceQueueId'],'CampaignId':_0xfca382[_0x43dfb7][_0x3759('0x39')]}});_0x144328['push'](_0x36073b);}return BPromise[_0x3759('0x3a')](_0x144328);}})[_0x3759('0x18')](function(_0x439a0e){logger[_0x3759('0x3b')](_0x3759('0x3c'),util['inspect'](_0x439a0e,{'showHidden':![],'depth':null}));_0x185259();})[_0x3759('0x21')](function(_0x64b3f){logger[_0x3759('0x25')](_0x3759('0x3d'),util[_0x3759('0x15')](_0x64b3f,{'showHidden':![],'depth':null}));_0x185259(_0x64b3f);});}}});};