c7a5a79d161a9e8f794fbdb293d2ebdb4a3abcc0
[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 _0x644f=['moment','bluebird','request-promise','path','rimraf','../../config/environment','define','cm_contacts','phone','fti_cm_contacts','firstName','mobile','fax','email','models','get','error','cmContact','afterCreate','inspect','findOne','then','isNil','cmContact,\x20afterCreate\x20','create','ListId','priority','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','CmHopper','CmHopperFinal','cmContact,\x20afterCreate,\x20','\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','outbound','dialCheckDuplicateType','Lists','onlyIfOpen','findAll','CmList','BlackLists','length','CmHopperBlack','cmContact,\x20%s,\x20%s','Campaign','always','message','List\x20not\x20found','isEmpty','destroy','VoiceQueueId','CampaignId','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','util','../../config/logger','api'];(function(_0x74a3e5,_0x3fe04b){var _0xbc600a=function(_0x16d7a6){while(--_0x16d7a6){_0x74a3e5['push'](_0x74a3e5['shift']());}};_0xbc600a(++_0x3fe04b);}(_0x644f,0x177));var _0xf644=function(_0x149ca0,_0x2ac7f5){_0x149ca0=_0x149ca0-0x0;var _0x56b6b9=_0x644f[_0x149ca0];return _0x56b6b9;};'use strict';var _=require('lodash');var util=require(_0xf644('0x0'));var logger=require(_0xf644('0x1'))(_0xf644('0x2'));var moment=require(_0xf644('0x3'));var BPromise=require(_0xf644('0x4'));var rp=require(_0xf644('0x5'));var fs=require('fs');var path=require(_0xf644('0x6'));var rimraf=require(_0xf644('0x7'));var config=require(_0xf644('0x8'));var attributes=require('./cmContact.attributes');module['exports']=function(_0x23cd49,_0x16e2f2){return _0x23cd49[_0xf644('0x9')]('CmContact',attributes,{'tableName':_0xf644('0xa'),'paranoid':!![],'indexes':[{'name':_0xf644('0xb'),'fields':[_0xf644('0xb')]},{'name':_0xf644('0xc'),'fields':[_0xf644('0xd'),'lastName',_0xf644('0xb'),_0xf644('0xe'),_0xf644('0xf'),_0xf644('0x10')],'type':'FULLTEXT'}],'timestamps':!![],'hooks':{'afterCreate':function(_0x5d403f,_0x82ed2d,_0x517447){var _0x5914c8=_0x23cd49[_0xf644('0x11')];var _0x329395=_0x5d403f[_0xf644('0x12')]({'plain':!![]});function _0x5c103f(){return function(_0x3f6a40){logger[_0xf644('0x13')](_0xf644('0x14'),_0xf644('0x15'),util[_0xf644('0x16')](_0x3f6a40,{'showHidden':![],'depth':null}));};}var _0x4ddae2=function(_0x5dd82b){return new BPromise(function(_0x48150d,_0x58e7bf){_0x5914c8['CmHopper'][_0xf644('0x17')]({'where':{'phone':_0x329395['phone'],'VoiceQueueId':_0x5dd82b['id']}})[_0xf644('0x18')](function(_0x1af7cb){if(!_[_0xf644('0x19')](_0x1af7cb)){throw new Error(_0xf644('0x1a')+_0x329395[_0xf644('0xb')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0x5dd82b['id']);}return _0x5914c8['CmHopper'][_0xf644('0x1b')]({'phone':_0x329395[_0xf644('0xb')],'ContactId':_0x329395['id'],'ListId':_0x329395[_0xf644('0x1c')],'priority':_0x329395[_0xf644('0x1d')]?_0x329395[_0xf644('0x1d')]:0x2,'scheduledat':_0x329395[_0xf644('0x1e')]?_0x329395[_0xf644('0x1e')]:moment()[_0xf644('0x1f')](_0xf644('0x20')),'callback':_0x329395[_0xf644('0x21')]?!![]:![],'callbackuniqueid':_0x329395[_0xf644('0x21')]?_0x329395['callbackUniqueid']:undefined,'callbackat':_0x329395[_0xf644('0x21')]?moment()[_0xf644('0x1f')](_0xf644('0x20')):undefined,'VoiceQueueId':_0x5dd82b['id']});})[_0xf644('0x22')](function(_0x19f7fb){_0x58e7bf(_0x19f7fb);});});};var _0x5be5e5=function(_0x440314){return new BPromise(function(_0x1aa245,_0x4264f1){_0x5914c8[_0xf644('0x23')][_0xf644('0x17')]({'where':{'phone':_0x329395['phone'],'VoiceQueueId':_0x440314['id']}})[_0xf644('0x18')](function(_0x2fe394){if(!_[_0xf644('0x19')](_0x2fe394)){throw new Error(_0xf644('0x1a')+_0x329395['phone']+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20'+_0x440314['id']);}return _0x5914c8[_0xf644('0x24')][_0xf644('0x17')]({'where':{'calleridnum':_0x329395[_0xf644('0xb')],'VoiceQueueId':_0x440314['id']}});})['then'](function(_0x5dd1b5){if(!_[_0xf644('0x19')](_0x5dd1b5)){throw new Error(_0xf644('0x25')+_0x329395[_0xf644('0xb')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20'+_0x440314['id']);}return _0x5914c8[_0xf644('0x23')]['create']({'phone':_0x329395[_0xf644('0xb')],'ContactId':_0x329395['id'],'ListId':_0x329395[_0xf644('0x1c')],'priority':_0x329395[_0xf644('0x1d')]?_0x329395[_0xf644('0x1d')]:0x2,'scheduledat':_0x329395[_0xf644('0x1e')]?_0x329395['scheduledat']:moment()['format'](_0xf644('0x20')),'callback':_0x329395[_0xf644('0x21')]?!![]:![],'callbackuniqueid':_0x329395[_0xf644('0x21')]?_0x329395['callbackUniqueid']:undefined,'callbackat':_0x329395[_0xf644('0x21')]?moment()[_0xf644('0x1f')](_0xf644('0x20')):undefined,'VoiceQueueId':_0x440314['id']});})[_0xf644('0x22')](function(_0x192cf0){_0x4264f1(_0x192cf0);});});};function _0x485dbe(){return function(_0x24ea0f){logger[_0xf644('0x13')](_0xf644('0x14'),'afterCreate',util['inspect'](_0x24ea0f,{'showHidden':![],'depth':null}));};}var _0x474ada=function(_0x4dcf5b){return new BPromise(function(_0x307ce0,_0x8c3425){_0x5914c8[_0xf644('0x23')][_0xf644('0x17')]({'where':{'phone':_0x329395[_0xf644('0xb')],'CampaignId':_0x4dcf5b['id']}})[_0xf644('0x18')](function(_0x85c1b){if(!_[_0xf644('0x19')](_0x85c1b)){throw new Error('cmContact,\x20afterCreate\x20'+_0x329395['phone']+_0xf644('0x26')+_0x4dcf5b['id']);}return _0x5914c8['CmHopper'][_0xf644('0x1b')]({'phone':_0x329395[_0xf644('0xb')],'ContactId':_0x329395['id'],'ListId':_0x329395['ListId'],'priority':_0x329395[_0xf644('0x1d')]?_0x329395['priority']:0x2,'scheduledat':_0x329395[_0xf644('0x1e')]?_0x329395[_0xf644('0x1e')]:moment()[_0xf644('0x1f')](_0xf644('0x20')),'callback':_0x329395[_0xf644('0x21')]?!![]:![],'callbackuniqueid':_0x329395[_0xf644('0x21')]?_0x329395[_0xf644('0x21')]:undefined,'callbackat':_0x329395[_0xf644('0x21')]?moment()[_0xf644('0x1f')](_0xf644('0x20')):undefined,'CampaignId':_0x4dcf5b['id']});})[_0xf644('0x22')](function(_0x417276){_0x8c3425(_0x417276);});});};var _0x5bb969=function(_0x47675f){return new BPromise(function(_0x4254a5,_0x30a6c2){_0x5914c8[_0xf644('0x23')][_0xf644('0x17')]({'where':{'phone':_0x329395[_0xf644('0xb')],'CampaignId':_0x47675f['id']}})[_0xf644('0x18')](function(_0x3b8b10){if(!_['isNil'](_0x3b8b10)){throw new Error(_0xf644('0x1a')+_0x329395['phone']+_0xf644('0x27')+_0x47675f['id']);}return _0x5914c8[_0xf644('0x24')][_0xf644('0x17')]({'where':{'calleridnum':_0x329395[_0xf644('0xb')],'CampaignId':_0x47675f['id']}});})[_0xf644('0x18')](function(_0x47bb6e){if(!_[_0xf644('0x19')](_0x47bb6e)){throw new Error(_0xf644('0x25')+_0x329395['phone']+_0xf644('0x28')+_0x47675f['id']);}return _0x5914c8[_0xf644('0x23')][_0xf644('0x1b')]({'phone':_0x329395['phone'],'ContactId':_0x329395['id'],'ListId':_0x329395[_0xf644('0x1c')],'priority':_0x329395['priority']?_0x329395[_0xf644('0x1d')]:0x2,'scheduledat':_0x329395['scheduledat']?_0x329395['scheduledat']:moment()[_0xf644('0x1f')](_0xf644('0x20')),'callback':_0x329395[_0xf644('0x21')]?!![]:![],'callbackuniqueid':_0x329395[_0xf644('0x21')]?_0x329395['callbackUniqueid']:undefined,'callbackat':_0x329395[_0xf644('0x21')]?moment()[_0xf644('0x1f')](_0xf644('0x20')):undefined,'CampaignId':_0x47675f['id']});})[_0xf644('0x22')](function(_0x124c63){_0x30a6c2(_0x124c63);});});};if(_0x329395[_0xf644('0x1c')]){_0x5914c8['VoiceQueue']['findAll']({'where':{'type':_0xf644('0x29')},'attributes':['id',_0xf644('0x2a')],'include':[{'model':_0x5914c8['CmList'],'as':_0xf644('0x2b'),'where':{'id':_0x329395[_0xf644('0x1c')]}}]})[_0xf644('0x18')](function(_0x8946a7){if(_0x8946a7){var _0x326aff=[];for(var _0x395db8=0x0,_0x267858;_0x395db8<_0x8946a7['length'];_0x395db8+=0x1){_0x267858=_0x8946a7[_0x395db8][_0xf644('0x12')]({'plain':!![]});switch(_0x267858['dialCheckDuplicateType']){case'always':_0x5be5e5(_0x267858)[_0xf644('0x22')](_0x5c103f());break;case _0xf644('0x2c'):_0x4ddae2(_0x267858)[_0xf644('0x22')](_0x5c103f());break;default:_0x5914c8['CmHopper']['create']({'phone':_0x329395['phone'],'ContactId':_0x329395['id'],'ListId':_0x329395['ListId'],'scheduledat':_0x329395[_0xf644('0x1e')]?_0x329395[_0xf644('0x1e')]:moment()[_0xf644('0x1f')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x329395[_0xf644('0x1d')]?_0x329395[_0xf644('0x1d')]:0x2,'callback':_0x329395[_0xf644('0x21')]?!![]:![],'callbackuniqueid':_0x329395[_0xf644('0x21')]?_0x329395[_0xf644('0x21')]:undefined,'callbackat':_0x329395[_0xf644('0x21')]?moment()[_0xf644('0x1f')](_0xf644('0x20')):undefined,'VoiceQueueId':_0x267858['id']})[_0xf644('0x22')](_0x5c103f());}}}})[_0xf644('0x18')](function(){return _0x5914c8['VoiceQueue'][_0xf644('0x2d')]({'where':{'type':_0xf644('0x29')},'attributes':['id'],'include':[{'model':_0x5914c8[_0xf644('0x2e')],'as':_0xf644('0x2f'),'where':{'id':_0x329395[_0xf644('0x1c')]}}]})[_0xf644('0x18')](function(_0x433017){if(_0x433017){var _0x2bd0cd=[];for(var _0x5df799=0x0,_0xadb2b;_0x5df799<_0x433017[_0xf644('0x30')];_0x5df799+=0x1){_0xadb2b=_0x433017[_0x5df799]['get']({'plain':!![]});_0x5914c8[_0xf644('0x31')][_0xf644('0x1b')]({'phone':_0x329395['phone'],'ContactId':_0x329395['id'],'ListId':_0x329395[_0xf644('0x1c')],'VoiceQueueId':_0xadb2b['id']})[_0xf644('0x22')](function(_0x232853){logger[_0xf644('0x13')](_0xf644('0x32'),'afterCreate',_0x232853['message']);});}}});})[_0xf644('0x18')](function(){return _0x5914c8[_0xf644('0x33')]['findAll']({'attributes':['id','dialCheckDuplicateType'],'include':[{'model':_0x5914c8[_0xf644('0x2e')],'as':_0xf644('0x2b'),'where':{'id':_0x329395[_0xf644('0x1c')]}}]})['then'](function(_0x48851a){if(_0x48851a){var _0xc59059=[];for(var _0xd926a6=0x0,_0x5f3e42;_0xd926a6<_0x48851a[_0xf644('0x30')];_0xd926a6+=0x1){_0x5f3e42=_0x48851a[_0xd926a6][_0xf644('0x12')]({'plain':!![]});switch(_0x5f3e42[_0xf644('0x2a')]){case _0xf644('0x34'):_0x5bb969(_0x5f3e42)[_0xf644('0x22')](_0x485dbe());break;case'onlyIfOpen':_0x474ada(_0x5f3e42)[_0xf644('0x22')](_0x485dbe());break;default:_0x5914c8[_0xf644('0x23')][_0xf644('0x1b')]({'phone':_0x329395[_0xf644('0xb')],'ContactId':_0x329395['id'],'ListId':_0x329395[_0xf644('0x1c')],'scheduledat':_0x329395[_0xf644('0x1e')]?_0x329395[_0xf644('0x1e')]:moment()[_0xf644('0x1f')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x329395['priority']?_0x329395[_0xf644('0x1d')]:0x2,'callback':_0x329395[_0xf644('0x21')]?!![]:![],'callbackuniqueid':_0x329395[_0xf644('0x21')]?_0x329395[_0xf644('0x21')]:undefined,'callbackat':_0x329395[_0xf644('0x21')]?moment()[_0xf644('0x1f')](_0xf644('0x20')):undefined,'CampaignId':_0x5f3e42['id']})[_0xf644('0x22')](_0x485dbe());}}}});})[_0xf644('0x18')](function(){return _0x5914c8[_0xf644('0x33')][_0xf644('0x2d')]({'attributes':['id'],'include':[{'model':_0x5914c8[_0xf644('0x2e')],'as':_0xf644('0x2f'),'where':{'id':_0x329395[_0xf644('0x1c')]}}]})[_0xf644('0x18')](function(_0x46d9c2){if(_0x46d9c2){var _0x588e2f=[];for(var _0x99dc8=0x0,_0x49ec7d;_0x99dc8<_0x46d9c2['length'];_0x99dc8+=0x1){_0x49ec7d=_0x46d9c2[_0x99dc8]['get']({'plain':!![]});_0x5914c8[_0xf644('0x31')][_0xf644('0x1b')]({'phone':_0x329395['phone'],'ContactId':_0x329395['id'],'ListId':_0x329395[_0xf644('0x1c')],'CampaignId':_0x49ec7d['id']})['catch'](function(_0x79c27a){logger[_0xf644('0x13')](_0xf644('0x32'),_0xf644('0x15'),_0x79c27a['message']);});}}});})[_0xf644('0x18')](function(){_0x517447();})[_0xf644('0x22')](function(_0x86c632){logger[_0xf644('0x13')](_0xf644('0x32'),_0xf644('0x15'),_0x86c632[_0xf644('0x35')]);_0x517447(_0x86c632);});}else{_0x517447(new Error(_0xf644('0x36')));}},'afterDestroy':function(_0x286015,_0x173921,_0x5041e9){var _0xb2778=_0x23cd49[_0xf644('0x11')];var _0x1080dd=_0x286015[_0xf644('0x12')]({'plain':!![]});return _0xb2778[_0xf644('0x23')][_0xf644('0x2d')]({'raw':!![],'where':{'ListId':_0x1080dd['ListId'],'ContactId':_0x1080dd['id']}})[_0xf644('0x18')](function(_0x2ba72b){if(!_[_0xf644('0x37')](_0x2ba72b)){var _0x545dbf=[];for(var _0xc246de=0x0;_0xc246de<_0x2ba72b[_0xf644('0x30')];_0xc246de+=0x1){var _0x5059b0=_0xb2778[_0xf644('0x23')][_0xf644('0x38')]({'where':{'ListId':_0x2ba72b[_0xc246de]['ListId'],'ContactId':_0x2ba72b[_0xc246de]['ContactId'],'VoiceQueueId':_0x2ba72b[_0xc246de][_0xf644('0x39')],'CampaignId':_0x2ba72b[_0xc246de][_0xf644('0x3a')]}});_0x545dbf['push'](_0x5059b0);}return BPromise[_0xf644('0x3b')](_0x545dbf);}})[_0xf644('0x18')](function(_0x1f089e){logger[_0xf644('0x3c')](_0xf644('0x3d'),util[_0xf644('0x16')](_0x1f089e,{'showHidden':![],'depth':null}));_0x5041e9();})[_0xf644('0x22')](function(_0x49c233){logger['error'](_0xf644('0x3e'),util['inspect'](_0x49c233,{'showHidden':![],'depth':null}));_0x5041e9(_0x49c233);});}}});};