65a10ce4617aebddaa40479c3b21ea9e4ac9832c
[motion2.git] / server / services / ami / trigger / index.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 _0x9572=['mailSubstatus','null','substatus','offlineMessage','created','equals','exports','util','url','mustache','./urlforward','./integration','./cmContact','./script','../../../config/logger','trigger','../ami','redis','popup','winapp','triggers','agents','voiceQueues','templates','voiceExtensions','zendeskAccounts','freshdeskAccounts','salesforceAccounts','sugarcrmAccounts','zohoAccounts','deskAccounts','vtigerAccounts','servicenowAccounts','dynamics365Accounts','accounts','freshsalesAccounts','custom:agentcalled','syncCustomEvent','custom:agentconnect','bind','custom:agentcomplete','custom:agentnosuchnumber','custom:agentnoanswer','custom:agentbusy','custom:agentreject','custom:queuecallerabandon','custom:timeout','custom:dialbegin','custom:dialconnect','syncAgentDial','custom:dialend','custom:substatus','custom:incomingmessage','custom:outgoingmessage','custom:offlineMessage','prototype','emit','info','stringify','undefined','motionChannel','voice','debug','checkTriggers','hasOwnProperty','status','checkTrigger','merge','Actions','action','toLowerCase','motionbar','browser','data1','TemplateId','data2','html','render','uri','buildUri','isNil','data3','data4','location','uriLocation','windowWidth','800','windowHeight','data6','600','winApplication','winArguments','format','user:%s','interface','trigger:%s:%s','jscripty','name','contactmanager','exec','integration','routeId','query','replace','AllConditions','length','every','channel','AnyConditions','checkTrigger:\x20trigger','inspect','checkConditions','field','account','operator','ChatWebsiteId','AccountId','value','upperFirst','interaction','event','queue','lastevent'];(function(_0x34dbee,_0x2aa10c){var _0x38d197=function(_0x50d485){while(--_0x50d485){_0x34dbee['push'](_0x34dbee['shift']());}};_0x38d197(++_0x2aa10c);}(_0x9572,0x82));var _0x2957=function(_0x5743e1,_0x293772){_0x5743e1=_0x5743e1-0x0;var _0x18e205=_0x9572[_0x5743e1];return _0x18e205;};'use strict';var util=require(_0x2957('0x0'));var _=require('lodash');var url=require(_0x2957('0x1'));var Mustache=require(_0x2957('0x2'));var Redis=require('ioredis');var urlforward=require(_0x2957('0x3'));var integration=require(_0x2957('0x4'));var cmContact=require(_0x2957('0x5'));var script=require(_0x2957('0x6'));var config=require('../../../config/environment');var logger=require(_0x2957('0x7'))(_0x2957('0x8'));var ami=require(_0x2957('0x9'));config[_0x2957('0xa')]=_['defaults'](config[_0x2957('0xa')],{'host':'localhost','port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x2957('0xa')]));var operators={'equals':function(_0x364561,_0x5a00e5){return _0x364561==_0x5a00e5;},'not_equals':function(_0x4a6938,_0x21d34b){return _0x4a6938!=_0x21d34b;}};var barAndWebActions=[_0x2957('0xb'),_0x2957('0x1'),_0x2957('0xc')];function Trigger(_0x16b943){this[_0x2957('0xd')]=_0x16b943[_0x2957('0xd')];this['agents']=_0x16b943[_0x2957('0xe')];this[_0x2957('0xf')]=_0x16b943[_0x2957('0xf')];this['templates']=_0x16b943[_0x2957('0x10')];this['voiceExtensions']=_0x16b943[_0x2957('0x11')];this[_0x2957('0x12')]=_0x16b943[_0x2957('0x12')];this[_0x2957('0x13')]=_0x16b943[_0x2957('0x13')];this[_0x2957('0x14')]=_0x16b943[_0x2957('0x14')];this[_0x2957('0x15')]=_0x16b943[_0x2957('0x15')];this[_0x2957('0x16')]=_0x16b943['zohoAccounts'];this[_0x2957('0x17')]=_0x16b943['deskAccounts'];this[_0x2957('0x18')]=_0x16b943['vtigerAccounts'];this[_0x2957('0x19')]=_0x16b943[_0x2957('0x19')];this[_0x2957('0x1a')]=_0x16b943[_0x2957('0x1a')];this['freshsalesAccounts']=_0x16b943['freshsalesAccounts'];this[_0x2957('0x1b')]={'zendesk':this[_0x2957('0x12')],'salesforce':this[_0x2957('0x14')],'freshdesk':this['freshdeskAccounts'],'sugarcrm':this[_0x2957('0x15')],'zoho':this[_0x2957('0x16')],'desk':this[_0x2957('0x17')],'vtiger':this[_0x2957('0x18')],'dynamics365':this['dynamics365Accounts'],'freshsales':this[_0x2957('0x1c')],'servicenow':this[_0x2957('0x19')]};ami['on'](_0x2957('0x1d'),this[_0x2957('0x1e')]['bind'](this));ami['on'](_0x2957('0x1f'),this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on']('custom:agentringnoanswer',this['syncCustomEvent']['bind'](this));ami['on'](_0x2957('0x21'),this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on'](_0x2957('0x22'),this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on'](_0x2957('0x23'),this[_0x2957('0x1e')]['bind'](this));ami['on'](_0x2957('0x24'),this[_0x2957('0x1e')]['bind'](this));ami['on']('custom:agentcongestion',this[_0x2957('0x1e')]['bind'](this));ami['on'](_0x2957('0x25'),this['syncCustomEvent'][_0x2957('0x20')](this));ami['on']('custom:amd',this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on'](_0x2957('0x26'),this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on'](_0x2957('0x27'),this['syncCustomEvent'][_0x2957('0x20')](this));ami['on'](_0x2957('0x28'),this['syncAgentDial'][_0x2957('0x20')](this));ami['on'](_0x2957('0x29'),this[_0x2957('0x2a')][_0x2957('0x20')](this));ami['on'](_0x2957('0x2b'),this[_0x2957('0x2a')]['bind'](this));ami['on']('custom:newinteraction',this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on']('custom:closedinteraction',this['syncCustomEvent'][_0x2957('0x20')](this));ami['on'](_0x2957('0x2c'),this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on'](_0x2957('0x2d'),this[_0x2957('0x1e')]['bind'](this));ami['on'](_0x2957('0x2e'),this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on']('custom:acceptmessage',this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on']('custom:rejectmessage',this[_0x2957('0x1e')][_0x2957('0x20')](this));ami['on'](_0x2957('0x2f'),this[_0x2957('0x1e')]['bind'](this));}Trigger[_0x2957('0x30')][_0x2957('0x31')]=function(_0x3d6675,_0x458d98,_0x462f10){logger[_0x2957('0x32')](_0x3d6675,_0x458d98,JSON[_0x2957('0x33')](_0x462f10));io['to'](_0x3d6675)[_0x2957('0x31')](_0x458d98,_0x462f10);};Trigger[_0x2957('0x30')][_0x2957('0x1e')]=function(_0x30311a){var _0x1a6c2e=_['clone'](_0x30311a);if(typeof _0x1a6c2e['motionChannel']===_0x2957('0x34')){_0x1a6c2e[_0x2957('0x35')]=_0x2957('0x36');}logger[_0x2957('0x37')]('syncCustomEvent',util['inspect'](_0x1a6c2e,{'showHidden':!![],'depth':null}));this[_0x2957('0x38')](_0x1a6c2e);};Trigger[_0x2957('0x30')]['syncAgentDial']=function(_0x5806ec){if(this[_0x2957('0xe')][_0x5806ec['interface']]){this['syncCustomEvent'](_0x5806ec);}};Trigger[_0x2957('0x30')][_0x2957('0x38')]=function(_0x3cc261){var _0x1d6dc2;for(var _0x5cdd09 in this[_0x2957('0xd')]){if(this[_0x2957('0xd')][_0x2957('0x39')](_0x5cdd09)){if(this[_0x2957('0xd')][_0x5cdd09][_0x2957('0x3a')]){_0x1d6dc2=this[_0x2957('0x3b')](this['triggers'][_0x5cdd09],_0x3cc261);if(_0x1d6dc2){_[_0x2957('0x3c')](_0x3cc261,_0x3cc261['variables']);for(var _0x4aaa53=0x0,_0x4de7ad,_0x57f6f1,_0x32b961;_0x4aaa53<this[_0x2957('0xd')][_0x5cdd09][_0x2957('0x3d')]['length'];_0x4aaa53+=0x1){_0x4de7ad=this['triggers'][_0x5cdd09]['Actions'][_0x4aaa53];if(this[_0x2957('0xe')][_0x3cc261['interface']]){switch(_0x4de7ad[_0x2957('0x3e')][_0x2957('0x3f')]()){case'contactmanager':break;case _0x2957('0x40'):case _0x2957('0x41'):var _0x32dad6={};switch(barAndWebActions[_0x4de7ad[_0x2957('0x42')]]){case _0x2957('0xb'):_0x32dad6[_0x2957('0x43')]=_0x4de7ad[_0x2957('0x44')];_0x32dad6[_0x2957('0x45')]=this['templates'][_0x4de7ad[_0x2957('0x44')]]?Mustache[_0x2957('0x46')](this['templates'][_0x4de7ad[_0x2957('0x44')]][_0x2957('0x45')],_0x3cc261):'template\x20not\x20found';break;case _0x2957('0x1'):_0x32dad6[_0x2957('0x47')]=this[_0x2957('0x48')](_0x4de7ad[_0x2957('0x44')],_0x3cc261,_[_0x2957('0x49')](_0x4de7ad[_0x2957('0x4a')])?!![]:parseInt(_0x4de7ad['data3'],0xa));if(_['isNil'](_0x4de7ad[_0x2957('0x4b')])){_0x32dad6[_0x2957('0x4c')]='0';}else{_0x32dad6[_0x2957('0x4d')]=_0x4de7ad[_0x2957('0x4b')];_0x32dad6[_0x2957('0x4e')]=_[_0x2957('0x49')](_0x4de7ad['data5'])?_0x2957('0x4f'):_0x4de7ad['data5'];_0x32dad6[_0x2957('0x50')]=_['isNil'](_0x4de7ad[_0x2957('0x51')])?_0x2957('0x52'):_0x4de7ad[_0x2957('0x51')];}break;case _0x2957('0xc'):_0x32dad6[_0x2957('0x53')]=_0x4de7ad[_0x2957('0x44')]?Mustache['render'](_0x4de7ad[_0x2957('0x44')],_0x3cc261):'';_0x32dad6[_0x2957('0x54')]=_0x4de7ad[_0x2957('0x4a')]?Mustache[_0x2957('0x46')](_0x4de7ad[_0x2957('0x4a')],_0x3cc261):'';break;}this[_0x2957('0x31')](util[_0x2957('0x55')](_0x2957('0x56'),this[_0x2957('0xe')][_0x3cc261[_0x2957('0x57')]]['name']),util[_0x2957('0x55')](_0x2957('0x58'),_0x4de7ad[_0x2957('0x3e')][_0x2957('0x3f')](),barAndWebActions[_0x4de7ad[_0x2957('0x42')]]),_[_0x2957('0x3c')](_0x32dad6,_0x3cc261));break;case _0x2957('0x59'):this[_0x2957('0x31')](util[_0x2957('0x55')]('user:%s',this[_0x2957('0xe')][_0x3cc261[_0x2957('0x57')]][_0x2957('0x5a')]),util[_0x2957('0x55')]('trigger:%s',_0x4de7ad[_0x2957('0x3e')]['toLowerCase']()),_[_0x2957('0x3c')]({'ProjectId':parseInt(_0x4de7ad[_0x2957('0x42')],0xa)},_0x3cc261));break;default:this[_0x2957('0x31')](util['format'](_0x2957('0x56'),this[_0x2957('0xe')][_0x3cc261[_0x2957('0x57')]][_0x2957('0x5a')]),util[_0x2957('0x55')]('trigger:%s',_0x4de7ad[_0x2957('0x3e')][_0x2957('0x3f')]()),_[_0x2957('0x3c')]({},_0x4de7ad,_0x3cc261));}}switch(_0x4de7ad[_0x2957('0x3e')]['toLowerCase']()){case _0x2957('0x5b'):cmContact[_0x2957('0x5c')](this[_0x2957('0xe')][_0x3cc261[_0x2957('0x57')]],this['triggers'][_0x5cdd09],_0x4de7ad,_0x3cc261);break;case _0x2957('0x5d'):integration['exec'](_0x4de7ad,_0x3cc261,this['accounts'],this[_0x2957('0xe')][_0x3cc261['interface']],this[_0x2957('0xf')][_0x3cc261['queue']],this[_0x2957('0x11')][_0x3cc261[_0x2957('0x5e')]]);break;case'urlforward':urlforward[_0x2957('0x5c')](_0x4de7ad,_0x3cc261);break;case'script':script[_0x2957('0x5c')](_0x4de7ad,_0x3cc261);break;}}}}}}};Trigger[_0x2957('0x30')][_0x2957('0x48')]=function(_0x1c8c06,_0x54b572,_0x385140){var _0x2b349c=url['parse'](Mustache[_0x2957('0x46')](_0x1c8c06,_0x54b572),!![]);if(_0x385140){_[_0x2957('0x3c')](_0x2b349c[_0x2957('0x5f')],_0x54b572);}delete _0x2b349c['search'];var _0x88b422=url[_0x2957('0x55')](_0x2b349c);_0x88b422=_0x88b422[_0x2957('0x60')](/\/\?/g,'?');return _0x88b422;};Trigger[_0x2957('0x30')][_0x2957('0x3b')]=function(_0x50da4a,_0x2dd1fc){var _0x4ab954=!![];var _0x5c4598=!![];var _0x1d0bf9=![];if(_0x50da4a['channel']===_0x2dd1fc['motionChannel']){_0x1d0bf9=!![];}if(_0x50da4a[_0x2957('0x61')][_0x2957('0x62')]){_0x4ab954=_[_0x2957('0x63')](_0x50da4a[_0x2957('0x61')],this['checkConditions'](_0x2dd1fc,_0x50da4a[_0x2957('0x64')]));}if(_0x50da4a['AnyConditions'][_0x2957('0x62')]){_0x5c4598=_['some'](_0x50da4a[_0x2957('0x65')],this['checkConditions'](_0x2dd1fc,_0x50da4a[_0x2957('0x64')]));}var _0x51a9d1=_0x1d0bf9&&_0x4ab954&&_0x5c4598;logger[_0x2957('0x37')](_0x2957('0x66'),util[_0x2957('0x67')](_0x50da4a,{'showHidden':!![],'depth':null}),'evt',util[_0x2957('0x67')](_0x2dd1fc,{'showHidden':!![],'depth':null}),_0x1d0bf9,_0x4ab954,_0x5c4598);return _0x51a9d1;};Trigger[_0x2957('0x30')][_0x2957('0x68')]=function(_0x5e6595,_0x52fd34){return function(_0xcba10f){switch(_0xcba10f[_0x2957('0x69')]){case _0x2957('0x6a'):if(_0x5e6595[_0x2957('0x64')]===_0x52fd34){if(_0x52fd34==='chat'){return _0x5e6595['ChatWebsiteId']?operators[_0xcba10f[_0x2957('0x6b')]](Number(_0xcba10f['value']),_0x5e6595[_0x2957('0x6c')]):![];}else{return _0x5e6595[_['upperFirst'](_0x52fd34)+_0x2957('0x6d')]?operators[_0xcba10f[_0x2957('0x6b')]](Number(_0xcba10f[_0x2957('0x6e')]),_0x5e6595[_[_0x2957('0x6f')](_0x52fd34)+_0x2957('0x6d')]):![];}}break;case _0x2957('0x70'):case'message':if(_0x5e6595[_0x2957('0x64')]===_0x52fd34){return _0x5e6595[_0x2957('0x71')]?operators[_0xcba10f[_0x2957('0x6b')]](_0xcba10f[_0x2957('0x6e')],_0x5e6595['event']):![];}break;case _0x2957('0x72'):return _0x5e6595['queue']?operators[_0xcba10f[_0x2957('0x6b')]](_0xcba10f['value'],_0x5e6595[_0x2957('0x72')]):![];case'agent':return _0x5e6595['membername']?operators[_0xcba10f[_0x2957('0x6b')]](_0xcba10f['value'],_0x5e6595['membername']):![];case _0x2957('0x73'):return _0x5e6595[_0x2957('0x73')]?operators[_0xcba10f[_0x2957('0x6b')]](_0xcba10f[_0x2957('0x6e')],_0x5e6595[_0x2957('0x73')]):![];case _0x2957('0x5e'):return _0x5e6595[_0x2957('0x5e')]?operators[_0xcba10f[_0x2957('0x6b')]](_0xcba10f['value'],_0x5e6595[_0x2957('0x5e')]):![];case _0x2957('0x74'):if(_0xcba10f['value']===_0x2957('0x75'))_0xcba10f[_0x2957('0x6e')]=null;return typeof _0x5e6595['substatus']!=='undefined'?operators[_0xcba10f['operator']](_0xcba10f[_0x2957('0x6e')],_0x5e6595[_0x2957('0x76')]):![];case _0x2957('0x77'):if(_0x5e6595[_0x2957('0x64')]===_0x52fd34){return _0xcba10f['value']===_0x2957('0x78')&&_0xcba10f[_0x2957('0x6b')]===_0x2957('0x79')?!![]:![];}break;}return![];};};module[_0x2957('0x7a')]=Trigger;