Built motion from commit ff78e754.|2.6.29
[motion2.git] / server / api / webbar / webbar.controller.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 _0x74f0=['callbackUrl','entityFound:','user:%s','json','originate\x20executed!','catch','hangup','hangup\x20fired:','hangup\x20executed!','answer','answer\x20fired:','format','webbar:answer','hold','webbar:hold','unhold','unhold\x20fired:','webbar:unhold','unhold\x20executed!','calls','calls\x20fired:','rows','conference','conference\x20executed!','transfer','webbar:transfer','transfer\x20executed!','lodash','util','bluebird','jayson/promise','client','webbarapi','ioredis','redis','defaults','localhost','socket.io-emitter','validate','number','string','callNumber\x20is\x20required','userId\x20is\x20required','sessionId\x20is\x20required','transferNumber\x20is\x20required','info','emit','log','handleError:','error','name','status','send','NotFound','request','then','code','message','User','findOne','all','filter','UserId','sessionId','length','\x20not\x20found!','originate','originate\x20fired:','body','userId','callNumber','callerId'];(function(_0x37177f,_0x509f2c){var _0x14d9b8=function(_0x785371){while(--_0x785371){_0x37177f['push'](_0x37177f['shift']());}};_0x14d9b8(++_0x509f2c);}(_0x74f0,0xf3));var _0x074f=function(_0x399d76,_0xdc4493){_0x399d76=_0x399d76-0x0;var _0x3b19a1=_0x74f0[_0x399d76];return _0x3b19a1;};'use strict';var path=require('path');var fs=require('fs');var _=require(_0x074f('0x0'));var db=require('../../mysqldb')['db'];var config=require('../../config/environment');var util=require(_0x074f('0x1'));var BPromise=require(_0x074f('0x2'));var jayson=require(_0x074f('0x3'));var amiClient=jayson[_0x074f('0x4')]['http']({'port':0x232a});var logger=require('../../config/logger')(_0x074f('0x5'));var Redis=require(_0x074f('0x6'));config[_0x074f('0x7')]=_[_0x074f('0x8')](config[_0x074f('0x7')],{'host':_0x074f('0x9'),'port':0x18eb});var io=require(_0x074f('0xa'))(new Redis(config['redis']));var Schema=require(_0x074f('0xb'));var originateValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],'userId\x20is\x20required']},'callNumber':{'type':_0x074f('0xd'),'required':[!![],_0x074f('0xe')]},'callerId':{'type':_0x074f('0xd')},'callbackUrl':{'type':_0x074f('0xd')}});var hangupValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],'userId\x20is\x20required']},'sessionId':{'type':'string','required':![]}});var answerValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],_0x074f('0xf')]},'sessionId':{'type':_0x074f('0xd'),'required':![]}});var transferValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],_0x074f('0xf')]},'sessionId':{'type':_0x074f('0xd'),'required':[!![],_0x074f('0x10')]},'transferNumber':{'type':_0x074f('0xd'),'required':[!![],_0x074f('0x11')]}});var holdValidation=new Schema({'userId':{'type':'number','required':[!![],_0x074f('0xf')]},'sessionId':{'type':'string','required':![]}});var unholdValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],_0x074f('0xf')]},'sessionId':{'type':_0x074f('0xd'),'required':![]}});var callsValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],_0x074f('0xf')]}});var conferenceValidation=new Schema({'userId':{'type':_0x074f('0xc'),'required':[!![],_0x074f('0xf')]}});function emit(_0x1edf9c,_0x3f1a47,_0x428987){logger[_0x074f('0x12')](_0x1edf9c,_0x3f1a47,JSON['stringify'](_0x428987));io['to'](_0x1edf9c)[_0x074f('0x13')](_0x3f1a47,_0x428987);}function handleError(_0x367b52){return function(_0x200e43){console[_0x074f('0x14')](_0x074f('0x15'),_0x200e43,_0x200e43 instanceof RangeError);logger[_0x074f('0x16')](_0x200e43['stack']);if(_0x200e43 instanceof RangeError){if(_0x200e43['name']){delete _0x200e43[_0x074f('0x17')];}_0x367b52['status'](0x194)['send'](_0x200e43);}else{if(_0x200e43[_0x074f('0x17')]){delete _0x200e43[_0x074f('0x17')];}_0x367b52[_0x074f('0x18')](0x1f4)[_0x074f('0x19')](_0x200e43);}};}function checkError(){return function(_0xfa07ea){if(!_0xfa07ea){throw new RangeError(_0x074f('0x1a'));}return _0xfa07ea;};}function respondWithRpcPromise(_0x23406f,_0x3ecb4b){return new BPromise(function(_0x1acf0a,_0x2c3b23){var _0x335170=amiClient;return _0x335170[_0x074f('0x1b')](_0x23406f,_0x3ecb4b)[_0x074f('0x1c')](function(_0x2fc5b0){if(_0x2fc5b0[_0x074f('0x16')]){if(_0x2fc5b0[_0x074f('0x16')][_0x074f('0x1d')]===0x1f4){return _0x2c3b23(_0x2fc5b0[_0x074f('0x16')][_0x074f('0x1e')]);}return _0x1acf0a(_0x2fc5b0[_0x074f('0x16')]['message']);}else{_0x1acf0a(_0x2fc5b0['result']);}})['catch'](function(_0x1051ce){_0x2c3b23(_0x1051ce);});});}function getAgent(_0x22ce4f){return db[_0x074f('0x1f')][_0x074f('0x20')]({'where':{'id':_0x22ce4f,'role':'agent'}})[_0x074f('0x1c')](checkError());}function getAgentCheckSession(_0x339370,_0x3fb202){return new BPromise(function(_0x312119,_0x470acc){var _0xb9722a=respondWithRpcPromise('VoiceChannels');var _0x1bf574=getAgent(_0x339370);Promise[_0x074f('0x21')]([_0xb9722a,_0x1bf574])[_0x074f('0x1c')](function(_0x53b76d){var _0x1ee652=_0x53b76d[0x0];var _0x5d6edd=_0x53b76d[0x1];var _0x4fcfb5=_[_0x074f('0x22')](_0x1ee652['rows'],function(_0x4aa96f){return _0x4aa96f[_0x074f('0x23')]==_0x5d6edd['id']&&_0x4aa96f[_0x074f('0x24')]==_0x3fb202;});if(_0x4fcfb5[_0x074f('0x25')]==0x1){_0x312119(_0x5d6edd);}else{_0x470acc(new RangeError('sessionId\x20'+_0x3fb202+_0x074f('0x26')));}});});}exports[_0x074f('0x27')]=function(_0x269df3,_0x50314d){logger[_0x074f('0x12')](_0x074f('0x28'),_0x269df3[_0x074f('0x29')]);var _0x41ccb4=originateValidation[_0x074f('0xb')](_0x269df3['body']);if(_0x41ccb4[_0x074f('0x25')]>0x0){return _0x50314d[_0x074f('0x18')](0x190)['json'](_0x41ccb4);}var _0x16ff4c=_0x269df3[_0x074f('0x29')][_0x074f('0x2a')];var _0x498288=_0x269df3[_0x074f('0x29')][_0x074f('0x2b')];var _0x493cc3=_0x269df3[_0x074f('0x29')][_0x074f('0x2c')];var _0x3407bd=_0x269df3[_0x074f('0x29')][_0x074f('0x2d')];return getAgent(_0x16ff4c)[_0x074f('0x1c')](function(_0x283211){logger[_0x074f('0x12')](_0x074f('0x2e'),_0x283211[_0x074f('0x17')]);emit(util['format'](_0x074f('0x2f'),_0x283211[_0x074f('0x17')]),'webbar:originate',{'callNumber':_0x498288,'callerId':_0x493cc3,'callbackUrl':_0x3407bd});return _0x50314d[_0x074f('0x18')](0xc8)[_0x074f('0x30')]({'error':![],'message':_0x074f('0x31')});})[_0x074f('0x32')](handleError(_0x50314d));};exports[_0x074f('0x33')]=function(_0x380c0e,_0x29d5c7){logger['info'](_0x074f('0x34'),_0x380c0e[_0x074f('0x29')]);var _0x251532=hangupValidation[_0x074f('0xb')](_0x380c0e[_0x074f('0x29')]);if(_0x251532[_0x074f('0x25')]>0x0){return _0x29d5c7['status'](0x190)[_0x074f('0x30')](_0x251532);}var _0x19866e=_0x380c0e['body'][_0x074f('0x2a')];var _0x41b1f2=_0x380c0e[_0x074f('0x29')]['sessionId'];return getAgent(_0x19866e)['then'](function(_0x5f39e5){logger[_0x074f('0x12')](_0x074f('0x2e'),_0x5f39e5[_0x074f('0x17')]);emit(util['format'](_0x074f('0x2f'),_0x5f39e5[_0x074f('0x17')]),'webbar:hangup',{'sessionId':_0x41b1f2});return _0x29d5c7[_0x074f('0x18')](0xc8)[_0x074f('0x30')]({'error':![],'message':_0x074f('0x35')});})[_0x074f('0x32')](handleError(_0x29d5c7));};exports[_0x074f('0x36')]=function(_0x42de45,_0x40dac1){logger[_0x074f('0x12')](_0x074f('0x37'),_0x42de45[_0x074f('0x29')]);var _0x58ff4d=answerValidation[_0x074f('0xb')](_0x42de45['body']);if(_0x58ff4d[_0x074f('0x25')]>0x0){return _0x40dac1['status'](0x190)[_0x074f('0x30')](_0x58ff4d);}var _0x4c59ec=_0x42de45[_0x074f('0x29')]['userId'];var _0x654e6e=_0x42de45[_0x074f('0x29')][_0x074f('0x24')];return getAgent(_0x4c59ec)[_0x074f('0x1c')](function(_0xe20f2b){logger[_0x074f('0x12')](_0x074f('0x2e'),_0xe20f2b['name']);emit(util[_0x074f('0x38')](_0x074f('0x2f'),_0xe20f2b[_0x074f('0x17')]),_0x074f('0x39'),{'sessionId':_0x654e6e});return _0x40dac1['status'](0xc8)[_0x074f('0x30')]({'error':![],'message':'answer\x20executed!'});})[_0x074f('0x32')](handleError(_0x40dac1));};exports[_0x074f('0x3a')]=function(_0x324ca3,_0x5ebf87){logger[_0x074f('0x12')]('hold\x20fired:',_0x324ca3[_0x074f('0x29')]);var _0x2ed08a=holdValidation['validate'](_0x324ca3[_0x074f('0x29')]);if(_0x2ed08a['length']>0x0){return _0x5ebf87[_0x074f('0x18')](0x190)[_0x074f('0x30')](_0x2ed08a);}var _0x598fee=_0x324ca3['body']['userId'];var _0x4d5731=_0x324ca3[_0x074f('0x29')][_0x074f('0x24')];return getAgent(_0x598fee)[_0x074f('0x1c')](function(_0xdd3709){logger['info'](_0x074f('0x2e'),_0xdd3709[_0x074f('0x17')]);emit(util[_0x074f('0x38')](_0x074f('0x2f'),_0xdd3709[_0x074f('0x17')]),_0x074f('0x3b'),{'sessionId':_0x4d5731});return _0x5ebf87[_0x074f('0x18')](0xc8)[_0x074f('0x30')]({'error':![],'message':'hold\x20executed!'});})[_0x074f('0x32')](handleError(_0x5ebf87));};exports[_0x074f('0x3c')]=function(_0x2ff3ab,_0x306a17){logger['info'](_0x074f('0x3d'),_0x2ff3ab[_0x074f('0x29')]);var _0x50bb3d=unholdValidation['validate'](_0x2ff3ab[_0x074f('0x29')]);if(_0x50bb3d[_0x074f('0x25')]>0x0){return _0x306a17[_0x074f('0x18')](0x190)[_0x074f('0x30')](_0x50bb3d);}var _0x563343=_0x2ff3ab['body'][_0x074f('0x2a')];var _0xc38f21=_0x2ff3ab[_0x074f('0x29')][_0x074f('0x24')];return getAgent(_0x563343)[_0x074f('0x1c')](function(_0x151c90){logger[_0x074f('0x12')](_0x074f('0x2e'),_0x151c90[_0x074f('0x17')]);emit(util['format'](_0x074f('0x2f'),_0x151c90[_0x074f('0x17')]),_0x074f('0x3e'),{'sessionId':_0xc38f21});return _0x306a17[_0x074f('0x18')](0xc8)[_0x074f('0x30')]({'error':![],'message':_0x074f('0x3f')});})['catch'](handleError(_0x306a17));};exports[_0x074f('0x40')]=function(_0xe69ce5,_0x56b286){logger[_0x074f('0x12')](_0x074f('0x41'),_0x56b286[_0x074f('0x29')]);var _0x417fa1=callsValidation[_0x074f('0xb')](_0xe69ce5[_0x074f('0x29')]);if(_0x417fa1[_0x074f('0x25')]>0x0){return _0x56b286[_0x074f('0x18')](0x190)[_0x074f('0x30')](_0x417fa1);}var _0x2d2787=_0xe69ce5['body'][_0x074f('0x2a')];var _0x51ede6=respondWithRpcPromise('VoiceChannels');var _0x41aec7=getAgent(_0x2d2787);return Promise['all']([_0x51ede6,_0x41aec7])['then'](function(_0x24eb7a){var _0x183c51=_0x24eb7a[0x0];var _0x668a47=_0x24eb7a[0x1];var _0x3f3762=_[_0x074f('0x22')](_0x183c51[_0x074f('0x42')],function(_0x311d36){return _0x311d36[_0x074f('0x23')]==_0x668a47['id'];});return _0x56b286['status'](0xc8)['json']({'count':_0x3f3762[_0x074f('0x25')],'rows':_0x3f3762});})[_0x074f('0x32')](handleError(_0x56b286));};exports[_0x074f('0x43')]=function(_0x5ce8d2,_0x1de8f2){logger[_0x074f('0x12')]('conference\x20fired:',_0x5ce8d2['body']);var _0x274ce9=conferenceValidation['validate'](_0x5ce8d2[_0x074f('0x29')]);if(_0x274ce9[_0x074f('0x25')]>0x0){return _0x1de8f2['status'](0x190)[_0x074f('0x30')](_0x274ce9);}var _0x104933=_0x5ce8d2[_0x074f('0x29')][_0x074f('0x2a')];return getAgent(_0x104933)[_0x074f('0x1c')](function(_0x9fe79e){logger[_0x074f('0x12')](_0x074f('0x2e'),_0x9fe79e[_0x074f('0x17')]);emit(util[_0x074f('0x38')](_0x074f('0x2f'),_0x9fe79e[_0x074f('0x17')]),'webbar:conference');return _0x1de8f2['status'](0xc8)['json']({'error':![],'message':_0x074f('0x44')});})['catch'](handleError(_0x1de8f2));};exports[_0x074f('0x45')]=function(_0x4e449f,_0x30296f){logger[_0x074f('0x12')]('transfer\x20fired:',_0x4e449f[_0x074f('0x29')]);var _0x1253f7=transferValidation[_0x074f('0xb')](_0x4e449f[_0x074f('0x29')]);if(_0x1253f7[_0x074f('0x25')]>0x0){return _0x30296f[_0x074f('0x18')](0x190)[_0x074f('0x30')](_0x1253f7);}var _0x5bb76b=_0x4e449f[_0x074f('0x29')][_0x074f('0x2a')];var _0x547922=_0x4e449f[_0x074f('0x29')][_0x074f('0x24')];var _0x125d07=_0x4e449f[_0x074f('0x29')]['transferNumber'];return getAgent(_0x5bb76b)[_0x074f('0x1c')](function(_0x110fa1){logger[_0x074f('0x12')]('entityFound:',_0x110fa1['name']);emit(util[_0x074f('0x38')](_0x074f('0x2f'),_0x110fa1[_0x074f('0x17')]),_0x074f('0x46'),{'sessionId':_0x547922,'transferNumber':_0x125d07});return _0x30296f[_0x074f('0x18')](0xc8)['json']({'error':![],'message':_0x074f('0x47')});})['catch'](handleError(_0x30296f));};