1c06193c567f91baac2ad21ca48f697d84d01480
[motion2.git] / server / api / cmHopperAdditionalPhone / cmHopperAdditionalPhone.rpc.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 _0x74cc=['stringify','code','error','message','result','catch','VerifyAdditionalPhones','CmHopper','findOne','body','ContactId','ListId','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','OrderBy','ASC','countbusyretry','countcongestionretry','countnosuchnumberretry','countdropretry','countabandonedretry','countmachineretry','countagentrejectretry','entity','SwapAdditionalPhones','waitTime','duration','isNil','scheduledat','minutes','add','phone','countnoanswerretry','createdAt','updatedAt','transaction','update','activateAdditionalPhone','IncrementPreviewCounters','lodash','util','moment','bluebird','randomstring','../../mysqldb','../../config/utils','../../config/logger','../../config/environment','client','http','request','then','info','CmHopperAdditionalPhone,\x20%s,\x20%s','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s'];(function(_0x18c50a,_0x22144f){var _0x6dd007=function(_0x19d742){while(--_0x19d742){_0x18c50a['push'](_0x18c50a['shift']());}};_0x6dd007(++_0x22144f);}(_0x74cc,0x184));var _0xc74c=function(_0x243b2f,_0x30aa15){_0x243b2f=_0x243b2f-0x0;var _0xcb3c2d=_0x74cc[_0x243b2f];return _0xcb3c2d;};'use strict';var _=require(_0xc74c('0x0'));var util=require(_0xc74c('0x1'));var moment=require(_0xc74c('0x2'));var BPromise=require(_0xc74c('0x3'));var rs=require(_0xc74c('0x4'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0xc74c('0x5'))['db'];var utils=require(_0xc74c('0x6'));var logger=require(_0xc74c('0x7'))('rpc');var config=require(_0xc74c('0x8'));var jayson=require('jayson/promise');var client=jayson[_0xc74c('0x9')][_0xc74c('0xa')]({'port':0x232a});function respondWithRpcPromise(_0x40f77f,_0x1e2016,_0x51495b){return new BPromise(function(_0x20d7f2,_0x1e0595){return client[_0xc74c('0xb')](_0x40f77f,_0x51495b)[_0xc74c('0xc')](function(_0x28f5c5){logger[_0xc74c('0xd')](_0xc74c('0xe'),_0x1e2016,_0xc74c('0xf'));logger[_0xc74c('0x10')](_0xc74c('0x11'),_0x1e2016,_0xc74c('0xf'),JSON[_0xc74c('0x12')](_0x28f5c5));if(_0x28f5c5['error']){if(_0x28f5c5['error'][_0xc74c('0x13')]===0x1f4){logger[_0xc74c('0x14')](_0xc74c('0xe'),_0x1e2016,_0x28f5c5['error'][_0xc74c('0x15')]);return _0x1e0595(_0x28f5c5[_0xc74c('0x14')][_0xc74c('0x15')]);}logger['error'](_0xc74c('0xe'),_0x1e2016,_0x28f5c5[_0xc74c('0x14')]['message']);return _0x20d7f2(_0x28f5c5[_0xc74c('0x14')][_0xc74c('0x15')]);}else{logger[_0xc74c('0xd')](_0xc74c('0xe'),_0x1e2016,_0xc74c('0xf'));_0x20d7f2(_0x28f5c5[_0xc74c('0x16')][_0xc74c('0x15')]);}})[_0xc74c('0x17')](function(_0x85c971){logger[_0xc74c('0x14')]('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x1e2016,_0x85c971);_0x1e0595(_0x85c971);});});}exports[_0xc74c('0x18')]=function(_0x490e04){var _0x494fef=this;return new Promise(function(_0x5d3a7e,_0x5e70a0){return db[_0xc74c('0x19')][_0xc74c('0x1a')]({'raw':!![],'where':{'ContactId':_0x490e04[_0xc74c('0x1b')][_0xc74c('0x1c')],'ListId':_0x490e04[_0xc74c('0x1b')][_0xc74c('0x1d')],'VoiceQueueId':_0x490e04['body'][_0xc74c('0x1e')]===undefined?null:_0x490e04['body'][_0xc74c('0x1e')],'CampaignId':_0x490e04[_0xc74c('0x1b')]['CampaignId']===undefined?null:_0x490e04['body'][_0xc74c('0x1f')]}})['then'](function(_0x269ba5){if(_0x269ba5!=null){return db[_0xc74c('0x20')][_0xc74c('0x1a')]({'raw':!![],'where':{'CmHopperId':_0x269ba5['id'],'OrderBy':{'$gt':_0x269ba5[_0xc74c('0x21')]}},'order':[['OrderBy',_0xc74c('0x22')]]})[_0xc74c('0xc')](function(_0x66e8b3){var _0x2bf652=0x0;if(_0x66e8b3){if(_0x66e8b3[_0xc74c('0x23')]+_0x66e8b3[_0xc74c('0x24')]+_0x66e8b3['countnoanswerretry']+_0x66e8b3[_0xc74c('0x25')]+_0x66e8b3[_0xc74c('0x26')]+_0x66e8b3[_0xc74c('0x27')]+_0x66e8b3[_0xc74c('0x28')]+_0x66e8b3[_0xc74c('0x29')]<_0x490e04['entity']['dialGlobalMaxRetry'])_0x2bf652=0x1;_0x5d3a7e({'additional':_0x2bf652});}else{return db[_0xc74c('0x20')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x269ba5['id']},'order':[[_0xc74c('0x21'),_0xc74c('0x22')]]})['then'](function(_0x55a221){var _0x22a6cc=0x0;if(_0x55a221){if(_0x55a221['countbusyretry']+_0x55a221[_0xc74c('0x24')]+_0x55a221['countnoanswerretry']+_0x55a221[_0xc74c('0x25')]+_0x55a221[_0xc74c('0x26')]+_0x55a221['countabandonedretry']+_0x55a221[_0xc74c('0x28')]+_0x55a221[_0xc74c('0x29')]<_0x490e04[_0xc74c('0x2a')]['dialGlobalMaxRetry'])_0x22a6cc=0x1;_0x5d3a7e({'additional':_0x22a6cc});}else{_0x5d3a7e({'additional':0x0});}})[_0xc74c('0x17')](function(_0x1dda8e){logger[_0xc74c('0x14')](_0xc74c('0x18'),_0x1dda8e['message']);_0x5e70a0(_0x494fef[_0xc74c('0x14')](0x1f4,_0x1dda8e[_0xc74c('0x15')]));});}})[_0xc74c('0x17')](function(_0x3ef860){logger[_0xc74c('0x14')](_0xc74c('0x18'),_0x3ef860[_0xc74c('0x15')]);_0x5e70a0(_0x494fef[_0xc74c('0x14')](0x1f4,_0x3ef860['message']));});}else _0x5d3a7e({'additional':0x0});});});};exports[_0xc74c('0x2b')]=function(_0x1df364){var _0xd20f61=this;return new Promise(function(_0x183a1d,_0x103bea){return db['CmHopper'][_0xc74c('0x1a')]({'raw':!![],'where':{'ContactId':_0x1df364[_0xc74c('0x1b')]['ContactId'],'ListId':_0x1df364[_0xc74c('0x1b')][_0xc74c('0x1d')],'VoiceQueueId':_0x1df364[_0xc74c('0x1b')][_0xc74c('0x1e')]===undefined?null:_0x1df364[_0xc74c('0x1b')]['VoiceQueueId'],'CampaignId':_0x1df364[_0xc74c('0x1b')][_0xc74c('0x1f')]===undefined?null:_0x1df364['body'][_0xc74c('0x1f')]}})['then'](function(_0x1a5a67){if(_0x1a5a67!=null){return db[_0xc74c('0x20')][_0xc74c('0x1a')]({'raw':!![],'where':{'CmHopperId':_0x1a5a67['id'],'OrderBy':{'$gte':_0x1a5a67[_0xc74c('0x21')]}},'order':[[_0xc74c('0x21'),_0xc74c('0x22')]]})[_0xc74c('0xc')](function(_0x3440a1){if(_0x3440a1)return activateAdditionalPhone(_0x1a5a67,_0x1df364['body'],_0x3440a1,_0x1df364['duration'],_0x1df364[_0xc74c('0x2c')])[_0xc74c('0xc')](function(_0x139b72){_0x183a1d(_0x139b72);});else{return db[_0xc74c('0x20')][_0xc74c('0x1a')]({'raw':!![],'where':{'CmHopperId':_0x1a5a67['id']},'order':[[_0xc74c('0x21'),_0xc74c('0x22')]]})[_0xc74c('0xc')](function(_0x31be7c){if(_0x31be7c)return activateAdditionalPhone(_0x1a5a67,_0x1df364[_0xc74c('0x1b')],_0x31be7c,_0x1df364[_0xc74c('0x2d')],_0x1df364[_0xc74c('0x2c')])[_0xc74c('0xc')](function(_0x4e402b){_0x183a1d(_0x4e402b);});else{_0x183a1d({'id':_0x1a5a67['id'],'phone':''});}})['catch'](function(_0x2c0723){logger[_0xc74c('0x14')](_0xc74c('0x18'),_0x2c0723[_0xc74c('0x15')]);_0x103bea(_0xd20f61[_0xc74c('0x14')](0x1f4,_0x2c0723[_0xc74c('0x15')]));});}})[_0xc74c('0x17')](function(_0x1648af){logger[_0xc74c('0x14')](_0xc74c('0x18'),_0x1648af[_0xc74c('0x15')]);_0x103bea(_0xd20f61['error'](0x1f4,_0x1648af['message']));});}else _0x183a1d(0x1);});});};function activateAdditionalPhone(_0xeb94c4,_0x4d73b9,_0x7fa2c5,_0x422691,_0x30078a){return new Promise(function(_0x182d23,_0x51018f){var _0x163472;if(_[_0xc74c('0x2e')](_0x7fa2c5[_0xc74c('0x2f')])){_0x7fa2c5[_0xc74c('0x2f')]=moment();}if(moment()>moment(_0x7fa2c5[_0xc74c('0x2f')])){_0x163472=moment()['add'](_0x30078a||0x3,_0xc74c('0x30'));}else{if(_0x7fa2c5[_0xc74c('0x21')]==0x0){_0x163472=moment(_0x7fa2c5[_0xc74c('0x2f')]);}else{_0x163472=moment()[_0xc74c('0x31')](_0x30078a||0x3,_0xc74c('0x30'));}}if(_[_0xc74c('0x2e')](_0x163472)){_0x163472=moment()['add'](_0x30078a||0x3,_0xc74c('0x30'));}var _0x43fd68={'phone':_0xeb94c4[_0xc74c('0x32')],'active':0x0,'OrderBy':_[_0xc74c('0x2e')](_0xeb94c4['OrderBy'])?0x0:_0xeb94c4[_0xc74c('0x21')],'scheduledat':moment()[_0xc74c('0x31')](_0x422691||0x1,'minutes'),'countbusyretry':_0x4d73b9[_0xc74c('0x23')],'countcongestionretry':_0x4d73b9[_0xc74c('0x24')],'countnoanswerretry':_0x4d73b9[_0xc74c('0x33')],'countnosuchnumberretry':_0x4d73b9[_0xc74c('0x25')],'countdropretry':_0x4d73b9[_0xc74c('0x26')],'countabandonedretry':_0x4d73b9[_0xc74c('0x27')],'countmachineretry':_0x4d73b9[_0xc74c('0x28')],'countagentrejectretry':_0x4d73b9[_0xc74c('0x29')],'createdAt':_0x4d73b9[_0xc74c('0x34')],'updatedAt':_0x4d73b9['updatedAt']};var _0x252883={'CmHopperId':_0xeb94c4['id'],'phone':_0x7fa2c5[_0xc74c('0x32')],'active':0x0,'OrderBy':_[_0xc74c('0x2e')](_0x7fa2c5[_0xc74c('0x21')])?0x0:_0x7fa2c5[_0xc74c('0x21')],'scheduledat':_0x163472,'countbusyretry':_0x7fa2c5[_0xc74c('0x23')],'countcongestionretry':_0x7fa2c5[_0xc74c('0x24')],'countnoanswerretry':_0x7fa2c5[_0xc74c('0x33')],'countnosuchnumberretry':_0x7fa2c5[_0xc74c('0x25')],'countdropretry':_0x7fa2c5[_0xc74c('0x26')],'countabandonedretry':_0x7fa2c5[_0xc74c('0x27')],'countmachineretry':_0x7fa2c5[_0xc74c('0x28')],'countagentrejectretry':_0x7fa2c5[_0xc74c('0x29')],'createdAt':_0x7fa2c5[_0xc74c('0x34')],'updatedAt':_0x7fa2c5[_0xc74c('0x35')]};return db['sequelize'][_0xc74c('0x36')](function(_0x5ef0bf){return db['CmHopperAdditionalPhone'][_0xc74c('0x37')](_0x43fd68,{'where':{'id':_0x7fa2c5['id']}},{'transaction':_0x5ef0bf})[_0xc74c('0xc')](function(){return db['CmHopper'][_0xc74c('0x37')](_0x252883,{'where':{'id':_0xeb94c4['id']}},{'transaction':_0x5ef0bf});});})[_0xc74c('0xc')](function(_0x550be2){_0x182d23({'id':_0xeb94c4['id'],'phone':_0x7fa2c5['phone']});})[_0xc74c('0x17')](function(_0x79400a){logger[_0xc74c('0x14')](_0xc74c('0x38'),_0x79400a[_0xc74c('0x15')]);_0x51018f({'error':_0x79400a[_0xc74c('0x15')]});});});}exports[_0xc74c('0x39')]=function(_0x3cb1bd){var _0x5ebdb6=this;return new Promise(function(_0x3f61cd,_0xe84949){return db[_0xc74c('0x19')][_0xc74c('0x37')](_0x3cb1bd['body']['field'],{'where':{'id':_0x3cb1bd[_0xc74c('0x1b')]['id']}})[_0xc74c('0xc')](function(_0x3c6511){_0x3f61cd(_0x3c6511);})['catch'](function(_0x4a9fcb){logger[_0xc74c('0x14')]('IncrementPreviewCounters',_0x4a9fcb[_0xc74c('0x15')]);_0xe84949(_0x5ebdb6[_0xc74c('0x14')](0x1f4,_0x4a9fcb[_0xc74c('0x15')]));});});};