Built motion from commit b0a9a629.|2.6.33
[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 _0xe5d6=['add','minutes','isNil','phone','createdAt','updatedAt','update','activateAdditionalPhone','IncrementPreviewCounters','lodash','util','moment','bluebird','randomstring','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','request','CmHopperAdditionalPhone,\x20%s,\x20%s','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','stringify','error','code','message','info','result','catch','VerifyAdditionalPhones','CmHopper','body','ListId','VoiceQueueId','CampaignId','then','OrderBy','ASC','countbusyretry','countnoanswerretry','countnosuchnumberretry','countdropretry','countabandonedretry','countmachineretry','countagentrejectretry','entity','dialGlobalMaxRetry','CmHopperAdditionalPhone','countcongestionretry','findOne','ContactId','waitTime','duration','scheduledat'];(function(_0x157df5,_0x33f022){var _0x4972ed=function(_0x4d6792){while(--_0x4d6792){_0x157df5['push'](_0x157df5['shift']());}};_0x4972ed(++_0x33f022);}(_0xe5d6,0xba));var _0x6e5d=function(_0x5bd426,_0x4878e5){_0x5bd426=_0x5bd426-0x0;var _0x23a7eb=_0xe5d6[_0x5bd426];return _0x23a7eb;};'use strict';var _=require(_0x6e5d('0x0'));var util=require(_0x6e5d('0x1'));var moment=require(_0x6e5d('0x2'));var BPromise=require(_0x6e5d('0x3'));var rs=require(_0x6e5d('0x4'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0x6e5d('0x5'))['db'];var utils=require(_0x6e5d('0x6'));var logger=require(_0x6e5d('0x7'))(_0x6e5d('0x8'));var config=require(_0x6e5d('0x9'));var jayson=require(_0x6e5d('0xa'));var client=jayson[_0x6e5d('0xb')][_0x6e5d('0xc')]({'port':0x232a});function respondWithRpcPromise(_0x18f7b4,_0x25152b,_0x264e29){return new BPromise(function(_0x52387b,_0x5b8f3c){return client[_0x6e5d('0xd')](_0x18f7b4,_0x264e29)['then'](function(_0x28ac0a){logger['info'](_0x6e5d('0xe'),_0x25152b,_0x6e5d('0xf'));logger[_0x6e5d('0x10')](_0x6e5d('0x11'),_0x25152b,_0x6e5d('0xf'),JSON[_0x6e5d('0x12')](_0x28ac0a));if(_0x28ac0a['error']){if(_0x28ac0a[_0x6e5d('0x13')][_0x6e5d('0x14')]===0x1f4){logger[_0x6e5d('0x13')](_0x6e5d('0xe'),_0x25152b,_0x28ac0a[_0x6e5d('0x13')][_0x6e5d('0x15')]);return _0x5b8f3c(_0x28ac0a[_0x6e5d('0x13')]['message']);}logger[_0x6e5d('0x13')](_0x6e5d('0xe'),_0x25152b,_0x28ac0a[_0x6e5d('0x13')]['message']);return _0x52387b(_0x28ac0a[_0x6e5d('0x13')][_0x6e5d('0x15')]);}else{logger[_0x6e5d('0x16')](_0x6e5d('0xe'),_0x25152b,'request\x20sent');_0x52387b(_0x28ac0a[_0x6e5d('0x17')][_0x6e5d('0x15')]);}})[_0x6e5d('0x18')](function(_0x3e4745){logger['error'](_0x6e5d('0xe'),_0x25152b,_0x3e4745);_0x5b8f3c(_0x3e4745);});});}exports[_0x6e5d('0x19')]=function(_0x3b11ad){var _0x1a8b08=this;return new Promise(function(_0x71f6be,_0x4606f3){return db[_0x6e5d('0x1a')]['findOne']({'raw':!![],'where':{'ContactId':_0x3b11ad[_0x6e5d('0x1b')]['ContactId'],'ListId':_0x3b11ad[_0x6e5d('0x1b')][_0x6e5d('0x1c')],'VoiceQueueId':_0x3b11ad['body'][_0x6e5d('0x1d')]===undefined?null:_0x3b11ad[_0x6e5d('0x1b')]['VoiceQueueId'],'CampaignId':_0x3b11ad[_0x6e5d('0x1b')][_0x6e5d('0x1e')]===undefined?null:_0x3b11ad['body'][_0x6e5d('0x1e')]}})[_0x6e5d('0x1f')](function(_0x4a9c5f){if(_0x4a9c5f!=null){return db['CmHopperAdditionalPhone']['findOne']({'raw':!![],'where':{'CmHopperId':_0x4a9c5f['id'],'OrderBy':{'$gt':_0x4a9c5f[_0x6e5d('0x20')]}},'order':[[_0x6e5d('0x20'),_0x6e5d('0x21')]]})[_0x6e5d('0x1f')](function(_0xc1c432){var _0x323bc4=0x0;if(_0xc1c432){if(_0xc1c432[_0x6e5d('0x22')]+_0xc1c432['countcongestionretry']+_0xc1c432[_0x6e5d('0x23')]+_0xc1c432[_0x6e5d('0x24')]+_0xc1c432[_0x6e5d('0x25')]+_0xc1c432[_0x6e5d('0x26')]+_0xc1c432[_0x6e5d('0x27')]+_0xc1c432[_0x6e5d('0x28')]<_0x3b11ad[_0x6e5d('0x29')][_0x6e5d('0x2a')])_0x323bc4=0x1;_0x71f6be({'additional':_0x323bc4});}else{return db[_0x6e5d('0x2b')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x4a9c5f['id']},'order':[[_0x6e5d('0x20'),_0x6e5d('0x21')]]})[_0x6e5d('0x1f')](function(_0x5b5225){var _0x3c98ba=0x0;if(_0x5b5225){if(_0x5b5225['countbusyretry']+_0x5b5225[_0x6e5d('0x2c')]+_0x5b5225[_0x6e5d('0x23')]+_0x5b5225['countnosuchnumberretry']+_0x5b5225[_0x6e5d('0x25')]+_0x5b5225[_0x6e5d('0x26')]+_0x5b5225[_0x6e5d('0x27')]+_0x5b5225[_0x6e5d('0x28')]<_0x3b11ad[_0x6e5d('0x29')]['dialGlobalMaxRetry'])_0x3c98ba=0x1;_0x71f6be({'additional':_0x3c98ba});}else{_0x71f6be({'additional':0x0});}})[_0x6e5d('0x18')](function(_0x5cb6db){logger[_0x6e5d('0x13')](_0x6e5d('0x19'),_0x5cb6db[_0x6e5d('0x15')]);_0x4606f3(_0x1a8b08[_0x6e5d('0x13')](0x1f4,_0x5cb6db['message']));});}})['catch'](function(_0x6c3d49){logger[_0x6e5d('0x13')](_0x6e5d('0x19'),_0x6c3d49[_0x6e5d('0x15')]);_0x4606f3(_0x1a8b08[_0x6e5d('0x13')](0x1f4,_0x6c3d49[_0x6e5d('0x15')]));});}else _0x71f6be({'additional':0x0});});});};exports['SwapAdditionalPhones']=function(_0x47df1c){var _0x34ce1e=this;return new Promise(function(_0x54fe34,_0x36e8df){return db[_0x6e5d('0x1a')][_0x6e5d('0x2d')]({'raw':!![],'where':{'ContactId':_0x47df1c[_0x6e5d('0x1b')][_0x6e5d('0x2e')],'ListId':_0x47df1c[_0x6e5d('0x1b')][_0x6e5d('0x1c')],'VoiceQueueId':_0x47df1c[_0x6e5d('0x1b')]['VoiceQueueId']===undefined?null:_0x47df1c[_0x6e5d('0x1b')][_0x6e5d('0x1d')],'CampaignId':_0x47df1c['body'][_0x6e5d('0x1e')]===undefined?null:_0x47df1c['body'][_0x6e5d('0x1e')]}})[_0x6e5d('0x1f')](function(_0x46b431){if(_0x46b431!=null){return db[_0x6e5d('0x2b')][_0x6e5d('0x2d')]({'raw':!![],'where':{'CmHopperId':_0x46b431['id'],'OrderBy':{'$gte':_0x46b431[_0x6e5d('0x20')]}},'order':[[_0x6e5d('0x20'),_0x6e5d('0x21')]]})['then'](function(_0x55a7b6){if(_0x55a7b6)return activateAdditionalPhone(_0x46b431,_0x47df1c['body'],_0x55a7b6,_0x47df1c['duration'],_0x47df1c[_0x6e5d('0x2f')])[_0x6e5d('0x1f')](function(_0xb4be9c){_0x54fe34(_0xb4be9c);});else{return db[_0x6e5d('0x2b')]['findOne']({'raw':!![],'where':{'CmHopperId':_0x46b431['id']},'order':[['OrderBy',_0x6e5d('0x21')]]})[_0x6e5d('0x1f')](function(_0x315c0c){if(_0x315c0c)return activateAdditionalPhone(_0x46b431,_0x47df1c[_0x6e5d('0x1b')],_0x315c0c,_0x47df1c[_0x6e5d('0x30')],_0x47df1c[_0x6e5d('0x2f')])[_0x6e5d('0x1f')](function(_0x393fae){_0x54fe34(_0x393fae);});else{_0x54fe34({'id':_0x46b431['id'],'phone':''});}})[_0x6e5d('0x18')](function(_0x20aa81){logger[_0x6e5d('0x13')](_0x6e5d('0x19'),_0x20aa81[_0x6e5d('0x15')]);_0x36e8df(_0x34ce1e[_0x6e5d('0x13')](0x1f4,_0x20aa81[_0x6e5d('0x15')]));});}})['catch'](function(_0x545d75){logger[_0x6e5d('0x13')](_0x6e5d('0x19'),_0x545d75[_0x6e5d('0x15')]);_0x36e8df(_0x34ce1e[_0x6e5d('0x13')](0x1f4,_0x545d75[_0x6e5d('0x15')]));});}else _0x54fe34(0x1);});});};function activateAdditionalPhone(_0x1da82a,_0x65fb1c,_0x412b8f,_0x15ff06,_0x2ed923){return new Promise(function(_0x3c1214,_0x4b844d){var _0x212a96;if(_['isNil'](_0x412b8f['scheduledat'])){_0x412b8f[_0x6e5d('0x31')]=moment();}if(moment()>moment(_0x412b8f[_0x6e5d('0x31')])){_0x212a96=moment()[_0x6e5d('0x32')](_0x2ed923||0x3,_0x6e5d('0x33'));}else{if(_0x412b8f['OrderBy']==0x0){_0x212a96=moment(_0x412b8f['scheduledat']);}else{_0x212a96=moment()['add'](_0x2ed923||0x3,_0x6e5d('0x33'));}}if(_[_0x6e5d('0x34')](_0x212a96)){_0x212a96=moment()[_0x6e5d('0x32')](_0x2ed923||0x3,_0x6e5d('0x33'));}var _0x50e05a={'phone':_0x1da82a[_0x6e5d('0x35')],'active':0x0,'OrderBy':_[_0x6e5d('0x34')](_0x1da82a[_0x6e5d('0x20')])?0x0:_0x1da82a[_0x6e5d('0x20')],'scheduledat':moment()[_0x6e5d('0x32')](_0x15ff06||0x1,'minutes'),'countbusyretry':_0x65fb1c['countbusyretry'],'countcongestionretry':_0x65fb1c['countcongestionretry'],'countnoanswerretry':_0x65fb1c[_0x6e5d('0x23')],'countnosuchnumberretry':_0x65fb1c[_0x6e5d('0x24')],'countdropretry':_0x65fb1c[_0x6e5d('0x25')],'countabandonedretry':_0x65fb1c[_0x6e5d('0x26')],'countmachineretry':_0x65fb1c[_0x6e5d('0x27')],'countagentrejectretry':_0x65fb1c[_0x6e5d('0x28')],'createdAt':_0x65fb1c[_0x6e5d('0x36')],'updatedAt':_0x65fb1c[_0x6e5d('0x37')]};var _0x6acd34={'CmHopperId':_0x1da82a['id'],'phone':_0x412b8f[_0x6e5d('0x35')],'active':0x0,'OrderBy':_[_0x6e5d('0x34')](_0x412b8f['OrderBy'])?0x0:_0x412b8f[_0x6e5d('0x20')],'scheduledat':_0x212a96,'countbusyretry':_0x412b8f[_0x6e5d('0x22')],'countcongestionretry':_0x412b8f[_0x6e5d('0x2c')],'countnoanswerretry':_0x412b8f[_0x6e5d('0x23')],'countnosuchnumberretry':_0x412b8f['countnosuchnumberretry'],'countdropretry':_0x412b8f[_0x6e5d('0x25')],'countabandonedretry':_0x412b8f[_0x6e5d('0x26')],'countmachineretry':_0x412b8f[_0x6e5d('0x27')],'countagentrejectretry':_0x412b8f[_0x6e5d('0x28')],'createdAt':_0x412b8f['createdAt'],'updatedAt':_0x412b8f['updatedAt']};return db['sequelize']['transaction'](function(_0xc66dba){return db[_0x6e5d('0x2b')][_0x6e5d('0x38')](_0x50e05a,{'where':{'id':_0x412b8f['id']}},{'transaction':_0xc66dba})['then'](function(){return db[_0x6e5d('0x1a')][_0x6e5d('0x38')](_0x6acd34,{'where':{'id':_0x1da82a['id']}},{'transaction':_0xc66dba});});})[_0x6e5d('0x1f')](function(_0x4339b2){_0x3c1214({'id':_0x1da82a['id'],'phone':_0x412b8f[_0x6e5d('0x35')]});})['catch'](function(_0x3aa8e3){logger[_0x6e5d('0x13')](_0x6e5d('0x39'),_0x3aa8e3[_0x6e5d('0x15')]);_0x4b844d({'error':_0x3aa8e3[_0x6e5d('0x15')]});});});}exports[_0x6e5d('0x3a')]=function(_0x21f7fd){var _0x6fc33=this;return new Promise(function(_0x26e82e,_0x118d70){return db[_0x6e5d('0x1a')]['update'](_0x21f7fd[_0x6e5d('0x1b')]['field'],{'where':{'id':_0x21f7fd[_0x6e5d('0x1b')]['id']}})[_0x6e5d('0x1f')](function(_0x16cd63){_0x26e82e(_0x16cd63);})['catch'](function(_0x64b79){logger[_0x6e5d('0x13')](_0x6e5d('0x3a'),_0x64b79[_0x6e5d('0x15')]);_0x118d70(_0x6fc33['error'](0x1f4,_0x64b79[_0x6e5d('0x15')]));});});};