cd317ad0afeb0759b379047afed6167e72e2aadd
[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 _0xc1c6=['phone','countmachineretry','createdAt','updatedAt','sequelize','transaction','update','CmHopper','IncrementPreviewCounters','field','lodash','util','moment','bluebird','randomstring','../../mysqldb','../../config/logger','rpc','../../config/environment','jayson/promise','http','then','info','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','stringify','error','code','CmHopperAdditionalPhone,\x20%s,\x20%s','message','catch','VerifyAdditionalPhones','body','ContactId','ListId','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','findOne','OrderBy','ASC','countbusyretry','countcongestionretry','countnoanswerretry','countnosuchnumberretry','entity','dialGlobalMaxRetry','countdropretry','countabandonedretry','countagentrejectretry','SwapAdditionalPhones','duration','waitTime','isNil','scheduledat','minutes','add'];(function(_0x1dca26,_0x36f303){var _0x46dcfd=function(_0x140ade){while(--_0x140ade){_0x1dca26['push'](_0x1dca26['shift']());}};_0x46dcfd(++_0x36f303);}(_0xc1c6,0x166));var _0x6c1c=function(_0x1aaca6,_0x107e9e){_0x1aaca6=_0x1aaca6-0x0;var _0x502fb9=_0xc1c6[_0x1aaca6];return _0x502fb9;};'use strict';var _=require(_0x6c1c('0x0'));var util=require(_0x6c1c('0x1'));var moment=require(_0x6c1c('0x2'));var BPromise=require(_0x6c1c('0x3'));var rs=require(_0x6c1c('0x4'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0x6c1c('0x5'))['db'];var utils=require('../../config/utils');var logger=require(_0x6c1c('0x6'))(_0x6c1c('0x7'));var config=require(_0x6c1c('0x8'));var jayson=require(_0x6c1c('0x9'));var client=jayson['client'][_0x6c1c('0xa')]({'port':0x232a});function respondWithRpcPromise(_0x36583c,_0x327a3e,_0x3a2c67){return new BPromise(function(_0x135e23,_0x39ebf6){return client['request'](_0x36583c,_0x3a2c67)[_0x6c1c('0xb')](function(_0xc4f16c){logger[_0x6c1c('0xc')]('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x327a3e,_0x6c1c('0xd'));logger[_0x6c1c('0xe')](_0x6c1c('0xf'),_0x327a3e,'request\x20sent',JSON[_0x6c1c('0x10')](_0xc4f16c));if(_0xc4f16c[_0x6c1c('0x11')]){if(_0xc4f16c['error'][_0x6c1c('0x12')]===0x1f4){logger['error'](_0x6c1c('0x13'),_0x327a3e,_0xc4f16c[_0x6c1c('0x11')][_0x6c1c('0x14')]);return _0x39ebf6(_0xc4f16c[_0x6c1c('0x11')]['message']);}logger['error'](_0x6c1c('0x13'),_0x327a3e,_0xc4f16c[_0x6c1c('0x11')][_0x6c1c('0x14')]);return _0x135e23(_0xc4f16c['error']['message']);}else{logger[_0x6c1c('0xc')](_0x6c1c('0x13'),_0x327a3e,_0x6c1c('0xd'));_0x135e23(_0xc4f16c['result'][_0x6c1c('0x14')]);}})[_0x6c1c('0x15')](function(_0x399190){logger[_0x6c1c('0x11')](_0x6c1c('0x13'),_0x327a3e,_0x399190);_0x39ebf6(_0x399190);});});}exports[_0x6c1c('0x16')]=function(_0x3d6f45){var _0xfa1893=this;return new Promise(function(_0x56baa5,_0x2f89bd){return db['CmHopper']['findOne']({'raw':!![],'where':{'ContactId':_0x3d6f45[_0x6c1c('0x17')][_0x6c1c('0x18')],'ListId':_0x3d6f45[_0x6c1c('0x17')][_0x6c1c('0x19')],'VoiceQueueId':_0x3d6f45['body'][_0x6c1c('0x1a')]===undefined?null:_0x3d6f45[_0x6c1c('0x17')][_0x6c1c('0x1a')],'CampaignId':_0x3d6f45[_0x6c1c('0x17')]['CampaignId']===undefined?null:_0x3d6f45['body'][_0x6c1c('0x1b')]}})['then'](function(_0xfca82f){if(_0xfca82f!=null){return db[_0x6c1c('0x1c')][_0x6c1c('0x1d')]({'raw':!![],'where':{'CmHopperId':_0xfca82f['id'],'OrderBy':{'$gt':_0xfca82f[_0x6c1c('0x1e')]}},'order':[[_0x6c1c('0x1e'),_0x6c1c('0x1f')]]})[_0x6c1c('0xb')](function(_0x5e9376){var _0x13f9f8=0x0;if(_0x5e9376){if(_0x5e9376[_0x6c1c('0x20')]+_0x5e9376[_0x6c1c('0x21')]+_0x5e9376[_0x6c1c('0x22')]+_0x5e9376[_0x6c1c('0x23')]+_0x5e9376['countdropretry']+_0x5e9376['countabandonedretry']+_0x5e9376['countmachineretry']+_0x5e9376['countagentrejectretry']<_0x3d6f45[_0x6c1c('0x24')][_0x6c1c('0x25')])_0x13f9f8=0x1;_0x56baa5({'additional':_0x13f9f8});}else{return db['CmHopperAdditionalPhone']['findOne']({'raw':!![],'where':{'CmHopperId':_0xfca82f['id']},'order':[[_0x6c1c('0x1e'),'ASC']]})['then'](function(_0x3d4782){var _0x59dbda=0x0;if(_0x3d4782){if(_0x3d4782[_0x6c1c('0x20')]+_0x3d4782['countcongestionretry']+_0x3d4782[_0x6c1c('0x22')]+_0x3d4782['countnosuchnumberretry']+_0x3d4782[_0x6c1c('0x26')]+_0x3d4782[_0x6c1c('0x27')]+_0x3d4782['countmachineretry']+_0x3d4782[_0x6c1c('0x28')]<_0x3d6f45[_0x6c1c('0x24')]['dialGlobalMaxRetry'])_0x59dbda=0x1;_0x56baa5({'additional':_0x59dbda});}else{_0x56baa5({'additional':0x0});}})[_0x6c1c('0x15')](function(_0x55b9ee){logger[_0x6c1c('0x11')]('VerifyAdditionalPhones',_0x55b9ee[_0x6c1c('0x14')]);_0x2f89bd(_0xfa1893['error'](0x1f4,_0x55b9ee[_0x6c1c('0x14')]));});}})[_0x6c1c('0x15')](function(_0x161341){logger[_0x6c1c('0x11')](_0x6c1c('0x16'),_0x161341[_0x6c1c('0x14')]);_0x2f89bd(_0xfa1893['error'](0x1f4,_0x161341['message']));});}else _0x56baa5({'additional':0x0});});});};exports[_0x6c1c('0x29')]=function(_0xb128f6){var _0x2c79aa=this;return new Promise(function(_0x5275f7,_0x274655){return db['CmHopper']['findOne']({'raw':!![],'where':{'ContactId':_0xb128f6[_0x6c1c('0x17')][_0x6c1c('0x18')],'ListId':_0xb128f6['body']['ListId'],'VoiceQueueId':_0xb128f6['body'][_0x6c1c('0x1a')]===undefined?null:_0xb128f6[_0x6c1c('0x17')]['VoiceQueueId'],'CampaignId':_0xb128f6['body'][_0x6c1c('0x1b')]===undefined?null:_0xb128f6[_0x6c1c('0x17')][_0x6c1c('0x1b')]}})[_0x6c1c('0xb')](function(_0x530c3){if(_0x530c3!=null){return db[_0x6c1c('0x1c')][_0x6c1c('0x1d')]({'raw':!![],'where':{'CmHopperId':_0x530c3['id'],'OrderBy':{'$gte':_0x530c3[_0x6c1c('0x1e')]}},'order':[[_0x6c1c('0x1e'),_0x6c1c('0x1f')]]})[_0x6c1c('0xb')](function(_0x294329){if(_0x294329)return activateAdditionalPhone(_0x530c3,_0xb128f6[_0x6c1c('0x17')],_0x294329,_0xb128f6[_0x6c1c('0x2a')],_0xb128f6[_0x6c1c('0x2b')])[_0x6c1c('0xb')](function(_0x189f09){_0x5275f7(_0x189f09);});else{return db[_0x6c1c('0x1c')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x530c3['id']},'order':[['OrderBy','ASC']]})[_0x6c1c('0xb')](function(_0x51a937){if(_0x51a937)return activateAdditionalPhone(_0x530c3,_0xb128f6[_0x6c1c('0x17')],_0x51a937,_0xb128f6[_0x6c1c('0x2a')],_0xb128f6[_0x6c1c('0x2b')])[_0x6c1c('0xb')](function(_0x253fd4){_0x5275f7(_0x253fd4);});else{_0x5275f7({'id':_0x530c3['id'],'phone':''});}})[_0x6c1c('0x15')](function(_0x3f9256){logger[_0x6c1c('0x11')](_0x6c1c('0x16'),_0x3f9256[_0x6c1c('0x14')]);_0x274655(_0x2c79aa[_0x6c1c('0x11')](0x1f4,_0x3f9256[_0x6c1c('0x14')]));});}})[_0x6c1c('0x15')](function(_0x1bf799){logger[_0x6c1c('0x11')](_0x6c1c('0x16'),_0x1bf799[_0x6c1c('0x14')]);_0x274655(_0x2c79aa[_0x6c1c('0x11')](0x1f4,_0x1bf799[_0x6c1c('0x14')]));});}else _0x5275f7(0x1);});});};function activateAdditionalPhone(_0x5e7897,_0x177821,_0xbb7e7,_0x212454,_0x44c541){return new Promise(function(_0x10da18,_0x12181d){var _0x5a1db9;if(_[_0x6c1c('0x2c')](_0xbb7e7[_0x6c1c('0x2d')])){_0xbb7e7[_0x6c1c('0x2d')]=moment();}if(moment()>moment(_0xbb7e7[_0x6c1c('0x2d')])){_0x5a1db9=moment()['add'](_0x44c541||0x3,_0x6c1c('0x2e'));}else{if(_0xbb7e7['OrderBy']==0x0){_0x5a1db9=moment(_0xbb7e7[_0x6c1c('0x2d')]);}else{_0x5a1db9=moment()[_0x6c1c('0x2f')](_0x44c541||0x3,_0x6c1c('0x2e'));}}if(_[_0x6c1c('0x2c')](_0x5a1db9)){_0x5a1db9=moment()['add'](_0x44c541||0x3,_0x6c1c('0x2e'));}var _0x23a15a={'phone':_0x5e7897[_0x6c1c('0x30')],'active':0x0,'OrderBy':_[_0x6c1c('0x2c')](_0x5e7897[_0x6c1c('0x1e')])?0x0:_0x5e7897[_0x6c1c('0x1e')],'scheduledat':moment()[_0x6c1c('0x2f')](_0x212454||0x1,_0x6c1c('0x2e')),'countbusyretry':_0x177821[_0x6c1c('0x20')],'countcongestionretry':_0x177821['countcongestionretry'],'countnoanswerretry':_0x177821[_0x6c1c('0x22')],'countnosuchnumberretry':_0x177821[_0x6c1c('0x23')],'countdropretry':_0x177821[_0x6c1c('0x26')],'countabandonedretry':_0x177821[_0x6c1c('0x27')],'countmachineretry':_0x177821[_0x6c1c('0x31')],'countagentrejectretry':_0x177821[_0x6c1c('0x28')],'createdAt':_0x177821[_0x6c1c('0x32')],'updatedAt':_0x177821[_0x6c1c('0x33')]};var _0x3c9eaa={'CmHopperId':_0x5e7897['id'],'phone':_0xbb7e7[_0x6c1c('0x30')],'active':0x0,'OrderBy':_['isNil'](_0xbb7e7[_0x6c1c('0x1e')])?0x0:_0xbb7e7[_0x6c1c('0x1e')],'scheduledat':_0x5a1db9,'countbusyretry':_0xbb7e7[_0x6c1c('0x20')],'countcongestionretry':_0xbb7e7['countcongestionretry'],'countnoanswerretry':_0xbb7e7[_0x6c1c('0x22')],'countnosuchnumberretry':_0xbb7e7[_0x6c1c('0x23')],'countdropretry':_0xbb7e7[_0x6c1c('0x26')],'countabandonedretry':_0xbb7e7[_0x6c1c('0x27')],'countmachineretry':_0xbb7e7['countmachineretry'],'countagentrejectretry':_0xbb7e7['countagentrejectretry'],'createdAt':_0xbb7e7[_0x6c1c('0x32')],'updatedAt':_0xbb7e7[_0x6c1c('0x33')]};return db[_0x6c1c('0x34')][_0x6c1c('0x35')](function(_0x36a438){return db[_0x6c1c('0x1c')][_0x6c1c('0x36')](_0x23a15a,{'where':{'id':_0xbb7e7['id']}},{'transaction':_0x36a438})[_0x6c1c('0xb')](function(){return db[_0x6c1c('0x37')][_0x6c1c('0x36')](_0x3c9eaa,{'where':{'id':_0x5e7897['id']}},{'transaction':_0x36a438});});})[_0x6c1c('0xb')](function(_0x387dae){_0x10da18({'id':_0x5e7897['id'],'phone':_0xbb7e7[_0x6c1c('0x30')]});})[_0x6c1c('0x15')](function(_0x35f2f0){logger[_0x6c1c('0x11')]('activateAdditionalPhone',_0x35f2f0[_0x6c1c('0x14')]);_0x12181d({'error':_0x35f2f0[_0x6c1c('0x14')]});});});}exports[_0x6c1c('0x38')]=function(_0x2ef199){var _0x153939=this;return new Promise(function(_0x448572,_0x1c0ef6){return db[_0x6c1c('0x37')][_0x6c1c('0x36')](_0x2ef199[_0x6c1c('0x17')][_0x6c1c('0x39')],{'where':{'id':_0x2ef199['body']['id']}})[_0x6c1c('0xb')](function(_0x4a3f2a){_0x448572(_0x4a3f2a);})[_0x6c1c('0x15')](function(_0x24010b){logger[_0x6c1c('0x11')](_0x6c1c('0x38'),_0x24010b[_0x6c1c('0x14')]);_0x1c0ef6(_0x153939['error'](0x1f4,_0x24010b[_0x6c1c('0x14')]));});});};