Built motion from commit d1eab355.|2.6.28
[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 _0x41bb=['then','error','code','message','result','findOne','agent','VoiceChannels','all','rows','sessionId','sessionId\x20','\x20not\x20found!','originate\x20fired:','body','validate','length','callNumber','callerId','callbackUrl','entityFound:','format','user:%s','webbar:originate','originate\x20executed!','catch','hangup\x20fired:','json','userId','webbar:hangup','hangup\x20executed!','answer','answer\x20fired:','webbar:answer','hold\x20fired:','webbar:hold','hold\x20executed!','unhold','unhold\x20fired:','unhold\x20executed!','calls','calls\x20fired:','filter','UserId','conference','conference\x20executed!','transfer','transfer\x20fired:','transfer\x20executed!','path','lodash','../../config/environment','bluebird','jayson/promise','client','http','webbarapi','ioredis','redis','socket.io-emitter','number','userId\x20is\x20required','string','callNumber\x20is\x20required','transferNumber\x20is\x20required','info','stringify','emit','log','name','status','send','NotFound','request'];(function(_0x3422bf,_0x21b1da){var _0x4de668=function(_0x271cce){while(--_0x271cce){_0x3422bf['push'](_0x3422bf['shift']());}};_0x4de668(++_0x21b1da);}(_0x41bb,0xc5));var _0xb41b=function(_0x11786d,_0x805509){_0x11786d=_0x11786d-0x0;var _0x26a9ad=_0x41bb[_0x11786d];return _0x26a9ad;};'use strict';var path=require(_0xb41b('0x0'));var fs=require('fs');var _=require(_0xb41b('0x1'));var db=require('../../mysqldb')['db'];var config=require(_0xb41b('0x2'));var util=require('util');var BPromise=require(_0xb41b('0x3'));var jayson=require(_0xb41b('0x4'));var amiClient=jayson[_0xb41b('0x5')][_0xb41b('0x6')]({'port':0x232a});var logger=require('../../config/logger')(_0xb41b('0x7'));var Redis=require(_0xb41b('0x8'));config[_0xb41b('0x9')]=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0xb41b('0xa'))(new Redis(config[_0xb41b('0x9')]));var Schema=require('validate');var originateValidation=new Schema({'userId':{'type':_0xb41b('0xb'),'required':[!![],_0xb41b('0xc')]},'callNumber':{'type':_0xb41b('0xd'),'required':[!![],_0xb41b('0xe')]},'callerId':{'type':_0xb41b('0xd')},'callbackUrl':{'type':_0xb41b('0xd')}});var hangupValidation=new Schema({'userId':{'type':_0xb41b('0xb'),'required':[!![],'userId\x20is\x20required']},'sessionId':{'type':_0xb41b('0xd'),'required':![]}});var answerValidation=new Schema({'userId':{'type':'number','required':[!![],_0xb41b('0xc')]},'sessionId':{'type':_0xb41b('0xd'),'required':![]}});var transferValidation=new Schema({'userId':{'type':_0xb41b('0xb'),'required':[!![],_0xb41b('0xc')]},'sessionId':{'type':_0xb41b('0xd'),'required':[!![],'sessionId\x20is\x20required']},'transferNumber':{'type':_0xb41b('0xd'),'required':[!![],_0xb41b('0xf')]}});var holdValidation=new Schema({'userId':{'type':_0xb41b('0xb'),'required':[!![],_0xb41b('0xc')]},'sessionId':{'type':_0xb41b('0xd'),'required':![]}});var unholdValidation=new Schema({'userId':{'type':'number','required':[!![],'userId\x20is\x20required']},'sessionId':{'type':'string','required':![]}});var callsValidation=new Schema({'userId':{'type':_0xb41b('0xb'),'required':[!![],_0xb41b('0xc')]}});var conferenceValidation=new Schema({'userId':{'type':_0xb41b('0xb'),'required':[!![],_0xb41b('0xc')]}});function emit(_0x29ea51,_0x54591e,_0x2f3673){logger[_0xb41b('0x10')](_0x29ea51,_0x54591e,JSON[_0xb41b('0x11')](_0x2f3673));io['to'](_0x29ea51)[_0xb41b('0x12')](_0x54591e,_0x2f3673);}function handleError(_0x43f4ea){return function(_0x47154d){console[_0xb41b('0x13')]('handleError:',_0x47154d,_0x47154d instanceof RangeError);logger['error'](_0x47154d['stack']);if(_0x47154d instanceof RangeError){if(_0x47154d[_0xb41b('0x14')]){delete _0x47154d['name'];}_0x43f4ea[_0xb41b('0x15')](0x194)[_0xb41b('0x16')](_0x47154d);}else{if(_0x47154d[_0xb41b('0x14')]){delete _0x47154d['name'];}_0x43f4ea['status'](0x1f4)[_0xb41b('0x16')](_0x47154d);}};}function checkError(){return function(_0x3b6ffb){if(!_0x3b6ffb){throw new RangeError(_0xb41b('0x17'));}return _0x3b6ffb;};}function respondWithRpcPromise(_0x2373f4,_0x232ff1){return new BPromise(function(_0x42dccb,_0x4c73da){var _0x58f149=amiClient;return _0x58f149[_0xb41b('0x18')](_0x2373f4,_0x232ff1)[_0xb41b('0x19')](function(_0x871b35){if(_0x871b35['error']){if(_0x871b35[_0xb41b('0x1a')][_0xb41b('0x1b')]===0x1f4){return _0x4c73da(_0x871b35['error'][_0xb41b('0x1c')]);}return _0x42dccb(_0x871b35[_0xb41b('0x1a')]['message']);}else{_0x42dccb(_0x871b35[_0xb41b('0x1d')]);}})['catch'](function(_0x2d0f27){_0x4c73da(_0x2d0f27);});});}function getAgent(_0x4c6a91){return db['User'][_0xb41b('0x1e')]({'where':{'id':_0x4c6a91,'role':_0xb41b('0x1f')}})['then'](checkError());}function getAgentCheckSession(_0x4c2e41,_0x2a5286){return new BPromise(function(_0x46aecf,_0x6b98aa){var _0x36dda8=respondWithRpcPromise(_0xb41b('0x20'));var _0x181cbd=getAgent(_0x4c2e41);Promise[_0xb41b('0x21')]([_0x36dda8,_0x181cbd])['then'](function(_0x26ef96){var _0xb68029=_0x26ef96[0x0];var _0x5069aa=_0x26ef96[0x1];var _0xe64568=_['filter'](_0xb68029[_0xb41b('0x22')],function(_0x31d4cd){return _0x31d4cd['UserId']==_0x5069aa['id']&&_0x31d4cd[_0xb41b('0x23')]==_0x2a5286;});if(_0xe64568['length']==0x1){_0x46aecf(_0x5069aa);}else{_0x6b98aa(new RangeError(_0xb41b('0x24')+_0x2a5286+_0xb41b('0x25')));}});});}exports['originate']=function(_0x2c0a8d,_0x41b326){logger[_0xb41b('0x10')](_0xb41b('0x26'),_0x2c0a8d[_0xb41b('0x27')]);var _0x195d63=originateValidation[_0xb41b('0x28')](_0x2c0a8d[_0xb41b('0x27')]);if(_0x195d63[_0xb41b('0x29')]>0x0){return _0x41b326[_0xb41b('0x15')](0x190)['json'](_0x195d63);}var _0x976d56=_0x2c0a8d['body']['userId'];var _0x305803=_0x2c0a8d[_0xb41b('0x27')][_0xb41b('0x2a')];var _0x18e70e=_0x2c0a8d[_0xb41b('0x27')][_0xb41b('0x2b')];var _0x1de31b=_0x2c0a8d[_0xb41b('0x27')][_0xb41b('0x2c')];return getAgent(_0x976d56)[_0xb41b('0x19')](function(_0x3a06a0){logger['info'](_0xb41b('0x2d'),_0x3a06a0[_0xb41b('0x14')]);emit(util[_0xb41b('0x2e')](_0xb41b('0x2f'),_0x3a06a0[_0xb41b('0x14')]),_0xb41b('0x30'),{'callNumber':_0x305803,'callerId':_0x18e70e,'callbackUrl':_0x1de31b});return _0x41b326['status'](0xc8)['json']({'error':![],'message':_0xb41b('0x31')});})[_0xb41b('0x32')](handleError(_0x41b326));};exports['hangup']=function(_0x19c835,_0x47363e){logger[_0xb41b('0x10')](_0xb41b('0x33'),_0x19c835[_0xb41b('0x27')]);var _0x47c8bf=hangupValidation[_0xb41b('0x28')](_0x19c835['body']);if(_0x47c8bf[_0xb41b('0x29')]>0x0){return _0x47363e[_0xb41b('0x15')](0x190)[_0xb41b('0x34')](_0x47c8bf);}var _0x40789b=_0x19c835[_0xb41b('0x27')][_0xb41b('0x35')];var _0x428feb=_0x19c835[_0xb41b('0x27')][_0xb41b('0x23')];return getAgent(_0x40789b)['then'](function(_0x49e8c4){logger['info'](_0xb41b('0x2d'),_0x49e8c4['name']);emit(util['format'](_0xb41b('0x2f'),_0x49e8c4['name']),_0xb41b('0x36'),{'sessionId':_0x428feb});return _0x47363e[_0xb41b('0x15')](0xc8)[_0xb41b('0x34')]({'error':![],'message':_0xb41b('0x37')});})[_0xb41b('0x32')](handleError(_0x47363e));};exports[_0xb41b('0x38')]=function(_0xddb59c,_0x57e043){logger[_0xb41b('0x10')](_0xb41b('0x39'),_0xddb59c[_0xb41b('0x27')]);var _0x174d6c=answerValidation['validate'](_0xddb59c[_0xb41b('0x27')]);if(_0x174d6c['length']>0x0){return _0x57e043[_0xb41b('0x15')](0x190)[_0xb41b('0x34')](_0x174d6c);}var _0x46dbbf=_0xddb59c['body']['userId'];var _0xe71120=_0xddb59c[_0xb41b('0x27')][_0xb41b('0x23')];return getAgent(_0x46dbbf)[_0xb41b('0x19')](function(_0x412ec0){logger[_0xb41b('0x10')](_0xb41b('0x2d'),_0x412ec0['name']);emit(util['format'](_0xb41b('0x2f'),_0x412ec0['name']),_0xb41b('0x3a'),{'sessionId':_0xe71120});return _0x57e043['status'](0xc8)[_0xb41b('0x34')]({'error':![],'message':'answer\x20executed!'});})[_0xb41b('0x32')](handleError(_0x57e043));};exports['hold']=function(_0x606eff,_0x104825){logger[_0xb41b('0x10')](_0xb41b('0x3b'),_0x606eff['body']);var _0x2f8442=holdValidation[_0xb41b('0x28')](_0x606eff['body']);if(_0x2f8442[_0xb41b('0x29')]>0x0){return _0x104825['status'](0x190)['json'](_0x2f8442);}var _0x5733d0=_0x606eff['body'][_0xb41b('0x35')];var _0x23ea63=_0x606eff[_0xb41b('0x27')][_0xb41b('0x23')];return getAgent(_0x5733d0)[_0xb41b('0x19')](function(_0x349a6c){logger[_0xb41b('0x10')](_0xb41b('0x2d'),_0x349a6c[_0xb41b('0x14')]);emit(util[_0xb41b('0x2e')](_0xb41b('0x2f'),_0x349a6c[_0xb41b('0x14')]),_0xb41b('0x3c'),{'sessionId':_0x23ea63});return _0x104825[_0xb41b('0x15')](0xc8)[_0xb41b('0x34')]({'error':![],'message':_0xb41b('0x3d')});})['catch'](handleError(_0x104825));};exports[_0xb41b('0x3e')]=function(_0x5e041d,_0x4a7aa3){logger[_0xb41b('0x10')](_0xb41b('0x3f'),_0x5e041d[_0xb41b('0x27')]);var _0x1b576f=unholdValidation[_0xb41b('0x28')](_0x5e041d[_0xb41b('0x27')]);if(_0x1b576f[_0xb41b('0x29')]>0x0){return _0x4a7aa3[_0xb41b('0x15')](0x190)[_0xb41b('0x34')](_0x1b576f);}var _0x1977b1=_0x5e041d[_0xb41b('0x27')][_0xb41b('0x35')];var _0x236c05=_0x5e041d[_0xb41b('0x27')][_0xb41b('0x23')];return getAgent(_0x1977b1)[_0xb41b('0x19')](function(_0x26ce99){logger[_0xb41b('0x10')](_0xb41b('0x2d'),_0x26ce99[_0xb41b('0x14')]);emit(util[_0xb41b('0x2e')]('user:%s',_0x26ce99[_0xb41b('0x14')]),'webbar:unhold',{'sessionId':_0x236c05});return _0x4a7aa3[_0xb41b('0x15')](0xc8)[_0xb41b('0x34')]({'error':![],'message':_0xb41b('0x40')});})[_0xb41b('0x32')](handleError(_0x4a7aa3));};exports[_0xb41b('0x41')]=function(_0x305f0e,_0x2bae07){logger['info'](_0xb41b('0x42'),_0x2bae07[_0xb41b('0x27')]);var _0x140c71=callsValidation['validate'](_0x305f0e[_0xb41b('0x27')]);if(_0x140c71[_0xb41b('0x29')]>0x0){return _0x2bae07[_0xb41b('0x15')](0x190)[_0xb41b('0x34')](_0x140c71);}var _0x454671=_0x305f0e[_0xb41b('0x27')][_0xb41b('0x35')];var _0x540763=respondWithRpcPromise('VoiceChannels');var _0xaeb50e=getAgent(_0x454671);return Promise[_0xb41b('0x21')]([_0x540763,_0xaeb50e])['then'](function(_0x43ad9c){var _0x285099=_0x43ad9c[0x0];var _0x5a322a=_0x43ad9c[0x1];var _0x2dced1=_[_0xb41b('0x43')](_0x285099[_0xb41b('0x22')],function(_0xb1ccab){return _0xb1ccab[_0xb41b('0x44')]==_0x5a322a['id'];});return _0x2bae07[_0xb41b('0x15')](0xc8)[_0xb41b('0x34')]({'count':_0x2dced1[_0xb41b('0x29')],'rows':_0x2dced1});})[_0xb41b('0x32')](handleError(_0x2bae07));};exports[_0xb41b('0x45')]=function(_0x5c5e0d,_0x154fa5){logger[_0xb41b('0x10')]('conference\x20fired:',_0x5c5e0d['body']);var _0x2ad478=conferenceValidation[_0xb41b('0x28')](_0x5c5e0d[_0xb41b('0x27')]);if(_0x2ad478['length']>0x0){return _0x154fa5[_0xb41b('0x15')](0x190)[_0xb41b('0x34')](_0x2ad478);}var _0x1451e7=_0x5c5e0d[_0xb41b('0x27')][_0xb41b('0x35')];return getAgent(_0x1451e7)[_0xb41b('0x19')](function(_0x9bae17){logger[_0xb41b('0x10')](_0xb41b('0x2d'),_0x9bae17[_0xb41b('0x14')]);emit(util['format'](_0xb41b('0x2f'),_0x9bae17[_0xb41b('0x14')]),'webbar:conference');return _0x154fa5[_0xb41b('0x15')](0xc8)[_0xb41b('0x34')]({'error':![],'message':_0xb41b('0x46')});})['catch'](handleError(_0x154fa5));};exports[_0xb41b('0x47')]=function(_0x43edc7,_0x10d953){logger[_0xb41b('0x10')](_0xb41b('0x48'),_0x43edc7[_0xb41b('0x27')]);var _0x3c663a=transferValidation[_0xb41b('0x28')](_0x43edc7[_0xb41b('0x27')]);if(_0x3c663a[_0xb41b('0x29')]>0x0){return _0x10d953['status'](0x190)['json'](_0x3c663a);}var _0x4be316=_0x43edc7['body'][_0xb41b('0x35')];var _0x4ad6b5=_0x43edc7[_0xb41b('0x27')][_0xb41b('0x23')];var _0x46eed9=_0x43edc7[_0xb41b('0x27')]['transferNumber'];return getAgent(_0x4be316)[_0xb41b('0x19')](function(_0x5e34a2){logger[_0xb41b('0x10')]('entityFound:',_0x5e34a2[_0xb41b('0x14')]);emit(util['format'](_0xb41b('0x2f'),_0x5e34a2[_0xb41b('0x14')]),'webbar:transfer',{'sessionId':_0x4ad6b5,'transferNumber':_0x46eed9});return _0x10d953[_0xb41b('0x15')](0xc8)[_0xb41b('0x34')]({'error':![],'message':_0xb41b('0x49')});})[_0xb41b('0x32')](handleError(_0x10d953));};