Built motion from commit (unavailable).|2.5.26
[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 _0x4fb4=['lastName','mobile','fax','email','FULLTEXT','models','get','cmContact','afterCreate','inspect','CmHopper','findOne','then','isNil','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','create','ListId','priority','scheduledat','callbackUniqueid','YYYY-MM-DD\x20HH:mm:ss','catch','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20','cmContact,\x20afterCreate\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20','format','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20Campaign\x20','cmContact,\x20afterCreate,\x20','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20Campaign\x20','VoiceQueue','outbound','dialCheckDuplicateType','CmList','Lists','always','onlyIfOpen','BlackLists','length','error','cmContact,\x20%s,\x20%s','Campaign','findAll','CmHopperBlack','message','List\x20not\x20found','isEmpty','destroy','ContactId','CampaignId','all','info','[CmContact][afterDestroy][Succesfull]','[CmContact][afterDestroy][Error]','util','../../config/logger','api','moment','bluebird','request-promise','rimraf','../../config/environment','./cmContact.attributes','exports','define','CmContact','phone','firstName'];(function(_0x3c2518,_0x200a38){var _0x3e2984=function(_0x29024d){while(--_0x29024d){_0x3c2518['push'](_0x3c2518['shift']());}};_0x3e2984(++_0x200a38);}(_0x4fb4,0x190));var _0x44fb=function(_0x451596,_0x150c2f){_0x451596=_0x451596-0x0;var _0x24cd67=_0x4fb4[_0x451596];return _0x24cd67;};'use strict';var _=require('lodash');var util=require(_0x44fb('0x0'));var logger=require(_0x44fb('0x1'))(_0x44fb('0x2'));var moment=require(_0x44fb('0x3'));var BPromise=require(_0x44fb('0x4'));var rp=require(_0x44fb('0x5'));var fs=require('fs');var path=require('path');var rimraf=require(_0x44fb('0x6'));var config=require(_0x44fb('0x7'));var attributes=require(_0x44fb('0x8'));module[_0x44fb('0x9')]=function(_0x375d33,_0x173e70){return _0x375d33[_0x44fb('0xa')](_0x44fb('0xb'),attributes,{'tableName':'cm_contacts','paranoid':!![],'indexes':[{'name':_0x44fb('0xc'),'fields':[_0x44fb('0xc')]},{'name':'fti_cm_contacts','fields':[_0x44fb('0xd'),_0x44fb('0xe'),_0x44fb('0xc'),_0x44fb('0xf'),_0x44fb('0x10'),_0x44fb('0x11')],'type':_0x44fb('0x12')}],'timestamps':!![],'hooks':{'afterCreate':function(_0xaf5c16,_0x512670,_0x2ae684){var _0x4b202d=_0x375d33[_0x44fb('0x13')];var _0x1e8a60=_0xaf5c16[_0x44fb('0x14')]({'plain':!![]});function _0x103b56(){return function(_0x1c1cbc){logger['error'](_0x44fb('0x15'),_0x44fb('0x16'),util[_0x44fb('0x17')](_0x1c1cbc,{'showHidden':![],'depth':null}));};}var _0x449150=function(_0x9a367d){return new BPromise(function(_0x4985ce,_0xbc84db){_0x4b202d[_0x44fb('0x18')][_0x44fb('0x19')]({'where':{'phone':_0x1e8a60[_0x44fb('0xc')],'VoiceQueueId':_0x9a367d['id']}})[_0x44fb('0x1a')](function(_0x43ce3){if(!_[_0x44fb('0x1b')](_0x43ce3)){throw new Error('cmContact,\x20afterCreate\x20'+_0x1e8a60[_0x44fb('0xc')]+_0x44fb('0x1c')+_0x9a367d['id']);}return _0x4b202d[_0x44fb('0x18')][_0x44fb('0x1d')]({'phone':_0x1e8a60[_0x44fb('0xc')],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60[_0x44fb('0x1e')],'priority':_0x1e8a60[_0x44fb('0x1f')]?_0x1e8a60[_0x44fb('0x1f')]:0x2,'scheduledat':_0x1e8a60[_0x44fb('0x20')]?_0x1e8a60[_0x44fb('0x20')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x1e8a60['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x1e8a60[_0x44fb('0x21')]?_0x1e8a60[_0x44fb('0x21')]:undefined,'callbackat':_0x1e8a60[_0x44fb('0x21')]?moment()['format'](_0x44fb('0x22')):undefined,'VoiceQueueId':_0x9a367d['id']});})[_0x44fb('0x23')](function(_0x3394e2){_0xbc84db(_0x3394e2);});});};var _0x5adc98=function(_0x558283){return new BPromise(function(_0x38e834,_0x5cf7ec){_0x4b202d[_0x44fb('0x18')]['findOne']({'where':{'phone':_0x1e8a60[_0x44fb('0xc')],'VoiceQueueId':_0x558283['id']}})[_0x44fb('0x1a')](function(_0x1f3d5c){if(!_['isNil'](_0x1f3d5c)){throw new Error('cmContact,\x20afterCreate\x20'+_0x1e8a60[_0x44fb('0xc')]+_0x44fb('0x24')+_0x558283['id']);}return _0x4b202d[_0x44fb('0x25')][_0x44fb('0x19')]({'where':{'calleridnum':_0x1e8a60['phone'],'VoiceQueueId':_0x558283['id']}});})[_0x44fb('0x1a')](function(_0x3a7cd1){if(!_['isNil'](_0x3a7cd1)){throw new Error('cmContact,\x20afterCreate,\x20'+_0x1e8a60[_0x44fb('0xc')]+_0x44fb('0x26')+_0x558283['id']);}return _0x4b202d[_0x44fb('0x18')][_0x44fb('0x1d')]({'phone':_0x1e8a60[_0x44fb('0xc')],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60[_0x44fb('0x1e')],'priority':_0x1e8a60[_0x44fb('0x1f')]?_0x1e8a60[_0x44fb('0x1f')]:0x2,'scheduledat':_0x1e8a60[_0x44fb('0x20')]?_0x1e8a60['scheduledat']:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x1e8a60[_0x44fb('0x21')]?!![]:![],'callbackuniqueid':_0x1e8a60['callbackUniqueid']?_0x1e8a60[_0x44fb('0x21')]:undefined,'callbackat':_0x1e8a60[_0x44fb('0x21')]?moment()['format'](_0x44fb('0x22')):undefined,'VoiceQueueId':_0x558283['id']});})[_0x44fb('0x23')](function(_0xca9051){_0x5cf7ec(_0xca9051);});});};function _0x1b13bb(){return function(_0x6fa74e){logger['error']('cmContact',_0x44fb('0x16'),util['inspect'](_0x6fa74e,{'showHidden':![],'depth':null}));};}var _0x14fb90=function(_0x8afe2){return new BPromise(function(_0x15aa21,_0x3ef07c){_0x4b202d[_0x44fb('0x18')][_0x44fb('0x19')]({'where':{'phone':_0x1e8a60[_0x44fb('0xc')],'CampaignId':_0x8afe2['id']}})[_0x44fb('0x1a')](function(_0x3a7c7b){if(!_[_0x44fb('0x1b')](_0x3a7c7b)){throw new Error(_0x44fb('0x27')+_0x1e8a60[_0x44fb('0xc')]+_0x44fb('0x28')+_0x8afe2['id']);}return _0x4b202d[_0x44fb('0x18')][_0x44fb('0x1d')]({'phone':_0x1e8a60['phone'],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60['ListId'],'priority':_0x1e8a60['priority']?_0x1e8a60[_0x44fb('0x1f')]:0x2,'scheduledat':_0x1e8a60[_0x44fb('0x20')]?_0x1e8a60[_0x44fb('0x20')]:moment()[_0x44fb('0x29')]('YYYY-MM-DD\x20HH:mm:ss'),'callback':_0x1e8a60[_0x44fb('0x21')]?!![]:![],'callbackuniqueid':_0x1e8a60[_0x44fb('0x21')]?_0x1e8a60['callbackUniqueid']:undefined,'callbackat':_0x1e8a60[_0x44fb('0x21')]?moment()['format'](_0x44fb('0x22')):undefined,'CampaignId':_0x8afe2['id']});})['catch'](function(_0x2fabf0){_0x3ef07c(_0x2fabf0);});});};var _0x5d170a=function(_0x131f78){return new BPromise(function(_0x5c41e5,_0x5aa5f4){_0x4b202d[_0x44fb('0x18')][_0x44fb('0x19')]({'where':{'phone':_0x1e8a60[_0x44fb('0xc')],'CampaignId':_0x131f78['id']}})[_0x44fb('0x1a')](function(_0x32bf81){if(!_[_0x44fb('0x1b')](_0x32bf81)){throw new Error(_0x44fb('0x27')+_0x1e8a60[_0x44fb('0xc')]+_0x44fb('0x2a')+_0x131f78['id']);}return _0x4b202d[_0x44fb('0x25')]['findOne']({'where':{'calleridnum':_0x1e8a60[_0x44fb('0xc')],'CampaignId':_0x131f78['id']}});})[_0x44fb('0x1a')](function(_0x3e1040){if(!_[_0x44fb('0x1b')](_0x3e1040)){throw new Error(_0x44fb('0x2b')+_0x1e8a60['phone']+_0x44fb('0x2c')+_0x131f78['id']);}return _0x4b202d[_0x44fb('0x18')]['create']({'phone':_0x1e8a60['phone'],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60[_0x44fb('0x1e')],'priority':_0x1e8a60[_0x44fb('0x1f')]?_0x1e8a60[_0x44fb('0x1f')]:0x2,'scheduledat':_0x1e8a60['scheduledat']?_0x1e8a60[_0x44fb('0x20')]:moment()[_0x44fb('0x29')](_0x44fb('0x22')),'callback':_0x1e8a60[_0x44fb('0x21')]?!![]:![],'callbackuniqueid':_0x1e8a60[_0x44fb('0x21')]?_0x1e8a60[_0x44fb('0x21')]:undefined,'callbackat':_0x1e8a60['callbackUniqueid']?moment()[_0x44fb('0x29')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x131f78['id']});})[_0x44fb('0x23')](function(_0x10243b){_0x5aa5f4(_0x10243b);});});};if(_0x1e8a60[_0x44fb('0x1e')]){_0x4b202d[_0x44fb('0x2d')]['findAll']({'where':{'type':_0x44fb('0x2e')},'attributes':['id',_0x44fb('0x2f')],'include':[{'model':_0x4b202d[_0x44fb('0x30')],'as':_0x44fb('0x31'),'where':{'id':_0x1e8a60[_0x44fb('0x1e')]}}]})['then'](function(_0x27d233){if(_0x27d233){var _0x1df108=[];for(var _0x10cd8d=0x0,_0x16147a;_0x10cd8d<_0x27d233['length'];_0x10cd8d+=0x1){_0x16147a=_0x27d233[_0x10cd8d]['get']({'plain':!![]});switch(_0x16147a[_0x44fb('0x2f')]){case _0x44fb('0x32'):_0x5adc98(_0x16147a)['catch'](_0x103b56());break;case _0x44fb('0x33'):_0x449150(_0x16147a)['catch'](_0x103b56());break;default:_0x4b202d['CmHopper']['create']({'phone':_0x1e8a60['phone'],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60[_0x44fb('0x1e')],'scheduledat':_0x1e8a60[_0x44fb('0x20')]?_0x1e8a60[_0x44fb('0x20')]:moment()[_0x44fb('0x29')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x1e8a60[_0x44fb('0x1f')]?_0x1e8a60[_0x44fb('0x1f')]:0x2,'callback':_0x1e8a60[_0x44fb('0x21')]?!![]:![],'callbackuniqueid':_0x1e8a60[_0x44fb('0x21')]?_0x1e8a60[_0x44fb('0x21')]:undefined,'callbackat':_0x1e8a60[_0x44fb('0x21')]?moment()[_0x44fb('0x29')](_0x44fb('0x22')):undefined,'VoiceQueueId':_0x16147a['id']})['catch'](_0x103b56());}}}})[_0x44fb('0x1a')](function(){return _0x4b202d['VoiceQueue']['findAll']({'where':{'type':_0x44fb('0x2e')},'attributes':['id'],'include':[{'model':_0x4b202d[_0x44fb('0x30')],'as':_0x44fb('0x34'),'where':{'id':_0x1e8a60['ListId']}}]})[_0x44fb('0x1a')](function(_0x326931){if(_0x326931){var _0x3ff985=[];for(var _0x1a8bce=0x0,_0x174c48;_0x1a8bce<_0x326931[_0x44fb('0x35')];_0x1a8bce+=0x1){_0x174c48=_0x326931[_0x1a8bce][_0x44fb('0x14')]({'plain':!![]});_0x4b202d['CmHopperBlack'][_0x44fb('0x1d')]({'phone':_0x1e8a60['phone'],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60[_0x44fb('0x1e')],'VoiceQueueId':_0x174c48['id']})['catch'](function(_0x335437){logger[_0x44fb('0x36')](_0x44fb('0x37'),_0x44fb('0x16'),_0x335437['message']);});}}});})[_0x44fb('0x1a')](function(){return _0x4b202d[_0x44fb('0x38')][_0x44fb('0x39')]({'attributes':['id',_0x44fb('0x2f')],'include':[{'model':_0x4b202d[_0x44fb('0x30')],'as':_0x44fb('0x31'),'where':{'id':_0x1e8a60[_0x44fb('0x1e')]}}]})[_0x44fb('0x1a')](function(_0x240f9b){if(_0x240f9b){var _0x2a5d30=[];for(var _0x12775c=0x0,_0x2ebfc7;_0x12775c<_0x240f9b['length'];_0x12775c+=0x1){_0x2ebfc7=_0x240f9b[_0x12775c][_0x44fb('0x14')]({'plain':!![]});switch(_0x2ebfc7['dialCheckDuplicateType']){case _0x44fb('0x32'):_0x5d170a(_0x2ebfc7)[_0x44fb('0x23')](_0x1b13bb());break;case'onlyIfOpen':_0x14fb90(_0x2ebfc7)['catch'](_0x1b13bb());break;default:_0x4b202d['CmHopper'][_0x44fb('0x1d')]({'phone':_0x1e8a60[_0x44fb('0xc')],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60[_0x44fb('0x1e')],'scheduledat':_0x1e8a60[_0x44fb('0x20')]?_0x1e8a60[_0x44fb('0x20')]:moment()[_0x44fb('0x29')](_0x44fb('0x22')),'priority':_0x1e8a60[_0x44fb('0x1f')]?_0x1e8a60[_0x44fb('0x1f')]:0x2,'callback':_0x1e8a60[_0x44fb('0x21')]?!![]:![],'callbackuniqueid':_0x1e8a60[_0x44fb('0x21')]?_0x1e8a60[_0x44fb('0x21')]:undefined,'callbackat':_0x1e8a60[_0x44fb('0x21')]?moment()[_0x44fb('0x29')](_0x44fb('0x22')):undefined,'CampaignId':_0x2ebfc7['id']})[_0x44fb('0x23')](_0x1b13bb());}}}});})[_0x44fb('0x1a')](function(){return _0x4b202d[_0x44fb('0x38')][_0x44fb('0x39')]({'attributes':['id'],'include':[{'model':_0x4b202d['CmList'],'as':'BlackLists','where':{'id':_0x1e8a60[_0x44fb('0x1e')]}}]})['then'](function(_0x57ef23){if(_0x57ef23){var _0x56952d=[];for(var _0x3e1989=0x0,_0x399586;_0x3e1989<_0x57ef23[_0x44fb('0x35')];_0x3e1989+=0x1){_0x399586=_0x57ef23[_0x3e1989]['get']({'plain':!![]});_0x4b202d[_0x44fb('0x3a')]['create']({'phone':_0x1e8a60[_0x44fb('0xc')],'ContactId':_0x1e8a60['id'],'ListId':_0x1e8a60['ListId'],'CampaignId':_0x399586['id']})[_0x44fb('0x23')](function(_0x33ba35){logger[_0x44fb('0x36')]('cmContact,\x20%s,\x20%s',_0x44fb('0x16'),_0x33ba35[_0x44fb('0x3b')]);});}}});})[_0x44fb('0x1a')](function(){_0x2ae684();})['catch'](function(_0x646b2d){logger[_0x44fb('0x36')](_0x44fb('0x37'),'afterCreate',_0x646b2d['message']);_0x2ae684(_0x646b2d);});}else{_0x2ae684(new Error(_0x44fb('0x3c')));}},'afterDestroy':function(_0x35a96b,_0x2b45a0,_0x4b3cdb){var _0x5b3532=_0x375d33['models'];var _0x213066=_0x35a96b[_0x44fb('0x14')]({'plain':!![]});return _0x5b3532[_0x44fb('0x18')][_0x44fb('0x39')]({'raw':!![],'where':{'ListId':_0x213066[_0x44fb('0x1e')],'ContactId':_0x213066['id']}})[_0x44fb('0x1a')](function(_0x5a7d27){if(!_[_0x44fb('0x3d')](_0x5a7d27)){var _0x2558bf=[];for(var _0x1b575f=0x0;_0x1b575f<_0x5a7d27[_0x44fb('0x35')];_0x1b575f+=0x1){var _0x53410a=_0x5b3532['CmHopper'][_0x44fb('0x3e')]({'where':{'ListId':_0x5a7d27[_0x1b575f][_0x44fb('0x1e')],'ContactId':_0x5a7d27[_0x1b575f][_0x44fb('0x3f')],'VoiceQueueId':_0x5a7d27[_0x1b575f]['VoiceQueueId'],'CampaignId':_0x5a7d27[_0x1b575f][_0x44fb('0x40')]}});_0x2558bf['push'](_0x53410a);}return BPromise[_0x44fb('0x41')](_0x2558bf);}})[_0x44fb('0x1a')](function(_0x44dc2d){logger[_0x44fb('0x42')](_0x44fb('0x43'),util['inspect'](_0x44dc2d,{'showHidden':![],'depth':null}));_0x4b3cdb();})[_0x44fb('0x23')](function(_0x3ff75d){logger[_0x44fb('0x36')](_0x44fb('0x44'),util['inspect'](_0x3ff75d,{'showHidden':![],'depth':null}));_0x4b3cdb(_0x3ff75d);});}}});};