6934f5004c00507da521f3e009cbd28033e4685e
[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 _0x004d=['IncrementPreviewCounters','field','util','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','client','then','info','request\x20sent','debug','CmHopperAdditionalPhone,\x20%s,\x20%s,\x20%s','error','code','CmHopperAdditionalPhone,\x20%s,\x20%s','message','result','catch','VerifyAdditionalPhones','CmHopper','body','ContactId','VoiceQueueId','CampaignId','CmHopperAdditionalPhone','findOne','OrderBy','countbusyretry','countcongestionretry','countnoanswerretry','countnosuchnumberretry','countdropretry','countabandonedretry','countagentrejectretry','entity','dialGlobalMaxRetry','SwapAdditionalPhones','duration','waitTime','ASC','isNil','scheduledat','add','minutes','phone','countmachineretry','createdAt','updatedAt','sequelize','update','activateAdditionalPhone'];(function(_0x321e72,_0x23dfcc){var _0xb956bd=function(_0x39e18e){while(--_0x39e18e){_0x321e72['push'](_0x321e72['shift']());}};_0xb956bd(++_0x23dfcc);}(_0x004d,0x183));var _0xd004=function(_0x22081d,_0x279080){_0x22081d=_0x22081d-0x0;var _0x555642=_0x004d[_0x22081d];return _0x555642;};'use strict';var _=require('lodash');var util=require(_0xd004('0x0'));var moment=require('moment');var BPromise=require(_0xd004('0x1'));var rs=require(_0xd004('0x2'));var fs=require('fs');var Redis=require(_0xd004('0x3'));var db=require(_0xd004('0x4'))['db'];var utils=require(_0xd004('0x5'));var logger=require('../../config/logger')(_0xd004('0x6'));var config=require(_0xd004('0x7'));var jayson=require('jayson/promise');var client=jayson[_0xd004('0x8')]['http']({'port':0x232a});function respondWithRpcPromise(_0x5825b2,_0x243c8b,_0x582262){return new BPromise(function(_0x1f9d98,_0x10d51e){return client['request'](_0x5825b2,_0x582262)[_0xd004('0x9')](function(_0x296746){logger[_0xd004('0xa')]('CmHopperAdditionalPhone,\x20%s,\x20%s',_0x243c8b,_0xd004('0xb'));logger[_0xd004('0xc')](_0xd004('0xd'),_0x243c8b,_0xd004('0xb'),JSON['stringify'](_0x296746));if(_0x296746[_0xd004('0xe')]){if(_0x296746[_0xd004('0xe')][_0xd004('0xf')]===0x1f4){logger[_0xd004('0xe')](_0xd004('0x10'),_0x243c8b,_0x296746['error']['message']);return _0x10d51e(_0x296746[_0xd004('0xe')][_0xd004('0x11')]);}logger[_0xd004('0xe')](_0xd004('0x10'),_0x243c8b,_0x296746[_0xd004('0xe')][_0xd004('0x11')]);return _0x1f9d98(_0x296746[_0xd004('0xe')][_0xd004('0x11')]);}else{logger[_0xd004('0xa')](_0xd004('0x10'),_0x243c8b,_0xd004('0xb'));_0x1f9d98(_0x296746[_0xd004('0x12')]['message']);}})[_0xd004('0x13')](function(_0x21fc25){logger[_0xd004('0xe')](_0xd004('0x10'),_0x243c8b,_0x21fc25);_0x10d51e(_0x21fc25);});});}exports[_0xd004('0x14')]=function(_0x10383e){var _0x2281d1=this;return new Promise(function(_0x17b48e,_0x3c4a46){return db[_0xd004('0x15')]['findOne']({'raw':!![],'where':{'ContactId':_0x10383e[_0xd004('0x16')][_0xd004('0x17')],'ListId':_0x10383e['body']['ListId'],'VoiceQueueId':_0x10383e[_0xd004('0x16')][_0xd004('0x18')]===undefined?null:_0x10383e[_0xd004('0x16')][_0xd004('0x18')],'CampaignId':_0x10383e[_0xd004('0x16')]['CampaignId']===undefined?null:_0x10383e[_0xd004('0x16')][_0xd004('0x19')]}})['then'](function(_0x1e1443){if(_0x1e1443!=null){return db[_0xd004('0x1a')][_0xd004('0x1b')]({'raw':!![],'where':{'CmHopperId':_0x1e1443['id'],'OrderBy':{'$gt':_0x1e1443[_0xd004('0x1c')]}},'order':[[_0xd004('0x1c'),'ASC']]})['then'](function(_0x495515){var _0x3142bc=0x0;if(_0x495515){if(_0x495515[_0xd004('0x1d')]+_0x495515[_0xd004('0x1e')]+_0x495515[_0xd004('0x1f')]+_0x495515[_0xd004('0x20')]+_0x495515[_0xd004('0x21')]+_0x495515[_0xd004('0x22')]+_0x495515['countmachineretry']+_0x495515[_0xd004('0x23')]<_0x10383e[_0xd004('0x24')][_0xd004('0x25')])_0x3142bc=0x1;_0x17b48e({'additional':_0x3142bc});}else{return db[_0xd004('0x1a')][_0xd004('0x1b')]({'raw':!![],'where':{'CmHopperId':_0x1e1443['id']},'order':[[_0xd004('0x1c'),'ASC']]})[_0xd004('0x9')](function(_0x1a682){var _0xf3ad94=0x0;if(_0x1a682){if(_0x1a682[_0xd004('0x1d')]+_0x1a682['countcongestionretry']+_0x1a682['countnoanswerretry']+_0x1a682[_0xd004('0x20')]+_0x1a682[_0xd004('0x21')]+_0x1a682['countabandonedretry']+_0x1a682['countmachineretry']+_0x1a682['countagentrejectretry']<_0x10383e['entity']['dialGlobalMaxRetry'])_0xf3ad94=0x1;_0x17b48e({'additional':_0xf3ad94});}else{_0x17b48e({'additional':0x0});}})[_0xd004('0x13')](function(_0x1e5ed7){logger[_0xd004('0xe')](_0xd004('0x14'),_0x1e5ed7['message']);_0x3c4a46(_0x2281d1[_0xd004('0xe')](0x1f4,_0x1e5ed7[_0xd004('0x11')]));});}})[_0xd004('0x13')](function(_0x60bf28){logger[_0xd004('0xe')]('VerifyAdditionalPhones',_0x60bf28['message']);_0x3c4a46(_0x2281d1['error'](0x1f4,_0x60bf28['message']));});}else _0x17b48e({'additional':0x0});});});};exports[_0xd004('0x26')]=function(_0x2443b7){var _0x2d5ac1=this;return new Promise(function(_0x23c520,_0xcfef84){return db[_0xd004('0x15')][_0xd004('0x1b')]({'raw':!![],'where':{'ContactId':_0x2443b7['body']['ContactId'],'ListId':_0x2443b7[_0xd004('0x16')]['ListId'],'VoiceQueueId':_0x2443b7[_0xd004('0x16')][_0xd004('0x18')]===undefined?null:_0x2443b7['body']['VoiceQueueId'],'CampaignId':_0x2443b7['body']['CampaignId']===undefined?null:_0x2443b7[_0xd004('0x16')][_0xd004('0x19')]}})[_0xd004('0x9')](function(_0x52ea61){if(_0x52ea61!=null){return db[_0xd004('0x1a')][_0xd004('0x1b')]({'raw':!![],'where':{'CmHopperId':_0x52ea61['id'],'OrderBy':{'$gte':_0x52ea61[_0xd004('0x1c')]}},'order':[[_0xd004('0x1c'),'ASC']]})[_0xd004('0x9')](function(_0x12f048){if(_0x12f048)return activateAdditionalPhone(_0x52ea61,_0x2443b7[_0xd004('0x16')],_0x12f048,_0x2443b7[_0xd004('0x27')],_0x2443b7[_0xd004('0x28')])[_0xd004('0x9')](function(_0x5ccedc){_0x23c520(_0x5ccedc);});else{return db['CmHopperAdditionalPhone'][_0xd004('0x1b')]({'raw':!![],'where':{'CmHopperId':_0x52ea61['id']},'order':[[_0xd004('0x1c'),_0xd004('0x29')]]})['then'](function(_0x4dc20e){if(_0x4dc20e)return activateAdditionalPhone(_0x52ea61,_0x2443b7[_0xd004('0x16')],_0x4dc20e,_0x2443b7['duration'],_0x2443b7[_0xd004('0x28')])[_0xd004('0x9')](function(_0x183cd0){_0x23c520(_0x183cd0);});else{_0x23c520({'id':_0x52ea61['id'],'phone':''});}})['catch'](function(_0x4bb7ff){logger[_0xd004('0xe')](_0xd004('0x14'),_0x4bb7ff[_0xd004('0x11')]);_0xcfef84(_0x2d5ac1[_0xd004('0xe')](0x1f4,_0x4bb7ff[_0xd004('0x11')]));});}})['catch'](function(_0x41b7c9){logger[_0xd004('0xe')](_0xd004('0x14'),_0x41b7c9[_0xd004('0x11')]);_0xcfef84(_0x2d5ac1[_0xd004('0xe')](0x1f4,_0x41b7c9[_0xd004('0x11')]));});}else _0x23c520(0x1);});});};function activateAdditionalPhone(_0x13f33f,_0xc12836,_0x2f467b,_0x63d239,_0x5bba30){return new Promise(function(_0x57ee2e,_0x2cdcc6){var _0x5da4d0;if(_[_0xd004('0x2a')](_0x2f467b['scheduledat'])){_0x2f467b[_0xd004('0x2b')]=moment();}if(moment()>moment(_0x2f467b[_0xd004('0x2b')])){_0x5da4d0=moment()[_0xd004('0x2c')](_0x5bba30||0x3,'minutes');}else{if(_0x2f467b['OrderBy']==0x0){_0x5da4d0=moment(_0x2f467b[_0xd004('0x2b')]);}else{_0x5da4d0=moment()[_0xd004('0x2c')](_0x5bba30||0x3,_0xd004('0x2d'));}}if(_[_0xd004('0x2a')](_0x5da4d0)){_0x5da4d0=moment()[_0xd004('0x2c')](_0x5bba30||0x3,_0xd004('0x2d'));}var _0x270df6={'phone':_0x13f33f[_0xd004('0x2e')],'active':0x0,'OrderBy':_[_0xd004('0x2a')](_0x13f33f[_0xd004('0x1c')])?0x0:_0x13f33f['OrderBy'],'scheduledat':moment()[_0xd004('0x2c')](_0x63d239||0x1,_0xd004('0x2d')),'countbusyretry':_0xc12836[_0xd004('0x1d')],'countcongestionretry':_0xc12836[_0xd004('0x1e')],'countnoanswerretry':_0xc12836[_0xd004('0x1f')],'countnosuchnumberretry':_0xc12836['countnosuchnumberretry'],'countdropretry':_0xc12836[_0xd004('0x21')],'countabandonedretry':_0xc12836[_0xd004('0x22')],'countmachineretry':_0xc12836[_0xd004('0x2f')],'countagentrejectretry':_0xc12836[_0xd004('0x23')],'createdAt':_0xc12836[_0xd004('0x30')],'updatedAt':_0xc12836[_0xd004('0x31')]};var _0x4966c5={'CmHopperId':_0x13f33f['id'],'phone':_0x2f467b['phone'],'active':0x0,'OrderBy':_[_0xd004('0x2a')](_0x2f467b[_0xd004('0x1c')])?0x0:_0x2f467b[_0xd004('0x1c')],'scheduledat':_0x5da4d0,'countbusyretry':_0x2f467b[_0xd004('0x1d')],'countcongestionretry':_0x2f467b[_0xd004('0x1e')],'countnoanswerretry':_0x2f467b['countnoanswerretry'],'countnosuchnumberretry':_0x2f467b[_0xd004('0x20')],'countdropretry':_0x2f467b[_0xd004('0x21')],'countabandonedretry':_0x2f467b[_0xd004('0x22')],'countmachineretry':_0x2f467b[_0xd004('0x2f')],'countagentrejectretry':_0x2f467b[_0xd004('0x23')],'createdAt':_0x2f467b[_0xd004('0x30')],'updatedAt':_0x2f467b[_0xd004('0x31')]};return db[_0xd004('0x32')]['transaction'](function(_0x530a39){return db[_0xd004('0x1a')][_0xd004('0x33')](_0x270df6,{'where':{'id':_0x2f467b['id']}},{'transaction':_0x530a39})[_0xd004('0x9')](function(){return db[_0xd004('0x15')][_0xd004('0x33')](_0x4966c5,{'where':{'id':_0x13f33f['id']}},{'transaction':_0x530a39});});})[_0xd004('0x9')](function(_0x2dd774){_0x57ee2e({'id':_0x13f33f['id'],'phone':_0x2f467b[_0xd004('0x2e')]});})['catch'](function(_0x43efab){logger[_0xd004('0xe')](_0xd004('0x34'),_0x43efab[_0xd004('0x11')]);_0x2cdcc6({'error':_0x43efab[_0xd004('0x11')]});});});}exports[_0xd004('0x35')]=function(_0x488b49){var _0x19cfef=this;return new Promise(function(_0x143000,_0x5bdc6f){return db['CmHopper']['update'](_0x488b49[_0xd004('0x16')][_0xd004('0x36')],{'where':{'id':_0x488b49['body']['id']}})[_0xd004('0x9')](function(_0x3d8b5c){_0x143000(_0x3d8b5c);})[_0xd004('0x13')](function(_0x3af3b4){logger['error'](_0xd004('0x35'),_0x3af3b4['message']);_0x5bdc6f(_0x19cfef[_0xd004('0xe')](0x1f4,_0x3af3b4[_0xd004('0x11')]));});});};