Built motion from commit (unavailable).|2.5.21
[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 _0xc373=['destroy','ContactId','VoiceQueueId','push','all','info','[CmContact][afterDestroy][Error]','util','../../config/logger','api','request-promise','path','rimraf','../../config/environment','./cmContact.attributes','exports','define','CmContact','cm_contacts','phone','fti_cm_contacts','firstName','lastName','email','FULLTEXT','models','get','afterCreate','CmHopper','findOne','then','isNil','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','ListId','priority','scheduledat','YYYY-MM-DD\x20HH:mm:ss','callbackUniqueid','format','catch','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopper\x20VoiceQueue\x20','CmHopperFinal','cmContact,\x20afterCreate,\x20','create','error','inspect','\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20onlyIfOpen,\x20exist\x20in\x20hopper\x20Campaign\x20','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','dialCheckDuplicateType','CmList','Lists','length','onlyIfOpen','findAll','BlackLists','CmHopperBlack','cmContact,\x20%s,\x20%s','message','Campaign','isEmpty'];(function(_0x15c3f2,_0x2f59bc){var _0x1decfe=function(_0x2e394c){while(--_0x2e394c){_0x15c3f2['push'](_0x15c3f2['shift']());}};_0x1decfe(++_0x2f59bc);}(_0xc373,0x1c7));var _0x3c37=function(_0x53afa6,_0x59e786){_0x53afa6=_0x53afa6-0x0;var _0x51f86a=_0xc373[_0x53afa6];return _0x51f86a;};'use strict';var _=require('lodash');var util=require(_0x3c37('0x0'));var logger=require(_0x3c37('0x1'))(_0x3c37('0x2'));var moment=require('moment');var BPromise=require('bluebird');var rp=require(_0x3c37('0x3'));var fs=require('fs');var path=require(_0x3c37('0x4'));var rimraf=require(_0x3c37('0x5'));var config=require(_0x3c37('0x6'));var attributes=require(_0x3c37('0x7'));module[_0x3c37('0x8')]=function(_0x5b05c6,_0x4c71cc){return _0x5b05c6[_0x3c37('0x9')](_0x3c37('0xa'),attributes,{'tableName':_0x3c37('0xb'),'paranoid':!![],'indexes':[{'name':_0x3c37('0xc'),'fields':[_0x3c37('0xc')]},{'name':_0x3c37('0xd'),'fields':[_0x3c37('0xe'),_0x3c37('0xf'),_0x3c37('0xc'),'mobile','fax',_0x3c37('0x10')],'type':_0x3c37('0x11')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x2beb44,_0x141def,_0x17a164){var _0x27ec3d=_0x5b05c6[_0x3c37('0x12')];var _0x5e29ba=_0x2beb44[_0x3c37('0x13')]({'plain':!![]});function _0x454f92(){return function(_0x458002){logger['error']('cmContact',_0x3c37('0x14'),util['inspect'](_0x458002,{'showHidden':![],'depth':null}));};}var _0x52ab1f=function(_0x37904b){return new BPromise(function(_0x3be8b3,_0x2eca76){_0x27ec3d[_0x3c37('0x15')][_0x3c37('0x16')]({'where':{'phone':_0x5e29ba[_0x3c37('0xc')],'VoiceQueueId':_0x37904b['id']}})[_0x3c37('0x17')](function(_0x40d3ee){if(!_[_0x3c37('0x18')](_0x40d3ee)){throw new Error('cmContact,\x20afterCreate\x20'+_0x5e29ba[_0x3c37('0xc')]+_0x3c37('0x19')+_0x37904b['id']);}return _0x27ec3d['CmHopper']['create']({'phone':_0x5e29ba[_0x3c37('0xc')],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba[_0x3c37('0x1a')],'priority':_0x5e29ba[_0x3c37('0x1b')]?_0x5e29ba[_0x3c37('0x1b')]:0x2,'scheduledat':_0x5e29ba[_0x3c37('0x1c')]?_0x5e29ba[_0x3c37('0x1c')]:moment()['format'](_0x3c37('0x1d')),'callback':_0x5e29ba['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5e29ba['callbackUniqueid']?_0x5e29ba[_0x3c37('0x1e')]:undefined,'callbackat':_0x5e29ba[_0x3c37('0x1e')]?moment()[_0x3c37('0x1f')](_0x3c37('0x1d')):undefined,'VoiceQueueId':_0x37904b['id']});})[_0x3c37('0x20')](function(_0x5903ee){_0x2eca76(_0x5903ee);});});};var _0x27f83e=function(_0x54776f){return new BPromise(function(_0x3399a7,_0x510baf){_0x27ec3d[_0x3c37('0x15')][_0x3c37('0x16')]({'where':{'phone':_0x5e29ba[_0x3c37('0xc')],'VoiceQueueId':_0x54776f['id']}})['then'](function(_0x51d25d){if(!_['isNil'](_0x51d25d)){throw new Error('cmContact,\x20afterCreate\x20'+_0x5e29ba['phone']+_0x3c37('0x21')+_0x54776f['id']);}return _0x27ec3d[_0x3c37('0x22')][_0x3c37('0x16')]({'where':{'calleridnum':_0x5e29ba[_0x3c37('0xc')],'VoiceQueueId':_0x54776f['id']}});})['then'](function(_0x52b5d9){if(!_['isNil'](_0x52b5d9)){throw new Error(_0x3c37('0x23')+_0x5e29ba[_0x3c37('0xc')]+'\x20contact\x20not\x20insert\x20in\x20hopper,\x20check\x20duplicate\x20always,\x20exist\x20in\x20hopperFinal\x20VoiceQueue\x20'+_0x54776f['id']);}return _0x27ec3d['CmHopper'][_0x3c37('0x24')]({'phone':_0x5e29ba[_0x3c37('0xc')],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba[_0x3c37('0x1a')],'priority':_0x5e29ba[_0x3c37('0x1b')]?_0x5e29ba[_0x3c37('0x1b')]:0x2,'scheduledat':_0x5e29ba[_0x3c37('0x1c')]?_0x5e29ba[_0x3c37('0x1c')]:moment()[_0x3c37('0x1f')](_0x3c37('0x1d')),'callback':_0x5e29ba[_0x3c37('0x1e')]?!![]:![],'callbackuniqueid':_0x5e29ba['callbackUniqueid']?_0x5e29ba[_0x3c37('0x1e')]:undefined,'callbackat':_0x5e29ba[_0x3c37('0x1e')]?moment()['format']('YYYY-MM-DD\x20HH:mm:ss'):undefined,'VoiceQueueId':_0x54776f['id']});})[_0x3c37('0x20')](function(_0x3bc99a){_0x510baf(_0x3bc99a);});});};function _0x35590b(){return function(_0x4aefd0){logger[_0x3c37('0x25')]('cmContact',_0x3c37('0x14'),util[_0x3c37('0x26')](_0x4aefd0,{'showHidden':![],'depth':null}));};}var _0x2596d6=function(_0x45caee){return new BPromise(function(_0x20083e,_0x248ad6){_0x27ec3d[_0x3c37('0x15')][_0x3c37('0x16')]({'where':{'phone':_0x5e29ba[_0x3c37('0xc')],'CampaignId':_0x45caee['id']}})[_0x3c37('0x17')](function(_0x4bd569){if(!_['isNil'](_0x4bd569)){throw new Error('cmContact,\x20afterCreate\x20'+_0x5e29ba[_0x3c37('0xc')]+_0x3c37('0x27')+_0x45caee['id']);}return _0x27ec3d[_0x3c37('0x15')][_0x3c37('0x24')]({'phone':_0x5e29ba[_0x3c37('0xc')],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba[_0x3c37('0x1a')],'priority':_0x5e29ba[_0x3c37('0x1b')]?_0x5e29ba[_0x3c37('0x1b')]:0x2,'scheduledat':_0x5e29ba[_0x3c37('0x1c')]?_0x5e29ba[_0x3c37('0x1c')]:moment()[_0x3c37('0x1f')](_0x3c37('0x1d')),'callback':_0x5e29ba['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5e29ba[_0x3c37('0x1e')]?_0x5e29ba[_0x3c37('0x1e')]:undefined,'callbackat':_0x5e29ba['callbackUniqueid']?moment()['format'](_0x3c37('0x1d')):undefined,'CampaignId':_0x45caee['id']});})[_0x3c37('0x20')](function(_0x3c43a7){_0x248ad6(_0x3c43a7);});});};var _0x43a3f2=function(_0x2ac6e6){return new BPromise(function(_0x46c745,_0x466b28){_0x27ec3d[_0x3c37('0x15')][_0x3c37('0x16')]({'where':{'phone':_0x5e29ba['phone'],'CampaignId':_0x2ac6e6['id']}})[_0x3c37('0x17')](function(_0x38501f){if(!_[_0x3c37('0x18')](_0x38501f)){throw new Error(_0x3c37('0x28')+_0x5e29ba[_0x3c37('0xc')]+_0x3c37('0x29')+_0x2ac6e6['id']);}return _0x27ec3d[_0x3c37('0x22')][_0x3c37('0x16')]({'where':{'calleridnum':_0x5e29ba[_0x3c37('0xc')],'CampaignId':_0x2ac6e6['id']}});})[_0x3c37('0x17')](function(_0x4ca5d7){if(!_[_0x3c37('0x18')](_0x4ca5d7)){throw new Error(_0x3c37('0x23')+_0x5e29ba[_0x3c37('0xc')]+_0x3c37('0x2a')+_0x2ac6e6['id']);}return _0x27ec3d[_0x3c37('0x15')]['create']({'phone':_0x5e29ba[_0x3c37('0xc')],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba[_0x3c37('0x1a')],'priority':_0x5e29ba[_0x3c37('0x1b')]?_0x5e29ba[_0x3c37('0x1b')]:0x2,'scheduledat':_0x5e29ba['scheduledat']?_0x5e29ba[_0x3c37('0x1c')]:moment()['format'](_0x3c37('0x1d')),'callback':_0x5e29ba['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5e29ba[_0x3c37('0x1e')]?_0x5e29ba[_0x3c37('0x1e')]:undefined,'callbackat':_0x5e29ba[_0x3c37('0x1e')]?moment()['format'](_0x3c37('0x1d')):undefined,'CampaignId':_0x2ac6e6['id']});})[_0x3c37('0x20')](function(_0x2e82d0){_0x466b28(_0x2e82d0);});});};if(_0x5e29ba[_0x3c37('0x1a')]){_0x27ec3d[_0x3c37('0x2b')]['findAll']({'where':{'type':_0x3c37('0x2c')},'attributes':['id',_0x3c37('0x2d')],'include':[{'model':_0x27ec3d[_0x3c37('0x2e')],'as':_0x3c37('0x2f'),'where':{'id':_0x5e29ba[_0x3c37('0x1a')]}}]})[_0x3c37('0x17')](function(_0x35f8a2){if(_0x35f8a2){var _0x5e60eb=[];for(var _0x474358=0x0,_0x228f01;_0x474358<_0x35f8a2[_0x3c37('0x30')];_0x474358+=0x1){_0x228f01=_0x35f8a2[_0x474358]['get']({'plain':!![]});switch(_0x228f01[_0x3c37('0x2d')]){case'always':_0x27f83e(_0x228f01)[_0x3c37('0x20')](_0x454f92());break;case _0x3c37('0x31'):_0x52ab1f(_0x228f01)[_0x3c37('0x20')](_0x454f92());break;default:_0x27ec3d[_0x3c37('0x15')]['create']({'phone':_0x5e29ba['phone'],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba['ListId'],'scheduledat':_0x5e29ba[_0x3c37('0x1c')]?_0x5e29ba[_0x3c37('0x1c')]:moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x5e29ba[_0x3c37('0x1b')]?_0x5e29ba['priority']:0x2,'callback':_0x5e29ba[_0x3c37('0x1e')]?!![]:![],'callbackuniqueid':_0x5e29ba[_0x3c37('0x1e')]?_0x5e29ba[_0x3c37('0x1e')]:undefined,'callbackat':_0x5e29ba[_0x3c37('0x1e')]?moment()[_0x3c37('0x1f')](_0x3c37('0x1d')):undefined,'VoiceQueueId':_0x228f01['id']})[_0x3c37('0x20')](_0x454f92());}}}})[_0x3c37('0x17')](function(){return _0x27ec3d[_0x3c37('0x2b')][_0x3c37('0x32')]({'where':{'type':_0x3c37('0x2c')},'attributes':['id'],'include':[{'model':_0x27ec3d['CmList'],'as':_0x3c37('0x33'),'where':{'id':_0x5e29ba[_0x3c37('0x1a')]}}]})['then'](function(_0x173551){if(_0x173551){var _0x359f9c=[];for(var _0x42ddf4=0x0,_0x5a2351;_0x42ddf4<_0x173551[_0x3c37('0x30')];_0x42ddf4+=0x1){_0x5a2351=_0x173551[_0x42ddf4]['get']({'plain':!![]});_0x27ec3d[_0x3c37('0x34')][_0x3c37('0x24')]({'phone':_0x5e29ba[_0x3c37('0xc')],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba['ListId'],'VoiceQueueId':_0x5a2351['id']})[_0x3c37('0x20')](function(_0x415bac){logger[_0x3c37('0x25')](_0x3c37('0x35'),_0x3c37('0x14'),_0x415bac[_0x3c37('0x36')]);});}}});})[_0x3c37('0x17')](function(){return _0x27ec3d[_0x3c37('0x37')][_0x3c37('0x32')]({'attributes':['id',_0x3c37('0x2d')],'include':[{'model':_0x27ec3d[_0x3c37('0x2e')],'as':_0x3c37('0x2f'),'where':{'id':_0x5e29ba[_0x3c37('0x1a')]}}]})['then'](function(_0xed6864){if(_0xed6864){var _0x294ea3=[];for(var _0xd9f15d=0x0,_0x5803a6;_0xd9f15d<_0xed6864[_0x3c37('0x30')];_0xd9f15d+=0x1){_0x5803a6=_0xed6864[_0xd9f15d][_0x3c37('0x13')]({'plain':!![]});switch(_0x5803a6[_0x3c37('0x2d')]){case'always':_0x43a3f2(_0x5803a6)[_0x3c37('0x20')](_0x35590b());break;case _0x3c37('0x31'):_0x2596d6(_0x5803a6)[_0x3c37('0x20')](_0x35590b());break;default:_0x27ec3d[_0x3c37('0x15')][_0x3c37('0x24')]({'phone':_0x5e29ba['phone'],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba[_0x3c37('0x1a')],'scheduledat':_0x5e29ba[_0x3c37('0x1c')]?_0x5e29ba[_0x3c37('0x1c')]:moment()[_0x3c37('0x1f')]('YYYY-MM-DD\x20HH:mm:ss'),'priority':_0x5e29ba['priority']?_0x5e29ba[_0x3c37('0x1b')]:0x2,'callback':_0x5e29ba['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x5e29ba[_0x3c37('0x1e')]?_0x5e29ba[_0x3c37('0x1e')]:undefined,'callbackat':_0x5e29ba[_0x3c37('0x1e')]?moment()[_0x3c37('0x1f')]('YYYY-MM-DD\x20HH:mm:ss'):undefined,'CampaignId':_0x5803a6['id']})[_0x3c37('0x20')](_0x35590b());}}}});})[_0x3c37('0x17')](function(){return _0x27ec3d[_0x3c37('0x37')][_0x3c37('0x32')]({'attributes':['id'],'include':[{'model':_0x27ec3d['CmList'],'as':_0x3c37('0x33'),'where':{'id':_0x5e29ba[_0x3c37('0x1a')]}}]})['then'](function(_0x548e2d){if(_0x548e2d){var _0x41117f=[];for(var _0x521fde=0x0,_0x38fabf;_0x521fde<_0x548e2d[_0x3c37('0x30')];_0x521fde+=0x1){_0x38fabf=_0x548e2d[_0x521fde][_0x3c37('0x13')]({'plain':!![]});_0x27ec3d[_0x3c37('0x34')]['create']({'phone':_0x5e29ba[_0x3c37('0xc')],'ContactId':_0x5e29ba['id'],'ListId':_0x5e29ba['ListId'],'CampaignId':_0x38fabf['id']})[_0x3c37('0x20')](function(_0xfda3a2){logger[_0x3c37('0x25')](_0x3c37('0x35'),'afterCreate',_0xfda3a2[_0x3c37('0x36')]);});}}});})['then'](function(){_0x17a164();})[_0x3c37('0x20')](function(_0x6e6cfe){logger[_0x3c37('0x25')]('cmContact,\x20%s,\x20%s',_0x3c37('0x14'),_0x6e6cfe[_0x3c37('0x36')]);_0x17a164(_0x6e6cfe);});}else{_0x17a164(new Error('List\x20not\x20found'));}},'afterDestroy':function(_0x415627,_0x22a86c,_0x1d5c6f){var _0x5964d4=_0x5b05c6[_0x3c37('0x12')];var _0x3608c6=_0x415627['get']({'plain':!![]});return _0x5964d4['CmHopper']['findAll']({'raw':!![],'where':{'ListId':_0x3608c6[_0x3c37('0x1a')],'ContactId':_0x3608c6['id']}})[_0x3c37('0x17')](function(_0x1fef45){if(!_[_0x3c37('0x38')](_0x1fef45)){var _0x111d05=[];for(var _0x42e3db=0x0;_0x42e3db<_0x1fef45[_0x3c37('0x30')];_0x42e3db+=0x1){var _0x53455c=_0x5964d4[_0x3c37('0x15')][_0x3c37('0x39')]({'where':{'ListId':_0x1fef45[_0x42e3db][_0x3c37('0x1a')],'ContactId':_0x1fef45[_0x42e3db][_0x3c37('0x3a')],'VoiceQueueId':_0x1fef45[_0x42e3db][_0x3c37('0x3b')],'CampaignId':_0x1fef45[_0x42e3db]['CampaignId']}});_0x111d05[_0x3c37('0x3c')](_0x53455c);}return BPromise[_0x3c37('0x3d')](_0x111d05);}})[_0x3c37('0x17')](function(_0x34b8ec){logger[_0x3c37('0x3e')]('[CmContact][afterDestroy][Succesfull]',util[_0x3c37('0x26')](_0x34b8ec,{'showHidden':![],'depth':null}));_0x1d5c6f();})[_0x3c37('0x20')](function(_0x7972a3){logger[_0x3c37('0x25')](_0x3c37('0x3f'),util['inspect'](_0x7972a3,{'showHidden':![],'depth':null}));_0x1d5c6f(_0x7972a3);});}}});};