aded2f6ee14610474da6b41b35b50b1e1d814959
[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 _0xbf04=['then','code','message','result','catch','User','findOne','VoiceChannels','all','filter','rows','UserId','sessionId','length','sessionId\x20','\x20not\x20found!','info','originate\x20fired:','body','userId','callerId','callbackUrl','entityFound:','format','user:%s','json','originate\x20executed!','hangup','hangup\x20fired:','hangup\x20executed!','answer','webbar:answer','answer\x20executed!','hold','webbar:hold','unhold','unhold\x20fired:','webbar:unhold','calls\x20fired:','conference','conference\x20fired:','webbar:conference','conference\x20executed!','transfer','transfer\x20fired:','webbar:transfer','transfer\x20executed!','path','lodash','../../mysqldb','../../config/environment','bluebird','jayson/promise','../../config/logger','webbarapi','redis','defaults','localhost','validate','number','userId\x20is\x20required','string','sessionId\x20is\x20required','transferNumber\x20is\x20required','stringify','log','handleError:','error','stack','name','status','send','NotFound'];(function(_0x3299d1,_0x545733){var _0x44356e=function(_0x52fd77){while(--_0x52fd77){_0x3299d1['push'](_0x3299d1['shift']());}};_0x44356e(++_0x545733);}(_0xbf04,0xc1));var _0x4bf0=function(_0x9bc2e0,_0x153cf8){_0x9bc2e0=_0x9bc2e0-0x0;var _0x52c05f=_0xbf04[_0x9bc2e0];return _0x52c05f;};'use strict';var path=require(_0x4bf0('0x0'));var fs=require('fs');var _=require(_0x4bf0('0x1'));var db=require(_0x4bf0('0x2'))['db'];var config=require(_0x4bf0('0x3'));var util=require('util');var BPromise=require(_0x4bf0('0x4'));var jayson=require(_0x4bf0('0x5'));var amiClient=jayson['client']['http']({'port':0x232a});var logger=require(_0x4bf0('0x6'))(_0x4bf0('0x7'));var Redis=require('ioredis');config[_0x4bf0('0x8')]=_[_0x4bf0('0x9')](config[_0x4bf0('0x8')],{'host':_0x4bf0('0xa'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x4bf0('0x8')]));var Schema=require(_0x4bf0('0xb'));var originateValidation=new Schema({'userId':{'type':_0x4bf0('0xc'),'required':[!![],_0x4bf0('0xd')]},'callNumber':{'type':_0x4bf0('0xe'),'required':[!![],'callNumber\x20is\x20required']},'callerId':{'type':_0x4bf0('0xe')},'callbackUrl':{'type':'string'}});var hangupValidation=new Schema({'userId':{'type':_0x4bf0('0xc'),'required':[!![],_0x4bf0('0xd')]},'sessionId':{'type':_0x4bf0('0xe'),'required':![]}});var answerValidation=new Schema({'userId':{'type':_0x4bf0('0xc'),'required':[!![],'userId\x20is\x20required']},'sessionId':{'type':_0x4bf0('0xe'),'required':![]}});var transferValidation=new Schema({'userId':{'type':'number','required':[!![],'userId\x20is\x20required']},'sessionId':{'type':_0x4bf0('0xe'),'required':[!![],_0x4bf0('0xf')]},'transferNumber':{'type':_0x4bf0('0xe'),'required':[!![],_0x4bf0('0x10')]}});var holdValidation=new Schema({'userId':{'type':'number','required':[!![],'userId\x20is\x20required']},'sessionId':{'type':'string','required':![]}});var unholdValidation=new Schema({'userId':{'type':_0x4bf0('0xc'),'required':[!![],_0x4bf0('0xd')]},'sessionId':{'type':_0x4bf0('0xe'),'required':![]}});var callsValidation=new Schema({'userId':{'type':_0x4bf0('0xc'),'required':[!![],_0x4bf0('0xd')]}});var conferenceValidation=new Schema({'userId':{'type':_0x4bf0('0xc'),'required':[!![],_0x4bf0('0xd')]}});function emit(_0x56c73a,_0x11e0fb,_0x5e678c){logger['info'](_0x56c73a,_0x11e0fb,JSON[_0x4bf0('0x11')](_0x5e678c));io['to'](_0x56c73a)['emit'](_0x11e0fb,_0x5e678c);}function handleError(_0x8d8802){return function(_0x365e72){console[_0x4bf0('0x12')](_0x4bf0('0x13'),_0x365e72,_0x365e72 instanceof RangeError);logger[_0x4bf0('0x14')](_0x365e72[_0x4bf0('0x15')]);if(_0x365e72 instanceof RangeError){if(_0x365e72['name']){delete _0x365e72[_0x4bf0('0x16')];}_0x8d8802[_0x4bf0('0x17')](0x194)[_0x4bf0('0x18')](_0x365e72);}else{if(_0x365e72['name']){delete _0x365e72['name'];}_0x8d8802[_0x4bf0('0x17')](0x1f4)['send'](_0x365e72);}};}function checkError(){return function(_0x25a1d1){if(!_0x25a1d1){throw new RangeError(_0x4bf0('0x19'));}return _0x25a1d1;};}function respondWithRpcPromise(_0x5e3fdd,_0x1c269c){return new BPromise(function(_0x488b71,_0x4a5be8){var _0x35cbee=amiClient;return _0x35cbee['request'](_0x5e3fdd,_0x1c269c)[_0x4bf0('0x1a')](function(_0x4c2835){if(_0x4c2835[_0x4bf0('0x14')]){if(_0x4c2835[_0x4bf0('0x14')][_0x4bf0('0x1b')]===0x1f4){return _0x4a5be8(_0x4c2835['error'][_0x4bf0('0x1c')]);}return _0x488b71(_0x4c2835[_0x4bf0('0x14')][_0x4bf0('0x1c')]);}else{_0x488b71(_0x4c2835[_0x4bf0('0x1d')]);}})[_0x4bf0('0x1e')](function(_0x2ec628){_0x4a5be8(_0x2ec628);});});}function getAgent(_0x3b535f){return db[_0x4bf0('0x1f')][_0x4bf0('0x20')]({'where':{'id':_0x3b535f,'role':'agent'}})[_0x4bf0('0x1a')](checkError());}function getAgentCheckSession(_0xd86467,_0x4af1cd){return new BPromise(function(_0x1d4e37,_0x4d8e60){var _0x2773da=respondWithRpcPromise(_0x4bf0('0x21'));var _0x4de014=getAgent(_0xd86467);Promise[_0x4bf0('0x22')]([_0x2773da,_0x4de014])[_0x4bf0('0x1a')](function(_0x479cdf){var _0x4e53f2=_0x479cdf[0x0];var _0x5dda42=_0x479cdf[0x1];var _0x332a66=_[_0x4bf0('0x23')](_0x4e53f2[_0x4bf0('0x24')],function(_0x25909){return _0x25909[_0x4bf0('0x25')]==_0x5dda42['id']&&_0x25909[_0x4bf0('0x26')]==_0x4af1cd;});if(_0x332a66[_0x4bf0('0x27')]==0x1){_0x1d4e37(_0x5dda42);}else{_0x4d8e60(new RangeError(_0x4bf0('0x28')+_0x4af1cd+_0x4bf0('0x29')));}});});}exports['originate']=function(_0x2abc1e,_0x1868e4){logger[_0x4bf0('0x2a')](_0x4bf0('0x2b'),_0x2abc1e[_0x4bf0('0x2c')]);var _0x522b80=originateValidation[_0x4bf0('0xb')](_0x2abc1e[_0x4bf0('0x2c')]);if(_0x522b80[_0x4bf0('0x27')]>0x0){return _0x1868e4[_0x4bf0('0x17')](0x190)['json'](_0x522b80);}var _0x18c916=_0x2abc1e[_0x4bf0('0x2c')][_0x4bf0('0x2d')];var _0x22ad76=_0x2abc1e['body']['callNumber'];var _0x5f4b85=_0x2abc1e[_0x4bf0('0x2c')][_0x4bf0('0x2e')];var _0x3cdf5f=_0x2abc1e[_0x4bf0('0x2c')][_0x4bf0('0x2f')];return getAgent(_0x18c916)[_0x4bf0('0x1a')](function(_0x2c6d89){logger['info'](_0x4bf0('0x30'),_0x2c6d89[_0x4bf0('0x16')]);emit(util[_0x4bf0('0x31')](_0x4bf0('0x32'),_0x2c6d89[_0x4bf0('0x16')]),'webbar:originate',{'callNumber':_0x22ad76,'callerId':_0x5f4b85,'callbackUrl':_0x3cdf5f});return _0x1868e4[_0x4bf0('0x17')](0xc8)[_0x4bf0('0x33')]({'error':![],'message':_0x4bf0('0x34')});})['catch'](handleError(_0x1868e4));};exports[_0x4bf0('0x35')]=function(_0x2ba575,_0x578ca9){logger[_0x4bf0('0x2a')](_0x4bf0('0x36'),_0x2ba575['body']);var _0x34f478=hangupValidation['validate'](_0x2ba575['body']);if(_0x34f478[_0x4bf0('0x27')]>0x0){return _0x578ca9['status'](0x190)[_0x4bf0('0x33')](_0x34f478);}var _0x59bb06=_0x2ba575[_0x4bf0('0x2c')][_0x4bf0('0x2d')];var _0x218c32=_0x2ba575[_0x4bf0('0x2c')]['sessionId'];return getAgent(_0x59bb06)[_0x4bf0('0x1a')](function(_0x493da4){logger['info'](_0x4bf0('0x30'),_0x493da4[_0x4bf0('0x16')]);emit(util['format'](_0x4bf0('0x32'),_0x493da4[_0x4bf0('0x16')]),'webbar:hangup',{'sessionId':_0x218c32});return _0x578ca9[_0x4bf0('0x17')](0xc8)['json']({'error':![],'message':_0x4bf0('0x37')});})[_0x4bf0('0x1e')](handleError(_0x578ca9));};exports[_0x4bf0('0x38')]=function(_0xc31c13,_0x13ca49){logger['info']('answer\x20fired:',_0xc31c13['body']);var _0x458911=answerValidation['validate'](_0xc31c13[_0x4bf0('0x2c')]);if(_0x458911[_0x4bf0('0x27')]>0x0){return _0x13ca49['status'](0x190)[_0x4bf0('0x33')](_0x458911);}var _0x564c5a=_0xc31c13[_0x4bf0('0x2c')][_0x4bf0('0x2d')];var _0x3b02c5=_0xc31c13[_0x4bf0('0x2c')][_0x4bf0('0x26')];return getAgent(_0x564c5a)[_0x4bf0('0x1a')](function(_0x422b63){logger[_0x4bf0('0x2a')](_0x4bf0('0x30'),_0x422b63[_0x4bf0('0x16')]);emit(util[_0x4bf0('0x31')]('user:%s',_0x422b63[_0x4bf0('0x16')]),_0x4bf0('0x39'),{'sessionId':_0x3b02c5});return _0x13ca49[_0x4bf0('0x17')](0xc8)[_0x4bf0('0x33')]({'error':![],'message':_0x4bf0('0x3a')});})[_0x4bf0('0x1e')](handleError(_0x13ca49));};exports[_0x4bf0('0x3b')]=function(_0x3ed00b,_0x47521f){logger[_0x4bf0('0x2a')]('hold\x20fired:',_0x3ed00b[_0x4bf0('0x2c')]);var _0x622e4b=holdValidation[_0x4bf0('0xb')](_0x3ed00b[_0x4bf0('0x2c')]);if(_0x622e4b[_0x4bf0('0x27')]>0x0){return _0x47521f[_0x4bf0('0x17')](0x190)[_0x4bf0('0x33')](_0x622e4b);}var _0x5c5157=_0x3ed00b[_0x4bf0('0x2c')]['userId'];var _0x254a8d=_0x3ed00b[_0x4bf0('0x2c')][_0x4bf0('0x26')];return getAgent(_0x5c5157)[_0x4bf0('0x1a')](function(_0x52dc0){logger['info'](_0x4bf0('0x30'),_0x52dc0['name']);emit(util[_0x4bf0('0x31')](_0x4bf0('0x32'),_0x52dc0[_0x4bf0('0x16')]),_0x4bf0('0x3c'),{'sessionId':_0x254a8d});return _0x47521f[_0x4bf0('0x17')](0xc8)[_0x4bf0('0x33')]({'error':![],'message':'hold\x20executed!'});})['catch'](handleError(_0x47521f));};exports[_0x4bf0('0x3d')]=function(_0x4209a5,_0xd3611c){logger[_0x4bf0('0x2a')](_0x4bf0('0x3e'),_0x4209a5['body']);var _0x566fe9=unholdValidation[_0x4bf0('0xb')](_0x4209a5['body']);if(_0x566fe9[_0x4bf0('0x27')]>0x0){return _0xd3611c[_0x4bf0('0x17')](0x190)[_0x4bf0('0x33')](_0x566fe9);}var _0x24af84=_0x4209a5[_0x4bf0('0x2c')][_0x4bf0('0x2d')];var _0x274a31=_0x4209a5[_0x4bf0('0x2c')]['sessionId'];return getAgent(_0x24af84)[_0x4bf0('0x1a')](function(_0x3e4755){logger[_0x4bf0('0x2a')](_0x4bf0('0x30'),_0x3e4755[_0x4bf0('0x16')]);emit(util[_0x4bf0('0x31')](_0x4bf0('0x32'),_0x3e4755[_0x4bf0('0x16')]),_0x4bf0('0x3f'),{'sessionId':_0x274a31});return _0xd3611c['status'](0xc8)[_0x4bf0('0x33')]({'error':![],'message':'unhold\x20executed!'});})[_0x4bf0('0x1e')](handleError(_0xd3611c));};exports['calls']=function(_0x1f3df3,_0x5150c0){logger[_0x4bf0('0x2a')](_0x4bf0('0x40'),_0x5150c0[_0x4bf0('0x2c')]);var _0x311ad4=callsValidation[_0x4bf0('0xb')](_0x1f3df3['body']);if(_0x311ad4['length']>0x0){return _0x5150c0[_0x4bf0('0x17')](0x190)['json'](_0x311ad4);}var _0x3c26b5=_0x1f3df3['body']['userId'];var _0x2a4550=respondWithRpcPromise('VoiceChannels');var _0x560064=getAgent(_0x3c26b5);return Promise[_0x4bf0('0x22')]([_0x2a4550,_0x560064])['then'](function(_0x243527){var _0x293ff0=_0x243527[0x0];var _0x502bfa=_0x243527[0x1];var _0x57b45f=_[_0x4bf0('0x23')](_0x293ff0[_0x4bf0('0x24')],function(_0xbf04f7){return _0xbf04f7['UserId']==_0x502bfa['id'];});return _0x5150c0[_0x4bf0('0x17')](0xc8)[_0x4bf0('0x33')]({'count':_0x57b45f[_0x4bf0('0x27')],'rows':_0x57b45f});})['catch'](handleError(_0x5150c0));};exports[_0x4bf0('0x41')]=function(_0x1a625a,_0x3d4863){logger[_0x4bf0('0x2a')](_0x4bf0('0x42'),_0x1a625a[_0x4bf0('0x2c')]);var _0x3a1b0b=conferenceValidation[_0x4bf0('0xb')](_0x1a625a['body']);if(_0x3a1b0b['length']>0x0){return _0x3d4863[_0x4bf0('0x17')](0x190)[_0x4bf0('0x33')](_0x3a1b0b);}var _0x33834c=_0x1a625a[_0x4bf0('0x2c')][_0x4bf0('0x2d')];return getAgent(_0x33834c)['then'](function(_0x164288){logger[_0x4bf0('0x2a')](_0x4bf0('0x30'),_0x164288['name']);emit(util[_0x4bf0('0x31')](_0x4bf0('0x32'),_0x164288['name']),_0x4bf0('0x43'));return _0x3d4863[_0x4bf0('0x17')](0xc8)['json']({'error':![],'message':_0x4bf0('0x44')});})[_0x4bf0('0x1e')](handleError(_0x3d4863));};exports[_0x4bf0('0x45')]=function(_0x4ec516,_0x23afd1){logger[_0x4bf0('0x2a')](_0x4bf0('0x46'),_0x4ec516[_0x4bf0('0x2c')]);var _0x2cdee4=transferValidation[_0x4bf0('0xb')](_0x4ec516['body']);if(_0x2cdee4['length']>0x0){return _0x23afd1[_0x4bf0('0x17')](0x190)[_0x4bf0('0x33')](_0x2cdee4);}var _0x531808=_0x4ec516[_0x4bf0('0x2c')][_0x4bf0('0x2d')];var _0x37aef7=_0x4ec516[_0x4bf0('0x2c')][_0x4bf0('0x26')];var _0x1d8843=_0x4ec516[_0x4bf0('0x2c')]['transferNumber'];return getAgent(_0x531808)[_0x4bf0('0x1a')](function(_0x3ba6c5){logger[_0x4bf0('0x2a')]('entityFound:',_0x3ba6c5['name']);emit(util['format'](_0x4bf0('0x32'),_0x3ba6c5[_0x4bf0('0x16')]),_0x4bf0('0x47'),{'sessionId':_0x37aef7,'transferNumber':_0x1d8843});return _0x23afd1['status'](0xc8)[_0x4bf0('0x33')]({'error':![],'message':_0x4bf0('0x48')});})[_0x4bf0('0x1e')](handleError(_0x23afd1));};