Built motion from commit 9d0783e6.|2.6.13
[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 _0xf403=['answer\x20fired:','webbar:answer','answer\x20executed!','hold','hold\x20fired:','webbar:hold','hold\x20executed!','unhold','webbar:unhold','unhold\x20executed!','calls','calls\x20fired:','conference\x20fired:','conference\x20executed!','transfer','transfer\x20fired:','transfer\x20executed!','path','../../mysqldb','../../config/environment','util','bluebird','jayson/promise','client','http','../../config/logger','redis','defaults','localhost','userId\x20is\x20required','string','callNumber\x20is\x20required','number','transferNumber\x20is\x20required','stringify','emit','log','error','stack','name','status','send','NotFound','request','then','code','result','User','findOne','agent','VoiceChannels','all','filter','rows','UserId','sessionId','length','sessionId\x20','originate','info','originate\x20fired:','validate','body','json','userId','callNumber','callerId','callbackUrl','entityFound:','format','user:%s','webbar:originate','originate\x20executed!','catch','hangup'];(function(_0x3bbe72,_0x8d69f7){var _0x15d332=function(_0x2b850b){while(--_0x2b850b){_0x3bbe72['push'](_0x3bbe72['shift']());}};_0x15d332(++_0x8d69f7);}(_0xf403,0x1d3));var _0x3f40=function(_0x25bec2,_0x2a046c){_0x25bec2=_0x25bec2-0x0;var _0x116315=_0xf403[_0x25bec2];return _0x116315;};'use strict';var path=require(_0x3f40('0x0'));var fs=require('fs');var _=require('lodash');var db=require(_0x3f40('0x1'))['db'];var config=require(_0x3f40('0x2'));var util=require(_0x3f40('0x3'));var BPromise=require(_0x3f40('0x4'));var jayson=require(_0x3f40('0x5'));var amiClient=jayson[_0x3f40('0x6')][_0x3f40('0x7')]({'port':0x232a});var logger=require(_0x3f40('0x8'))('webbarapi');var Redis=require('ioredis');config[_0x3f40('0x9')]=_[_0x3f40('0xa')](config[_0x3f40('0x9')],{'host':_0x3f40('0xb'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x3f40('0x9')]));var Schema=require('validate');var originateValidation=new Schema({'userId':{'type':'number','required':[!![],_0x3f40('0xc')]},'callNumber':{'type':_0x3f40('0xd'),'required':[!![],_0x3f40('0xe')]},'callerId':{'type':'string'},'callbackUrl':{'type':'string'}});var hangupValidation=new Schema({'userId':{'type':_0x3f40('0xf'),'required':[!![],_0x3f40('0xc')]},'sessionId':{'type':_0x3f40('0xd'),'required':![]}});var answerValidation=new Schema({'userId':{'type':'number','required':[!![],_0x3f40('0xc')]},'sessionId':{'type':_0x3f40('0xd'),'required':![]}});var transferValidation=new Schema({'userId':{'type':_0x3f40('0xf'),'required':[!![],'userId\x20is\x20required']},'sessionId':{'type':_0x3f40('0xd'),'required':[!![],'sessionId\x20is\x20required']},'transferNumber':{'type':_0x3f40('0xd'),'required':[!![],_0x3f40('0x10')]}});var holdValidation=new Schema({'userId':{'type':_0x3f40('0xf'),'required':[!![],_0x3f40('0xc')]},'sessionId':{'type':'string','required':![]}});var unholdValidation=new Schema({'userId':{'type':_0x3f40('0xf'),'required':[!![],_0x3f40('0xc')]},'sessionId':{'type':_0x3f40('0xd'),'required':![]}});var callsValidation=new Schema({'userId':{'type':_0x3f40('0xf'),'required':[!![],_0x3f40('0xc')]}});var conferenceValidation=new Schema({'userId':{'type':'number','required':[!![],_0x3f40('0xc')]}});function emit(_0x3d11c9,_0x535f85,_0x4ef80e){logger['info'](_0x3d11c9,_0x535f85,JSON[_0x3f40('0x11')](_0x4ef80e));io['to'](_0x3d11c9)[_0x3f40('0x12')](_0x535f85,_0x4ef80e);}function handleError(_0x59718c){return function(_0x131e55){console[_0x3f40('0x13')]('handleError:',_0x131e55,_0x131e55 instanceof RangeError);logger[_0x3f40('0x14')](_0x131e55[_0x3f40('0x15')]);if(_0x131e55 instanceof RangeError){if(_0x131e55[_0x3f40('0x16')]){delete _0x131e55[_0x3f40('0x16')];}_0x59718c[_0x3f40('0x17')](0x194)['send'](_0x131e55);}else{if(_0x131e55[_0x3f40('0x16')]){delete _0x131e55['name'];}_0x59718c[_0x3f40('0x17')](0x1f4)[_0x3f40('0x18')](_0x131e55);}};}function checkError(){return function(_0x3f4a93){if(!_0x3f4a93){throw new RangeError(_0x3f40('0x19'));}return _0x3f4a93;};}function respondWithRpcPromise(_0x628816,_0x4aa559){return new BPromise(function(_0x27f5a5,_0x379b4e){var _0x117823=amiClient;return _0x117823[_0x3f40('0x1a')](_0x628816,_0x4aa559)[_0x3f40('0x1b')](function(_0xa1f39d){if(_0xa1f39d['error']){if(_0xa1f39d[_0x3f40('0x14')][_0x3f40('0x1c')]===0x1f4){return _0x379b4e(_0xa1f39d[_0x3f40('0x14')]['message']);}return _0x27f5a5(_0xa1f39d[_0x3f40('0x14')]['message']);}else{_0x27f5a5(_0xa1f39d[_0x3f40('0x1d')]);}})['catch'](function(_0x4b206b){_0x379b4e(_0x4b206b);});});}function getAgent(_0x4b0ade){return db[_0x3f40('0x1e')][_0x3f40('0x1f')]({'where':{'id':_0x4b0ade,'role':_0x3f40('0x20')}})[_0x3f40('0x1b')](checkError());}function getAgentCheckSession(_0x5b1079,_0x300665){return new BPromise(function(_0x47ec1c,_0x319997){var _0xbaf5d4=respondWithRpcPromise(_0x3f40('0x21'));var _0x5e0f25=getAgent(_0x5b1079);Promise[_0x3f40('0x22')]([_0xbaf5d4,_0x5e0f25])['then'](function(_0x1cab5c){var _0x5b9079=_0x1cab5c[0x0];var _0x49057b=_0x1cab5c[0x1];var _0xb6ab4d=_[_0x3f40('0x23')](_0x5b9079[_0x3f40('0x24')],function(_0xb23186){return _0xb23186[_0x3f40('0x25')]==_0x49057b['id']&&_0xb23186[_0x3f40('0x26')]==_0x300665;});if(_0xb6ab4d[_0x3f40('0x27')]==0x1){_0x47ec1c(_0x49057b);}else{_0x319997(new RangeError(_0x3f40('0x28')+_0x300665+'\x20not\x20found!'));}});});}exports[_0x3f40('0x29')]=function(_0x2d2624,_0x18dbb3){logger[_0x3f40('0x2a')](_0x3f40('0x2b'),_0x2d2624['body']);var _0x42b6b4=originateValidation[_0x3f40('0x2c')](_0x2d2624[_0x3f40('0x2d')]);if(_0x42b6b4['length']>0x0){return _0x18dbb3[_0x3f40('0x17')](0x190)[_0x3f40('0x2e')](_0x42b6b4);}var _0x239fd5=_0x2d2624[_0x3f40('0x2d')][_0x3f40('0x2f')];var _0x323908=_0x2d2624[_0x3f40('0x2d')][_0x3f40('0x30')];var _0x10356d=_0x2d2624[_0x3f40('0x2d')][_0x3f40('0x31')];var _0x25399a=_0x2d2624[_0x3f40('0x2d')][_0x3f40('0x32')];return getAgent(_0x239fd5)[_0x3f40('0x1b')](function(_0xf26a2f){logger[_0x3f40('0x2a')](_0x3f40('0x33'),_0xf26a2f[_0x3f40('0x16')]);emit(util[_0x3f40('0x34')](_0x3f40('0x35'),_0xf26a2f[_0x3f40('0x16')]),_0x3f40('0x36'),{'callNumber':_0x323908,'callerId':_0x10356d,'callbackUrl':_0x25399a});return _0x18dbb3[_0x3f40('0x17')](0xc8)[_0x3f40('0x2e')]({'error':![],'message':_0x3f40('0x37')});})[_0x3f40('0x38')](handleError(_0x18dbb3));};exports[_0x3f40('0x39')]=function(_0x411c98,_0x2df15b){logger['info']('hangup\x20fired:',_0x411c98[_0x3f40('0x2d')]);var _0x1b005e=hangupValidation[_0x3f40('0x2c')](_0x411c98[_0x3f40('0x2d')]);if(_0x1b005e[_0x3f40('0x27')]>0x0){return _0x2df15b[_0x3f40('0x17')](0x190)[_0x3f40('0x2e')](_0x1b005e);}var _0x4de3e7=_0x411c98[_0x3f40('0x2d')]['userId'];var _0x229bff=_0x411c98[_0x3f40('0x2d')][_0x3f40('0x26')];return getAgent(_0x4de3e7)['then'](function(_0x37a83d){logger[_0x3f40('0x2a')]('entityFound:',_0x37a83d[_0x3f40('0x16')]);emit(util[_0x3f40('0x34')]('user:%s',_0x37a83d[_0x3f40('0x16')]),'webbar:hangup',{'sessionId':_0x229bff});return _0x2df15b[_0x3f40('0x17')](0xc8)['json']({'error':![],'message':'hangup\x20executed!'});})[_0x3f40('0x38')](handleError(_0x2df15b));};exports['answer']=function(_0x2210bc,_0xe04271){logger[_0x3f40('0x2a')](_0x3f40('0x3a'),_0x2210bc['body']);var _0x237c91=answerValidation['validate'](_0x2210bc[_0x3f40('0x2d')]);if(_0x237c91[_0x3f40('0x27')]>0x0){return _0xe04271[_0x3f40('0x17')](0x190)['json'](_0x237c91);}var _0x4a198d=_0x2210bc[_0x3f40('0x2d')][_0x3f40('0x2f')];var _0x41bd93=_0x2210bc[_0x3f40('0x2d')][_0x3f40('0x26')];return getAgent(_0x4a198d)[_0x3f40('0x1b')](function(_0x38c5d6){logger[_0x3f40('0x2a')]('entityFound:',_0x38c5d6[_0x3f40('0x16')]);emit(util['format']('user:%s',_0x38c5d6[_0x3f40('0x16')]),_0x3f40('0x3b'),{'sessionId':_0x41bd93});return _0xe04271[_0x3f40('0x17')](0xc8)[_0x3f40('0x2e')]({'error':![],'message':_0x3f40('0x3c')});})[_0x3f40('0x38')](handleError(_0xe04271));};exports[_0x3f40('0x3d')]=function(_0x3d6cf8,_0x1cc958){logger[_0x3f40('0x2a')](_0x3f40('0x3e'),_0x3d6cf8[_0x3f40('0x2d')]);var _0x2068ab=holdValidation[_0x3f40('0x2c')](_0x3d6cf8['body']);if(_0x2068ab[_0x3f40('0x27')]>0x0){return _0x1cc958['status'](0x190)[_0x3f40('0x2e')](_0x2068ab);}var _0x2f16d6=_0x3d6cf8['body'][_0x3f40('0x2f')];var _0x1f66fa=_0x3d6cf8[_0x3f40('0x2d')][_0x3f40('0x26')];return getAgent(_0x2f16d6)[_0x3f40('0x1b')](function(_0x268ae4){logger[_0x3f40('0x2a')](_0x3f40('0x33'),_0x268ae4[_0x3f40('0x16')]);emit(util[_0x3f40('0x34')](_0x3f40('0x35'),_0x268ae4[_0x3f40('0x16')]),_0x3f40('0x3f'),{'sessionId':_0x1f66fa});return _0x1cc958[_0x3f40('0x17')](0xc8)[_0x3f40('0x2e')]({'error':![],'message':_0x3f40('0x40')});})[_0x3f40('0x38')](handleError(_0x1cc958));};exports[_0x3f40('0x41')]=function(_0x32bd05,_0x2d7e66){logger[_0x3f40('0x2a')]('unhold\x20fired:',_0x32bd05[_0x3f40('0x2d')]);var _0x50db96=unholdValidation[_0x3f40('0x2c')](_0x32bd05[_0x3f40('0x2d')]);if(_0x50db96[_0x3f40('0x27')]>0x0){return _0x2d7e66['status'](0x190)['json'](_0x50db96);}var _0x54b7cd=_0x32bd05[_0x3f40('0x2d')][_0x3f40('0x2f')];var _0x5f4f1a=_0x32bd05[_0x3f40('0x2d')][_0x3f40('0x26')];return getAgent(_0x54b7cd)['then'](function(_0x1cd70d){logger['info'](_0x3f40('0x33'),_0x1cd70d[_0x3f40('0x16')]);emit(util[_0x3f40('0x34')](_0x3f40('0x35'),_0x1cd70d[_0x3f40('0x16')]),_0x3f40('0x42'),{'sessionId':_0x5f4f1a});return _0x2d7e66[_0x3f40('0x17')](0xc8)[_0x3f40('0x2e')]({'error':![],'message':_0x3f40('0x43')});})[_0x3f40('0x38')](handleError(_0x2d7e66));};exports[_0x3f40('0x44')]=function(_0x13f191,_0x109752){logger['info'](_0x3f40('0x45'),_0x109752[_0x3f40('0x2d')]);var _0x1c94a1=callsValidation[_0x3f40('0x2c')](_0x13f191[_0x3f40('0x2d')]);if(_0x1c94a1['length']>0x0){return _0x109752[_0x3f40('0x17')](0x190)[_0x3f40('0x2e')](_0x1c94a1);}var _0x1c4b44=_0x13f191[_0x3f40('0x2d')][_0x3f40('0x2f')];var _0x5c030a=respondWithRpcPromise('VoiceChannels');var _0x6c81fe=getAgent(_0x1c4b44);return Promise[_0x3f40('0x22')]([_0x5c030a,_0x6c81fe])['then'](function(_0x701ed0){var _0x5a3456=_0x701ed0[0x0];var _0x204cff=_0x701ed0[0x1];var _0x557c0c=_[_0x3f40('0x23')](_0x5a3456['rows'],function(_0xab34b){return _0xab34b[_0x3f40('0x25')]==_0x204cff['id'];});return _0x109752[_0x3f40('0x17')](0xc8)['json']({'count':_0x557c0c['length'],'rows':_0x557c0c});})['catch'](handleError(_0x109752));};exports['conference']=function(_0x273739,_0x4e1c35){logger['info'](_0x3f40('0x46'),_0x273739[_0x3f40('0x2d')]);var _0x37f636=conferenceValidation[_0x3f40('0x2c')](_0x273739['body']);if(_0x37f636[_0x3f40('0x27')]>0x0){return _0x4e1c35['status'](0x190)['json'](_0x37f636);}var _0x48b0ed=_0x273739[_0x3f40('0x2d')]['userId'];return getAgent(_0x48b0ed)['then'](function(_0x45d7e5){logger[_0x3f40('0x2a')](_0x3f40('0x33'),_0x45d7e5[_0x3f40('0x16')]);emit(util[_0x3f40('0x34')]('user:%s',_0x45d7e5[_0x3f40('0x16')]),'webbar:conference');return _0x4e1c35[_0x3f40('0x17')](0xc8)[_0x3f40('0x2e')]({'error':![],'message':_0x3f40('0x47')});})['catch'](handleError(_0x4e1c35));};exports[_0x3f40('0x48')]=function(_0x2408f4,_0x46821c){logger[_0x3f40('0x2a')](_0x3f40('0x49'),_0x2408f4['body']);var _0x2f76a1=transferValidation[_0x3f40('0x2c')](_0x2408f4[_0x3f40('0x2d')]);if(_0x2f76a1[_0x3f40('0x27')]>0x0){return _0x46821c[_0x3f40('0x17')](0x190)[_0x3f40('0x2e')](_0x2f76a1);}var _0x29fdfc=_0x2408f4[_0x3f40('0x2d')][_0x3f40('0x2f')];var _0x37add1=_0x2408f4[_0x3f40('0x2d')][_0x3f40('0x26')];var _0x52c075=_0x2408f4['body']['transferNumber'];return getAgent(_0x29fdfc)[_0x3f40('0x1b')](function(_0x137cd8){logger[_0x3f40('0x2a')](_0x3f40('0x33'),_0x137cd8[_0x3f40('0x16')]);emit(util[_0x3f40('0x34')]('user:%s',_0x137cd8[_0x3f40('0x16')]),'webbar:transfer',{'sessionId':_0x37add1,'transferNumber':_0x52c075});return _0x46821c[_0x3f40('0x17')](0xc8)[_0x3f40('0x2e')]({'error':![],'message':_0x3f40('0x4a')});})[_0x3f40('0x38')](handleError(_0x46821c));};