Built motion from commit ba71417a.|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 _0xf021=['conference','conference\x20fired:','webbar:conference','conference\x20executed!','transfer','transfer\x20fired:','transferNumber','webbar:transfer','transfer\x20executed!','path','lodash','../../mysqldb','../../config/environment','util','jayson/promise','http','../../config/logger','redis','localhost','socket.io-emitter','validate','userId\x20is\x20required','string','number','sessionId\x20is\x20required','transferNumber\x20is\x20required','log','handleError:','stack','name','status','NotFound','request','then','error','code','message','result','catch','User','findOne','agent','all','filter','rows','UserId','sessionId','length','sessionId\x20','\x20not\x20found!','info','originate\x20fired:','body','json','userId','callbackUrl','entityFound:','user:%s','webbar:originate','originate\x20executed!','hangup','hangup\x20fired:','format','webbar:hangup','hangup\x20executed!','answer','answer\x20fired:','webbar:answer','answer\x20executed!','hold','hold\x20fired:','webbar:hold','hold\x20executed!','unhold','unhold\x20fired:','webbar:unhold','unhold\x20executed!','VoiceChannels'];(function(_0x4b4f24,_0x5654a8){var _0x1c13d4=function(_0x4940bf){while(--_0x4940bf){_0x4b4f24['push'](_0x4b4f24['shift']());}};_0x1c13d4(++_0x5654a8);}(_0xf021,0x18f));var _0x1f02=function(_0x4e502e,_0x2a7c94){_0x4e502e=_0x4e502e-0x0;var _0x25c957=_0xf021[_0x4e502e];return _0x25c957;};'use strict';var path=require(_0x1f02('0x0'));var fs=require('fs');var _=require(_0x1f02('0x1'));var db=require(_0x1f02('0x2'))['db'];var config=require(_0x1f02('0x3'));var util=require(_0x1f02('0x4'));var BPromise=require('bluebird');var jayson=require(_0x1f02('0x5'));var amiClient=jayson['client'][_0x1f02('0x6')]({'port':0x232a});var logger=require(_0x1f02('0x7'))('webbarapi');var Redis=require('ioredis');config[_0x1f02('0x8')]=_['defaults'](config[_0x1f02('0x8')],{'host':_0x1f02('0x9'),'port':0x18eb});var io=require(_0x1f02('0xa'))(new Redis(config[_0x1f02('0x8')]));var Schema=require(_0x1f02('0xb'));var originateValidation=new Schema({'userId':{'type':'number','required':[!![],_0x1f02('0xc')]},'callNumber':{'type':'string','required':[!![],'callNumber\x20is\x20required']},'callerId':{'type':'string'},'callbackUrl':{'type':_0x1f02('0xd')}});var hangupValidation=new Schema({'userId':{'type':_0x1f02('0xe'),'required':[!![],_0x1f02('0xc')]},'sessionId':{'type':_0x1f02('0xd'),'required':![]}});var answerValidation=new Schema({'userId':{'type':_0x1f02('0xe'),'required':[!![],'userId\x20is\x20required']},'sessionId':{'type':_0x1f02('0xd'),'required':![]}});var transferValidation=new Schema({'userId':{'type':_0x1f02('0xe'),'required':[!![],_0x1f02('0xc')]},'sessionId':{'type':'string','required':[!![],_0x1f02('0xf')]},'transferNumber':{'type':_0x1f02('0xd'),'required':[!![],_0x1f02('0x10')]}});var holdValidation=new Schema({'userId':{'type':'number','required':[!![],'userId\x20is\x20required']},'sessionId':{'type':'string','required':![]}});var unholdValidation=new Schema({'userId':{'type':_0x1f02('0xe'),'required':[!![],_0x1f02('0xc')]},'sessionId':{'type':_0x1f02('0xd'),'required':![]}});var callsValidation=new Schema({'userId':{'type':_0x1f02('0xe'),'required':[!![],_0x1f02('0xc')]}});var conferenceValidation=new Schema({'userId':{'type':_0x1f02('0xe'),'required':[!![],_0x1f02('0xc')]}});function emit(_0x3b0a3d,_0x4b76f7,_0x2e602d){logger['info'](_0x3b0a3d,_0x4b76f7,JSON['stringify'](_0x2e602d));io['to'](_0x3b0a3d)['emit'](_0x4b76f7,_0x2e602d);}function handleError(_0x5f4e55){return function(_0x4d1e4f){console[_0x1f02('0x11')](_0x1f02('0x12'),_0x4d1e4f,_0x4d1e4f instanceof RangeError);logger['error'](_0x4d1e4f[_0x1f02('0x13')]);if(_0x4d1e4f instanceof RangeError){if(_0x4d1e4f[_0x1f02('0x14')]){delete _0x4d1e4f[_0x1f02('0x14')];}_0x5f4e55[_0x1f02('0x15')](0x194)['send'](_0x4d1e4f);}else{if(_0x4d1e4f['name']){delete _0x4d1e4f[_0x1f02('0x14')];}_0x5f4e55[_0x1f02('0x15')](0x1f4)['send'](_0x4d1e4f);}};}function checkError(){return function(_0x5b399a){if(!_0x5b399a){throw new RangeError(_0x1f02('0x16'));}return _0x5b399a;};}function respondWithRpcPromise(_0xbdf82a,_0x2f637d){return new BPromise(function(_0xbabbd7,_0x480297){var _0x12c4af=amiClient;return _0x12c4af[_0x1f02('0x17')](_0xbdf82a,_0x2f637d)[_0x1f02('0x18')](function(_0x2f422c){if(_0x2f422c[_0x1f02('0x19')]){if(_0x2f422c[_0x1f02('0x19')][_0x1f02('0x1a')]===0x1f4){return _0x480297(_0x2f422c['error'][_0x1f02('0x1b')]);}return _0xbabbd7(_0x2f422c[_0x1f02('0x19')]['message']);}else{_0xbabbd7(_0x2f422c[_0x1f02('0x1c')]);}})[_0x1f02('0x1d')](function(_0x575a86){_0x480297(_0x575a86);});});}function getAgent(_0x57bb58){return db[_0x1f02('0x1e')][_0x1f02('0x1f')]({'where':{'id':_0x57bb58,'role':_0x1f02('0x20')}})[_0x1f02('0x18')](checkError());}function getAgentCheckSession(_0x3f5c5e,_0x3f5577){return new BPromise(function(_0x297cf4,_0x4c8891){var _0x4d5f7e=respondWithRpcPromise('VoiceChannels');var _0x3ef4bf=getAgent(_0x3f5c5e);Promise[_0x1f02('0x21')]([_0x4d5f7e,_0x3ef4bf])[_0x1f02('0x18')](function(_0x496afd){var _0x5c2c42=_0x496afd[0x0];var _0x227567=_0x496afd[0x1];var _0x6e88d5=_[_0x1f02('0x22')](_0x5c2c42[_0x1f02('0x23')],function(_0x220345){return _0x220345[_0x1f02('0x24')]==_0x227567['id']&&_0x220345[_0x1f02('0x25')]==_0x3f5577;});if(_0x6e88d5[_0x1f02('0x26')]==0x1){_0x297cf4(_0x227567);}else{_0x4c8891(new RangeError(_0x1f02('0x27')+_0x3f5577+_0x1f02('0x28')));}});});}exports['originate']=function(_0x137e06,_0x5ab7a2){logger[_0x1f02('0x29')](_0x1f02('0x2a'),_0x137e06[_0x1f02('0x2b')]);var _0x2bf78e=originateValidation[_0x1f02('0xb')](_0x137e06[_0x1f02('0x2b')]);if(_0x2bf78e['length']>0x0){return _0x5ab7a2['status'](0x190)[_0x1f02('0x2c')](_0x2bf78e);}var _0x15e655=_0x137e06['body'][_0x1f02('0x2d')];var _0x4d1aec=_0x137e06[_0x1f02('0x2b')]['callNumber'];var _0x3c473f=_0x137e06['body']['callerId'];var _0x26f33e=_0x137e06[_0x1f02('0x2b')][_0x1f02('0x2e')];return getAgent(_0x15e655)[_0x1f02('0x18')](function(_0x55c9a3){logger[_0x1f02('0x29')](_0x1f02('0x2f'),_0x55c9a3[_0x1f02('0x14')]);emit(util['format'](_0x1f02('0x30'),_0x55c9a3[_0x1f02('0x14')]),_0x1f02('0x31'),{'callNumber':_0x4d1aec,'callerId':_0x3c473f,'callbackUrl':_0x26f33e});return _0x5ab7a2[_0x1f02('0x15')](0xc8)[_0x1f02('0x2c')]({'error':![],'message':_0x1f02('0x32')});})['catch'](handleError(_0x5ab7a2));};exports[_0x1f02('0x33')]=function(_0x143e18,_0x1ad162){logger['info'](_0x1f02('0x34'),_0x143e18['body']);var _0x1e4fe5=hangupValidation['validate'](_0x143e18['body']);if(_0x1e4fe5[_0x1f02('0x26')]>0x0){return _0x1ad162['status'](0x190)['json'](_0x1e4fe5);}var _0x3ec5bd=_0x143e18[_0x1f02('0x2b')][_0x1f02('0x2d')];var _0x9b0e0=_0x143e18['body'][_0x1f02('0x25')];return getAgent(_0x3ec5bd)['then'](function(_0x2cd179){logger[_0x1f02('0x29')](_0x1f02('0x2f'),_0x2cd179[_0x1f02('0x14')]);emit(util[_0x1f02('0x35')](_0x1f02('0x30'),_0x2cd179[_0x1f02('0x14')]),_0x1f02('0x36'),{'sessionId':_0x9b0e0});return _0x1ad162[_0x1f02('0x15')](0xc8)['json']({'error':![],'message':_0x1f02('0x37')});})['catch'](handleError(_0x1ad162));};exports[_0x1f02('0x38')]=function(_0x544301,_0x3d895a){logger[_0x1f02('0x29')](_0x1f02('0x39'),_0x544301['body']);var _0x5938e1=answerValidation[_0x1f02('0xb')](_0x544301[_0x1f02('0x2b')]);if(_0x5938e1[_0x1f02('0x26')]>0x0){return _0x3d895a[_0x1f02('0x15')](0x190)[_0x1f02('0x2c')](_0x5938e1);}var _0x15fb49=_0x544301[_0x1f02('0x2b')][_0x1f02('0x2d')];var _0x391b57=_0x544301['body'][_0x1f02('0x25')];return getAgent(_0x15fb49)['then'](function(_0x51c8be){logger[_0x1f02('0x29')]('entityFound:',_0x51c8be['name']);emit(util[_0x1f02('0x35')]('user:%s',_0x51c8be['name']),_0x1f02('0x3a'),{'sessionId':_0x391b57});return _0x3d895a[_0x1f02('0x15')](0xc8)['json']({'error':![],'message':_0x1f02('0x3b')});})[_0x1f02('0x1d')](handleError(_0x3d895a));};exports[_0x1f02('0x3c')]=function(_0x3b1f5b,_0x5d45db){logger[_0x1f02('0x29')](_0x1f02('0x3d'),_0x3b1f5b[_0x1f02('0x2b')]);var _0x4d03ad=holdValidation[_0x1f02('0xb')](_0x3b1f5b[_0x1f02('0x2b')]);if(_0x4d03ad[_0x1f02('0x26')]>0x0){return _0x5d45db[_0x1f02('0x15')](0x190)[_0x1f02('0x2c')](_0x4d03ad);}var _0x282e5b=_0x3b1f5b[_0x1f02('0x2b')][_0x1f02('0x2d')];var _0x1abfaa=_0x3b1f5b[_0x1f02('0x2b')]['sessionId'];return getAgent(_0x282e5b)[_0x1f02('0x18')](function(_0x50d5ed){logger[_0x1f02('0x29')]('entityFound:',_0x50d5ed[_0x1f02('0x14')]);emit(util[_0x1f02('0x35')](_0x1f02('0x30'),_0x50d5ed[_0x1f02('0x14')]),_0x1f02('0x3e'),{'sessionId':_0x1abfaa});return _0x5d45db[_0x1f02('0x15')](0xc8)[_0x1f02('0x2c')]({'error':![],'message':_0x1f02('0x3f')});})[_0x1f02('0x1d')](handleError(_0x5d45db));};exports[_0x1f02('0x40')]=function(_0x1241c5,_0x5792d8){logger[_0x1f02('0x29')](_0x1f02('0x41'),_0x1241c5[_0x1f02('0x2b')]);var _0x102551=unholdValidation[_0x1f02('0xb')](_0x1241c5[_0x1f02('0x2b')]);if(_0x102551[_0x1f02('0x26')]>0x0){return _0x5792d8[_0x1f02('0x15')](0x190)[_0x1f02('0x2c')](_0x102551);}var _0x587d60=_0x1241c5[_0x1f02('0x2b')][_0x1f02('0x2d')];var _0x3e46c6=_0x1241c5[_0x1f02('0x2b')][_0x1f02('0x25')];return getAgent(_0x587d60)['then'](function(_0x12c1fb){logger['info'](_0x1f02('0x2f'),_0x12c1fb[_0x1f02('0x14')]);emit(util[_0x1f02('0x35')](_0x1f02('0x30'),_0x12c1fb[_0x1f02('0x14')]),_0x1f02('0x42'),{'sessionId':_0x3e46c6});return _0x5792d8[_0x1f02('0x15')](0xc8)[_0x1f02('0x2c')]({'error':![],'message':_0x1f02('0x43')});})[_0x1f02('0x1d')](handleError(_0x5792d8));};exports['calls']=function(_0x4a43eb,_0x4d084c){logger[_0x1f02('0x29')]('calls\x20fired:',_0x4d084c[_0x1f02('0x2b')]);var _0x449d58=callsValidation['validate'](_0x4a43eb[_0x1f02('0x2b')]);if(_0x449d58['length']>0x0){return _0x4d084c['status'](0x190)['json'](_0x449d58);}var _0x24192d=_0x4a43eb[_0x1f02('0x2b')][_0x1f02('0x2d')];var _0x390df6=respondWithRpcPromise(_0x1f02('0x44'));var _0x3321b3=getAgent(_0x24192d);return Promise['all']([_0x390df6,_0x3321b3])[_0x1f02('0x18')](function(_0xdd2e2b){var _0x496e3c=_0xdd2e2b[0x0];var _0x5c2745=_0xdd2e2b[0x1];var _0x2b0e0d=_[_0x1f02('0x22')](_0x496e3c[_0x1f02('0x23')],function(_0x47efdf){return _0x47efdf[_0x1f02('0x24')]==_0x5c2745['id'];});return _0x4d084c['status'](0xc8)[_0x1f02('0x2c')]({'count':_0x2b0e0d['length'],'rows':_0x2b0e0d});})[_0x1f02('0x1d')](handleError(_0x4d084c));};exports[_0x1f02('0x45')]=function(_0xd7be3b,_0x2b54ac){logger[_0x1f02('0x29')](_0x1f02('0x46'),_0xd7be3b[_0x1f02('0x2b')]);var _0x4ca1d9=conferenceValidation[_0x1f02('0xb')](_0xd7be3b[_0x1f02('0x2b')]);if(_0x4ca1d9[_0x1f02('0x26')]>0x0){return _0x2b54ac[_0x1f02('0x15')](0x190)[_0x1f02('0x2c')](_0x4ca1d9);}var _0x2e9881=_0xd7be3b[_0x1f02('0x2b')][_0x1f02('0x2d')];return getAgent(_0x2e9881)[_0x1f02('0x18')](function(_0x3a465f){logger[_0x1f02('0x29')](_0x1f02('0x2f'),_0x3a465f['name']);emit(util[_0x1f02('0x35')](_0x1f02('0x30'),_0x3a465f['name']),_0x1f02('0x47'));return _0x2b54ac[_0x1f02('0x15')](0xc8)['json']({'error':![],'message':_0x1f02('0x48')});})[_0x1f02('0x1d')](handleError(_0x2b54ac));};exports[_0x1f02('0x49')]=function(_0x4b3b3e,_0x4b8f28){logger[_0x1f02('0x29')](_0x1f02('0x4a'),_0x4b3b3e[_0x1f02('0x2b')]);var _0x3ce624=transferValidation[_0x1f02('0xb')](_0x4b3b3e[_0x1f02('0x2b')]);if(_0x3ce624[_0x1f02('0x26')]>0x0){return _0x4b8f28[_0x1f02('0x15')](0x190)[_0x1f02('0x2c')](_0x3ce624);}var _0x2ed1d7=_0x4b3b3e['body'][_0x1f02('0x2d')];var _0x5f054b=_0x4b3b3e['body'][_0x1f02('0x25')];var _0xe7dde7=_0x4b3b3e[_0x1f02('0x2b')][_0x1f02('0x4b')];return getAgent(_0x2ed1d7)[_0x1f02('0x18')](function(_0x6b73f5){logger[_0x1f02('0x29')](_0x1f02('0x2f'),_0x6b73f5[_0x1f02('0x14')]);emit(util[_0x1f02('0x35')](_0x1f02('0x30'),_0x6b73f5[_0x1f02('0x14')]),_0x1f02('0x4c'),{'sessionId':_0x5f054b,'transferNumber':_0xe7dde7});return _0x4b8f28['status'](0xc8)['json']({'error':![],'message':_0x1f02('0x4d')});})[_0x1f02('0x1d')](handleError(_0x4b8f28));};