9ea0df851b982c82a795bd3f2926485d1511ee60
[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 _0x62f9=['duration','waitTime','isNil','scheduledat','add','minutes','phone','updatedAt','createdAt','sequelize','transaction','update','activateAdditionalPhone','IncrementPreviewCounters','moment','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/environment','jayson/promise','client','http','request','then','info','CmHopperAdditionalPhone,\x20%s,\x20%s','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','VerifyAdditionalPhones','findOne','body','ListId','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','OrderBy','countbusyretry','countnoanswerretry','countnosuchnumberretry','countdropretry','countmachineretry','countagentrejectretry','entity','dialGlobalMaxRetry','ASC','countcongestionretry','countabandonedretry','CmHopper'];(function(_0x1dc4fd,_0x47c249){var _0x5631c1=function(_0x185da0){while(--_0x185da0){_0x1dc4fd['push'](_0x1dc4fd['shift']());}};_0x5631c1(++_0x47c249);}(_0x62f9,0x196));var _0x962f=function(_0x3ef45e,_0xe84f33){_0x3ef45e=_0x3ef45e-0x0;var _0x69e7c=_0x62f9[_0x3ef45e];return _0x69e7c;};'use strict';var _=require('lodash');var util=require('util');var moment=require(_0x962f('0x0'));var BPromise=require('bluebird');var rs=require(_0x962f('0x1'));var fs=require('fs');var Redis=require(_0x962f('0x2'));var db=require(_0x962f('0x3'))['db'];var utils=require(_0x962f('0x4'));var logger=require('../../config/logger')('rpc');var config=require(_0x962f('0x5'));var jayson=require(_0x962f('0x6'));var client=jayson[_0x962f('0x7')][_0x962f('0x8')]({'port':0x232a});function respondWithRpcPromise(_0x527511,_0x342958,_0x451259){return new BPromise(function(_0x461303,_0x26f8f6){return client[_0x962f('0x9')](_0x527511,_0x451259)[_0x962f('0xa')](function(_0x440db6){logger[_0x962f('0xb')](_0x962f('0xc'),_0x342958,_0x962f('0xd'));logger[_0x962f('0xe')](_0x962f('0xf'),_0x342958,'request\x20sent',JSON[_0x962f('0x10')](_0x440db6));if(_0x440db6[_0x962f('0x11')]){if(_0x440db6[_0x962f('0x11')][_0x962f('0x12')]===0x1f4){logger['error'](_0x962f('0xc'),_0x342958,_0x440db6[_0x962f('0x11')][_0x962f('0x13')]);return _0x26f8f6(_0x440db6[_0x962f('0x11')][_0x962f('0x13')]);}logger[_0x962f('0x11')](_0x962f('0xc'),_0x342958,_0x440db6[_0x962f('0x11')][_0x962f('0x13')]);return _0x461303(_0x440db6[_0x962f('0x11')][_0x962f('0x13')]);}else{logger[_0x962f('0xb')](_0x962f('0xc'),_0x342958,'request\x20sent');_0x461303(_0x440db6[_0x962f('0x14')][_0x962f('0x13')]);}})[_0x962f('0x15')](function(_0x2806bc){logger[_0x962f('0x11')](_0x962f('0xc'),_0x342958,_0x2806bc);_0x26f8f6(_0x2806bc);});});}exports[_0x962f('0x16')]=function(_0x5b5adb){var _0x278494=this;return new Promise(function(_0x1984d0,_0x244f25){return db['CmHopper'][_0x962f('0x17')]({'raw':!![],'where':{'ContactId':_0x5b5adb[_0x962f('0x18')]['ContactId'],'ListId':_0x5b5adb['body'][_0x962f('0x19')],'VoiceQueueId':_0x5b5adb[_0x962f('0x18')][_0x962f('0x1a')]===undefined?null:_0x5b5adb[_0x962f('0x18')][_0x962f('0x1a')],'CampaignId':_0x5b5adb[_0x962f('0x18')][_0x962f('0x1b')]===undefined?null:_0x5b5adb['body'][_0x962f('0x1b')]}})[_0x962f('0xa')](function(_0x1dd1ca){if(_0x1dd1ca!=null){return db[_0x962f('0x1c')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x1dd1ca['id'],'OrderBy':{'$gt':_0x1dd1ca[_0x962f('0x1d')]}},'order':[[_0x962f('0x1d'),'ASC']]})['then'](function(_0x71dea9){var _0x43e6f9=0x0;if(_0x71dea9){if(_0x71dea9[_0x962f('0x1e')]+_0x71dea9['countcongestionretry']+_0x71dea9[_0x962f('0x1f')]+_0x71dea9[_0x962f('0x20')]+_0x71dea9[_0x962f('0x21')]+_0x71dea9['countabandonedretry']+_0x71dea9[_0x962f('0x22')]+_0x71dea9[_0x962f('0x23')]<_0x5b5adb[_0x962f('0x24')][_0x962f('0x25')])_0x43e6f9=0x1;_0x1984d0({'additional':_0x43e6f9});}else{return db[_0x962f('0x1c')][_0x962f('0x17')]({'raw':!![],'where':{'CmHopperId':_0x1dd1ca['id']},'order':[['OrderBy',_0x962f('0x26')]]})['then'](function(_0x45c94d){var _0x435522=0x0;if(_0x45c94d){if(_0x45c94d[_0x962f('0x1e')]+_0x45c94d[_0x962f('0x27')]+_0x45c94d[_0x962f('0x1f')]+_0x45c94d[_0x962f('0x20')]+_0x45c94d['countdropretry']+_0x45c94d[_0x962f('0x28')]+_0x45c94d['countmachineretry']+_0x45c94d[_0x962f('0x23')]<_0x5b5adb[_0x962f('0x24')]['dialGlobalMaxRetry'])_0x435522=0x1;_0x1984d0({'additional':_0x435522});}else{_0x1984d0({'additional':0x0});}})['catch'](function(_0x438361){logger[_0x962f('0x11')](_0x962f('0x16'),_0x438361[_0x962f('0x13')]);_0x244f25(_0x278494['error'](0x1f4,_0x438361[_0x962f('0x13')]));});}})[_0x962f('0x15')](function(_0x42c5cf){logger[_0x962f('0x11')](_0x962f('0x16'),_0x42c5cf[_0x962f('0x13')]);_0x244f25(_0x278494[_0x962f('0x11')](0x1f4,_0x42c5cf[_0x962f('0x13')]));});}else _0x1984d0({'additional':0x0});});});};exports['SwapAdditionalPhones']=function(_0x387785){var _0x1782c5=this;return new Promise(function(_0x5ef603,_0x55eae5){return db[_0x962f('0x29')][_0x962f('0x17')]({'raw':!![],'where':{'ContactId':_0x387785[_0x962f('0x18')]['ContactId'],'ListId':_0x387785[_0x962f('0x18')][_0x962f('0x19')],'VoiceQueueId':_0x387785['body']['VoiceQueueId']===undefined?null:_0x387785['body'][_0x962f('0x1a')],'CampaignId':_0x387785['body']['CampaignId']===undefined?null:_0x387785[_0x962f('0x18')][_0x962f('0x1b')]}})[_0x962f('0xa')](function(_0x671cec){if(_0x671cec!=null){return db[_0x962f('0x1c')][_0x962f('0x17')]({'raw':!![],'where':{'CmHopperId':_0x671cec['id'],'OrderBy':{'$gte':_0x671cec[_0x962f('0x1d')]}},'order':[[_0x962f('0x1d'),'ASC']]})[_0x962f('0xa')](function(_0x57e72f){if(_0x57e72f)return activateAdditionalPhone(_0x671cec,_0x387785['body'],_0x57e72f,_0x387785[_0x962f('0x2a')],_0x387785[_0x962f('0x2b')])['then'](function(_0x2e5a41){_0x5ef603(_0x2e5a41);});else{return db[_0x962f('0x1c')][_0x962f('0x17')]({'raw':!![],'where':{'CmHopperId':_0x671cec['id']},'order':[[_0x962f('0x1d'),_0x962f('0x26')]]})[_0x962f('0xa')](function(_0x487667){if(_0x487667)return activateAdditionalPhone(_0x671cec,_0x387785[_0x962f('0x18')],_0x487667,_0x387785[_0x962f('0x2a')],_0x387785[_0x962f('0x2b')])[_0x962f('0xa')](function(_0x32ba68){_0x5ef603(_0x32ba68);});else{_0x5ef603({'id':_0x671cec['id'],'phone':''});}})['catch'](function(_0x5ab866){logger['error'](_0x962f('0x16'),_0x5ab866[_0x962f('0x13')]);_0x55eae5(_0x1782c5['error'](0x1f4,_0x5ab866[_0x962f('0x13')]));});}})[_0x962f('0x15')](function(_0x4d3ba3){logger[_0x962f('0x11')](_0x962f('0x16'),_0x4d3ba3['message']);_0x55eae5(_0x1782c5['error'](0x1f4,_0x4d3ba3[_0x962f('0x13')]));});}else _0x5ef603(0x1);});});};function activateAdditionalPhone(_0x5c6900,_0x513938,_0x5576b0,_0x1050f4,_0x410274){return new Promise(function(_0x41879b,_0x1aa883){var _0xf050f9;if(_[_0x962f('0x2c')](_0x5576b0[_0x962f('0x2d')])){_0x5576b0[_0x962f('0x2d')]=moment();}if(moment()>moment(_0x5576b0[_0x962f('0x2d')])){_0xf050f9=moment()[_0x962f('0x2e')](_0x410274||0x3,_0x962f('0x2f'));}else{if(_0x5576b0['OrderBy']==0x0){_0xf050f9=moment(_0x5576b0[_0x962f('0x2d')]);}else{_0xf050f9=moment()[_0x962f('0x2e')](_0x410274||0x3,_0x962f('0x2f'));}}if(_[_0x962f('0x2c')](_0xf050f9)){_0xf050f9=moment()[_0x962f('0x2e')](_0x410274||0x3,_0x962f('0x2f'));}var _0x1fc32b={'phone':_0x5c6900[_0x962f('0x30')],'active':0x0,'OrderBy':_[_0x962f('0x2c')](_0x5c6900[_0x962f('0x1d')])?0x0:_0x5c6900[_0x962f('0x1d')],'scheduledat':moment()[_0x962f('0x2e')](_0x1050f4||0x1,_0x962f('0x2f')),'countbusyretry':_0x513938[_0x962f('0x1e')],'countcongestionretry':_0x513938['countcongestionretry'],'countnoanswerretry':_0x513938[_0x962f('0x1f')],'countnosuchnumberretry':_0x513938[_0x962f('0x20')],'countdropretry':_0x513938[_0x962f('0x21')],'countabandonedretry':_0x513938[_0x962f('0x28')],'countmachineretry':_0x513938['countmachineretry'],'countagentrejectretry':_0x513938[_0x962f('0x23')],'createdAt':_0x513938['createdAt'],'updatedAt':_0x513938[_0x962f('0x31')]};var _0x4c0a0d={'CmHopperId':_0x5c6900['id'],'phone':_0x5576b0[_0x962f('0x30')],'active':0x0,'OrderBy':_[_0x962f('0x2c')](_0x5576b0[_0x962f('0x1d')])?0x0:_0x5576b0[_0x962f('0x1d')],'scheduledat':_0xf050f9,'countbusyretry':_0x5576b0[_0x962f('0x1e')],'countcongestionretry':_0x5576b0[_0x962f('0x27')],'countnoanswerretry':_0x5576b0[_0x962f('0x1f')],'countnosuchnumberretry':_0x5576b0[_0x962f('0x20')],'countdropretry':_0x5576b0['countdropretry'],'countabandonedretry':_0x5576b0[_0x962f('0x28')],'countmachineretry':_0x5576b0[_0x962f('0x22')],'countagentrejectretry':_0x5576b0['countagentrejectretry'],'createdAt':_0x5576b0[_0x962f('0x32')],'updatedAt':_0x5576b0[_0x962f('0x31')]};return db[_0x962f('0x33')][_0x962f('0x34')](function(_0xe211f6){return db['CmHopperAdditionalPhone'][_0x962f('0x35')](_0x1fc32b,{'where':{'id':_0x5576b0['id']}},{'transaction':_0xe211f6})[_0x962f('0xa')](function(){return db['CmHopper'][_0x962f('0x35')](_0x4c0a0d,{'where':{'id':_0x5c6900['id']}},{'transaction':_0xe211f6});});})[_0x962f('0xa')](function(_0x2bce60){_0x41879b({'id':_0x5c6900['id'],'phone':_0x5576b0[_0x962f('0x30')]});})['catch'](function(_0x398653){logger[_0x962f('0x11')](_0x962f('0x36'),_0x398653['message']);_0x1aa883({'error':_0x398653[_0x962f('0x13')]});});});}exports[_0x962f('0x37')]=function(_0x22b663){var _0x4f3a62=this;return new Promise(function(_0x3ce8ad,_0x192f38){return db[_0x962f('0x29')]['update'](_0x22b663[_0x962f('0x18')]['field'],{'where':{'id':_0x22b663[_0x962f('0x18')]['id']}})[_0x962f('0xa')](function(_0xd8126d){_0x3ce8ad(_0xd8126d);})['catch'](function(_0x49dde7){logger[_0x962f('0x11')]('IncrementPreviewCounters',_0x49dde7['message']);_0x192f38(_0x4f3a62['error'](0x1f4,_0x49dde7['message']));});});};