Built motion from commit b5996064.|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 _0x8f5b=['waitTime','scheduledat','add','minutes','isNil','createdAt','updatedAt','transaction','update','phone','activateAdditionalPhone','IncrementPreviewCounters','lodash','util','moment','bluebird','randomstring','../../mysqldb','../../config/utils','../../config/logger','rpc','jayson/promise','http','request','then','info','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','stringify','error','CmHopperAdditionalPhone,\x20%s,\x20%s','message','result','catch','VerifyAdditionalPhones','findOne','body','ContactId','ListId','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','OrderBy','ASC','countbusyretry','countcongestionretry','countnoanswerretry','countdropretry','countmachineretry','countagentrejectretry','dialGlobalMaxRetry','countnosuchnumberretry','countabandonedretry','SwapAdditionalPhones','CmHopper','duration'];(function(_0x54edc9,_0x345f01){var _0x2ae7a9=function(_0x24a545){while(--_0x24a545){_0x54edc9['push'](_0x54edc9['shift']());}};_0x2ae7a9(++_0x345f01);}(_0x8f5b,0x129));var _0xb8f5=function(_0x5b65b5,_0x42a4a4){_0x5b65b5=_0x5b65b5-0x0;var _0x27bd42=_0x8f5b[_0x5b65b5];return _0x27bd42;};'use strict';var _=require(_0xb8f5('0x0'));var util=require(_0xb8f5('0x1'));var moment=require(_0xb8f5('0x2'));var BPromise=require(_0xb8f5('0x3'));var rs=require(_0xb8f5('0x4'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0xb8f5('0x5'))['db'];var utils=require(_0xb8f5('0x6'));var logger=require(_0xb8f5('0x7'))(_0xb8f5('0x8'));var config=require('../../config/environment');var jayson=require(_0xb8f5('0x9'));var client=jayson['client'][_0xb8f5('0xa')]({'port':0x232a});function respondWithRpcPromise(_0x1d67fa,_0x31df4f,_0x3cb868){return new BPromise(function(_0x65e938,_0x2fbfd9){return client[_0xb8f5('0xb')](_0x1d67fa,_0x3cb868)[_0xb8f5('0xc')](function(_0x19c706){logger[_0xb8f5('0xd')]('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x31df4f,_0xb8f5('0xe'));logger[_0xb8f5('0xf')](_0xb8f5('0x10'),_0x31df4f,_0xb8f5('0xe'),JSON[_0xb8f5('0x11')](_0x19c706));if(_0x19c706['error']){if(_0x19c706[_0xb8f5('0x12')]['code']===0x1f4){logger['error'](_0xb8f5('0x13'),_0x31df4f,_0x19c706[_0xb8f5('0x12')]['message']);return _0x2fbfd9(_0x19c706[_0xb8f5('0x12')][_0xb8f5('0x14')]);}logger[_0xb8f5('0x12')](_0xb8f5('0x13'),_0x31df4f,_0x19c706[_0xb8f5('0x12')]['message']);return _0x65e938(_0x19c706[_0xb8f5('0x12')][_0xb8f5('0x14')]);}else{logger[_0xb8f5('0xd')]('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x31df4f,'request\x20sent');_0x65e938(_0x19c706[_0xb8f5('0x15')][_0xb8f5('0x14')]);}})[_0xb8f5('0x16')](function(_0x29f362){logger[_0xb8f5('0x12')](_0xb8f5('0x13'),_0x31df4f,_0x29f362);_0x2fbfd9(_0x29f362);});});}exports[_0xb8f5('0x17')]=function(_0x3f909b){var _0x411b3d=this;return new Promise(function(_0x3c4fdd,_0x49a5c1){return db['CmHopper'][_0xb8f5('0x18')]({'raw':!![],'where':{'ContactId':_0x3f909b[_0xb8f5('0x19')][_0xb8f5('0x1a')],'ListId':_0x3f909b[_0xb8f5('0x19')][_0xb8f5('0x1b')],'VoiceQueueId':_0x3f909b[_0xb8f5('0x19')]['VoiceQueueId']===undefined?null:_0x3f909b[_0xb8f5('0x19')][_0xb8f5('0x1c')],'CampaignId':_0x3f909b[_0xb8f5('0x19')][_0xb8f5('0x1d')]===undefined?null:_0x3f909b[_0xb8f5('0x19')]['CampaignId']}})[_0xb8f5('0xc')](function(_0x289abe){if(_0x289abe!=null){return db[_0xb8f5('0x1e')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x289abe['id'],'OrderBy':{'$gt':_0x289abe['OrderBy']}},'order':[[_0xb8f5('0x1f'),_0xb8f5('0x20')]]})[_0xb8f5('0xc')](function(_0x150beb){var _0x711867=0x0;if(_0x150beb){if(_0x150beb[_0xb8f5('0x21')]+_0x150beb[_0xb8f5('0x22')]+_0x150beb[_0xb8f5('0x23')]+_0x150beb['countnosuchnumberretry']+_0x150beb[_0xb8f5('0x24')]+_0x150beb['countabandonedretry']+_0x150beb[_0xb8f5('0x25')]+_0x150beb[_0xb8f5('0x26')]<_0x3f909b['entity'][_0xb8f5('0x27')])_0x711867=0x1;_0x3c4fdd({'additional':_0x711867});}else{return db[_0xb8f5('0x1e')][_0xb8f5('0x18')]({'raw':!![],'where':{'CmHopperId':_0x289abe['id']},'order':[[_0xb8f5('0x1f'),_0xb8f5('0x20')]]})[_0xb8f5('0xc')](function(_0x342bb7){var _0x1b8ace=0x0;if(_0x342bb7){if(_0x342bb7[_0xb8f5('0x21')]+_0x342bb7['countcongestionretry']+_0x342bb7[_0xb8f5('0x23')]+_0x342bb7[_0xb8f5('0x28')]+_0x342bb7['countdropretry']+_0x342bb7[_0xb8f5('0x29')]+_0x342bb7[_0xb8f5('0x25')]+_0x342bb7[_0xb8f5('0x26')]<_0x3f909b['entity'][_0xb8f5('0x27')])_0x1b8ace=0x1;_0x3c4fdd({'additional':_0x1b8ace});}else{_0x3c4fdd({'additional':0x0});}})['catch'](function(_0x5d0503){logger[_0xb8f5('0x12')]('VerifyAdditionalPhones',_0x5d0503[_0xb8f5('0x14')]);_0x49a5c1(_0x411b3d['error'](0x1f4,_0x5d0503[_0xb8f5('0x14')]));});}})['catch'](function(_0x302098){logger[_0xb8f5('0x12')](_0xb8f5('0x17'),_0x302098['message']);_0x49a5c1(_0x411b3d[_0xb8f5('0x12')](0x1f4,_0x302098[_0xb8f5('0x14')]));});}else _0x3c4fdd({'additional':0x0});});});};exports[_0xb8f5('0x2a')]=function(_0x112a92){var _0x90eb23=this;return new Promise(function(_0x4352b7,_0x42b7a2){return db[_0xb8f5('0x2b')][_0xb8f5('0x18')]({'raw':!![],'where':{'ContactId':_0x112a92['body'][_0xb8f5('0x1a')],'ListId':_0x112a92[_0xb8f5('0x19')][_0xb8f5('0x1b')],'VoiceQueueId':_0x112a92[_0xb8f5('0x19')][_0xb8f5('0x1c')]===undefined?null:_0x112a92['body'][_0xb8f5('0x1c')],'CampaignId':_0x112a92[_0xb8f5('0x19')]['CampaignId']===undefined?null:_0x112a92[_0xb8f5('0x19')][_0xb8f5('0x1d')]}})[_0xb8f5('0xc')](function(_0x38373e){if(_0x38373e!=null){return db[_0xb8f5('0x1e')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x38373e['id'],'OrderBy':{'$gte':_0x38373e[_0xb8f5('0x1f')]}},'order':[[_0xb8f5('0x1f'),_0xb8f5('0x20')]]})[_0xb8f5('0xc')](function(_0x4918c9){if(_0x4918c9)return activateAdditionalPhone(_0x38373e,_0x112a92['body'],_0x4918c9,_0x112a92[_0xb8f5('0x2c')],_0x112a92['waitTime'])['then'](function(_0x1ebb3b){_0x4352b7(_0x1ebb3b);});else{return db[_0xb8f5('0x1e')][_0xb8f5('0x18')]({'raw':!![],'where':{'CmHopperId':_0x38373e['id']},'order':[[_0xb8f5('0x1f'),_0xb8f5('0x20')]]})['then'](function(_0x291c33){if(_0x291c33)return activateAdditionalPhone(_0x38373e,_0x112a92[_0xb8f5('0x19')],_0x291c33,_0x112a92['duration'],_0x112a92[_0xb8f5('0x2d')])['then'](function(_0x2310c0){_0x4352b7(_0x2310c0);});else{_0x4352b7({'id':_0x38373e['id'],'phone':''});}})['catch'](function(_0x23f99b){logger[_0xb8f5('0x12')](_0xb8f5('0x17'),_0x23f99b[_0xb8f5('0x14')]);_0x42b7a2(_0x90eb23[_0xb8f5('0x12')](0x1f4,_0x23f99b[_0xb8f5('0x14')]));});}})['catch'](function(_0x59039e){logger[_0xb8f5('0x12')](_0xb8f5('0x17'),_0x59039e[_0xb8f5('0x14')]);_0x42b7a2(_0x90eb23[_0xb8f5('0x12')](0x1f4,_0x59039e[_0xb8f5('0x14')]));});}else _0x4352b7(0x1);});});};function activateAdditionalPhone(_0x4dbe8f,_0x1c9934,_0x1411c1,_0x351d86,_0x3511c6){return new Promise(function(_0x5c15b8,_0x6f167b){var _0x533938;if(_['isNil'](_0x1411c1[_0xb8f5('0x2e')])){_0x1411c1['scheduledat']=moment();}if(moment()>moment(_0x1411c1[_0xb8f5('0x2e')])){_0x533938=moment()[_0xb8f5('0x2f')](_0x3511c6||0x3,_0xb8f5('0x30'));}else{if(_0x1411c1[_0xb8f5('0x1f')]==0x0){_0x533938=moment(_0x1411c1[_0xb8f5('0x2e')]);}else{_0x533938=moment()['add'](_0x3511c6||0x3,_0xb8f5('0x30'));}}if(_['isNil'](_0x533938)){_0x533938=moment()['add'](_0x3511c6||0x3,'minutes');}var _0x1419d2={'phone':_0x4dbe8f['phone'],'active':0x0,'OrderBy':_[_0xb8f5('0x31')](_0x4dbe8f[_0xb8f5('0x1f')])?0x0:_0x4dbe8f[_0xb8f5('0x1f')],'scheduledat':moment()['add'](_0x351d86||0x1,_0xb8f5('0x30')),'countbusyretry':_0x1c9934[_0xb8f5('0x21')],'countcongestionretry':_0x1c9934['countcongestionretry'],'countnoanswerretry':_0x1c9934[_0xb8f5('0x23')],'countnosuchnumberretry':_0x1c9934[_0xb8f5('0x28')],'countdropretry':_0x1c9934[_0xb8f5('0x24')],'countabandonedretry':_0x1c9934[_0xb8f5('0x29')],'countmachineretry':_0x1c9934['countmachineretry'],'countagentrejectretry':_0x1c9934[_0xb8f5('0x26')],'createdAt':_0x1c9934[_0xb8f5('0x32')],'updatedAt':_0x1c9934[_0xb8f5('0x33')]};var _0x524b3c={'CmHopperId':_0x4dbe8f['id'],'phone':_0x1411c1['phone'],'active':0x0,'OrderBy':_['isNil'](_0x1411c1['OrderBy'])?0x0:_0x1411c1[_0xb8f5('0x1f')],'scheduledat':_0x533938,'countbusyretry':_0x1411c1[_0xb8f5('0x21')],'countcongestionretry':_0x1411c1['countcongestionretry'],'countnoanswerretry':_0x1411c1[_0xb8f5('0x23')],'countnosuchnumberretry':_0x1411c1[_0xb8f5('0x28')],'countdropretry':_0x1411c1['countdropretry'],'countabandonedretry':_0x1411c1[_0xb8f5('0x29')],'countmachineretry':_0x1411c1[_0xb8f5('0x25')],'countagentrejectretry':_0x1411c1[_0xb8f5('0x26')],'createdAt':_0x1411c1['createdAt'],'updatedAt':_0x1411c1[_0xb8f5('0x33')]};return db['sequelize'][_0xb8f5('0x34')](function(_0x40bd35){return db[_0xb8f5('0x1e')][_0xb8f5('0x35')](_0x1419d2,{'where':{'id':_0x1411c1['id']}},{'transaction':_0x40bd35})[_0xb8f5('0xc')](function(){return db[_0xb8f5('0x2b')][_0xb8f5('0x35')](_0x524b3c,{'where':{'id':_0x4dbe8f['id']}},{'transaction':_0x40bd35});});})[_0xb8f5('0xc')](function(_0x495aea){_0x5c15b8({'id':_0x4dbe8f['id'],'phone':_0x1411c1[_0xb8f5('0x36')]});})[_0xb8f5('0x16')](function(_0x3b401e){logger[_0xb8f5('0x12')](_0xb8f5('0x37'),_0x3b401e[_0xb8f5('0x14')]);_0x6f167b({'error':_0x3b401e[_0xb8f5('0x14')]});});});}exports[_0xb8f5('0x38')]=function(_0xef953f){var _0xa921d9=this;return new Promise(function(_0x5c7268,_0x4ffbc6){return db[_0xb8f5('0x2b')][_0xb8f5('0x35')](_0xef953f[_0xb8f5('0x19')]['field'],{'where':{'id':_0xef953f['body']['id']}})[_0xb8f5('0xc')](function(_0x4351c4){_0x5c7268(_0x4351c4);})[_0xb8f5('0x16')](function(_0x8db45a){logger[_0xb8f5('0x12')](_0xb8f5('0x38'),_0x8db45a[_0xb8f5('0x14')]);_0x4ffbc6(_0xa921d9[_0xb8f5('0x12')](0x1f4,_0x8db45a[_0xb8f5('0x14')]));});});};