Built motion from commit 9c91e9f5.|2.6.29
[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 _0x4f45=['lastName','fax','email','FULLTEXT','utf8mb4','models','error','[cmContact][afterCreate]\x20List\x20not\x20found\x20for\x20contact\x20with\x20id\x20','[cmContact][afterCreate]','message','get','isEmpty','destroy','ContactId','push','info','inspect','catch','[CmContact][afterDestroy][Error]','lodash','util','../../config/logger','api','Campaign','VoiceQueue','scheduledat','format','YYYY-MM-DD\x20HH:mm:ss','isNil','dialTimezone','resolve','findOne','then','timezones','utcOffset','add','minutes','phone','ListId','priority','callbackUniqueid','assign','CmHopper','\x20in\x20Campaign\x20with\x20id\x20','CmHopperFinal','[Always]\x20Hopper\x20Final\x20already\x20exists\x20for\x20contact\x20with\x20phone\x20','create','[OnlyIfOpen]\x20Hopper\x20already\x20exists\x20for\x20contact\x20with\x20phone\x20','CampaignId','VoiceQueueId','always','onlyIfOpen','dialCheckDuplicateType','CmList','Lists','map','all','findAll','outbound','BlackLists','CmHopperBlack','define','CmContact','cm_contacts','fti_cm_contacts','firstName'];(function(_0x2c7dc6,_0x3c4ce0){var _0x10002b=function(_0x26f390){while(--_0x26f390){_0x2c7dc6['push'](_0x2c7dc6['shift']());}};_0x10002b(++_0x3c4ce0);}(_0x4f45,0x97));var _0x54f4=function(_0x56222a,_0x13bb54){_0x56222a=_0x56222a-0x0;var _0x517ab3=_0x4f45[_0x56222a];return _0x517ab3;};'use strict';var _=require(_0x54f4('0x0'));var util=require(_0x54f4('0x1'));var logger=require(_0x54f4('0x2'))(_0x54f4('0x3'));var moment=require('moment');var shared=require('../../config/environment/shared');var attributes=require('./cmContact.attributes');function getTimezoneCallbackAt(_0x53e07e,_0x5f9247,_0x4e5b20,_0x33e2a0){var _0x2842d6=_0x33e2a0?_0x54f4('0x4'):_0x54f4('0x5');var _0xc85af3=_0x4e5b20['scheduledat']?_0x4e5b20[_0x54f4('0x6')]:moment()[_0x54f4('0x7')](_0x54f4('0x8'));if(_[_0x54f4('0x9')](_0x4e5b20[_0x54f4('0xa')])){return Promise[_0x54f4('0xb')](_0xc85af3);}return _0x53e07e[_0x2842d6][_0x54f4('0xc')]({'where':{'id':_0x5f9247}})[_0x54f4('0xd')](function(_0x525e53){if(_[_0x54f4('0x9')](_0x525e53)||_['isNil'](_0x525e53['dialTimezone'])){return Promise[_0x54f4('0xb')](_0xc85af3);}var _0x20e26a=shared[_0x54f4('0xe')];var _0x4275d3=_0x20e26a[_0x525e53[_0x54f4('0xa')]];var _0x24fc1b=_0x20e26a[_0x4e5b20[_0x54f4('0xa')]];if(_[_0x54f4('0x9')](_0x4275d3)&&_['isNil'](_0x24fc1b)){return Promise['resolve'](_0xc85af3);}var _0x4658aa=_0x4275d3[_0x54f4('0xf')]-_0x24fc1b[_0x54f4('0xf')];var _0x5628fb=_0x4e5b20['scheduledat']?_0x4e5b20[_0x54f4('0x6')]:moment();_0xc85af3=moment(_0x5628fb)[_0x54f4('0x10')](_0x4658aa,_0x54f4('0x11'))[_0x54f4('0x7')](_0x54f4('0x8'));return Promise[_0x54f4('0xb')](_0xc85af3);});}function getNewHopperPayload(_0x35d462){return{'phone':_0x35d462[_0x54f4('0x12')],'ContactId':_0x35d462['id'],'ListId':_0x35d462[_0x54f4('0x13')],'scheduledat':_0x35d462[_0x54f4('0x6')]?_0x35d462[_0x54f4('0x6')]:moment()[_0x54f4('0x7')](_0x54f4('0x8')),'priority':_0x35d462[_0x54f4('0x14')]?_0x35d462['priority']:0x2,'callback':_0x35d462['callbackUniqueid']?!![]:![],'callbackuniqueid':_0x35d462[_0x54f4('0x15')]?_0x35d462[_0x54f4('0x15')]:undefined,'callbackat':_0x35d462[_0x54f4('0x15')]?moment()['format'](_0x54f4('0x8')):undefined};}function handleAlwaysCheckDuplicate(_0x2904bc,_0x37f2cf,_0x21650a,_0x4f55ef){var _0x392de1={'phone':_0x21650a['phone']};var _0x54a245={'calleridnum':_0x21650a['phone']};if(_0x4f55ef){Object[_0x54f4('0x16')](_0x392de1,{'CampaignId':_0x37f2cf['id']});Object[_0x54f4('0x16')](_0x54a245,{'CampaignId':_0x37f2cf['id']});}else{Object[_0x54f4('0x16')](_0x392de1,{'VoiceQueueId':_0x37f2cf['id']});Object[_0x54f4('0x16')](_0x54a245,{'VoiceQueueId':_0x37f2cf['id']});}return _0x2904bc[_0x54f4('0x17')][_0x54f4('0xc')]({'where':_0x392de1})[_0x54f4('0xd')](function(_0x1ccf28){if(_0x1ccf28){throw new Error('[Always]\x20Hopper\x20already\x20exists\x20for\x20contact\x20with\x20phone\x20'+_0x21650a[_0x54f4('0x12')]+_0x54f4('0x18')+_0x37f2cf['id']);}return _0x2904bc[_0x54f4('0x19')][_0x54f4('0xc')]({'where':_0x54a245});})[_0x54f4('0xd')](function(_0x1537c9){if(_0x1537c9){throw new Error(_0x54f4('0x1a')+_0x21650a[_0x54f4('0x12')]+_0x54f4('0x18')+_0x37f2cf['id']);}var _0x628eb0=getNewHopperPayload(_0x21650a);if(_0x4f55ef){_0x628eb0['CampaignId']=_0x37f2cf['id'];}else{_0x628eb0['VoiceQueueId']=_0x37f2cf['id'];}return _0x2904bc['CmHopper'][_0x54f4('0x1b')](_0x628eb0);});}function handleOnlyIfOpenCheckDuplicate(_0x2e1128,_0x1af927,_0x1fb220,_0x10bcc9){var _0x4cca9e={'phone':_0x1fb220['phone']};if(_0x10bcc9){Object['assign'](_0x4cca9e,{'CampaignId':_0x1af927['id']});}else{Object[_0x54f4('0x16')](_0x4cca9e,{'VoiceQueueId':_0x1af927['id']});}return _0x2e1128['CmHopper'][_0x54f4('0xc')]({'where':_0x4cca9e})[_0x54f4('0xd')](function(_0x1e8fa9){if(_0x1e8fa9){throw new Error(_0x54f4('0x1c')+_0x1fb220[_0x54f4('0x12')]+_0x54f4('0x18')+_0x1af927['id']);}var _0x41c19b=getNewHopperPayload(_0x1fb220);if(_0x10bcc9){_0x41c19b[_0x54f4('0x1d')]=_0x1af927['id'];}else{_0x41c19b[_0x54f4('0x1e')]=_0x1af927['id'];}return _0x2e1128['CmHopper'][_0x54f4('0x1b')](_0x41c19b);});}function handleNeverCheckDuplicate(_0x4c6085,_0x110804,_0x59e52e){var _0x36017c=getNewHopperPayload(_0x59e52e);_0x36017c[_0x54f4('0x1e')]=_0x110804['id'];return _0x4c6085['CmHopper'][_0x54f4('0x1b')](_0x36017c);}function handleCheckDuplicate(_0x7220d2,_0x83fa61,_0x5f3c8b,_0x598aaf){return getTimezoneCallbackAt(_0x7220d2,_0x83fa61,_0x5f3c8b,_0x598aaf)[_0x54f4('0xd')](function(_0x48b551){_0x5f3c8b[_0x54f4('0x6')]=_0x48b551;switch(_0x83fa61['dialCheckDuplicateType']){case _0x54f4('0x1f'):return handleAlwaysCheckDuplicate(_0x7220d2,_0x83fa61,_0x5f3c8b,_0x598aaf);case _0x54f4('0x20'):return handleOnlyIfOpenCheckDuplicate(_0x7220d2,_0x83fa61,_0x5f3c8b,_0x598aaf);default:return handleNeverCheckDuplicate(_0x7220d2,_0x83fa61,_0x5f3c8b);}});}function createHopperForVoiceQueues(_0x2ba7b3,_0x4dc959){return _0x2ba7b3[_0x54f4('0x5')]['findAll']({'where':{'type':'outbound'},'attributes':['id',_0x54f4('0x21'),_0x54f4('0xa')],'include':[{'model':_0x2ba7b3[_0x54f4('0x22')],'as':'Lists','where':{'id':_0x4dc959[_0x54f4('0x13')]}}],'raw':!![]})[_0x54f4('0xd')](function(_0xee17f5){var _0x1b9fa7=_0xee17f5['map'](function(_0x3f9b0e){return handleCheckDuplicate(_0x2ba7b3,_0x3f9b0e,_0x4dc959,![]);});return Promise['all'](_0x1b9fa7);});}function createHopperForCampaigns(_0x2db092,_0x2a4b60){return _0x2db092[_0x54f4('0x4')]['findAll']({'attributes':['id',_0x54f4('0x21'),_0x54f4('0xa')],'include':[{'model':_0x2db092[_0x54f4('0x22')],'as':_0x54f4('0x23'),'where':{'id':_0x2a4b60[_0x54f4('0x13')]}}],'raw':!![]})[_0x54f4('0xd')](function(_0x28cda7){var _0x667174=_0x28cda7[_0x54f4('0x24')](function(_0x2d994e){return handleCheckDuplicate(_0x2db092,_0x2d994e,_0x2a4b60,!![]);});return Promise[_0x54f4('0x25')](_0x667174);});}function createHopperBlackForVoiceQueues(_0x3ee440,_0x22e1f8){return _0x3ee440['VoiceQueue'][_0x54f4('0x26')]({'where':{'type':_0x54f4('0x27')},'attributes':['id'],'include':[{'model':_0x3ee440[_0x54f4('0x22')],'as':_0x54f4('0x28'),'where':{'id':_0x22e1f8['ListId']}}],'raw':!![]})[_0x54f4('0xd')](function(_0x14ffea){var _0x5a9141=_0x14ffea[_0x54f4('0x24')](function(_0x424559){return _0x3ee440[_0x54f4('0x29')][_0x54f4('0x1b')]({'phone':_0x22e1f8[_0x54f4('0x12')],'ContactId':_0x22e1f8['id'],'ListId':_0x22e1f8[_0x54f4('0x13')],'VoiceQueueId':_0x424559['id']});});return Promise['all'](_0x5a9141);});}function createHopperBlackForCampaigns(_0x372d01,_0x5f32f0){return _0x372d01[_0x54f4('0x4')][_0x54f4('0x26')]({'attributes':['id'],'include':[{'model':_0x372d01[_0x54f4('0x22')],'as':'BlackLists','where':{'id':_0x5f32f0[_0x54f4('0x13')]}}],'raw':!![]})[_0x54f4('0xd')](function(_0x3d0559){var _0x26078a=_0x3d0559[_0x54f4('0x24')](function(_0x594faa){return _0x372d01['CmHopperBlack'][_0x54f4('0x1b')]({'phone':_0x5f32f0[_0x54f4('0x12')],'ContactId':_0x5f32f0['id'],'ListId':_0x5f32f0[_0x54f4('0x13')],'CampaignId':_0x594faa['id']});});return Promise['all'](_0x26078a);});}module['exports']=function(_0x2843bd){return _0x2843bd[_0x54f4('0x2a')](_0x54f4('0x2b'),attributes,{'tableName':_0x54f4('0x2c'),'paranoid':!![],'indexes':[{'name':_0x54f4('0x12'),'fields':[_0x54f4('0x12')]},{'name':_0x54f4('0x2d'),'fields':[_0x54f4('0x2e'),_0x54f4('0x2f'),_0x54f4('0x12'),'mobile',_0x54f4('0x30'),_0x54f4('0x31')],'type':_0x54f4('0x32')}],'timestamps':!![],'charset':_0x54f4('0x33'),'collate':'utf8mb4_unicode_ci','hooks':{'afterCreate':function(_0x305bd6,_0x56417e,_0x5239ed){var _0xa30235=_0x2843bd[_0x54f4('0x34')];var _0x361513=_0x305bd6['get']({'plain':!![]});if(!_0x361513[_0x54f4('0x13')]){logger[_0x54f4('0x35')](_0x54f4('0x36')+_0x361513['id']);return _0x5239ed();}if(!_0x361513[_0x54f4('0x12')]){return _0x5239ed();}return createHopperForVoiceQueues(_0xa30235,_0x361513)[_0x54f4('0xd')](function(){return createHopperBlackForVoiceQueues(_0xa30235,_0x361513);})['then'](function(){return createHopperForCampaigns(_0xa30235,_0x361513);})['then'](function(){return createHopperBlackForCampaigns(_0xa30235,_0x361513);})['then'](function(){return _0x5239ed();})['catch'](function(_0x2c200d){logger[_0x54f4('0x35')](_0x54f4('0x37'),_0x2c200d[_0x54f4('0x38')]);_0x5239ed();});},'afterDestroy':function(_0x2bb47b,_0x510592,_0x17632a){var _0x75893e=_0x2843bd[_0x54f4('0x34')];var _0x557ffe=_0x2bb47b[_0x54f4('0x39')]({'plain':!![]});return _0x75893e['CmHopper'][_0x54f4('0x26')]({'raw':!![],'where':{'ListId':_0x557ffe['ListId'],'ContactId':_0x557ffe['id']}})['then'](function(_0x111dff){if(!_[_0x54f4('0x3a')](_0x111dff)){var _0x37282d=[];for(var _0x25d61f=0x0;_0x25d61f<_0x111dff['length'];_0x25d61f+=0x1){var _0x4cb43b=_0x75893e['CmHopper'][_0x54f4('0x3b')]({'where':{'ListId':_0x111dff[_0x25d61f]['ListId'],'ContactId':_0x111dff[_0x25d61f][_0x54f4('0x3c')],'VoiceQueueId':_0x111dff[_0x25d61f][_0x54f4('0x1e')],'CampaignId':_0x111dff[_0x25d61f]['CampaignId']}});_0x37282d[_0x54f4('0x3d')](_0x4cb43b);}return Promise['all'](_0x37282d);}})[_0x54f4('0xd')](function(_0x2e8d2b){logger[_0x54f4('0x3e')]('[CmContact][afterDestroy][Succesfull]',util[_0x54f4('0x3f')](_0x2e8d2b,{'showHidden':![],'depth':null}));_0x17632a();})[_0x54f4('0x40')](function(_0x4f37e0){logger[_0x54f4('0x35')](_0x54f4('0x41'),util['inspect'](_0x4f37e0,{'showHidden':![],'depth':null}));_0x17632a(_0x4f37e0);});}}});};