Built motion from commit 56a37f2a.|2.6.21
[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 _0xbc38=['updatedAt','update','activateAdditionalPhone','IncrementPreviewCounters','field','lodash','util','moment','bluebird','../../config/utils','rpc','../../config/environment','jayson/promise','http','request','then','info','CmHopperAdditionalPhone,\x20%s,\x20%s','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','findOne','body','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','OrderBy','ASC','countbusyretry','countcongestionretry','countnoanswerretry','countnosuchnumberretry','countdropretry','countmachineretry','countagentrejectretry','dialGlobalMaxRetry','countabandonedretry','entity','VerifyAdditionalPhones','SwapAdditionalPhones','CmHopper','ContactId','ListId','waitTime','duration','isNil','scheduledat','add','minutes','phone','createdAt'];(function(_0x52e077,_0x5325c8){var _0x34dd3d=function(_0x37f4fc){while(--_0x37f4fc){_0x52e077['push'](_0x52e077['shift']());}};_0x34dd3d(++_0x5325c8);}(_0xbc38,0xb0));var _0x8bc3=function(_0x5655e6,_0x4a7423){_0x5655e6=_0x5655e6-0x0;var _0x3b8fdd=_0xbc38[_0x5655e6];return _0x3b8fdd;};'use strict';var _=require(_0x8bc3('0x0'));var util=require(_0x8bc3('0x1'));var moment=require(_0x8bc3('0x2'));var BPromise=require(_0x8bc3('0x3'));var rs=require('randomstring');var fs=require('fs');var Redis=require('ioredis');var db=require('../../mysqldb')['db'];var utils=require(_0x8bc3('0x4'));var logger=require('../../config/logger')(_0x8bc3('0x5'));var config=require(_0x8bc3('0x6'));var jayson=require(_0x8bc3('0x7'));var client=jayson['client'][_0x8bc3('0x8')]({'port':0x232a});function respondWithRpcPromise(_0x5db19d,_0x33a88b,_0x18f6a4){return new BPromise(function(_0x3e55e8,_0x1ad2e3){return client[_0x8bc3('0x9')](_0x5db19d,_0x18f6a4)[_0x8bc3('0xa')](function(_0x42a5b9){logger[_0x8bc3('0xb')](_0x8bc3('0xc'),_0x33a88b,'request\x20sent');logger[_0x8bc3('0xd')](_0x8bc3('0xe'),_0x33a88b,_0x8bc3('0xf'),JSON[_0x8bc3('0x10')](_0x42a5b9));if(_0x42a5b9[_0x8bc3('0x11')]){if(_0x42a5b9[_0x8bc3('0x11')][_0x8bc3('0x12')]===0x1f4){logger[_0x8bc3('0x11')](_0x8bc3('0xc'),_0x33a88b,_0x42a5b9[_0x8bc3('0x11')][_0x8bc3('0x13')]);return _0x1ad2e3(_0x42a5b9[_0x8bc3('0x11')][_0x8bc3('0x13')]);}logger[_0x8bc3('0x11')](_0x8bc3('0xc'),_0x33a88b,_0x42a5b9[_0x8bc3('0x11')][_0x8bc3('0x13')]);return _0x3e55e8(_0x42a5b9['error']['message']);}else{logger[_0x8bc3('0xb')](_0x8bc3('0xc'),_0x33a88b,'request\x20sent');_0x3e55e8(_0x42a5b9[_0x8bc3('0x14')][_0x8bc3('0x13')]);}})[_0x8bc3('0x15')](function(_0x84a40b){logger[_0x8bc3('0x11')]('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x33a88b,_0x84a40b);_0x1ad2e3(_0x84a40b);});});}exports['VerifyAdditionalPhones']=function(_0x38f2e8){var _0x2d3178=this;return new Promise(function(_0xd36dbb,_0x2e3ace){return db['CmHopper'][_0x8bc3('0x16')]({'raw':!![],'where':{'ContactId':_0x38f2e8['body']['ContactId'],'ListId':_0x38f2e8[_0x8bc3('0x17')]['ListId'],'VoiceQueueId':_0x38f2e8[_0x8bc3('0x17')][_0x8bc3('0x18')]===undefined?null:_0x38f2e8['body'][_0x8bc3('0x18')],'CampaignId':_0x38f2e8['body'][_0x8bc3('0x19')]===undefined?null:_0x38f2e8[_0x8bc3('0x17')]['CampaignId']}})['then'](function(_0x275d97){if(_0x275d97!=null){return db[_0x8bc3('0x1a')][_0x8bc3('0x16')]({'raw':!![],'where':{'CmHopperId':_0x275d97['id'],'OrderBy':{'$gt':_0x275d97['OrderBy']}},'order':[[_0x8bc3('0x1b'),_0x8bc3('0x1c')]]})['then'](function(_0x509ef0){var _0x33ffd8=0x0;if(_0x509ef0){if(_0x509ef0[_0x8bc3('0x1d')]+_0x509ef0[_0x8bc3('0x1e')]+_0x509ef0[_0x8bc3('0x1f')]+_0x509ef0[_0x8bc3('0x20')]+_0x509ef0[_0x8bc3('0x21')]+_0x509ef0['countabandonedretry']+_0x509ef0[_0x8bc3('0x22')]+_0x509ef0[_0x8bc3('0x23')]<_0x38f2e8['entity'][_0x8bc3('0x24')])_0x33ffd8=0x1;_0xd36dbb({'additional':_0x33ffd8});}else{return db['CmHopperAdditionalPhone'][_0x8bc3('0x16')]({'raw':!![],'where':{'CmHopperId':_0x275d97['id']},'order':[['OrderBy','ASC']]})[_0x8bc3('0xa')](function(_0x5ed1ad){var _0x55b548=0x0;if(_0x5ed1ad){if(_0x5ed1ad['countbusyretry']+_0x5ed1ad[_0x8bc3('0x1e')]+_0x5ed1ad[_0x8bc3('0x1f')]+_0x5ed1ad[_0x8bc3('0x20')]+_0x5ed1ad[_0x8bc3('0x21')]+_0x5ed1ad[_0x8bc3('0x25')]+_0x5ed1ad['countmachineretry']+_0x5ed1ad['countagentrejectretry']<_0x38f2e8[_0x8bc3('0x26')][_0x8bc3('0x24')])_0x55b548=0x1;_0xd36dbb({'additional':_0x55b548});}else{_0xd36dbb({'additional':0x0});}})[_0x8bc3('0x15')](function(_0x3139fb){logger[_0x8bc3('0x11')](_0x8bc3('0x27'),_0x3139fb[_0x8bc3('0x13')]);_0x2e3ace(_0x2d3178[_0x8bc3('0x11')](0x1f4,_0x3139fb[_0x8bc3('0x13')]));});}})['catch'](function(_0xc4ffeb){logger[_0x8bc3('0x11')](_0x8bc3('0x27'),_0xc4ffeb[_0x8bc3('0x13')]);_0x2e3ace(_0x2d3178['error'](0x1f4,_0xc4ffeb[_0x8bc3('0x13')]));});}else _0xd36dbb({'additional':0x0});});});};exports[_0x8bc3('0x28')]=function(_0x263ec7){var _0x335695=this;return new Promise(function(_0x2894f5,_0x36dbe2){return db[_0x8bc3('0x29')][_0x8bc3('0x16')]({'raw':!![],'where':{'ContactId':_0x263ec7['body'][_0x8bc3('0x2a')],'ListId':_0x263ec7[_0x8bc3('0x17')][_0x8bc3('0x2b')],'VoiceQueueId':_0x263ec7[_0x8bc3('0x17')][_0x8bc3('0x18')]===undefined?null:_0x263ec7['body'][_0x8bc3('0x18')],'CampaignId':_0x263ec7[_0x8bc3('0x17')][_0x8bc3('0x19')]===undefined?null:_0x263ec7[_0x8bc3('0x17')]['CampaignId']}})[_0x8bc3('0xa')](function(_0x4fbfc2){if(_0x4fbfc2!=null){return db['CmHopperAdditionalPhone'][_0x8bc3('0x16')]({'raw':!![],'where':{'CmHopperId':_0x4fbfc2['id'],'OrderBy':{'$gte':_0x4fbfc2['OrderBy']}},'order':[[_0x8bc3('0x1b'),'ASC']]})['then'](function(_0x216c2c){if(_0x216c2c)return activateAdditionalPhone(_0x4fbfc2,_0x263ec7['body'],_0x216c2c,_0x263ec7['duration'],_0x263ec7[_0x8bc3('0x2c')])['then'](function(_0x5295c1){_0x2894f5(_0x5295c1);});else{return db[_0x8bc3('0x1a')][_0x8bc3('0x16')]({'raw':!![],'where':{'CmHopperId':_0x4fbfc2['id']},'order':[[_0x8bc3('0x1b'),_0x8bc3('0x1c')]]})[_0x8bc3('0xa')](function(_0x42fa0a){if(_0x42fa0a)return activateAdditionalPhone(_0x4fbfc2,_0x263ec7[_0x8bc3('0x17')],_0x42fa0a,_0x263ec7[_0x8bc3('0x2d')],_0x263ec7[_0x8bc3('0x2c')])['then'](function(_0x404a3e){_0x2894f5(_0x404a3e);});else{_0x2894f5({'id':_0x4fbfc2['id'],'phone':''});}})[_0x8bc3('0x15')](function(_0x47ad6a){logger[_0x8bc3('0x11')](_0x8bc3('0x27'),_0x47ad6a['message']);_0x36dbe2(_0x335695[_0x8bc3('0x11')](0x1f4,_0x47ad6a[_0x8bc3('0x13')]));});}})[_0x8bc3('0x15')](function(_0x4cdcb7){logger[_0x8bc3('0x11')](_0x8bc3('0x27'),_0x4cdcb7['message']);_0x36dbe2(_0x335695[_0x8bc3('0x11')](0x1f4,_0x4cdcb7['message']));});}else _0x2894f5(0x1);});});};function activateAdditionalPhone(_0x1574b8,_0x16c63e,_0xd0f6a1,_0x1c087d,_0x4e4524){return new Promise(function(_0x165b31,_0x16b24c){var _0x1c31a1;if(_[_0x8bc3('0x2e')](_0xd0f6a1[_0x8bc3('0x2f')])){_0xd0f6a1['scheduledat']=moment();}if(moment()>moment(_0xd0f6a1[_0x8bc3('0x2f')])){_0x1c31a1=moment()[_0x8bc3('0x30')](_0x4e4524||0x3,_0x8bc3('0x31'));}else{if(_0xd0f6a1[_0x8bc3('0x1b')]==0x0){_0x1c31a1=moment(_0xd0f6a1[_0x8bc3('0x2f')]);}else{_0x1c31a1=moment()['add'](_0x4e4524||0x3,_0x8bc3('0x31'));}}if(_[_0x8bc3('0x2e')](_0x1c31a1)){_0x1c31a1=moment()['add'](_0x4e4524||0x3,'minutes');}var _0x4629ee={'phone':_0x1574b8[_0x8bc3('0x32')],'active':0x0,'OrderBy':_['isNil'](_0x1574b8[_0x8bc3('0x1b')])?0x0:_0x1574b8[_0x8bc3('0x1b')],'scheduledat':moment()['add'](_0x1c087d||0x1,_0x8bc3('0x31')),'countbusyretry':_0x16c63e[_0x8bc3('0x1d')],'countcongestionretry':_0x16c63e[_0x8bc3('0x1e')],'countnoanswerretry':_0x16c63e[_0x8bc3('0x1f')],'countnosuchnumberretry':_0x16c63e[_0x8bc3('0x20')],'countdropretry':_0x16c63e[_0x8bc3('0x21')],'countabandonedretry':_0x16c63e[_0x8bc3('0x25')],'countmachineretry':_0x16c63e[_0x8bc3('0x22')],'countagentrejectretry':_0x16c63e[_0x8bc3('0x23')],'createdAt':_0x16c63e[_0x8bc3('0x33')],'updatedAt':_0x16c63e['updatedAt']};var _0x4145f8={'CmHopperId':_0x1574b8['id'],'phone':_0xd0f6a1[_0x8bc3('0x32')],'active':0x0,'OrderBy':_[_0x8bc3('0x2e')](_0xd0f6a1[_0x8bc3('0x1b')])?0x0:_0xd0f6a1[_0x8bc3('0x1b')],'scheduledat':_0x1c31a1,'countbusyretry':_0xd0f6a1['countbusyretry'],'countcongestionretry':_0xd0f6a1['countcongestionretry'],'countnoanswerretry':_0xd0f6a1[_0x8bc3('0x1f')],'countnosuchnumberretry':_0xd0f6a1[_0x8bc3('0x20')],'countdropretry':_0xd0f6a1[_0x8bc3('0x21')],'countabandonedretry':_0xd0f6a1['countabandonedretry'],'countmachineretry':_0xd0f6a1[_0x8bc3('0x22')],'countagentrejectretry':_0xd0f6a1['countagentrejectretry'],'createdAt':_0xd0f6a1[_0x8bc3('0x33')],'updatedAt':_0xd0f6a1[_0x8bc3('0x34')]};return db['sequelize']['transaction'](function(_0x8838ae){return db[_0x8bc3('0x1a')][_0x8bc3('0x35')](_0x4629ee,{'where':{'id':_0xd0f6a1['id']}},{'transaction':_0x8838ae})['then'](function(){return db['CmHopper'][_0x8bc3('0x35')](_0x4145f8,{'where':{'id':_0x1574b8['id']}},{'transaction':_0x8838ae});});})[_0x8bc3('0xa')](function(_0x107f3c){_0x165b31({'id':_0x1574b8['id'],'phone':_0xd0f6a1[_0x8bc3('0x32')]});})[_0x8bc3('0x15')](function(_0xa8ea25){logger[_0x8bc3('0x11')](_0x8bc3('0x36'),_0xa8ea25[_0x8bc3('0x13')]);_0x16b24c({'error':_0xa8ea25['message']});});});}exports[_0x8bc3('0x37')]=function(_0x3d0dbf){var _0x436a7c=this;return new Promise(function(_0x237658,_0x1d45c4){return db[_0x8bc3('0x29')]['update'](_0x3d0dbf[_0x8bc3('0x17')][_0x8bc3('0x38')],{'where':{'id':_0x3d0dbf['body']['id']}})[_0x8bc3('0xa')](function(_0x22bc9e){_0x237658(_0x22bc9e);})['catch'](function(_0x25b656){logger[_0x8bc3('0x11')](_0x8bc3('0x37'),_0x25b656['message']);_0x1d45c4(_0x436a7c[_0x8bc3('0x11')](0x1f4,_0x25b656[_0x8bc3('0x13')]));});});};