2aa3f5f597e3770fc0a4ba949e97aa37038f028d
[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 _0x0e9f=['answer\x20executed!','hold','hold\x20fired:','unhold','webbar:unhold','unhold\x20executed!','calls','calls\x20fired:','VoiceChannels','conference','webbar:conference','conference\x20executed!','transfer\x20fired:','transferNumber','path','lodash','util','client','webbarapi','ioredis','defaults','localhost','socket.io-emitter','redis','userId\x20is\x20required','string','callNumber\x20is\x20required','number','transferNumber\x20is\x20required','info','stringify','log','handleError:','name','status','request','error','code','message','result','catch','User','then','all','filter','rows','UserId','sessionId','sessionId\x20','\x20not\x20found!','body','validate','length','json','userId','callNumber','callbackUrl','entityFound:','format','user:%s','originate\x20executed!','hangup','hangup\x20fired:','webbar:hangup','answer','answer\x20fired:'];(function(_0x32f489,_0x2bf8c5){var _0x2cac33=function(_0x3aaccd){while(--_0x3aaccd){_0x32f489['push'](_0x32f489['shift']());}};_0x2cac33(++_0x2bf8c5);}(_0x0e9f,0x116));var _0xf0e9=function(_0x1f0172,_0x5b2b8e){_0x1f0172=_0x1f0172-0x0;var _0x580f60=_0x0e9f[_0x1f0172];return _0x580f60;};'use strict';var path=require(_0xf0e9('0x0'));var fs=require('fs');var _=require(_0xf0e9('0x1'));var db=require('../../mysqldb')['db'];var config=require('../../config/environment');var util=require(_0xf0e9('0x2'));var BPromise=require('bluebird');var jayson=require('jayson/promise');var amiClient=jayson[_0xf0e9('0x3')]['http']({'port':0x232a});var logger=require('../../config/logger')(_0xf0e9('0x4'));var Redis=require(_0xf0e9('0x5'));config['redis']=_[_0xf0e9('0x6')](config['redis'],{'host':_0xf0e9('0x7'),'port':0x18eb});var io=require(_0xf0e9('0x8'))(new Redis(config[_0xf0e9('0x9')]));var Schema=require('validate');var originateValidation=new Schema({'userId':{'type':'number','required':[!![],_0xf0e9('0xa')]},'callNumber':{'type':_0xf0e9('0xb'),'required':[!![],_0xf0e9('0xc')]},'callerId':{'type':'string'},'callbackUrl':{'type':_0xf0e9('0xb')}});var hangupValidation=new Schema({'userId':{'type':_0xf0e9('0xd'),'required':[!![],_0xf0e9('0xa')]},'sessionId':{'type':'string','required':![]}});var answerValidation=new Schema({'userId':{'type':_0xf0e9('0xd'),'required':[!![],_0xf0e9('0xa')]},'sessionId':{'type':_0xf0e9('0xb'),'required':![]}});var transferValidation=new Schema({'userId':{'type':_0xf0e9('0xd'),'required':[!![],'userId\x20is\x20required']},'sessionId':{'type':_0xf0e9('0xb'),'required':[!![],'sessionId\x20is\x20required']},'transferNumber':{'type':_0xf0e9('0xb'),'required':[!![],_0xf0e9('0xe')]}});var holdValidation=new Schema({'userId':{'type':'number','required':[!![],_0xf0e9('0xa')]},'sessionId':{'type':_0xf0e9('0xb'),'required':![]}});var unholdValidation=new Schema({'userId':{'type':_0xf0e9('0xd'),'required':[!![],_0xf0e9('0xa')]},'sessionId':{'type':_0xf0e9('0xb'),'required':![]}});var callsValidation=new Schema({'userId':{'type':_0xf0e9('0xd'),'required':[!![],'userId\x20is\x20required']}});var conferenceValidation=new Schema({'userId':{'type':_0xf0e9('0xd'),'required':[!![],_0xf0e9('0xa')]}});function emit(_0x4f37b5,_0x320a29,_0xcf4be0){logger[_0xf0e9('0xf')](_0x4f37b5,_0x320a29,JSON[_0xf0e9('0x10')](_0xcf4be0));io['to'](_0x4f37b5)['emit'](_0x320a29,_0xcf4be0);}function handleError(_0x45a4fb){return function(_0x2f4ee3){console[_0xf0e9('0x11')](_0xf0e9('0x12'),_0x2f4ee3,_0x2f4ee3 instanceof RangeError);logger['error'](_0x2f4ee3['stack']);if(_0x2f4ee3 instanceof RangeError){if(_0x2f4ee3[_0xf0e9('0x13')]){delete _0x2f4ee3[_0xf0e9('0x13')];}_0x45a4fb[_0xf0e9('0x14')](0x194)['send'](_0x2f4ee3);}else{if(_0x2f4ee3['name']){delete _0x2f4ee3[_0xf0e9('0x13')];}_0x45a4fb[_0xf0e9('0x14')](0x1f4)['send'](_0x2f4ee3);}};}function checkError(){return function(_0x3d6c12){if(!_0x3d6c12){throw new RangeError('NotFound');}return _0x3d6c12;};}function respondWithRpcPromise(_0x3916aa,_0x1c956d){return new BPromise(function(_0x4afcff,_0x5dd652){var _0x186fa5=amiClient;return _0x186fa5[_0xf0e9('0x15')](_0x3916aa,_0x1c956d)['then'](function(_0x1cd143){if(_0x1cd143[_0xf0e9('0x16')]){if(_0x1cd143['error'][_0xf0e9('0x17')]===0x1f4){return _0x5dd652(_0x1cd143[_0xf0e9('0x16')]['message']);}return _0x4afcff(_0x1cd143[_0xf0e9('0x16')][_0xf0e9('0x18')]);}else{_0x4afcff(_0x1cd143[_0xf0e9('0x19')]);}})[_0xf0e9('0x1a')](function(_0x33048a){_0x5dd652(_0x33048a);});});}function getAgent(_0x25eacb){return db[_0xf0e9('0x1b')]['findOne']({'where':{'id':_0x25eacb,'role':'agent'}})[_0xf0e9('0x1c')](checkError());}function getAgentCheckSession(_0x4f9e96,_0x553788){return new BPromise(function(_0x5aac7f,_0x8b5167){var _0x8fd443=respondWithRpcPromise('VoiceChannels');var _0x258927=getAgent(_0x4f9e96);Promise[_0xf0e9('0x1d')]([_0x8fd443,_0x258927])[_0xf0e9('0x1c')](function(_0x480b7d){var _0x2fa5e9=_0x480b7d[0x0];var _0x337a59=_0x480b7d[0x1];var _0x439072=_[_0xf0e9('0x1e')](_0x2fa5e9[_0xf0e9('0x1f')],function(_0xf90eea){return _0xf90eea[_0xf0e9('0x20')]==_0x337a59['id']&&_0xf90eea[_0xf0e9('0x21')]==_0x553788;});if(_0x439072['length']==0x1){_0x5aac7f(_0x337a59);}else{_0x8b5167(new RangeError(_0xf0e9('0x22')+_0x553788+_0xf0e9('0x23')));}});});}exports['originate']=function(_0x4822c9,_0x25452f){logger[_0xf0e9('0xf')]('originate\x20fired:',_0x4822c9[_0xf0e9('0x24')]);var _0x10ae6c=originateValidation[_0xf0e9('0x25')](_0x4822c9[_0xf0e9('0x24')]);if(_0x10ae6c[_0xf0e9('0x26')]>0x0){return _0x25452f[_0xf0e9('0x14')](0x190)[_0xf0e9('0x27')](_0x10ae6c);}var _0x11812e=_0x4822c9['body'][_0xf0e9('0x28')];var _0x2dc765=_0x4822c9['body'][_0xf0e9('0x29')];var _0x4a8c60=_0x4822c9[_0xf0e9('0x24')]['callerId'];var _0x592402=_0x4822c9[_0xf0e9('0x24')][_0xf0e9('0x2a')];return getAgent(_0x11812e)[_0xf0e9('0x1c')](function(_0x4661bc){logger[_0xf0e9('0xf')](_0xf0e9('0x2b'),_0x4661bc['name']);emit(util[_0xf0e9('0x2c')](_0xf0e9('0x2d'),_0x4661bc[_0xf0e9('0x13')]),'webbar:originate',{'callNumber':_0x2dc765,'callerId':_0x4a8c60,'callbackUrl':_0x592402});return _0x25452f[_0xf0e9('0x14')](0xc8)[_0xf0e9('0x27')]({'error':![],'message':_0xf0e9('0x2e')});})[_0xf0e9('0x1a')](handleError(_0x25452f));};exports[_0xf0e9('0x2f')]=function(_0x3ab157,_0xd20a18){logger[_0xf0e9('0xf')](_0xf0e9('0x30'),_0x3ab157[_0xf0e9('0x24')]);var _0x48040c=hangupValidation[_0xf0e9('0x25')](_0x3ab157[_0xf0e9('0x24')]);if(_0x48040c['length']>0x0){return _0xd20a18[_0xf0e9('0x14')](0x190)[_0xf0e9('0x27')](_0x48040c);}var _0x390f75=_0x3ab157[_0xf0e9('0x24')][_0xf0e9('0x28')];var _0x4ef477=_0x3ab157[_0xf0e9('0x24')][_0xf0e9('0x21')];return getAgent(_0x390f75)['then'](function(_0x27173f){logger['info'](_0xf0e9('0x2b'),_0x27173f['name']);emit(util[_0xf0e9('0x2c')](_0xf0e9('0x2d'),_0x27173f['name']),_0xf0e9('0x31'),{'sessionId':_0x4ef477});return _0xd20a18['status'](0xc8)['json']({'error':![],'message':'hangup\x20executed!'});})[_0xf0e9('0x1a')](handleError(_0xd20a18));};exports[_0xf0e9('0x32')]=function(_0x13975e,_0x405bf3){logger['info'](_0xf0e9('0x33'),_0x13975e[_0xf0e9('0x24')]);var _0x50fc1b=answerValidation['validate'](_0x13975e[_0xf0e9('0x24')]);if(_0x50fc1b[_0xf0e9('0x26')]>0x0){return _0x405bf3[_0xf0e9('0x14')](0x190)[_0xf0e9('0x27')](_0x50fc1b);}var _0x1e9c7e=_0x13975e['body'][_0xf0e9('0x28')];var _0x75c120=_0x13975e[_0xf0e9('0x24')][_0xf0e9('0x21')];return getAgent(_0x1e9c7e)[_0xf0e9('0x1c')](function(_0x33a3b5){logger[_0xf0e9('0xf')]('entityFound:',_0x33a3b5[_0xf0e9('0x13')]);emit(util[_0xf0e9('0x2c')](_0xf0e9('0x2d'),_0x33a3b5[_0xf0e9('0x13')]),'webbar:answer',{'sessionId':_0x75c120});return _0x405bf3[_0xf0e9('0x14')](0xc8)[_0xf0e9('0x27')]({'error':![],'message':_0xf0e9('0x34')});})['catch'](handleError(_0x405bf3));};exports[_0xf0e9('0x35')]=function(_0x33f702,_0x176871){logger[_0xf0e9('0xf')](_0xf0e9('0x36'),_0x33f702[_0xf0e9('0x24')]);var _0x25f741=holdValidation[_0xf0e9('0x25')](_0x33f702[_0xf0e9('0x24')]);if(_0x25f741[_0xf0e9('0x26')]>0x0){return _0x176871[_0xf0e9('0x14')](0x190)[_0xf0e9('0x27')](_0x25f741);}var _0x317419=_0x33f702[_0xf0e9('0x24')][_0xf0e9('0x28')];var _0x44967c=_0x33f702[_0xf0e9('0x24')][_0xf0e9('0x21')];return getAgent(_0x317419)[_0xf0e9('0x1c')](function(_0x39def0){logger[_0xf0e9('0xf')](_0xf0e9('0x2b'),_0x39def0[_0xf0e9('0x13')]);emit(util[_0xf0e9('0x2c')](_0xf0e9('0x2d'),_0x39def0[_0xf0e9('0x13')]),'webbar:hold',{'sessionId':_0x44967c});return _0x176871[_0xf0e9('0x14')](0xc8)[_0xf0e9('0x27')]({'error':![],'message':'hold\x20executed!'});})[_0xf0e9('0x1a')](handleError(_0x176871));};exports[_0xf0e9('0x37')]=function(_0x26789c,_0x19419e){logger[_0xf0e9('0xf')]('unhold\x20fired:',_0x26789c[_0xf0e9('0x24')]);var _0x5ea49e=unholdValidation[_0xf0e9('0x25')](_0x26789c['body']);if(_0x5ea49e[_0xf0e9('0x26')]>0x0){return _0x19419e[_0xf0e9('0x14')](0x190)[_0xf0e9('0x27')](_0x5ea49e);}var _0xc7c4fa=_0x26789c['body']['userId'];var _0x2eaceb=_0x26789c['body'][_0xf0e9('0x21')];return getAgent(_0xc7c4fa)[_0xf0e9('0x1c')](function(_0x58f225){logger[_0xf0e9('0xf')]('entityFound:',_0x58f225[_0xf0e9('0x13')]);emit(util['format'](_0xf0e9('0x2d'),_0x58f225['name']),_0xf0e9('0x38'),{'sessionId':_0x2eaceb});return _0x19419e[_0xf0e9('0x14')](0xc8)[_0xf0e9('0x27')]({'error':![],'message':_0xf0e9('0x39')});})['catch'](handleError(_0x19419e));};exports[_0xf0e9('0x3a')]=function(_0x5ee5fb,_0xabea45){logger['info'](_0xf0e9('0x3b'),_0xabea45[_0xf0e9('0x24')]);var _0x43fdc5=callsValidation[_0xf0e9('0x25')](_0x5ee5fb['body']);if(_0x43fdc5[_0xf0e9('0x26')]>0x0){return _0xabea45['status'](0x190)[_0xf0e9('0x27')](_0x43fdc5);}var _0x573349=_0x5ee5fb[_0xf0e9('0x24')][_0xf0e9('0x28')];var _0x6f08d9=respondWithRpcPromise(_0xf0e9('0x3c'));var _0x54a911=getAgent(_0x573349);return Promise['all']([_0x6f08d9,_0x54a911])[_0xf0e9('0x1c')](function(_0xc9dcf2){var _0xaa2ec8=_0xc9dcf2[0x0];var _0x356a71=_0xc9dcf2[0x1];var _0x20333d=_[_0xf0e9('0x1e')](_0xaa2ec8[_0xf0e9('0x1f')],function(_0xe36788){return _0xe36788[_0xf0e9('0x20')]==_0x356a71['id'];});return _0xabea45[_0xf0e9('0x14')](0xc8)[_0xf0e9('0x27')]({'count':_0x20333d[_0xf0e9('0x26')],'rows':_0x20333d});})[_0xf0e9('0x1a')](handleError(_0xabea45));};exports[_0xf0e9('0x3d')]=function(_0x5d9ebb,_0x56c259){logger[_0xf0e9('0xf')]('conference\x20fired:',_0x5d9ebb[_0xf0e9('0x24')]);var _0x42189e=conferenceValidation['validate'](_0x5d9ebb[_0xf0e9('0x24')]);if(_0x42189e[_0xf0e9('0x26')]>0x0){return _0x56c259[_0xf0e9('0x14')](0x190)[_0xf0e9('0x27')](_0x42189e);}var _0x393d6b=_0x5d9ebb[_0xf0e9('0x24')][_0xf0e9('0x28')];return getAgent(_0x393d6b)[_0xf0e9('0x1c')](function(_0x3550ea){logger[_0xf0e9('0xf')]('entityFound:',_0x3550ea[_0xf0e9('0x13')]);emit(util['format']('user:%s',_0x3550ea[_0xf0e9('0x13')]),_0xf0e9('0x3e'));return _0x56c259[_0xf0e9('0x14')](0xc8)['json']({'error':![],'message':_0xf0e9('0x3f')});})[_0xf0e9('0x1a')](handleError(_0x56c259));};exports['transfer']=function(_0x4b64d5,_0x75a07b){logger[_0xf0e9('0xf')](_0xf0e9('0x40'),_0x4b64d5[_0xf0e9('0x24')]);var _0x2e8ef8=transferValidation['validate'](_0x4b64d5[_0xf0e9('0x24')]);if(_0x2e8ef8[_0xf0e9('0x26')]>0x0){return _0x75a07b[_0xf0e9('0x14')](0x190)['json'](_0x2e8ef8);}var _0x2669e6=_0x4b64d5['body'][_0xf0e9('0x28')];var _0x19061d=_0x4b64d5['body'][_0xf0e9('0x21')];var _0x20a111=_0x4b64d5[_0xf0e9('0x24')][_0xf0e9('0x41')];return getAgent(_0x2669e6)[_0xf0e9('0x1c')](function(_0x11a77a){logger[_0xf0e9('0xf')](_0xf0e9('0x2b'),_0x11a77a[_0xf0e9('0x13')]);emit(util[_0xf0e9('0x2c')](_0xf0e9('0x2d'),_0x11a77a[_0xf0e9('0x13')]),'webbar:transfer',{'sessionId':_0x19061d,'transferNumber':_0x20a111});return _0x75a07b[_0xf0e9('0x14')](0xc8)['json']({'error':![],'message':'transfer\x20executed!'});})['catch'](handleError(_0x75a07b));};