Built motion from commit 0555bc41.|2.5.41
[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 _0xb714=['ListId','\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','VoiceQueue','findAll','outbound','dialCheckDuplicateType','CmList','Lists','length','onlyIfOpen','BlackLists','CmHopperBlack','cmContact,\x20%s,\x20%s','message','Campaign','always','List\x20not\x20found','isEmpty','destroy','ContactId','CampaignId','push','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','api','moment','bluebird','rimraf','../../config/environment','./cmContact.attributes','exports','define','CmContact','phone','fti_cm_contacts','firstName','lastName','mobile','fax','utf8mb4','utf8mb4_unicode_ci','models','get','error','cmContact','afterCreate','inspect','CmHopper','then','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','priority','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','catch','isNil','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','findOne','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','create'];(function(_0x4267a8,_0x4dbb90){var _0x29415d=function(_0x72ee3a){while(--_0x72ee3a){_0x4267a8['push'](_0x4267a8['shift']());}};_0x29415d(++_0x4dbb90);}(_0xb714,0xf1));var _0x4b71=function(_0xc09bac,_0x2fecd0){_0xc09bac=_0xc09bac-0x0;var _0x54eb83=_0xb714[_0xc09bac];return _0x54eb83;};'use strict';var _=require(_0x4b71('0x0'));var util=require(_0x4b71('0x1'));var logger=require(_0x4b71('0x2'))(_0x4b71('0x3'));var moment=require(_0x4b71('0x4'));var BPromise=require(_0x4b71('0x5'));var rp=require('request-promise');var fs=require('fs');var path=require('path');var rimraf=require(_0x4b71('0x6'));var config=require(_0x4b71('0x7'));var attributes=require(_0x4b71('0x8'));module[_0x4b71('0x9')]=function(_0x1dd5d0,_0x5650c3){return _0x1dd5d0[_0x4b71('0xa')](_0x4b71('0xb'),attributes,{'tableName':'cm_contacts','paranoid':!![],'indexes':[{'name':_0x4b71('0xc'),'fields':[_0x4b71('0xc')]},{'name':_0x4b71('0xd'),'fields':[_0x4b71('0xe'),_0x4b71('0xf'),_0x4b71('0xc'),_0x4b71('0x10'),_0x4b71('0x11'),'email'],'type':'FULLTEXT'}],'timestamps':!![],'charset':_0x4b71('0x12'),'collate':_0x4b71('0x13'),'hooks':{'afterCreate':function(_0x4cc5ae,_0x4f1c0f,_0x5344c7){var _0x31a94a=_0x1dd5d0[_0x4b71('0x14')];var _0x520cc1=_0x4cc5ae[_0x4b71('0x15')]({'plain':!![]});function _0x5afe14(){return function(_0x4b92f8){logger[_0x4b71('0x16')](_0x4b71('0x17'),_0x4b71('0x18'),util[_0x4b71('0x19')](_0x4b92f8,{'showHidden':![],'depth':null}));};}var _0x2c1ce9=function(_0x31f6c9){return new BPromise(function(_0x3edc22,_0x362a04){_0x31a94a[_0x4b71('0x1a')]['findOne']({'where':{'phone':_0x520cc1['phone'],'VoiceQueueId':_0x31f6c9['id']}})[_0x4b71('0x1b')](function(_0x1a03c5){if(!_['isNil'](_0x1a03c5)){throw new Error(_0x4b71('0x1c')+_0x520cc1[_0x4b71('0xc')]+_0x4b71('0x1d')+_0x31f6c9['id']);}return _0x31a94a[_0x4b71('0x1a')]['create']({'phone':_0x520cc1[_0x4b71('0xc')],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1['ListId'],'priority':_0x520cc1[_0x4b71('0x1e')]?_0x520cc1[_0x4b71('0x1e')]:0x2,'scheduledat':_0x520cc1['scheduledat']?_0x520cc1[_0x4b71('0x1f')]:moment()[_0x4b71('0x20')](_0x4b71('0x21')),'callback':_0x520cc1[_0x4b71('0x22')]?!![]:![],'callbackuniqueid':_0x520cc1['callbackUniqueid']?_0x520cc1[_0x4b71('0x22')]:undefined,'callbackat':_0x520cc1[_0x4b71('0x22')]?moment()[_0x4b71('0x20')](_0x4b71('0x21')):undefined,'VoiceQueueId':_0x31f6c9['id']});})[_0x4b71('0x23')](function(_0x2f5ab2){_0x362a04(_0x2f5ab2);});});};var _0x247cfe=function(_0x501156){return new BPromise(function(_0x3dbb1b,_0x5921d9){_0x31a94a[_0x4b71('0x1a')]['findOne']({'where':{'phone':_0x520cc1['phone'],'VoiceQueueId':_0x501156['id']}})[_0x4b71('0x1b')](function(_0x414a25){if(!_[_0x4b71('0x24')](_0x414a25)){throw new Error(_0x4b71('0x1c')+_0x520cc1[_0x4b71('0xc')]+_0x4b71('0x25')+_0x501156['id']);}return _0x31a94a[_0x4b71('0x26')][_0x4b71('0x27')]({'where':{'calleridnum':_0x520cc1['phone'],'VoiceQueueId':_0x501156['id']}});})[_0x4b71('0x1b')](function(_0xf07c68){if(!_['isNil'](_0xf07c68)){throw new Error(_0x4b71('0x28')+_0x520cc1[_0x4b71('0xc')]+_0x4b71('0x29')+_0x501156['id']);}return _0x31a94a['CmHopper'][_0x4b71('0x2a')]({'phone':_0x520cc1['phone'],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1[_0x4b71('0x2b')],'priority':_0x520cc1['priority']?_0x520cc1[_0x4b71('0x1e')]:0x2,'scheduledat':_0x520cc1[_0x4b71('0x1f')]?_0x520cc1[_0x4b71('0x1f')]:moment()[_0x4b71('0x20')](_0x4b71('0x21')),'callback':_0x520cc1['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x520cc1[_0x4b71('0x22')]?_0x520cc1[_0x4b71('0x22')]:undefined,'callbackat':_0x520cc1[_0x4b71('0x22')]?moment()[_0x4b71('0x20')](_0x4b71('0x21')):undefined,'VoiceQueueId':_0x501156['id']});})['catch'](function(_0x27ede8){_0x5921d9(_0x27ede8);});});};function _0x2182ea(){return function(_0x265658){logger[_0x4b71('0x16')]('cmContact',_0x4b71('0x18'),util['inspect'](_0x265658,{'showHidden':![],'depth':null}));};}var _0x377333=function(_0x1b7f3e){return new BPromise(function(_0x20553f,_0x2e3f07){_0x31a94a[_0x4b71('0x1a')][_0x4b71('0x27')]({'where':{'phone':_0x520cc1['phone'],'CampaignId':_0x1b7f3e['id']}})[_0x4b71('0x1b')](function(_0x1ee541){if(!_[_0x4b71('0x24')](_0x1ee541)){throw new Error('cmContact,\x20afterCreate\x20'+_0x520cc1[_0x4b71('0xc')]+_0x4b71('0x2c')+_0x1b7f3e['id']);}return _0x31a94a[_0x4b71('0x1a')][_0x4b71('0x2a')]({'phone':_0x520cc1[_0x4b71('0xc')],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1[_0x4b71('0x2b')],'priority':_0x520cc1[_0x4b71('0x1e')]?_0x520cc1[_0x4b71('0x1e')]:0x2,'scheduledat':_0x520cc1[_0x4b71('0x1f')]?_0x520cc1['scheduledat']:moment()[_0x4b71('0x20')](_0x4b71('0x21')),'callback':_0x520cc1[_0x4b71('0x22')]?!![]:![],'callbackuniqueid':_0x520cc1['callbackUniqueid']?_0x520cc1[_0x4b71('0x22')]:undefined,'callbackat':_0x520cc1[_0x4b71('0x22')]?moment()['format'](_0x4b71('0x21')):undefined,'CampaignId':_0x1b7f3e['id']});})['catch'](function(_0x36975b){_0x2e3f07(_0x36975b);});});};var _0x3bd5f0=function(_0x9db6d6){return new BPromise(function(_0x3b393b,_0x4c7153){_0x31a94a['CmHopper'][_0x4b71('0x27')]({'where':{'phone':_0x520cc1[_0x4b71('0xc')],'CampaignId':_0x9db6d6['id']}})[_0x4b71('0x1b')](function(_0x40f36c){if(!_[_0x4b71('0x24')](_0x40f36c)){throw new Error(_0x4b71('0x1c')+_0x520cc1['phone']+_0x4b71('0x2d')+_0x9db6d6['id']);}return _0x31a94a[_0x4b71('0x26')][_0x4b71('0x27')]({'where':{'calleridnum':_0x520cc1[_0x4b71('0xc')],'CampaignId':_0x9db6d6['id']}});})[_0x4b71('0x1b')](function(_0x27c00a){if(!_[_0x4b71('0x24')](_0x27c00a)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x520cc1['phone']+_0x4b71('0x2e')+_0x9db6d6['id']);}return _0x31a94a[_0x4b71('0x1a')][_0x4b71('0x2a')]({'phone':_0x520cc1[_0x4b71('0xc')],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1[_0x4b71('0x2b')],'priority':_0x520cc1['priority']?_0x520cc1[_0x4b71('0x1e')]:0x2,'scheduledat':_0x520cc1[_0x4b71('0x1f')]?_0x520cc1['scheduledat']:moment()[_0x4b71('0x20')](_0x4b71('0x21')),'callback':_0x520cc1[_0x4b71('0x22')]?!![]:![],'callbackuniqueid':_0x520cc1[_0x4b71('0x22')]?_0x520cc1[_0x4b71('0x22')]:undefined,'callbackat':_0x520cc1[_0x4b71('0x22')]?moment()[_0x4b71('0x20')](_0x4b71('0x21')):undefined,'CampaignId':_0x9db6d6['id']});})['catch'](function(_0x11e1dc){_0x4c7153(_0x11e1dc);});});};if(_0x520cc1[_0x4b71('0x2b')]){_0x31a94a[_0x4b71('0x2f')][_0x4b71('0x30')]({'where':{'type':_0x4b71('0x31')},'attributes':['id',_0x4b71('0x32')],'include':[{'model':_0x31a94a[_0x4b71('0x33')],'as':_0x4b71('0x34'),'where':{'id':_0x520cc1[_0x4b71('0x2b')]}}]})[_0x4b71('0x1b')](function(_0x3f9852){if(_0x3f9852){var _0x52b477=[];for(var _0x2d75cc=0x0,_0x15dcab;_0x2d75cc<_0x3f9852[_0x4b71('0x35')];_0x2d75cc+=0x1){_0x15dcab=_0x3f9852[_0x2d75cc][_0x4b71('0x15')]({'plain':!![]});switch(_0x15dcab['dialCheckDuplicateType']){case'always':_0x247cfe(_0x15dcab)[_0x4b71('0x23')](_0x5afe14());break;case _0x4b71('0x36'):_0x2c1ce9(_0x15dcab)[_0x4b71('0x23')](_0x5afe14());break;default:_0x31a94a['CmHopper']['create']({'phone':_0x520cc1[_0x4b71('0xc')],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1[_0x4b71('0x2b')],'scheduledat':_0x520cc1['scheduledat']?_0x520cc1[_0x4b71('0x1f')]:moment()[_0x4b71('0x20')](_0x4b71('0x21')),'priority':_0x520cc1['priority']?_0x520cc1[_0x4b71('0x1e')]:0x2,'callback':_0x520cc1[_0x4b71('0x22')]?!![]:![],'callbackuniqueid':_0x520cc1[_0x4b71('0x22')]?_0x520cc1[_0x4b71('0x22')]:undefined,'callbackat':_0x520cc1['callbackUniqueid']?moment()[_0x4b71('0x20')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x15dcab['id']})[_0x4b71('0x23')](_0x5afe14());}}}})[_0x4b71('0x1b')](function(){return _0x31a94a[_0x4b71('0x2f')]['findAll']({'where':{'type':_0x4b71('0x31')},'attributes':['id'],'include':[{'model':_0x31a94a['CmList'],'as':_0x4b71('0x37'),'where':{'id':_0x520cc1[_0x4b71('0x2b')]}}]})[_0x4b71('0x1b')](function(_0x5513e8){if(_0x5513e8){var _0x160845=[];for(var _0x195eed=0x0,_0xd97f80;_0x195eed<_0x5513e8[_0x4b71('0x35')];_0x195eed+=0x1){_0xd97f80=_0x5513e8[_0x195eed][_0x4b71('0x15')]({'plain':!![]});_0x31a94a[_0x4b71('0x38')][_0x4b71('0x2a')]({'phone':_0x520cc1[_0x4b71('0xc')],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1['ListId'],'VoiceQueueId':_0xd97f80['id']})['catch'](function(_0x3ef5fd){logger[_0x4b71('0x16')](_0x4b71('0x39'),_0x4b71('0x18'),_0x3ef5fd[_0x4b71('0x3a')]);});}}});})[_0x4b71('0x1b')](function(){return _0x31a94a[_0x4b71('0x3b')]['findAll']({'attributes':['id',_0x4b71('0x32')],'include':[{'model':_0x31a94a[_0x4b71('0x33')],'as':'Lists','where':{'id':_0x520cc1[_0x4b71('0x2b')]}}]})[_0x4b71('0x1b')](function(_0x10239d){if(_0x10239d){var _0x32ca38=[];for(var _0x55ce56=0x0,_0x1bdda3;_0x55ce56<_0x10239d['length'];_0x55ce56+=0x1){_0x1bdda3=_0x10239d[_0x55ce56]['get']({'plain':!![]});switch(_0x1bdda3[_0x4b71('0x32')]){case _0x4b71('0x3c'):_0x3bd5f0(_0x1bdda3)['catch'](_0x2182ea());break;case _0x4b71('0x36'):_0x377333(_0x1bdda3)['catch'](_0x2182ea());break;default:_0x31a94a[_0x4b71('0x1a')][_0x4b71('0x2a')]({'phone':_0x520cc1[_0x4b71('0xc')],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1['ListId'],'scheduledat':_0x520cc1['scheduledat']?_0x520cc1[_0x4b71('0x1f')]:moment()[_0x4b71('0x20')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x520cc1[_0x4b71('0x1e')]?_0x520cc1[_0x4b71('0x1e')]:0x2,'callback':_0x520cc1[_0x4b71('0x22')]?!![]:![],'callbackuniqueid':_0x520cc1[_0x4b71('0x22')]?_0x520cc1[_0x4b71('0x22')]:undefined,'callbackat':_0x520cc1[_0x4b71('0x22')]?moment()[_0x4b71('0x20')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x1bdda3['id']})[_0x4b71('0x23')](_0x2182ea());}}}});})[_0x4b71('0x1b')](function(){return _0x31a94a[_0x4b71('0x3b')][_0x4b71('0x30')]({'attributes':['id'],'include':[{'model':_0x31a94a[_0x4b71('0x33')],'as':_0x4b71('0x37'),'where':{'id':_0x520cc1[_0x4b71('0x2b')]}}]})[_0x4b71('0x1b')](function(_0x5a6356){if(_0x5a6356){var _0x2ff1bc=[];for(var _0x48aa5d=0x0,_0x1b4381;_0x48aa5d<_0x5a6356[_0x4b71('0x35')];_0x48aa5d+=0x1){_0x1b4381=_0x5a6356[_0x48aa5d][_0x4b71('0x15')]({'plain':!![]});_0x31a94a[_0x4b71('0x38')][_0x4b71('0x2a')]({'phone':_0x520cc1['phone'],'ContactId':_0x520cc1['id'],'ListId':_0x520cc1[_0x4b71('0x2b')],'CampaignId':_0x1b4381['id']})[_0x4b71('0x23')](function(_0x3e24df){logger[_0x4b71('0x16')](_0x4b71('0x39'),_0x4b71('0x18'),_0x3e24df[_0x4b71('0x3a')]);});}}});})[_0x4b71('0x1b')](function(){_0x5344c7();})['catch'](function(_0xcee3b6){logger[_0x4b71('0x16')]('cmContact,\x20%s,\x20%s',_0x4b71('0x18'),_0xcee3b6[_0x4b71('0x3a')]);_0x5344c7(_0xcee3b6);});}else{_0x5344c7(new Error(_0x4b71('0x3d')));}},'afterDestroy':function(_0x10a091,_0x156df7,_0x5505c7){var _0x55cd6e=_0x1dd5d0[_0x4b71('0x14')];var _0x54a629=_0x10a091[_0x4b71('0x15')]({'plain':!![]});return _0x55cd6e['CmHopper'][_0x4b71('0x30')]({'raw':!![],'where':{'ListId':_0x54a629[_0x4b71('0x2b')],'ContactId':_0x54a629['id']}})['then'](function(_0x233baa){if(!_[_0x4b71('0x3e')](_0x233baa)){var _0x2248da=[];for(var _0x50861c=0x0;_0x50861c<_0x233baa[_0x4b71('0x35')];_0x50861c+=0x1){var _0x333882=_0x55cd6e['CmHopper'][_0x4b71('0x3f')]({'where':{'ListId':_0x233baa[_0x50861c][_0x4b71('0x2b')],'ContactId':_0x233baa[_0x50861c][_0x4b71('0x40')],'VoiceQueueId':_0x233baa[_0x50861c]['VoiceQueueId'],'CampaignId':_0x233baa[_0x50861c][_0x4b71('0x41')]}});_0x2248da[_0x4b71('0x42')](_0x333882);}return BPromise[_0x4b71('0x43')](_0x2248da);}})[_0x4b71('0x1b')](function(_0x42add4){logger[_0x4b71('0x44')](_0x4b71('0x45'),util[_0x4b71('0x19')](_0x42add4,{'showHidden':![],'depth':null}));_0x5505c7();})[_0x4b71('0x23')](function(_0x32bede){logger[_0x4b71('0x16')](_0x4b71('0x46'),util[_0x4b71('0x19')](_0x32bede,{'showHidden':![],'depth':null}));_0x5505c7(_0x32bede);});}}});};