03c14469e90d22e25bca0329cfaa79c7a1d61e3b
[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 _0xf290=['lodash','../../config/environment','bluebird','jayson/promise','client','http','../../config/logger','ioredis','defaults','redis','localhost','socket.io-emitter','validate','number','userId\x20is\x20required','string','callNumber\x20is\x20required','sessionId\x20is\x20required','transferNumber\x20is\x20required','info','stringify','log','name','status','send','then','error','code','message','result','catch','User','VoiceChannels','all','rows','sessionId','length','sessionId\x20','\x20not\x20found!','body','json','userId','callerId','callbackUrl','entityFound:','user:%s','webbar:originate','originate\x20executed!','hangup','hangup\x20fired:','format','hangup\x20executed!','answer\x20fired:','webbar:answer','answer\x20executed!','hold','hold\x20fired:','webbar:hold','hold\x20executed!','unhold','unhold\x20fired:','unhold\x20executed!','calls','calls\x20fired:','filter','conference','conference\x20fired:','webbar:conference','conference\x20executed!','transfer','transfer\x20fired:','transferNumber','webbar:transfer','transfer\x20executed!'];(function(_0x7bdda5,_0x3358f5){var _0x45be29=function(_0x7b64ff){while(--_0x7b64ff){_0x7bdda5['push'](_0x7bdda5['shift']());}};_0x45be29(++_0x3358f5);}(_0xf290,0xde));var _0x0f29=function(_0x219d87,_0x1cb6f7){_0x219d87=_0x219d87-0x0;var _0x22823d=_0xf290[_0x219d87];return _0x22823d;};'use strict';var path=require('path');var fs=require('fs');var _=require(_0x0f29('0x0'));var db=require('../../mysqldb')['db'];var config=require(_0x0f29('0x1'));var util=require('util');var BPromise=require(_0x0f29('0x2'));var jayson=require(_0x0f29('0x3'));var amiClient=jayson[_0x0f29('0x4')][_0x0f29('0x5')]({'port':0x232a});var logger=require(_0x0f29('0x6'))('webbarapi');var Redis=require(_0x0f29('0x7'));config['redis']=_[_0x0f29('0x8')](config[_0x0f29('0x9')],{'host':_0x0f29('0xa'),'port':0x18eb});var io=require(_0x0f29('0xb'))(new Redis(config[_0x0f29('0x9')]));var Schema=require(_0x0f29('0xc'));var originateValidation=new Schema({'userId':{'type':_0x0f29('0xd'),'required':[!![],_0x0f29('0xe')]},'callNumber':{'type':_0x0f29('0xf'),'required':[!![],_0x0f29('0x10')]},'callerId':{'type':_0x0f29('0xf')},'callbackUrl':{'type':_0x0f29('0xf')}});var hangupValidation=new Schema({'userId':{'type':_0x0f29('0xd'),'required':[!![],_0x0f29('0xe')]},'sessionId':{'type':_0x0f29('0xf'),'required':![]}});var answerValidation=new Schema({'userId':{'type':'number','required':[!![],_0x0f29('0xe')]},'sessionId':{'type':'string','required':![]}});var transferValidation=new Schema({'userId':{'type':_0x0f29('0xd'),'required':[!![],_0x0f29('0xe')]},'sessionId':{'type':_0x0f29('0xf'),'required':[!![],_0x0f29('0x11')]},'transferNumber':{'type':_0x0f29('0xf'),'required':[!![],_0x0f29('0x12')]}});var holdValidation=new Schema({'userId':{'type':'number','required':[!![],_0x0f29('0xe')]},'sessionId':{'type':_0x0f29('0xf'),'required':![]}});var unholdValidation=new Schema({'userId':{'type':_0x0f29('0xd'),'required':[!![],_0x0f29('0xe')]},'sessionId':{'type':_0x0f29('0xf'),'required':![]}});var callsValidation=new Schema({'userId':{'type':_0x0f29('0xd'),'required':[!![],_0x0f29('0xe')]}});var conferenceValidation=new Schema({'userId':{'type':_0x0f29('0xd'),'required':[!![],'userId\x20is\x20required']}});function emit(_0x4ac37d,_0x2be27f,_0x3151a5){logger[_0x0f29('0x13')](_0x4ac37d,_0x2be27f,JSON[_0x0f29('0x14')](_0x3151a5));io['to'](_0x4ac37d)['emit'](_0x2be27f,_0x3151a5);}function handleError(_0x122bfa){return function(_0x5d72a5){console[_0x0f29('0x15')]('handleError:',_0x5d72a5,_0x5d72a5 instanceof RangeError);logger['error'](_0x5d72a5['stack']);if(_0x5d72a5 instanceof RangeError){if(_0x5d72a5[_0x0f29('0x16')]){delete _0x5d72a5[_0x0f29('0x16')];}_0x122bfa[_0x0f29('0x17')](0x194)[_0x0f29('0x18')](_0x5d72a5);}else{if(_0x5d72a5[_0x0f29('0x16')]){delete _0x5d72a5[_0x0f29('0x16')];}_0x122bfa[_0x0f29('0x17')](0x1f4)['send'](_0x5d72a5);}};}function checkError(){return function(_0x49d954){if(!_0x49d954){throw new RangeError('NotFound');}return _0x49d954;};}function respondWithRpcPromise(_0x5ab592,_0x55d92e){return new BPromise(function(_0x5d94b9,_0x38a1ce){var _0x4a9dd8=amiClient;return _0x4a9dd8['request'](_0x5ab592,_0x55d92e)[_0x0f29('0x19')](function(_0x1840e0){if(_0x1840e0[_0x0f29('0x1a')]){if(_0x1840e0[_0x0f29('0x1a')][_0x0f29('0x1b')]===0x1f4){return _0x38a1ce(_0x1840e0['error'][_0x0f29('0x1c')]);}return _0x5d94b9(_0x1840e0['error'][_0x0f29('0x1c')]);}else{_0x5d94b9(_0x1840e0[_0x0f29('0x1d')]);}})[_0x0f29('0x1e')](function(_0x28cfaa){_0x38a1ce(_0x28cfaa);});});}function getAgent(_0x57667b){return db[_0x0f29('0x1f')]['findOne']({'where':{'id':_0x57667b,'role':'agent'}})[_0x0f29('0x19')](checkError());}function getAgentCheckSession(_0x5c0202,_0x5bfcac){return new BPromise(function(_0x57dcc7,_0x4dd0b0){var _0x3c6ce9=respondWithRpcPromise(_0x0f29('0x20'));var _0x4cc5a0=getAgent(_0x5c0202);Promise[_0x0f29('0x21')]([_0x3c6ce9,_0x4cc5a0])[_0x0f29('0x19')](function(_0x274c9d){var _0x51e7dc=_0x274c9d[0x0];var _0x56e729=_0x274c9d[0x1];var _0x37ebcc=_['filter'](_0x51e7dc[_0x0f29('0x22')],function(_0x561c31){return _0x561c31['UserId']==_0x56e729['id']&&_0x561c31[_0x0f29('0x23')]==_0x5bfcac;});if(_0x37ebcc[_0x0f29('0x24')]==0x1){_0x57dcc7(_0x56e729);}else{_0x4dd0b0(new RangeError(_0x0f29('0x25')+_0x5bfcac+_0x0f29('0x26')));}});});}exports['originate']=function(_0x52a501,_0x3bb3c8){logger['info']('originate\x20fired:',_0x52a501[_0x0f29('0x27')]);var _0x5baebb=originateValidation[_0x0f29('0xc')](_0x52a501['body']);if(_0x5baebb['length']>0x0){return _0x3bb3c8[_0x0f29('0x17')](0x190)[_0x0f29('0x28')](_0x5baebb);}var _0x3ea452=_0x52a501[_0x0f29('0x27')][_0x0f29('0x29')];var _0x4d0504=_0x52a501[_0x0f29('0x27')]['callNumber'];var _0x2cc510=_0x52a501[_0x0f29('0x27')][_0x0f29('0x2a')];var _0x1320a3=_0x52a501['body'][_0x0f29('0x2b')];return getAgent(_0x3ea452)[_0x0f29('0x19')](function(_0x5be4fa){logger[_0x0f29('0x13')](_0x0f29('0x2c'),_0x5be4fa['name']);emit(util['format'](_0x0f29('0x2d'),_0x5be4fa[_0x0f29('0x16')]),_0x0f29('0x2e'),{'callNumber':_0x4d0504,'callerId':_0x2cc510,'callbackUrl':_0x1320a3});return _0x3bb3c8[_0x0f29('0x17')](0xc8)[_0x0f29('0x28')]({'error':![],'message':_0x0f29('0x2f')});})['catch'](handleError(_0x3bb3c8));};exports[_0x0f29('0x30')]=function(_0x5a538e,_0x331dc7){logger['info'](_0x0f29('0x31'),_0x5a538e[_0x0f29('0x27')]);var _0x6d4c82=hangupValidation['validate'](_0x5a538e['body']);if(_0x6d4c82[_0x0f29('0x24')]>0x0){return _0x331dc7['status'](0x190)[_0x0f29('0x28')](_0x6d4c82);}var _0x3f49f8=_0x5a538e[_0x0f29('0x27')][_0x0f29('0x29')];var _0x9ae45d=_0x5a538e['body'][_0x0f29('0x23')];return getAgent(_0x3f49f8)[_0x0f29('0x19')](function(_0x42872b){logger[_0x0f29('0x13')](_0x0f29('0x2c'),_0x42872b[_0x0f29('0x16')]);emit(util[_0x0f29('0x32')](_0x0f29('0x2d'),_0x42872b[_0x0f29('0x16')]),'webbar:hangup',{'sessionId':_0x9ae45d});return _0x331dc7[_0x0f29('0x17')](0xc8)[_0x0f29('0x28')]({'error':![],'message':_0x0f29('0x33')});})[_0x0f29('0x1e')](handleError(_0x331dc7));};exports['answer']=function(_0x10e800,_0xfc9257){logger[_0x0f29('0x13')](_0x0f29('0x34'),_0x10e800[_0x0f29('0x27')]);var _0x56062d=answerValidation[_0x0f29('0xc')](_0x10e800[_0x0f29('0x27')]);if(_0x56062d[_0x0f29('0x24')]>0x0){return _0xfc9257['status'](0x190)[_0x0f29('0x28')](_0x56062d);}var _0x284279=_0x10e800[_0x0f29('0x27')][_0x0f29('0x29')];var _0x35f753=_0x10e800[_0x0f29('0x27')][_0x0f29('0x23')];return getAgent(_0x284279)[_0x0f29('0x19')](function(_0x4b6b6a){logger['info']('entityFound:',_0x4b6b6a[_0x0f29('0x16')]);emit(util[_0x0f29('0x32')](_0x0f29('0x2d'),_0x4b6b6a[_0x0f29('0x16')]),_0x0f29('0x35'),{'sessionId':_0x35f753});return _0xfc9257[_0x0f29('0x17')](0xc8)['json']({'error':![],'message':_0x0f29('0x36')});})['catch'](handleError(_0xfc9257));};exports[_0x0f29('0x37')]=function(_0x57242c,_0x284036){logger[_0x0f29('0x13')](_0x0f29('0x38'),_0x57242c[_0x0f29('0x27')]);var _0x301fc8=holdValidation['validate'](_0x57242c[_0x0f29('0x27')]);if(_0x301fc8[_0x0f29('0x24')]>0x0){return _0x284036[_0x0f29('0x17')](0x190)['json'](_0x301fc8);}var _0x14b747=_0x57242c['body']['userId'];var _0x21c0a4=_0x57242c[_0x0f29('0x27')][_0x0f29('0x23')];return getAgent(_0x14b747)[_0x0f29('0x19')](function(_0x20aa53){logger[_0x0f29('0x13')]('entityFound:',_0x20aa53[_0x0f29('0x16')]);emit(util[_0x0f29('0x32')](_0x0f29('0x2d'),_0x20aa53[_0x0f29('0x16')]),_0x0f29('0x39'),{'sessionId':_0x21c0a4});return _0x284036[_0x0f29('0x17')](0xc8)[_0x0f29('0x28')]({'error':![],'message':_0x0f29('0x3a')});})[_0x0f29('0x1e')](handleError(_0x284036));};exports[_0x0f29('0x3b')]=function(_0x5cd4a9,_0x5e4479){logger[_0x0f29('0x13')](_0x0f29('0x3c'),_0x5cd4a9[_0x0f29('0x27')]);var _0x178f21=unholdValidation['validate'](_0x5cd4a9[_0x0f29('0x27')]);if(_0x178f21[_0x0f29('0x24')]>0x0){return _0x5e4479[_0x0f29('0x17')](0x190)[_0x0f29('0x28')](_0x178f21);}var _0xbf8f22=_0x5cd4a9[_0x0f29('0x27')][_0x0f29('0x29')];var _0x59aeb9=_0x5cd4a9[_0x0f29('0x27')][_0x0f29('0x23')];return getAgent(_0xbf8f22)[_0x0f29('0x19')](function(_0x2c6a95){logger[_0x0f29('0x13')](_0x0f29('0x2c'),_0x2c6a95['name']);emit(util[_0x0f29('0x32')](_0x0f29('0x2d'),_0x2c6a95[_0x0f29('0x16')]),'webbar:unhold',{'sessionId':_0x59aeb9});return _0x5e4479[_0x0f29('0x17')](0xc8)['json']({'error':![],'message':_0x0f29('0x3d')});})['catch'](handleError(_0x5e4479));};exports[_0x0f29('0x3e')]=function(_0x3fcda8,_0x4a490a){logger[_0x0f29('0x13')](_0x0f29('0x3f'),_0x4a490a[_0x0f29('0x27')]);var _0x4a2c08=callsValidation[_0x0f29('0xc')](_0x3fcda8['body']);if(_0x4a2c08[_0x0f29('0x24')]>0x0){return _0x4a490a[_0x0f29('0x17')](0x190)['json'](_0x4a2c08);}var _0x1c46a6=_0x3fcda8[_0x0f29('0x27')]['userId'];var _0xbe013b=respondWithRpcPromise('VoiceChannels');var _0x1baaf8=getAgent(_0x1c46a6);return Promise['all']([_0xbe013b,_0x1baaf8])[_0x0f29('0x19')](function(_0x302d05){var _0x36a7df=_0x302d05[0x0];var _0x4ba5c3=_0x302d05[0x1];var _0x36e714=_[_0x0f29('0x40')](_0x36a7df[_0x0f29('0x22')],function(_0x46c22c){return _0x46c22c['UserId']==_0x4ba5c3['id'];});return _0x4a490a[_0x0f29('0x17')](0xc8)['json']({'count':_0x36e714[_0x0f29('0x24')],'rows':_0x36e714});})['catch'](handleError(_0x4a490a));};exports[_0x0f29('0x41')]=function(_0x3eafc8,_0x306b24){logger[_0x0f29('0x13')](_0x0f29('0x42'),_0x3eafc8[_0x0f29('0x27')]);var _0x272a9e=conferenceValidation['validate'](_0x3eafc8[_0x0f29('0x27')]);if(_0x272a9e['length']>0x0){return _0x306b24[_0x0f29('0x17')](0x190)[_0x0f29('0x28')](_0x272a9e);}var _0x357437=_0x3eafc8['body'][_0x0f29('0x29')];return getAgent(_0x357437)[_0x0f29('0x19')](function(_0x115dbe){logger[_0x0f29('0x13')](_0x0f29('0x2c'),_0x115dbe['name']);emit(util['format'](_0x0f29('0x2d'),_0x115dbe[_0x0f29('0x16')]),_0x0f29('0x43'));return _0x306b24['status'](0xc8)['json']({'error':![],'message':_0x0f29('0x44')});})[_0x0f29('0x1e')](handleError(_0x306b24));};exports[_0x0f29('0x45')]=function(_0x4a7e9f,_0x520e92){logger['info'](_0x0f29('0x46'),_0x4a7e9f[_0x0f29('0x27')]);var _0x49a7b3=transferValidation[_0x0f29('0xc')](_0x4a7e9f[_0x0f29('0x27')]);if(_0x49a7b3['length']>0x0){return _0x520e92[_0x0f29('0x17')](0x190)['json'](_0x49a7b3);}var _0x41f20a=_0x4a7e9f[_0x0f29('0x27')]['userId'];var _0x2e423a=_0x4a7e9f[_0x0f29('0x27')][_0x0f29('0x23')];var _0x2d5ce4=_0x4a7e9f[_0x0f29('0x27')][_0x0f29('0x47')];return getAgent(_0x41f20a)['then'](function(_0x2babb0){logger[_0x0f29('0x13')](_0x0f29('0x2c'),_0x2babb0['name']);emit(util[_0x0f29('0x32')](_0x0f29('0x2d'),_0x2babb0['name']),_0x0f29('0x48'),{'sessionId':_0x2e423a,'transferNumber':_0x2d5ce4});return _0x520e92['status'](0xc8)[_0x0f29('0x28')]({'error':![],'message':_0x0f29('0x49')});})[_0x0f29('0x1e')](handleError(_0x520e92));};