d703ecadad644b8ac5a8864727ef9e30c28caa01
[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 _0x13cb=['duration','scheduledat','minutes','add','isNil','phone','updatedAt','createdAt','transaction','update','activateAdditionalPhone','IncrementPreviewCounters','field','lodash','util','moment','bluebird','randomstring','../../mysqldb','../../config/logger','rpc','../../config/environment','client','request','info','CmHopperAdditionalPhone,\x20%s,\x20%s','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','error','code','message','result','VerifyAdditionalPhones','CmHopper','findOne','body','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','OrderBy','ASC','then','countnoanswerretry','countnosuchnumberretry','countdropretry','countmachineretry','countagentrejectretry','entity','dialGlobalMaxRetry','countbusyretry','countcongestionretry','countabandonedretry','catch','SwapAdditionalPhones','ListId','waitTime'];(function(_0x4faa24,_0x4a2cf7){var _0xa991a5=function(_0x38802e){while(--_0x38802e){_0x4faa24['push'](_0x4faa24['shift']());}};_0xa991a5(++_0x4a2cf7);}(_0x13cb,0x19c));var _0xb13c=function(_0x7ee493,_0x2cc2cb){_0x7ee493=_0x7ee493-0x0;var _0x1f2c25=_0x13cb[_0x7ee493];return _0x1f2c25;};'use strict';var _=require(_0xb13c('0x0'));var util=require(_0xb13c('0x1'));var moment=require(_0xb13c('0x2'));var BPromise=require(_0xb13c('0x3'));var rs=require(_0xb13c('0x4'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0xb13c('0x5'))['db'];var utils=require('../../config/utils');var logger=require(_0xb13c('0x6'))(_0xb13c('0x7'));var config=require(_0xb13c('0x8'));var jayson=require('jayson/promise');var client=jayson[_0xb13c('0x9')]['http']({'port':0x232a});function respondWithRpcPromise(_0xad7b36,_0x3c05e7,_0x3a55e3){return new BPromise(function(_0x3cd9a8,_0x55bc75){return client[_0xb13c('0xa')](_0xad7b36,_0x3a55e3)['then'](function(_0x24aa56){logger[_0xb13c('0xb')](_0xb13c('0xc'),_0x3c05e7,_0xb13c('0xd'));logger[_0xb13c('0xe')](_0xb13c('0xf'),_0x3c05e7,_0xb13c('0xd'),JSON['stringify'](_0x24aa56));if(_0x24aa56[_0xb13c('0x10')]){if(_0x24aa56[_0xb13c('0x10')][_0xb13c('0x11')]===0x1f4){logger[_0xb13c('0x10')](_0xb13c('0xc'),_0x3c05e7,_0x24aa56[_0xb13c('0x10')][_0xb13c('0x12')]);return _0x55bc75(_0x24aa56[_0xb13c('0x10')]['message']);}logger[_0xb13c('0x10')](_0xb13c('0xc'),_0x3c05e7,_0x24aa56[_0xb13c('0x10')][_0xb13c('0x12')]);return _0x3cd9a8(_0x24aa56[_0xb13c('0x10')]['message']);}else{logger[_0xb13c('0xb')](_0xb13c('0xc'),_0x3c05e7,_0xb13c('0xd'));_0x3cd9a8(_0x24aa56[_0xb13c('0x13')][_0xb13c('0x12')]);}})['catch'](function(_0x2d4d53){logger[_0xb13c('0x10')](_0xb13c('0xc'),_0x3c05e7,_0x2d4d53);_0x55bc75(_0x2d4d53);});});}exports[_0xb13c('0x14')]=function(_0x2ebd45){var _0x2304ed=this;return new Promise(function(_0x228852,_0x5aab7a){return db[_0xb13c('0x15')][_0xb13c('0x16')]({'raw':!![],'where':{'ContactId':_0x2ebd45[_0xb13c('0x17')]['ContactId'],'ListId':_0x2ebd45[_0xb13c('0x17')]['ListId'],'VoiceQueueId':_0x2ebd45[_0xb13c('0x17')][_0xb13c('0x18')]===undefined?null:_0x2ebd45[_0xb13c('0x17')][_0xb13c('0x18')],'CampaignId':_0x2ebd45[_0xb13c('0x17')]['CampaignId']===undefined?null:_0x2ebd45[_0xb13c('0x17')][_0xb13c('0x19')]}})['then'](function(_0xf23c22){if(_0xf23c22!=null){return db[_0xb13c('0x1a')][_0xb13c('0x16')]({'raw':!![],'where':{'CmHopperId':_0xf23c22['id'],'OrderBy':{'$gt':_0xf23c22[_0xb13c('0x1b')]}},'order':[[_0xb13c('0x1b'),_0xb13c('0x1c')]]})[_0xb13c('0x1d')](function(_0x254da3){var _0x4c3ffc=0x0;if(_0x254da3){if(_0x254da3['countbusyretry']+_0x254da3['countcongestionretry']+_0x254da3[_0xb13c('0x1e')]+_0x254da3[_0xb13c('0x1f')]+_0x254da3[_0xb13c('0x20')]+_0x254da3['countabandonedretry']+_0x254da3[_0xb13c('0x21')]+_0x254da3[_0xb13c('0x22')]<_0x2ebd45[_0xb13c('0x23')][_0xb13c('0x24')])_0x4c3ffc=0x1;_0x228852({'additional':_0x4c3ffc});}else{return db[_0xb13c('0x1a')]['findOne']({'raw':!![],'where':{'CmHopperId':_0xf23c22['id']},'order':[[_0xb13c('0x1b'),_0xb13c('0x1c')]]})['then'](function(_0x2cc6f7){var _0x521f0d=0x0;if(_0x2cc6f7){if(_0x2cc6f7[_0xb13c('0x25')]+_0x2cc6f7[_0xb13c('0x26')]+_0x2cc6f7[_0xb13c('0x1e')]+_0x2cc6f7['countnosuchnumberretry']+_0x2cc6f7[_0xb13c('0x20')]+_0x2cc6f7[_0xb13c('0x27')]+_0x2cc6f7[_0xb13c('0x21')]+_0x2cc6f7[_0xb13c('0x22')]<_0x2ebd45[_0xb13c('0x23')][_0xb13c('0x24')])_0x521f0d=0x1;_0x228852({'additional':_0x521f0d});}else{_0x228852({'additional':0x0});}})[_0xb13c('0x28')](function(_0x4a0d42){logger[_0xb13c('0x10')](_0xb13c('0x14'),_0x4a0d42['message']);_0x5aab7a(_0x2304ed[_0xb13c('0x10')](0x1f4,_0x4a0d42[_0xb13c('0x12')]));});}})[_0xb13c('0x28')](function(_0x286f18){logger[_0xb13c('0x10')](_0xb13c('0x14'),_0x286f18[_0xb13c('0x12')]);_0x5aab7a(_0x2304ed[_0xb13c('0x10')](0x1f4,_0x286f18[_0xb13c('0x12')]));});}else _0x228852({'additional':0x0});});});};exports[_0xb13c('0x29')]=function(_0x16ac97){var _0x39ab6b=this;return new Promise(function(_0x17ed2c,_0x37e187){return db[_0xb13c('0x15')][_0xb13c('0x16')]({'raw':!![],'where':{'ContactId':_0x16ac97[_0xb13c('0x17')]['ContactId'],'ListId':_0x16ac97[_0xb13c('0x17')][_0xb13c('0x2a')],'VoiceQueueId':_0x16ac97[_0xb13c('0x17')][_0xb13c('0x18')]===undefined?null:_0x16ac97[_0xb13c('0x17')][_0xb13c('0x18')],'CampaignId':_0x16ac97[_0xb13c('0x17')][_0xb13c('0x19')]===undefined?null:_0x16ac97[_0xb13c('0x17')][_0xb13c('0x19')]}})['then'](function(_0x153924){if(_0x153924!=null){return db[_0xb13c('0x1a')][_0xb13c('0x16')]({'raw':!![],'where':{'CmHopperId':_0x153924['id'],'OrderBy':{'$gte':_0x153924['OrderBy']}},'order':[[_0xb13c('0x1b'),'ASC']]})[_0xb13c('0x1d')](function(_0x5e470a){if(_0x5e470a)return activateAdditionalPhone(_0x153924,_0x16ac97['body'],_0x5e470a,_0x16ac97['duration'],_0x16ac97[_0xb13c('0x2b')])[_0xb13c('0x1d')](function(_0x1c05b3){_0x17ed2c(_0x1c05b3);});else{return db[_0xb13c('0x1a')][_0xb13c('0x16')]({'raw':!![],'where':{'CmHopperId':_0x153924['id']},'order':[[_0xb13c('0x1b'),_0xb13c('0x1c')]]})['then'](function(_0x1ebf36){if(_0x1ebf36)return activateAdditionalPhone(_0x153924,_0x16ac97[_0xb13c('0x17')],_0x1ebf36,_0x16ac97[_0xb13c('0x2c')],_0x16ac97[_0xb13c('0x2b')])[_0xb13c('0x1d')](function(_0x462188){_0x17ed2c(_0x462188);});else{_0x17ed2c({'id':_0x153924['id'],'phone':''});}})[_0xb13c('0x28')](function(_0x25f972){logger[_0xb13c('0x10')](_0xb13c('0x14'),_0x25f972[_0xb13c('0x12')]);_0x37e187(_0x39ab6b[_0xb13c('0x10')](0x1f4,_0x25f972[_0xb13c('0x12')]));});}})[_0xb13c('0x28')](function(_0x1e8dff){logger['error'](_0xb13c('0x14'),_0x1e8dff[_0xb13c('0x12')]);_0x37e187(_0x39ab6b['error'](0x1f4,_0x1e8dff[_0xb13c('0x12')]));});}else _0x17ed2c(0x1);});});};function activateAdditionalPhone(_0x56332f,_0x21a071,_0x4f2c3e,_0x15a74b,_0x437831){return new Promise(function(_0x270d85,_0x55ea55){var _0x357b1c;if(_['isNil'](_0x4f2c3e['scheduledat'])){_0x4f2c3e['scheduledat']=moment();}if(moment()>moment(_0x4f2c3e[_0xb13c('0x2d')])){_0x357b1c=moment()['add'](_0x437831||0x3,_0xb13c('0x2e'));}else{if(_0x4f2c3e['OrderBy']==0x0){_0x357b1c=moment(_0x4f2c3e['scheduledat']);}else{_0x357b1c=moment()[_0xb13c('0x2f')](_0x437831||0x3,_0xb13c('0x2e'));}}if(_[_0xb13c('0x30')](_0x357b1c)){_0x357b1c=moment()[_0xb13c('0x2f')](_0x437831||0x3,_0xb13c('0x2e'));}var _0x4b967d={'phone':_0x56332f[_0xb13c('0x31')],'active':0x0,'OrderBy':_[_0xb13c('0x30')](_0x56332f[_0xb13c('0x1b')])?0x0:_0x56332f[_0xb13c('0x1b')],'scheduledat':moment()['add'](_0x15a74b||0x1,_0xb13c('0x2e')),'countbusyretry':_0x21a071['countbusyretry'],'countcongestionretry':_0x21a071[_0xb13c('0x26')],'countnoanswerretry':_0x21a071[_0xb13c('0x1e')],'countnosuchnumberretry':_0x21a071[_0xb13c('0x1f')],'countdropretry':_0x21a071[_0xb13c('0x20')],'countabandonedretry':_0x21a071[_0xb13c('0x27')],'countmachineretry':_0x21a071['countmachineretry'],'countagentrejectretry':_0x21a071[_0xb13c('0x22')],'createdAt':_0x21a071['createdAt'],'updatedAt':_0x21a071[_0xb13c('0x32')]};var _0x24bc3e={'CmHopperId':_0x56332f['id'],'phone':_0x4f2c3e[_0xb13c('0x31')],'active':0x0,'OrderBy':_[_0xb13c('0x30')](_0x4f2c3e[_0xb13c('0x1b')])?0x0:_0x4f2c3e[_0xb13c('0x1b')],'scheduledat':_0x357b1c,'countbusyretry':_0x4f2c3e[_0xb13c('0x25')],'countcongestionretry':_0x4f2c3e['countcongestionretry'],'countnoanswerretry':_0x4f2c3e[_0xb13c('0x1e')],'countnosuchnumberretry':_0x4f2c3e['countnosuchnumberretry'],'countdropretry':_0x4f2c3e[_0xb13c('0x20')],'countabandonedretry':_0x4f2c3e[_0xb13c('0x27')],'countmachineretry':_0x4f2c3e[_0xb13c('0x21')],'countagentrejectretry':_0x4f2c3e[_0xb13c('0x22')],'createdAt':_0x4f2c3e[_0xb13c('0x33')],'updatedAt':_0x4f2c3e[_0xb13c('0x32')]};return db['sequelize'][_0xb13c('0x34')](function(_0xda2c7d){return db[_0xb13c('0x1a')][_0xb13c('0x35')](_0x4b967d,{'where':{'id':_0x4f2c3e['id']}},{'transaction':_0xda2c7d})[_0xb13c('0x1d')](function(){return db[_0xb13c('0x15')][_0xb13c('0x35')](_0x24bc3e,{'where':{'id':_0x56332f['id']}},{'transaction':_0xda2c7d});});})[_0xb13c('0x1d')](function(_0x16275b){_0x270d85({'id':_0x56332f['id'],'phone':_0x4f2c3e[_0xb13c('0x31')]});})['catch'](function(_0x37e99a){logger[_0xb13c('0x10')](_0xb13c('0x36'),_0x37e99a[_0xb13c('0x12')]);_0x55ea55({'error':_0x37e99a[_0xb13c('0x12')]});});});}exports[_0xb13c('0x37')]=function(_0x4c3eca){var _0x532374=this;return new Promise(function(_0x156f73,_0x359103){return db[_0xb13c('0x15')][_0xb13c('0x35')](_0x4c3eca[_0xb13c('0x17')][_0xb13c('0x38')],{'where':{'id':_0x4c3eca[_0xb13c('0x17')]['id']}})[_0xb13c('0x1d')](function(_0x30f9f6){_0x156f73(_0x30f9f6);})[_0xb13c('0x28')](function(_0x13bcc0){logger[_0xb13c('0x10')]('IncrementPreviewCounters',_0x13bcc0['message']);_0x359103(_0x532374['error'](0x1f4,_0x13bcc0[_0xb13c('0x12')]));});});};