Built motion from commit 616d3211.|2.6.4
[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 _0xf404=['countnoanswerretry','countagentrejectretry','entity','dialGlobalMaxRetry','VerifyAdditionalPhones','CmHopper','ContactId','duration','waitTime','isNil','scheduledat','add','minutes','createdAt','updatedAt','sequelize','update','phone','IncrementPreviewCounters','field','moment','../../mysqldb','../../config/logger','jayson/promise','client','http','info','CmHopperAdditionalPhone,\x20%s,\x20%s','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','findOne','body','ListId','VoiceQueueId','CampaignId','then','CmHopperAdditionalPhone','OrderBy','ASC','countbusyretry','countcongestionretry','countnosuchnumberretry','countdropretry','countabandonedretry','countmachineretry'];(function(_0x3bf83f,_0x181cac){var _0x22c4a9=function(_0xb3ff0d){while(--_0xb3ff0d){_0x3bf83f['push'](_0x3bf83f['shift']());}};_0x22c4a9(++_0x181cac);}(_0xf404,0xe4));var _0x4f40=function(_0x19a475,_0x359d03){_0x19a475=_0x19a475-0x0;var _0x5e34b7=_0xf404[_0x19a475];return _0x5e34b7;};'use strict';var _=require('lodash');var util=require('util');var moment=require(_0x4f40('0x0'));var BPromise=require('bluebird');var rs=require('randomstring');var fs=require('fs');var Redis=require('ioredis');var db=require(_0x4f40('0x1'))['db'];var utils=require('../../config/utils');var logger=require(_0x4f40('0x2'))('rpc');var config=require('../../config/environment');var jayson=require(_0x4f40('0x3'));var client=jayson[_0x4f40('0x4')][_0x4f40('0x5')]({'port':0x232a});function respondWithRpcPromise(_0x4b762f,_0x3958d7,_0x12e64d){return new BPromise(function(_0x2ab8cc,_0x2c5d9f){return client['request'](_0x4b762f,_0x12e64d)['then'](function(_0x3751fe){logger[_0x4f40('0x6')](_0x4f40('0x7'),_0x3958d7,'request\x20sent');logger[_0x4f40('0x8')](_0x4f40('0x9'),_0x3958d7,_0x4f40('0xa'),JSON[_0x4f40('0xb')](_0x3751fe));if(_0x3751fe[_0x4f40('0xc')]){if(_0x3751fe['error'][_0x4f40('0xd')]===0x1f4){logger[_0x4f40('0xc')](_0x4f40('0x7'),_0x3958d7,_0x3751fe[_0x4f40('0xc')][_0x4f40('0xe')]);return _0x2c5d9f(_0x3751fe['error'][_0x4f40('0xe')]);}logger[_0x4f40('0xc')](_0x4f40('0x7'),_0x3958d7,_0x3751fe[_0x4f40('0xc')][_0x4f40('0xe')]);return _0x2ab8cc(_0x3751fe['error'][_0x4f40('0xe')]);}else{logger[_0x4f40('0x6')](_0x4f40('0x7'),_0x3958d7,_0x4f40('0xa'));_0x2ab8cc(_0x3751fe[_0x4f40('0xf')][_0x4f40('0xe')]);}})[_0x4f40('0x10')](function(_0x33930c){logger[_0x4f40('0xc')](_0x4f40('0x7'),_0x3958d7,_0x33930c);_0x2c5d9f(_0x33930c);});});}exports['VerifyAdditionalPhones']=function(_0x689ee9){var _0x1e43f5=this;return new Promise(function(_0x1a0b9a,_0x331839){return db['CmHopper'][_0x4f40('0x11')]({'raw':!![],'where':{'ContactId':_0x689ee9[_0x4f40('0x12')]['ContactId'],'ListId':_0x689ee9[_0x4f40('0x12')][_0x4f40('0x13')],'VoiceQueueId':_0x689ee9[_0x4f40('0x12')][_0x4f40('0x14')]===undefined?null:_0x689ee9[_0x4f40('0x12')]['VoiceQueueId'],'CampaignId':_0x689ee9[_0x4f40('0x12')][_0x4f40('0x15')]===undefined?null:_0x689ee9['body'][_0x4f40('0x15')]}})[_0x4f40('0x16')](function(_0x4e44af){if(_0x4e44af!=null){return db[_0x4f40('0x17')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x4e44af['id'],'OrderBy':{'$gt':_0x4e44af[_0x4f40('0x18')]}},'order':[[_0x4f40('0x18'),_0x4f40('0x19')]]})['then'](function(_0x4f0eb1){var _0x1fd64b=0x0;if(_0x4f0eb1){if(_0x4f0eb1[_0x4f40('0x1a')]+_0x4f0eb1[_0x4f40('0x1b')]+_0x4f0eb1['countnoanswerretry']+_0x4f0eb1[_0x4f40('0x1c')]+_0x4f0eb1[_0x4f40('0x1d')]+_0x4f0eb1[_0x4f40('0x1e')]+_0x4f0eb1[_0x4f40('0x1f')]+_0x4f0eb1['countagentrejectretry']<_0x689ee9['entity']['dialGlobalMaxRetry'])_0x1fd64b=0x1;_0x1a0b9a({'additional':_0x1fd64b});}else{return db['CmHopperAdditionalPhone'][_0x4f40('0x11')]({'raw':!![],'where':{'CmHopperId':_0x4e44af['id']},'order':[[_0x4f40('0x18'),_0x4f40('0x19')]]})[_0x4f40('0x16')](function(_0x2b8be7){var _0x1d5352=0x0;if(_0x2b8be7){if(_0x2b8be7['countbusyretry']+_0x2b8be7['countcongestionretry']+_0x2b8be7[_0x4f40('0x20')]+_0x2b8be7['countnosuchnumberretry']+_0x2b8be7[_0x4f40('0x1d')]+_0x2b8be7[_0x4f40('0x1e')]+_0x2b8be7[_0x4f40('0x1f')]+_0x2b8be7[_0x4f40('0x21')]<_0x689ee9[_0x4f40('0x22')][_0x4f40('0x23')])_0x1d5352=0x1;_0x1a0b9a({'additional':_0x1d5352});}else{_0x1a0b9a({'additional':0x0});}})[_0x4f40('0x10')](function(_0x18c5d2){logger[_0x4f40('0xc')](_0x4f40('0x24'),_0x18c5d2['message']);_0x331839(_0x1e43f5['error'](0x1f4,_0x18c5d2[_0x4f40('0xe')]));});}})['catch'](function(_0x31307e){logger['error']('VerifyAdditionalPhones',_0x31307e[_0x4f40('0xe')]);_0x331839(_0x1e43f5[_0x4f40('0xc')](0x1f4,_0x31307e[_0x4f40('0xe')]));});}else _0x1a0b9a({'additional':0x0});});});};exports['SwapAdditionalPhones']=function(_0x5a258c){var _0x31478a=this;return new Promise(function(_0x487739,_0x240de4){return db[_0x4f40('0x25')][_0x4f40('0x11')]({'raw':!![],'where':{'ContactId':_0x5a258c[_0x4f40('0x12')][_0x4f40('0x26')],'ListId':_0x5a258c[_0x4f40('0x12')][_0x4f40('0x13')],'VoiceQueueId':_0x5a258c['body'][_0x4f40('0x14')]===undefined?null:_0x5a258c[_0x4f40('0x12')][_0x4f40('0x14')],'CampaignId':_0x5a258c[_0x4f40('0x12')][_0x4f40('0x15')]===undefined?null:_0x5a258c[_0x4f40('0x12')][_0x4f40('0x15')]}})[_0x4f40('0x16')](function(_0x372a8f){if(_0x372a8f!=null){return db[_0x4f40('0x17')][_0x4f40('0x11')]({'raw':!![],'where':{'CmHopperId':_0x372a8f['id'],'OrderBy':{'$gte':_0x372a8f[_0x4f40('0x18')]}},'order':[[_0x4f40('0x18'),_0x4f40('0x19')]]})[_0x4f40('0x16')](function(_0x56df71){if(_0x56df71)return activateAdditionalPhone(_0x372a8f,_0x5a258c[_0x4f40('0x12')],_0x56df71,_0x5a258c[_0x4f40('0x27')],_0x5a258c[_0x4f40('0x28')])['then'](function(_0x214665){_0x487739(_0x214665);});else{return db['CmHopperAdditionalPhone'][_0x4f40('0x11')]({'raw':!![],'where':{'CmHopperId':_0x372a8f['id']},'order':[[_0x4f40('0x18'),_0x4f40('0x19')]]})[_0x4f40('0x16')](function(_0x46366b){if(_0x46366b)return activateAdditionalPhone(_0x372a8f,_0x5a258c[_0x4f40('0x12')],_0x46366b,_0x5a258c[_0x4f40('0x27')],_0x5a258c[_0x4f40('0x28')])[_0x4f40('0x16')](function(_0x27a148){_0x487739(_0x27a148);});else{_0x487739({'id':_0x372a8f['id'],'phone':''});}})[_0x4f40('0x10')](function(_0x16bc68){logger[_0x4f40('0xc')](_0x4f40('0x24'),_0x16bc68[_0x4f40('0xe')]);_0x240de4(_0x31478a[_0x4f40('0xc')](0x1f4,_0x16bc68[_0x4f40('0xe')]));});}})[_0x4f40('0x10')](function(_0x132d70){logger[_0x4f40('0xc')](_0x4f40('0x24'),_0x132d70[_0x4f40('0xe')]);_0x240de4(_0x31478a[_0x4f40('0xc')](0x1f4,_0x132d70[_0x4f40('0xe')]));});}else _0x487739(0x1);});});};function activateAdditionalPhone(_0x11e509,_0x2b5f06,_0x4468d2,_0x56ccb4,_0x47ce1a){return new Promise(function(_0x339dec,_0x561f2c){var _0x463f84;if(_[_0x4f40('0x29')](_0x4468d2[_0x4f40('0x2a')])){_0x4468d2[_0x4f40('0x2a')]=moment();}if(moment()>moment(_0x4468d2['scheduledat'])){_0x463f84=moment()[_0x4f40('0x2b')](_0x47ce1a||0x3,_0x4f40('0x2c'));}else{if(_0x4468d2[_0x4f40('0x18')]==0x0){_0x463f84=moment(_0x4468d2[_0x4f40('0x2a')]);}else{_0x463f84=moment()[_0x4f40('0x2b')](_0x47ce1a||0x3,_0x4f40('0x2c'));}}if(_[_0x4f40('0x29')](_0x463f84)){_0x463f84=moment()[_0x4f40('0x2b')](_0x47ce1a||0x3,_0x4f40('0x2c'));}var _0x1facf0={'phone':_0x11e509['phone'],'active':0x0,'OrderBy':_[_0x4f40('0x29')](_0x11e509['OrderBy'])?0x0:_0x11e509['OrderBy'],'scheduledat':moment()[_0x4f40('0x2b')](_0x56ccb4||0x1,_0x4f40('0x2c')),'countbusyretry':_0x2b5f06[_0x4f40('0x1a')],'countcongestionretry':_0x2b5f06[_0x4f40('0x1b')],'countnoanswerretry':_0x2b5f06[_0x4f40('0x20')],'countnosuchnumberretry':_0x2b5f06[_0x4f40('0x1c')],'countdropretry':_0x2b5f06[_0x4f40('0x1d')],'countabandonedretry':_0x2b5f06[_0x4f40('0x1e')],'countmachineretry':_0x2b5f06[_0x4f40('0x1f')],'countagentrejectretry':_0x2b5f06['countagentrejectretry'],'createdAt':_0x2b5f06[_0x4f40('0x2d')],'updatedAt':_0x2b5f06[_0x4f40('0x2e')]};var _0x19cc4d={'CmHopperId':_0x11e509['id'],'phone':_0x4468d2['phone'],'active':0x0,'OrderBy':_[_0x4f40('0x29')](_0x4468d2['OrderBy'])?0x0:_0x4468d2[_0x4f40('0x18')],'scheduledat':_0x463f84,'countbusyretry':_0x4468d2[_0x4f40('0x1a')],'countcongestionretry':_0x4468d2[_0x4f40('0x1b')],'countnoanswerretry':_0x4468d2[_0x4f40('0x20')],'countnosuchnumberretry':_0x4468d2['countnosuchnumberretry'],'countdropretry':_0x4468d2['countdropretry'],'countabandonedretry':_0x4468d2[_0x4f40('0x1e')],'countmachineretry':_0x4468d2[_0x4f40('0x1f')],'countagentrejectretry':_0x4468d2[_0x4f40('0x21')],'createdAt':_0x4468d2[_0x4f40('0x2d')],'updatedAt':_0x4468d2[_0x4f40('0x2e')]};return db[_0x4f40('0x2f')]['transaction'](function(_0x3f326e){return db[_0x4f40('0x17')][_0x4f40('0x30')](_0x1facf0,{'where':{'id':_0x4468d2['id']}},{'transaction':_0x3f326e})[_0x4f40('0x16')](function(){return db['CmHopper'][_0x4f40('0x30')](_0x19cc4d,{'where':{'id':_0x11e509['id']}},{'transaction':_0x3f326e});});})[_0x4f40('0x16')](function(_0x322b43){_0x339dec({'id':_0x11e509['id'],'phone':_0x4468d2[_0x4f40('0x31')]});})['catch'](function(_0x1377da){logger['error']('activateAdditionalPhone',_0x1377da[_0x4f40('0xe')]);_0x561f2c({'error':_0x1377da[_0x4f40('0xe')]});});});}exports[_0x4f40('0x32')]=function(_0x5e36c4){var _0x330d2c=this;return new Promise(function(_0xbb2ea3,_0x2d704e){return db[_0x4f40('0x25')][_0x4f40('0x30')](_0x5e36c4[_0x4f40('0x12')][_0x4f40('0x33')],{'where':{'id':_0x5e36c4[_0x4f40('0x12')]['id']}})[_0x4f40('0x16')](function(_0xe2ae50){_0xbb2ea3(_0xe2ae50);})[_0x4f40('0x10')](function(_0x346461){logger[_0x4f40('0xc')](_0x4f40('0x32'),_0x346461[_0x4f40('0xe')]);_0x2d704e(_0x330d2c['error'](0x1f4,_0x346461[_0x4f40('0xe')]));});});};