f59a1255542c52f2cfc9e6644e74bed80677e7af
[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 _0x4bcf=['message','models','isEmpty','length','destroy','ContactId','push','info','[CmContact][afterDestroy][Succesfull]','inspect','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','api','moment','../../config/environment/shared','./cmContact.attributes','Campaign','VoiceQueue','format','YYYY-MM-DD\x20HH:mm:ss','isNil','dialTimezone','resolve','findOne','then','utcOffset','add','minutes','ListId','scheduledat','priority','callbackUniqueid','phone','assign','CmHopperFinal','[Always]\x20Hopper\x20Final\x20already\x20exists\x20for\x20contact\x20with\x20phone\x20','\x20in\x20Campaign\x20with\x20id\x20','CampaignId','CmHopper','create','[OnlyIfOpen]\x20Hopper\x20already\x20exists\x20for\x20contact\x20with\x20phone\x20','VoiceQueueId','always','onlyIfOpen','findAll','outbound','dialCheckDuplicateType','CmList','Lists','map','all','BlackLists','CmHopperBlack','exports','define','CmContact','cm_contacts','fti_cm_contacts','firstName','lastName','mobile','fax','email','FULLTEXT','utf8mb4','get','[cmContact][afterCreate]\x20List\x20not\x20found\x20for\x20contact\x20with\x20id\x20','catch','error'];(function(_0x5f6a8a,_0x45f474){var _0x2cce4d=function(_0x590b80){while(--_0x590b80){_0x5f6a8a['push'](_0x5f6a8a['shift']());}};_0x2cce4d(++_0x45f474);}(_0x4bcf,0xe0));var _0xf4bc=function(_0xe6aba1,_0x4ff2b4){_0xe6aba1=_0xe6aba1-0x0;var _0x56482c=_0x4bcf[_0xe6aba1];return _0x56482c;};'use strict';var _=require(_0xf4bc('0x0'));var util=require(_0xf4bc('0x1'));var logger=require(_0xf4bc('0x2'))(_0xf4bc('0x3'));var moment=require(_0xf4bc('0x4'));var shared=require(_0xf4bc('0x5'));var attributes=require(_0xf4bc('0x6'));function getTimezoneCallbackAt(_0x1cc4d0,_0x286cbe,_0xef7287,_0x45f1c5){var _0x1034f2=_0x45f1c5?_0xf4bc('0x7'):_0xf4bc('0x8');var _0x5c7a82=_0xef7287['scheduledat']?_0xef7287['scheduledat']:moment()[_0xf4bc('0x9')](_0xf4bc('0xa'));if(_[_0xf4bc('0xb')](_0xef7287[_0xf4bc('0xc')])){return Promise[_0xf4bc('0xd')](_0x5c7a82);}return _0x1cc4d0[_0x1034f2][_0xf4bc('0xe')]({'where':{'id':_0x286cbe}})[_0xf4bc('0xf')](function(_0xeaa122){if(_['isNil'](_0xeaa122)||_[_0xf4bc('0xb')](_0xeaa122[_0xf4bc('0xc')])){return Promise[_0xf4bc('0xd')](_0x5c7a82);}var _0x3ff818=shared['timezones'];var _0x17353f=_0x3ff818[_0xeaa122[_0xf4bc('0xc')]];var _0x59e2df=_0x3ff818[_0xef7287[_0xf4bc('0xc')]];if(_[_0xf4bc('0xb')](_0x17353f)&&_[_0xf4bc('0xb')](_0x59e2df)){return Promise[_0xf4bc('0xd')](_0x5c7a82);}var _0x74ab20=_0x17353f['utcOffset']-_0x59e2df[_0xf4bc('0x10')];var _0x30bff0=_0xef7287['scheduledat']?_0xef7287['scheduledat']:moment();_0x5c7a82=moment(_0x30bff0)[_0xf4bc('0x11')](_0x74ab20,_0xf4bc('0x12'))[_0xf4bc('0x9')](_0xf4bc('0xa'));return Promise[_0xf4bc('0xd')](_0x5c7a82);});}function getNewHopperPayload(_0x118699){return{'phone':_0x118699['phone'],'ContactId':_0x118699['id'],'ListId':_0x118699[_0xf4bc('0x13')],'scheduledat':_0x118699[_0xf4bc('0x14')]?_0x118699[_0xf4bc('0x14')]:moment()['format'](_0xf4bc('0xa')),'priority':_0x118699[_0xf4bc('0x15')]?_0x118699['priority']:0x2,'callback':_0x118699[_0xf4bc('0x16')]?!![]:![],'callbackuniqueid':_0x118699[_0xf4bc('0x16')]?_0x118699[_0xf4bc('0x16')]:undefined,'callbackat':_0x118699[_0xf4bc('0x16')]?moment()[_0xf4bc('0x9')](_0xf4bc('0xa')):undefined};}function handleAlwaysCheckDuplicate(_0x4ac63f,_0x495f18,_0x10ff98,_0x19f0e6){var _0x5d9f6f={'phone':_0x10ff98[_0xf4bc('0x17')]};var _0x1b0418={'calleridnum':_0x10ff98[_0xf4bc('0x17')]};if(_0x19f0e6){Object[_0xf4bc('0x18')](_0x5d9f6f,{'CampaignId':_0x495f18['id']});Object[_0xf4bc('0x18')](_0x1b0418,{'CampaignId':_0x495f18['id']});}else{Object[_0xf4bc('0x18')](_0x5d9f6f,{'VoiceQueueId':_0x495f18['id']});Object['assign'](_0x1b0418,{'VoiceQueueId':_0x495f18['id']});}return _0x4ac63f['CmHopper'][_0xf4bc('0xe')]({'where':_0x5d9f6f})['then'](function(_0x12991c){if(_0x12991c){throw new Error('[Always]\x20Hopper\x20already\x20exists\x20for\x20contact\x20with\x20phone\x20'+_0x10ff98[_0xf4bc('0x17')]+'\x20in\x20Campaign\x20with\x20id\x20'+_0x495f18['id']);}return _0x4ac63f[_0xf4bc('0x19')][_0xf4bc('0xe')]({'where':_0x1b0418});})[_0xf4bc('0xf')](function(_0x3c2b45){if(_0x3c2b45){throw new Error(_0xf4bc('0x1a')+_0x10ff98[_0xf4bc('0x17')]+_0xf4bc('0x1b')+_0x495f18['id']);}var _0x4b47e6=getNewHopperPayload(_0x10ff98);if(_0x19f0e6){_0x4b47e6[_0xf4bc('0x1c')]=_0x495f18['id'];}else{_0x4b47e6['VoiceQueueId']=_0x495f18['id'];}return _0x4ac63f[_0xf4bc('0x1d')][_0xf4bc('0x1e')](_0x4b47e6);});}function handleOnlyIfOpenCheckDuplicate(_0x505c3a,_0x22e13b,_0xb82f93,_0x106bd2){var _0x5bec11={'phone':_0xb82f93[_0xf4bc('0x17')]};if(_0x106bd2){Object[_0xf4bc('0x18')](_0x5bec11,{'CampaignId':_0x22e13b['id']});}else{Object[_0xf4bc('0x18')](_0x5bec11,{'VoiceQueueId':_0x22e13b['id']});}return _0x505c3a[_0xf4bc('0x1d')][_0xf4bc('0xe')]({'where':_0x5bec11})[_0xf4bc('0xf')](function(_0x428ae){if(_0x428ae){throw new Error(_0xf4bc('0x1f')+_0xb82f93[_0xf4bc('0x17')]+_0xf4bc('0x1b')+_0x22e13b['id']);}var _0x4a6e1b=getNewHopperPayload(_0xb82f93);if(_0x106bd2){_0x4a6e1b[_0xf4bc('0x1c')]=_0x22e13b['id'];}else{_0x4a6e1b[_0xf4bc('0x20')]=_0x22e13b['id'];}return _0x505c3a[_0xf4bc('0x1d')][_0xf4bc('0x1e')](_0x4a6e1b);});}function handleNeverCheckDuplicate(_0x348c15,_0x59fd0f,_0x2ac0fc){var _0x3995af=getNewHopperPayload(_0x2ac0fc);_0x3995af['VoiceQueueId']=_0x59fd0f['id'];return _0x348c15['CmHopper'][_0xf4bc('0x1e')](_0x3995af);}function handleCheckDuplicate(_0x1d0583,_0x52db0e,_0xe1289e,_0x4cc2b0){return getTimezoneCallbackAt(_0x1d0583,_0x52db0e,_0xe1289e,_0x4cc2b0)['then'](function(_0x408906){_0xe1289e[_0xf4bc('0x14')]=_0x408906;switch(_0x52db0e['dialCheckDuplicateType']){case _0xf4bc('0x21'):return handleAlwaysCheckDuplicate(_0x1d0583,_0x52db0e,_0xe1289e,_0x4cc2b0);case _0xf4bc('0x22'):return handleOnlyIfOpenCheckDuplicate(_0x1d0583,_0x52db0e,_0xe1289e,_0x4cc2b0);default:return handleNeverCheckDuplicate(_0x1d0583,_0x52db0e,_0xe1289e);}});}function createHopperForVoiceQueues(_0x59964a,_0x17e281){return _0x59964a[_0xf4bc('0x8')][_0xf4bc('0x23')]({'where':{'type':_0xf4bc('0x24')},'attributes':['id',_0xf4bc('0x25'),_0xf4bc('0xc')],'include':[{'model':_0x59964a[_0xf4bc('0x26')],'as':_0xf4bc('0x27'),'where':{'id':_0x17e281['ListId']}}],'raw':!![]})[_0xf4bc('0xf')](function(_0x2e52a0){var _0x53d719=_0x2e52a0[_0xf4bc('0x28')](function(_0x4d32b6){return handleCheckDuplicate(_0x59964a,_0x4d32b6,_0x17e281,![]);});return Promise[_0xf4bc('0x29')](_0x53d719);});}function createHopperForCampaigns(_0xeba7b,_0x4c4734){return _0xeba7b[_0xf4bc('0x7')][_0xf4bc('0x23')]({'attributes':['id',_0xf4bc('0x25'),_0xf4bc('0xc')],'include':[{'model':_0xeba7b['CmList'],'as':_0xf4bc('0x27'),'where':{'id':_0x4c4734[_0xf4bc('0x13')]}}],'raw':!![]})[_0xf4bc('0xf')](function(_0x5532d4){var _0x3a5860=_0x5532d4[_0xf4bc('0x28')](function(_0x6c595c){return handleCheckDuplicate(_0xeba7b,_0x6c595c,_0x4c4734,!![]);});return Promise[_0xf4bc('0x29')](_0x3a5860);});}function createHopperBlackForVoiceQueues(_0xfb8b5a,_0x797547){return _0xfb8b5a[_0xf4bc('0x8')]['findAll']({'where':{'type':'outbound'},'attributes':['id'],'include':[{'model':_0xfb8b5a[_0xf4bc('0x26')],'as':'BlackLists','where':{'id':_0x797547[_0xf4bc('0x13')]}}],'raw':!![]})[_0xf4bc('0xf')](function(_0x1436ff){var _0x5c535a=_0x1436ff[_0xf4bc('0x28')](function(_0x5b5e9a){return _0xfb8b5a['CmHopperBlack'][_0xf4bc('0x1e')]({'phone':_0x797547[_0xf4bc('0x17')],'ContactId':_0x797547['id'],'ListId':_0x797547[_0xf4bc('0x13')],'VoiceQueueId':_0x5b5e9a['id']});});return Promise['all'](_0x5c535a);});}function createHopperBlackForCampaigns(_0x520f9b,_0x181e00){return _0x520f9b[_0xf4bc('0x7')]['findAll']({'attributes':['id'],'include':[{'model':_0x520f9b[_0xf4bc('0x26')],'as':_0xf4bc('0x2a'),'where':{'id':_0x181e00[_0xf4bc('0x13')]}}],'raw':!![]})[_0xf4bc('0xf')](function(_0x2c35f0){var _0x3de3bf=_0x2c35f0['map'](function(_0x2f8f62){return _0x520f9b[_0xf4bc('0x2b')][_0xf4bc('0x1e')]({'phone':_0x181e00['phone'],'ContactId':_0x181e00['id'],'ListId':_0x181e00[_0xf4bc('0x13')],'CampaignId':_0x2f8f62['id']});});return Promise[_0xf4bc('0x29')](_0x3de3bf);});}module[_0xf4bc('0x2c')]=function(_0x229dd7){return _0x229dd7[_0xf4bc('0x2d')](_0xf4bc('0x2e'),attributes,{'tableName':_0xf4bc('0x2f'),'paranoid':!![],'indexes':[{'name':'phone','fields':[_0xf4bc('0x17')]},{'name':_0xf4bc('0x30'),'fields':[_0xf4bc('0x31'),_0xf4bc('0x32'),_0xf4bc('0x17'),_0xf4bc('0x33'),_0xf4bc('0x34'),_0xf4bc('0x35')],'type':_0xf4bc('0x36')}],'timestamps':!![],'charset':_0xf4bc('0x37'),'collate':'utf8mb4_unicode_ci','hooks':{'afterCreate':function(_0x29121d,_0x2e28e8,_0x3c09da){var _0x422d51=_0x229dd7['models'];var _0x1b2202=_0x29121d[_0xf4bc('0x38')]({'plain':!![]});if(!_0x1b2202['ListId']){logger['error'](_0xf4bc('0x39')+_0x1b2202['id']);return _0x3c09da();}if(!_0x1b2202['phone']){return _0x3c09da();}return createHopperForVoiceQueues(_0x422d51,_0x1b2202)[_0xf4bc('0xf')](function(){return createHopperBlackForVoiceQueues(_0x422d51,_0x1b2202);})[_0xf4bc('0xf')](function(){return createHopperForCampaigns(_0x422d51,_0x1b2202);})[_0xf4bc('0xf')](function(){return createHopperBlackForCampaigns(_0x422d51,_0x1b2202);})[_0xf4bc('0xf')](function(){return _0x3c09da();})[_0xf4bc('0x3a')](function(_0x25fc4e){logger[_0xf4bc('0x3b')]('[cmContact][afterCreate]',_0x25fc4e[_0xf4bc('0x3c')]);_0x3c09da();});},'afterDestroy':function(_0x4c4307,_0xc2f22e,_0x4e3f0f){var _0x16fecb=_0x229dd7[_0xf4bc('0x3d')];var _0x456a8b=_0x4c4307[_0xf4bc('0x38')]({'plain':!![]});return _0x16fecb[_0xf4bc('0x1d')][_0xf4bc('0x23')]({'raw':!![],'where':{'ListId':_0x456a8b[_0xf4bc('0x13')],'ContactId':_0x456a8b['id']}})[_0xf4bc('0xf')](function(_0x54f6e4){if(!_[_0xf4bc('0x3e')](_0x54f6e4)){var _0x10107e=[];for(var _0x2eb2a4=0x0;_0x2eb2a4<_0x54f6e4[_0xf4bc('0x3f')];_0x2eb2a4+=0x1){var _0x25f12f=_0x16fecb['CmHopper'][_0xf4bc('0x40')]({'where':{'ListId':_0x54f6e4[_0x2eb2a4][_0xf4bc('0x13')],'ContactId':_0x54f6e4[_0x2eb2a4][_0xf4bc('0x41')],'VoiceQueueId':_0x54f6e4[_0x2eb2a4][_0xf4bc('0x20')],'CampaignId':_0x54f6e4[_0x2eb2a4]['CampaignId']}});_0x10107e[_0xf4bc('0x42')](_0x25f12f);}return Promise[_0xf4bc('0x29')](_0x10107e);}})[_0xf4bc('0xf')](function(_0x266b16){logger[_0xf4bc('0x43')](_0xf4bc('0x44'),util[_0xf4bc('0x45')](_0x266b16,{'showHidden':![],'depth':null}));_0x4e3f0f();})['catch'](function(_0x47d551){logger['error'](_0xf4bc('0x46'),util['inspect'](_0x47d551,{'showHidden':![],'depth':null}));_0x4e3f0f(_0x47d551);});}}});};