86303c888988dbe74848c9be57e71ac4d64ead1a
[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 _0xdcda=['countnosuchnumberretry','countdropretry','countabandonedretry','countmachineretry','entity','dialGlobalMaxRetry','CmHopperAdditionalPhone','countcongestionretry','countagentrejectretry','VoiceQueueId','waitTime','duration','isNil','scheduledat','add','minutes','phone','countnoanswerretry','updatedAt','createdAt','sequelize','transaction','CmHopper','update','activateAdditionalPhone','lodash','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','request','then','request\x20sent','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','stringify','error','code','CmHopperAdditionalPhone,\x20%s,\x20%s','message','result','catch','VerifyAdditionalPhones','findOne','body','ContactId','CampaignId','OrderBy','ASC','countbusyretry'];(function(_0xb72818,_0xd5575a){var _0x37dc47=function(_0x4c9faf){while(--_0x4c9faf){_0xb72818['push'](_0xb72818['shift']());}};_0x37dc47(++_0xd5575a);}(_0xdcda,0x101));var _0xadcd=function(_0x8c43de,_0x90ff31){_0x8c43de=_0x8c43de-0x0;var _0xa6591b=_0xdcda[_0x8c43de];return _0xa6591b;};'use strict';var _=require(_0xadcd('0x0'));var util=require(_0xadcd('0x1'));var moment=require(_0xadcd('0x2'));var BPromise=require(_0xadcd('0x3'));var rs=require(_0xadcd('0x4'));var fs=require('fs');var Redis=require(_0xadcd('0x5'));var db=require(_0xadcd('0x6'))['db'];var utils=require(_0xadcd('0x7'));var logger=require(_0xadcd('0x8'))(_0xadcd('0x9'));var config=require(_0xadcd('0xa'));var jayson=require(_0xadcd('0xb'));var client=jayson[_0xadcd('0xc')][_0xadcd('0xd')]({'port':0x232a});function respondWithRpcPromise(_0x525c1d,_0x1104ce,_0x415bb6){return new BPromise(function(_0x22647b,_0x229e9a){return client[_0xadcd('0xe')](_0x525c1d,_0x415bb6)[_0xadcd('0xf')](function(_0x45c8c4){logger['info']('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x1104ce,_0xadcd('0x10'));logger['debug'](_0xadcd('0x11'),_0x1104ce,'request\x20sent',JSON[_0xadcd('0x12')](_0x45c8c4));if(_0x45c8c4['error']){if(_0x45c8c4[_0xadcd('0x13')][_0xadcd('0x14')]===0x1f4){logger[_0xadcd('0x13')](_0xadcd('0x15'),_0x1104ce,_0x45c8c4[_0xadcd('0x13')][_0xadcd('0x16')]);return _0x229e9a(_0x45c8c4[_0xadcd('0x13')][_0xadcd('0x16')]);}logger[_0xadcd('0x13')](_0xadcd('0x15'),_0x1104ce,_0x45c8c4['error']['message']);return _0x22647b(_0x45c8c4[_0xadcd('0x13')][_0xadcd('0x16')]);}else{logger['info'](_0xadcd('0x15'),_0x1104ce,_0xadcd('0x10'));_0x22647b(_0x45c8c4[_0xadcd('0x17')]['message']);}})[_0xadcd('0x18')](function(_0x6aeba0){logger['error']('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x1104ce,_0x6aeba0);_0x229e9a(_0x6aeba0);});});}exports[_0xadcd('0x19')]=function(_0x28c355){var _0x2b0e6d=this;return new Promise(function(_0x2a3dda,_0x1e9e06){return db['CmHopper'][_0xadcd('0x1a')]({'raw':!![],'where':{'ContactId':_0x28c355[_0xadcd('0x1b')][_0xadcd('0x1c')],'ListId':_0x28c355['body']['ListId'],'VoiceQueueId':_0x28c355['body']['VoiceQueueId']===undefined?null:_0x28c355[_0xadcd('0x1b')]['VoiceQueueId'],'CampaignId':_0x28c355['body'][_0xadcd('0x1d')]===undefined?null:_0x28c355[_0xadcd('0x1b')][_0xadcd('0x1d')]}})[_0xadcd('0xf')](function(_0x5c9775){if(_0x5c9775!=null){return db['CmHopperAdditionalPhone'][_0xadcd('0x1a')]({'raw':!![],'where':{'CmHopperId':_0x5c9775['id'],'OrderBy':{'$gt':_0x5c9775[_0xadcd('0x1e')]}},'order':[[_0xadcd('0x1e'),_0xadcd('0x1f')]]})[_0xadcd('0xf')](function(_0x459334){var _0x55e7df=0x0;if(_0x459334){if(_0x459334[_0xadcd('0x20')]+_0x459334['countcongestionretry']+_0x459334['countnoanswerretry']+_0x459334[_0xadcd('0x21')]+_0x459334[_0xadcd('0x22')]+_0x459334[_0xadcd('0x23')]+_0x459334[_0xadcd('0x24')]+_0x459334['countagentrejectretry']<_0x28c355[_0xadcd('0x25')][_0xadcd('0x26')])_0x55e7df=0x1;_0x2a3dda({'additional':_0x55e7df});}else{return db[_0xadcd('0x27')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x5c9775['id']},'order':[[_0xadcd('0x1e'),_0xadcd('0x1f')]]})['then'](function(_0x54b9ee){var _0x14042a=0x0;if(_0x54b9ee){if(_0x54b9ee[_0xadcd('0x20')]+_0x54b9ee[_0xadcd('0x28')]+_0x54b9ee['countnoanswerretry']+_0x54b9ee[_0xadcd('0x21')]+_0x54b9ee[_0xadcd('0x22')]+_0x54b9ee[_0xadcd('0x23')]+_0x54b9ee[_0xadcd('0x24')]+_0x54b9ee[_0xadcd('0x29')]<_0x28c355[_0xadcd('0x25')][_0xadcd('0x26')])_0x14042a=0x1;_0x2a3dda({'additional':_0x14042a});}else{_0x2a3dda({'additional':0x0});}})['catch'](function(_0x3e05b0){logger[_0xadcd('0x13')](_0xadcd('0x19'),_0x3e05b0['message']);_0x1e9e06(_0x2b0e6d[_0xadcd('0x13')](0x1f4,_0x3e05b0[_0xadcd('0x16')]));});}})[_0xadcd('0x18')](function(_0x1ab058){logger[_0xadcd('0x13')](_0xadcd('0x19'),_0x1ab058[_0xadcd('0x16')]);_0x1e9e06(_0x2b0e6d['error'](0x1f4,_0x1ab058['message']));});}else _0x2a3dda({'additional':0x0});});});};exports['SwapAdditionalPhones']=function(_0x458fc9){var _0x1d8591=this;return new Promise(function(_0x4176e9,_0x6ca8d8){return db['CmHopper']['findOne']({'raw':!![],'where':{'ContactId':_0x458fc9[_0xadcd('0x1b')][_0xadcd('0x1c')],'ListId':_0x458fc9[_0xadcd('0x1b')]['ListId'],'VoiceQueueId':_0x458fc9[_0xadcd('0x1b')][_0xadcd('0x2a')]===undefined?null:_0x458fc9['body'][_0xadcd('0x2a')],'CampaignId':_0x458fc9[_0xadcd('0x1b')][_0xadcd('0x1d')]===undefined?null:_0x458fc9[_0xadcd('0x1b')][_0xadcd('0x1d')]}})[_0xadcd('0xf')](function(_0x332787){if(_0x332787!=null){return db[_0xadcd('0x27')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x332787['id'],'OrderBy':{'$gte':_0x332787['OrderBy']}},'order':[[_0xadcd('0x1e'),_0xadcd('0x1f')]]})[_0xadcd('0xf')](function(_0x475f6e){if(_0x475f6e)return activateAdditionalPhone(_0x332787,_0x458fc9[_0xadcd('0x1b')],_0x475f6e,_0x458fc9['duration'],_0x458fc9[_0xadcd('0x2b')])[_0xadcd('0xf')](function(_0x47f33f){_0x4176e9(_0x47f33f);});else{return db[_0xadcd('0x27')][_0xadcd('0x1a')]({'raw':!![],'where':{'CmHopperId':_0x332787['id']},'order':[['OrderBy',_0xadcd('0x1f')]]})[_0xadcd('0xf')](function(_0x570ff2){if(_0x570ff2)return activateAdditionalPhone(_0x332787,_0x458fc9[_0xadcd('0x1b')],_0x570ff2,_0x458fc9[_0xadcd('0x2c')],_0x458fc9[_0xadcd('0x2b')])[_0xadcd('0xf')](function(_0x3e1bf8){_0x4176e9(_0x3e1bf8);});else{_0x4176e9({'id':_0x332787['id'],'phone':''});}})[_0xadcd('0x18')](function(_0x3c5ead){logger[_0xadcd('0x13')]('VerifyAdditionalPhones',_0x3c5ead['message']);_0x6ca8d8(_0x1d8591[_0xadcd('0x13')](0x1f4,_0x3c5ead[_0xadcd('0x16')]));});}})[_0xadcd('0x18')](function(_0x15ce75){logger['error'](_0xadcd('0x19'),_0x15ce75[_0xadcd('0x16')]);_0x6ca8d8(_0x1d8591[_0xadcd('0x13')](0x1f4,_0x15ce75[_0xadcd('0x16')]));});}else _0x4176e9(0x1);});});};function activateAdditionalPhone(_0x1ae04c,_0x33866a,_0x3d5c64,_0x1d05bc,_0xa69556){return new Promise(function(_0x3638f2,_0x1e7934){var _0xe9a630;if(_[_0xadcd('0x2d')](_0x3d5c64[_0xadcd('0x2e')])){_0x3d5c64[_0xadcd('0x2e')]=moment();}if(moment()>moment(_0x3d5c64['scheduledat'])){_0xe9a630=moment()[_0xadcd('0x2f')](_0xa69556||0x3,_0xadcd('0x30'));}else{if(_0x3d5c64[_0xadcd('0x1e')]==0x0){_0xe9a630=moment(_0x3d5c64['scheduledat']);}else{_0xe9a630=moment()[_0xadcd('0x2f')](_0xa69556||0x3,_0xadcd('0x30'));}}if(_[_0xadcd('0x2d')](_0xe9a630)){_0xe9a630=moment()[_0xadcd('0x2f')](_0xa69556||0x3,_0xadcd('0x30'));}var _0x121da8={'phone':_0x1ae04c[_0xadcd('0x31')],'active':0x0,'OrderBy':_[_0xadcd('0x2d')](_0x1ae04c[_0xadcd('0x1e')])?0x0:_0x1ae04c[_0xadcd('0x1e')],'scheduledat':moment()['add'](_0x1d05bc||0x1,_0xadcd('0x30')),'countbusyretry':_0x33866a[_0xadcd('0x20')],'countcongestionretry':_0x33866a[_0xadcd('0x28')],'countnoanswerretry':_0x33866a[_0xadcd('0x32')],'countnosuchnumberretry':_0x33866a[_0xadcd('0x21')],'countdropretry':_0x33866a[_0xadcd('0x22')],'countabandonedretry':_0x33866a[_0xadcd('0x23')],'countmachineretry':_0x33866a['countmachineretry'],'countagentrejectretry':_0x33866a[_0xadcd('0x29')],'createdAt':_0x33866a['createdAt'],'updatedAt':_0x33866a[_0xadcd('0x33')]};var _0x3566a2={'CmHopperId':_0x1ae04c['id'],'phone':_0x3d5c64[_0xadcd('0x31')],'active':0x0,'OrderBy':_[_0xadcd('0x2d')](_0x3d5c64[_0xadcd('0x1e')])?0x0:_0x3d5c64[_0xadcd('0x1e')],'scheduledat':_0xe9a630,'countbusyretry':_0x3d5c64[_0xadcd('0x20')],'countcongestionretry':_0x3d5c64[_0xadcd('0x28')],'countnoanswerretry':_0x3d5c64[_0xadcd('0x32')],'countnosuchnumberretry':_0x3d5c64[_0xadcd('0x21')],'countdropretry':_0x3d5c64[_0xadcd('0x22')],'countabandonedretry':_0x3d5c64[_0xadcd('0x23')],'countmachineretry':_0x3d5c64[_0xadcd('0x24')],'countagentrejectretry':_0x3d5c64[_0xadcd('0x29')],'createdAt':_0x3d5c64[_0xadcd('0x34')],'updatedAt':_0x3d5c64[_0xadcd('0x33')]};return db[_0xadcd('0x35')][_0xadcd('0x36')](function(_0x371396){return db['CmHopperAdditionalPhone']['update'](_0x121da8,{'where':{'id':_0x3d5c64['id']}},{'transaction':_0x371396})[_0xadcd('0xf')](function(){return db[_0xadcd('0x37')][_0xadcd('0x38')](_0x3566a2,{'where':{'id':_0x1ae04c['id']}},{'transaction':_0x371396});});})[_0xadcd('0xf')](function(_0x374ce8){_0x3638f2({'id':_0x1ae04c['id'],'phone':_0x3d5c64[_0xadcd('0x31')]});})['catch'](function(_0xd70c4a){logger[_0xadcd('0x13')](_0xadcd('0x39'),_0xd70c4a[_0xadcd('0x16')]);_0x1e7934({'error':_0xd70c4a['message']});});});}exports['IncrementPreviewCounters']=function(_0x54a27f){var _0x417c55=this;return new Promise(function(_0x1efb8f,_0x41d09a){return db[_0xadcd('0x37')]['update'](_0x54a27f[_0xadcd('0x1b')]['field'],{'where':{'id':_0x54a27f['body']['id']}})[_0xadcd('0xf')](function(_0x45e8ce){_0x1efb8f(_0x45e8ce);})[_0xadcd('0x18')](function(_0x563acb){logger[_0xadcd('0x13')]('IncrementPreviewCounters',_0x563acb[_0xadcd('0x16')]);_0x41d09a(_0x417c55[_0xadcd('0x13')](0x1f4,_0x563acb[_0xadcd('0x16')]));});});};