6d3f8960109152d25c61848453e7d1e611ffe6b1
[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 _0xf88a=['answer\x20executed!','hold','hold\x20fired:','webbar:hold','hold\x20executed!','unhold','unhold\x20fired:','webbar:unhold','unhold\x20executed!','calls','conference','conference\x20fired:','webbar:conference','conference\x20executed!','transfer','transfer\x20fired:','transferNumber','transfer\x20executed!','path','lodash','../../mysqldb','../../config/environment','jayson/promise','client','http','../../config/logger','ioredis','redis','defaults','localhost','socket.io-emitter','number','userId\x20is\x20required','string','info','stringify','emit','stack','name','send','NotFound','request','then','error','code','message','catch','findOne','agent','VoiceChannels','all','filter','rows','sessionId','length','sessionId\x20','\x20not\x20found!','originate','originate\x20fired:','body','json','callNumber','callerId','callbackUrl','entityFound:','user:%s','webbar:originate','originate\x20executed!','hangup','hangup\x20fired:','validate','status','userId','format','hangup\x20executed!','answer\x20fired:','webbar:answer'];(function(_0xf8dca4,_0x108077){var _0x554055=function(_0x253606){while(--_0x253606){_0xf8dca4['push'](_0xf8dca4['shift']());}};_0x554055(++_0x108077);}(_0xf88a,0x1e0));var _0xaf88=function(_0x16f73d,_0x574564){_0x16f73d=_0x16f73d-0x0;var _0x39bbcf=_0xf88a[_0x16f73d];return _0x39bbcf;};'use strict';var path=require(_0xaf88('0x0'));var fs=require('fs');var _=require(_0xaf88('0x1'));var db=require(_0xaf88('0x2'))['db'];var config=require(_0xaf88('0x3'));var util=require('util');var BPromise=require('bluebird');var jayson=require(_0xaf88('0x4'));var amiClient=jayson[_0xaf88('0x5')][_0xaf88('0x6')]({'port':0x232a});var logger=require(_0xaf88('0x7'))('webbarapi');var Redis=require(_0xaf88('0x8'));config[_0xaf88('0x9')]=_[_0xaf88('0xa')](config['redis'],{'host':_0xaf88('0xb'),'port':0x18eb});var io=require(_0xaf88('0xc'))(new Redis(config[_0xaf88('0x9')]));var Schema=require('validate');var originateValidation=new Schema({'userId':{'type':_0xaf88('0xd'),'required':[!![],_0xaf88('0xe')]},'callNumber':{'type':'string','required':[!![],'callNumber\x20is\x20required']},'callerId':{'type':_0xaf88('0xf')},'callbackUrl':{'type':_0xaf88('0xf')}});var hangupValidation=new Schema({'userId':{'type':_0xaf88('0xd'),'required':[!![],_0xaf88('0xe')]},'sessionId':{'type':_0xaf88('0xf'),'required':![]}});var answerValidation=new Schema({'userId':{'type':_0xaf88('0xd'),'required':[!![],_0xaf88('0xe')]},'sessionId':{'type':_0xaf88('0xf'),'required':![]}});var transferValidation=new Schema({'userId':{'type':'number','required':[!![],_0xaf88('0xe')]},'sessionId':{'type':_0xaf88('0xf'),'required':[!![],'sessionId\x20is\x20required']},'transferNumber':{'type':_0xaf88('0xf'),'required':[!![],'transferNumber\x20is\x20required']}});var holdValidation=new Schema({'userId':{'type':_0xaf88('0xd'),'required':[!![],_0xaf88('0xe')]},'sessionId':{'type':_0xaf88('0xf'),'required':![]}});var unholdValidation=new Schema({'userId':{'type':_0xaf88('0xd'),'required':[!![],_0xaf88('0xe')]},'sessionId':{'type':_0xaf88('0xf'),'required':![]}});var callsValidation=new Schema({'userId':{'type':'number','required':[!![],_0xaf88('0xe')]}});var conferenceValidation=new Schema({'userId':{'type':'number','required':[!![],_0xaf88('0xe')]}});function emit(_0x4df1a2,_0x7a25a3,_0x26e920){logger[_0xaf88('0x10')](_0x4df1a2,_0x7a25a3,JSON[_0xaf88('0x11')](_0x26e920));io['to'](_0x4df1a2)[_0xaf88('0x12')](_0x7a25a3,_0x26e920);}function handleError(_0x984e1a){return function(_0x3d760b){console['log']('handleError:',_0x3d760b,_0x3d760b instanceof RangeError);logger['error'](_0x3d760b[_0xaf88('0x13')]);if(_0x3d760b instanceof RangeError){if(_0x3d760b[_0xaf88('0x14')]){delete _0x3d760b[_0xaf88('0x14')];}_0x984e1a['status'](0x194)[_0xaf88('0x15')](_0x3d760b);}else{if(_0x3d760b[_0xaf88('0x14')]){delete _0x3d760b[_0xaf88('0x14')];}_0x984e1a['status'](0x1f4)[_0xaf88('0x15')](_0x3d760b);}};}function checkError(){return function(_0x2b640b){if(!_0x2b640b){throw new RangeError(_0xaf88('0x16'));}return _0x2b640b;};}function respondWithRpcPromise(_0x106a91,_0x9b69d4){return new BPromise(function(_0x1fafae,_0x1c1fc4){var _0x2182a1=amiClient;return _0x2182a1[_0xaf88('0x17')](_0x106a91,_0x9b69d4)[_0xaf88('0x18')](function(_0x3b643d){if(_0x3b643d['error']){if(_0x3b643d[_0xaf88('0x19')][_0xaf88('0x1a')]===0x1f4){return _0x1c1fc4(_0x3b643d['error'][_0xaf88('0x1b')]);}return _0x1fafae(_0x3b643d[_0xaf88('0x19')][_0xaf88('0x1b')]);}else{_0x1fafae(_0x3b643d['result']);}})[_0xaf88('0x1c')](function(_0x381755){_0x1c1fc4(_0x381755);});});}function getAgent(_0x5c796d){return db['User'][_0xaf88('0x1d')]({'where':{'id':_0x5c796d,'role':_0xaf88('0x1e')}})[_0xaf88('0x18')](checkError());}function getAgentCheckSession(_0x975730,_0xbb28f4){return new BPromise(function(_0x406571,_0x3f543c){var _0x50ed01=respondWithRpcPromise(_0xaf88('0x1f'));var _0x4aba1a=getAgent(_0x975730);Promise[_0xaf88('0x20')]([_0x50ed01,_0x4aba1a])['then'](function(_0x9eb2db){var _0x135537=_0x9eb2db[0x0];var _0xc3f744=_0x9eb2db[0x1];var _0x1d56b0=_[_0xaf88('0x21')](_0x135537[_0xaf88('0x22')],function(_0x7bd9f9){return _0x7bd9f9['UserId']==_0xc3f744['id']&&_0x7bd9f9[_0xaf88('0x23')]==_0xbb28f4;});if(_0x1d56b0[_0xaf88('0x24')]==0x1){_0x406571(_0xc3f744);}else{_0x3f543c(new RangeError(_0xaf88('0x25')+_0xbb28f4+_0xaf88('0x26')));}});});}exports[_0xaf88('0x27')]=function(_0x444aa1,_0x1b2814){logger[_0xaf88('0x10')](_0xaf88('0x28'),_0x444aa1['body']);var _0x4e0751=originateValidation['validate'](_0x444aa1[_0xaf88('0x29')]);if(_0x4e0751[_0xaf88('0x24')]>0x0){return _0x1b2814['status'](0x190)[_0xaf88('0x2a')](_0x4e0751);}var _0x3003f8=_0x444aa1[_0xaf88('0x29')]['userId'];var _0x226849=_0x444aa1['body'][_0xaf88('0x2b')];var _0x2e6377=_0x444aa1[_0xaf88('0x29')][_0xaf88('0x2c')];var _0x5b539c=_0x444aa1[_0xaf88('0x29')][_0xaf88('0x2d')];return getAgent(_0x3003f8)[_0xaf88('0x18')](function(_0x5d90c9){logger['info'](_0xaf88('0x2e'),_0x5d90c9[_0xaf88('0x14')]);emit(util['format'](_0xaf88('0x2f'),_0x5d90c9[_0xaf88('0x14')]),_0xaf88('0x30'),{'callNumber':_0x226849,'callerId':_0x2e6377,'callbackUrl':_0x5b539c});return _0x1b2814['status'](0xc8)[_0xaf88('0x2a')]({'error':![],'message':_0xaf88('0x31')});})['catch'](handleError(_0x1b2814));};exports[_0xaf88('0x32')]=function(_0x4500a1,_0x4dcedc){logger[_0xaf88('0x10')](_0xaf88('0x33'),_0x4500a1[_0xaf88('0x29')]);var _0x23d19d=hangupValidation[_0xaf88('0x34')](_0x4500a1[_0xaf88('0x29')]);if(_0x23d19d['length']>0x0){return _0x4dcedc[_0xaf88('0x35')](0x190)[_0xaf88('0x2a')](_0x23d19d);}var _0x5d9420=_0x4500a1[_0xaf88('0x29')][_0xaf88('0x36')];var _0x3689c1=_0x4500a1[_0xaf88('0x29')][_0xaf88('0x23')];return getAgent(_0x5d9420)[_0xaf88('0x18')](function(_0x29a773){logger[_0xaf88('0x10')]('entityFound:',_0x29a773['name']);emit(util[_0xaf88('0x37')](_0xaf88('0x2f'),_0x29a773['name']),'webbar:hangup',{'sessionId':_0x3689c1});return _0x4dcedc['status'](0xc8)[_0xaf88('0x2a')]({'error':![],'message':_0xaf88('0x38')});})['catch'](handleError(_0x4dcedc));};exports['answer']=function(_0x1fcb25,_0x438fe9){logger[_0xaf88('0x10')](_0xaf88('0x39'),_0x1fcb25[_0xaf88('0x29')]);var _0x337902=answerValidation['validate'](_0x1fcb25[_0xaf88('0x29')]);if(_0x337902['length']>0x0){return _0x438fe9[_0xaf88('0x35')](0x190)[_0xaf88('0x2a')](_0x337902);}var _0x3ad774=_0x1fcb25[_0xaf88('0x29')][_0xaf88('0x36')];var _0x3e0795=_0x1fcb25['body'][_0xaf88('0x23')];return getAgent(_0x3ad774)['then'](function(_0x2ae64b){logger[_0xaf88('0x10')](_0xaf88('0x2e'),_0x2ae64b['name']);emit(util['format']('user:%s',_0x2ae64b[_0xaf88('0x14')]),_0xaf88('0x3a'),{'sessionId':_0x3e0795});return _0x438fe9['status'](0xc8)[_0xaf88('0x2a')]({'error':![],'message':_0xaf88('0x3b')});})[_0xaf88('0x1c')](handleError(_0x438fe9));};exports[_0xaf88('0x3c')]=function(_0x434d05,_0x5c4c45){logger['info'](_0xaf88('0x3d'),_0x434d05['body']);var _0x13afe4=holdValidation[_0xaf88('0x34')](_0x434d05[_0xaf88('0x29')]);if(_0x13afe4['length']>0x0){return _0x5c4c45[_0xaf88('0x35')](0x190)[_0xaf88('0x2a')](_0x13afe4);}var _0x299a04=_0x434d05[_0xaf88('0x29')][_0xaf88('0x36')];var _0x2a3613=_0x434d05[_0xaf88('0x29')][_0xaf88('0x23')];return getAgent(_0x299a04)[_0xaf88('0x18')](function(_0xcd22d){logger[_0xaf88('0x10')]('entityFound:',_0xcd22d['name']);emit(util[_0xaf88('0x37')](_0xaf88('0x2f'),_0xcd22d['name']),_0xaf88('0x3e'),{'sessionId':_0x2a3613});return _0x5c4c45[_0xaf88('0x35')](0xc8)['json']({'error':![],'message':_0xaf88('0x3f')});})[_0xaf88('0x1c')](handleError(_0x5c4c45));};exports[_0xaf88('0x40')]=function(_0x5ae8ad,_0x33ec50){logger['info'](_0xaf88('0x41'),_0x5ae8ad[_0xaf88('0x29')]);var _0x9bf951=unholdValidation[_0xaf88('0x34')](_0x5ae8ad[_0xaf88('0x29')]);if(_0x9bf951[_0xaf88('0x24')]>0x0){return _0x33ec50[_0xaf88('0x35')](0x190)['json'](_0x9bf951);}var _0x33c56b=_0x5ae8ad[_0xaf88('0x29')]['userId'];var _0x2eb003=_0x5ae8ad['body']['sessionId'];return getAgent(_0x33c56b)[_0xaf88('0x18')](function(_0x35cfdc){logger[_0xaf88('0x10')](_0xaf88('0x2e'),_0x35cfdc[_0xaf88('0x14')]);emit(util['format'](_0xaf88('0x2f'),_0x35cfdc[_0xaf88('0x14')]),_0xaf88('0x42'),{'sessionId':_0x2eb003});return _0x33ec50['status'](0xc8)[_0xaf88('0x2a')]({'error':![],'message':_0xaf88('0x43')});})[_0xaf88('0x1c')](handleError(_0x33ec50));};exports[_0xaf88('0x44')]=function(_0x4ef310,_0x5af475){logger[_0xaf88('0x10')]('calls\x20fired:',_0x5af475[_0xaf88('0x29')]);var _0x5e8282=callsValidation[_0xaf88('0x34')](_0x4ef310[_0xaf88('0x29')]);if(_0x5e8282[_0xaf88('0x24')]>0x0){return _0x5af475['status'](0x190)['json'](_0x5e8282);}var _0x100117=_0x4ef310['body'][_0xaf88('0x36')];var _0xf2fa1=respondWithRpcPromise(_0xaf88('0x1f'));var _0x2d86d2=getAgent(_0x100117);return Promise[_0xaf88('0x20')]([_0xf2fa1,_0x2d86d2])[_0xaf88('0x18')](function(_0x31905b){var _0x3c9509=_0x31905b[0x0];var _0x1722b0=_0x31905b[0x1];var _0x2eb2cd=_[_0xaf88('0x21')](_0x3c9509[_0xaf88('0x22')],function(_0x3337db){return _0x3337db['UserId']==_0x1722b0['id'];});return _0x5af475[_0xaf88('0x35')](0xc8)[_0xaf88('0x2a')]({'count':_0x2eb2cd['length'],'rows':_0x2eb2cd});})[_0xaf88('0x1c')](handleError(_0x5af475));};exports[_0xaf88('0x45')]=function(_0x38dcbf,_0x4ecf7d){logger['info'](_0xaf88('0x46'),_0x38dcbf[_0xaf88('0x29')]);var _0x4945a2=conferenceValidation['validate'](_0x38dcbf[_0xaf88('0x29')]);if(_0x4945a2['length']>0x0){return _0x4ecf7d['status'](0x190)[_0xaf88('0x2a')](_0x4945a2);}var _0x100d14=_0x38dcbf['body'][_0xaf88('0x36')];return getAgent(_0x100d14)[_0xaf88('0x18')](function(_0x54f3c2){logger[_0xaf88('0x10')](_0xaf88('0x2e'),_0x54f3c2[_0xaf88('0x14')]);emit(util['format'](_0xaf88('0x2f'),_0x54f3c2['name']),_0xaf88('0x47'));return _0x4ecf7d[_0xaf88('0x35')](0xc8)[_0xaf88('0x2a')]({'error':![],'message':_0xaf88('0x48')});})[_0xaf88('0x1c')](handleError(_0x4ecf7d));};exports[_0xaf88('0x49')]=function(_0x2c705b,_0x42534d){logger[_0xaf88('0x10')](_0xaf88('0x4a'),_0x2c705b[_0xaf88('0x29')]);var _0x43ca5f=transferValidation[_0xaf88('0x34')](_0x2c705b[_0xaf88('0x29')]);if(_0x43ca5f[_0xaf88('0x24')]>0x0){return _0x42534d[_0xaf88('0x35')](0x190)[_0xaf88('0x2a')](_0x43ca5f);}var _0x52be3b=_0x2c705b[_0xaf88('0x29')][_0xaf88('0x36')];var _0x18d5f1=_0x2c705b[_0xaf88('0x29')]['sessionId'];var _0x5894e6=_0x2c705b[_0xaf88('0x29')][_0xaf88('0x4b')];return getAgent(_0x52be3b)[_0xaf88('0x18')](function(_0x212124){logger['info']('entityFound:',_0x212124[_0xaf88('0x14')]);emit(util[_0xaf88('0x37')](_0xaf88('0x2f'),_0x212124[_0xaf88('0x14')]),'webbar:transfer',{'sessionId':_0x18d5f1,'transferNumber':_0x5894e6});return _0x42534d[_0xaf88('0x35')](0xc8)['json']({'error':![],'message':_0xaf88('0x4c')});})['catch'](handleError(_0x42534d));};