48642836c2798011111085fd0b607ccf56d04b58
[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 _0x2edf=['AllConditions','every','checkConditions','some','AnyConditions','checkTrigger:\x20trigger','evt','field','ChatWebsiteId','operator','AccountId','value','upperFirst','interaction','event','agent','membername','lastevent','mailSubstatus','null','substatus','undefined','created','exports','util','lodash','url','./integration','./cmContact','./script','trigger','../ami','defaults','socket.io-emitter','popup','winapp','triggers','agents','voiceQueues','voiceExtensions','zendeskAccounts','freshdeskAccounts','salesforceAccounts','sugarcrmAccounts','zohoAccounts','deskAccounts','vtigerAccounts','servicenowAccounts','dynamics365Accounts','freshsalesAccounts','accounts','custom:agentcalled','syncCustomEvent','bind','custom:agentconnect','custom:agentringnoanswer','custom:agentcomplete','custom:amd','custom:queuecallerabandon','custom:dialbegin','syncAgentDial','custom:dialconnect','custom:dialend','custom:newinteraction','custom:substatus','custom:incomingmessage','custom:outgoingmessage','custom:rejectmessage','custom:offlineMessage','emit','info','stringify','prototype','motionChannel','voice','debug','inspect','interface','checkTriggers','hasOwnProperty','checkTrigger','merge','variables','Actions','length','toLowerCase','contactmanager','motionbar','browser','TemplateId','data2','html','render','template\x20not\x20found','uri','buildUri','data3','isNil','data4','location','uriLocation','data5','windowHeight','data6','600','winApplication','winArguments','format','user:%s','name','trigger:%s:%s','action','data1','jscripty','trigger:%s','exec','integration','queue','routeId','urlforward','parse','query','replace','channel'];(function(_0x12015b,_0x5b4562){var _0x132680=function(_0x329bb9){while(--_0x329bb9){_0x12015b['push'](_0x12015b['shift']());}};_0x132680(++_0x5b4562);}(_0x2edf,0x94));var _0xf2ed=function(_0x404eed,_0x5390c5){_0x404eed=_0x404eed-0x0;var _0xd45887=_0x2edf[_0x404eed];return _0xd45887;};'use strict';var util=require(_0xf2ed('0x0'));var _=require(_0xf2ed('0x1'));var url=require(_0xf2ed('0x2'));var Mustache=require('mustache');var Redis=require('ioredis');var urlforward=require('./urlforward');var integration=require(_0xf2ed('0x3'));var cmContact=require(_0xf2ed('0x4'));var script=require(_0xf2ed('0x5'));var config=require('../../../config/environment');var logger=require('../../../config/logger')(_0xf2ed('0x6'));var ami=require(_0xf2ed('0x7'));config['redis']=_[_0xf2ed('0x8')](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0xf2ed('0x9'))(new Redis(config['redis']));var operators={'equals':function(_0x330c9d,_0x2daebf){return _0x330c9d==_0x2daebf;},'not_equals':function(_0x97d52a,_0x1d12f6){return _0x97d52a!=_0x1d12f6;}};var barAndWebActions=[_0xf2ed('0xa'),'url',_0xf2ed('0xb')];function Trigger(_0x3021f7){this['triggers']=_0x3021f7[_0xf2ed('0xc')];this['agents']=_0x3021f7[_0xf2ed('0xd')];this[_0xf2ed('0xe')]=_0x3021f7[_0xf2ed('0xe')];this['templates']=_0x3021f7['templates'];this[_0xf2ed('0xf')]=_0x3021f7[_0xf2ed('0xf')];this['zendeskAccounts']=_0x3021f7[_0xf2ed('0x10')];this[_0xf2ed('0x11')]=_0x3021f7[_0xf2ed('0x11')];this['salesforceAccounts']=_0x3021f7[_0xf2ed('0x12')];this[_0xf2ed('0x13')]=_0x3021f7[_0xf2ed('0x13')];this['zohoAccounts']=_0x3021f7[_0xf2ed('0x14')];this[_0xf2ed('0x15')]=_0x3021f7['deskAccounts'];this['vtigerAccounts']=_0x3021f7[_0xf2ed('0x16')];this[_0xf2ed('0x17')]=_0x3021f7[_0xf2ed('0x17')];this[_0xf2ed('0x18')]=_0x3021f7[_0xf2ed('0x18')];this[_0xf2ed('0x19')]=_0x3021f7[_0xf2ed('0x19')];this[_0xf2ed('0x1a')]={'zendesk':this[_0xf2ed('0x10')],'salesforce':this['salesforceAccounts'],'freshdesk':this[_0xf2ed('0x11')],'sugarcrm':this['sugarcrmAccounts'],'zoho':this[_0xf2ed('0x14')],'desk':this[_0xf2ed('0x15')],'vtiger':this[_0xf2ed('0x16')],'dynamics365':this[_0xf2ed('0x18')],'freshsales':this[_0xf2ed('0x19')],'servicenow':this['servicenowAccounts']};ami['on'](_0xf2ed('0x1b'),this[_0xf2ed('0x1c')][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x1e'),this[_0xf2ed('0x1c')][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x1f'),this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x20'),this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on']('custom:agentnosuchnumber',this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on']('custom:agentnoanswer',this[_0xf2ed('0x1c')]['bind'](this));ami['on']('custom:agentbusy',this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on']('custom:agentcongestion',this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on']('custom:agentreject',this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x21'),this[_0xf2ed('0x1c')]['bind'](this));ami['on'](_0xf2ed('0x22'),this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on']('custom:timeout',this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x23'),this[_0xf2ed('0x24')][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x25'),this[_0xf2ed('0x24')][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x26'),this[_0xf2ed('0x24')]['bind'](this));ami['on'](_0xf2ed('0x27'),this['syncCustomEvent']['bind'](this));ami['on']('custom:closedinteraction',this[_0xf2ed('0x1c')]['bind'](this));ami['on'](_0xf2ed('0x28'),this['syncCustomEvent']['bind'](this));ami['on'](_0xf2ed('0x29'),this['syncCustomEvent'][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x2a'),this[_0xf2ed('0x1c')]['bind'](this));ami['on']('custom:acceptmessage',this[_0xf2ed('0x1c')][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x2b'),this[_0xf2ed('0x1c')][_0xf2ed('0x1d')](this));ami['on'](_0xf2ed('0x2c'),this[_0xf2ed('0x1c')]['bind'](this));}Trigger['prototype'][_0xf2ed('0x2d')]=function(_0xa2bbd8,_0x1e77f4,_0x212364){logger[_0xf2ed('0x2e')](_0xa2bbd8,_0x1e77f4,JSON[_0xf2ed('0x2f')](_0x212364));io['to'](_0xa2bbd8)[_0xf2ed('0x2d')](_0x1e77f4,_0x212364);};Trigger[_0xf2ed('0x30')][_0xf2ed('0x1c')]=function(_0x42b774){var _0x17c5cc=_['clone'](_0x42b774);if(typeof _0x17c5cc[_0xf2ed('0x31')]==='undefined'){_0x17c5cc[_0xf2ed('0x31')]=_0xf2ed('0x32');}logger[_0xf2ed('0x33')]('syncCustomEvent',util[_0xf2ed('0x34')](_0x17c5cc,{'showHidden':!![],'depth':null}));this['checkTriggers'](_0x17c5cc);};Trigger[_0xf2ed('0x30')][_0xf2ed('0x24')]=function(_0x23eaf2){if(this[_0xf2ed('0xd')][_0x23eaf2[_0xf2ed('0x35')]]){this[_0xf2ed('0x1c')](_0x23eaf2);}};Trigger[_0xf2ed('0x30')][_0xf2ed('0x36')]=function(_0x15d9e1){var _0x1d07d5;for(var _0x34c2f9 in this['triggers']){if(this[_0xf2ed('0xc')][_0xf2ed('0x37')](_0x34c2f9)){if(this['triggers'][_0x34c2f9]['status']){_0x1d07d5=this[_0xf2ed('0x38')](this[_0xf2ed('0xc')][_0x34c2f9],_0x15d9e1);if(_0x1d07d5){_[_0xf2ed('0x39')](_0x15d9e1,_0x15d9e1[_0xf2ed('0x3a')]);for(var _0x1c4652=0x0,_0x1262e3,_0x324f34,_0x33a845;_0x1c4652<this[_0xf2ed('0xc')][_0x34c2f9][_0xf2ed('0x3b')][_0xf2ed('0x3c')];_0x1c4652+=0x1){_0x1262e3=this[_0xf2ed('0xc')][_0x34c2f9][_0xf2ed('0x3b')][_0x1c4652];if(this[_0xf2ed('0xd')][_0x15d9e1['interface']]){switch(_0x1262e3['action'][_0xf2ed('0x3d')]()){case _0xf2ed('0x3e'):break;case _0xf2ed('0x3f'):case _0xf2ed('0x40'):var _0x3a1a8f={};switch(barAndWebActions[_0x1262e3['data1']]){case _0xf2ed('0xa'):_0x3a1a8f[_0xf2ed('0x41')]=_0x1262e3[_0xf2ed('0x42')];_0x3a1a8f[_0xf2ed('0x43')]=this['templates'][_0x1262e3['data2']]?Mustache[_0xf2ed('0x44')](this['templates'][_0x1262e3['data2']]['html'],_0x15d9e1):_0xf2ed('0x45');break;case _0xf2ed('0x2'):_0x3a1a8f[_0xf2ed('0x46')]=this[_0xf2ed('0x47')](_0x1262e3[_0xf2ed('0x42')],_0x15d9e1,_['isNil'](_0x1262e3['data3'])?!![]:parseInt(_0x1262e3[_0xf2ed('0x48')],0xa));if(_[_0xf2ed('0x49')](_0x1262e3[_0xf2ed('0x4a')])){_0x3a1a8f[_0xf2ed('0x4b')]='0';}else{_0x3a1a8f[_0xf2ed('0x4c')]=_0x1262e3[_0xf2ed('0x4a')];_0x3a1a8f['windowWidth']=_[_0xf2ed('0x49')](_0x1262e3[_0xf2ed('0x4d')])?'800':_0x1262e3[_0xf2ed('0x4d')];_0x3a1a8f[_0xf2ed('0x4e')]=_[_0xf2ed('0x49')](_0x1262e3[_0xf2ed('0x4f')])?_0xf2ed('0x50'):_0x1262e3[_0xf2ed('0x4f')];}break;case _0xf2ed('0xb'):_0x3a1a8f[_0xf2ed('0x51')]=_0x1262e3['data2']?Mustache[_0xf2ed('0x44')](_0x1262e3[_0xf2ed('0x42')],_0x15d9e1):'';_0x3a1a8f[_0xf2ed('0x52')]=_0x1262e3[_0xf2ed('0x48')]?Mustache['render'](_0x1262e3[_0xf2ed('0x48')],_0x15d9e1):'';break;}this[_0xf2ed('0x2d')](util[_0xf2ed('0x53')](_0xf2ed('0x54'),this['agents'][_0x15d9e1['interface']][_0xf2ed('0x55')]),util[_0xf2ed('0x53')](_0xf2ed('0x56'),_0x1262e3[_0xf2ed('0x57')][_0xf2ed('0x3d')](),barAndWebActions[_0x1262e3[_0xf2ed('0x58')]]),_[_0xf2ed('0x39')](_0x3a1a8f,_0x15d9e1));break;case _0xf2ed('0x59'):this[_0xf2ed('0x2d')](util[_0xf2ed('0x53')](_0xf2ed('0x54'),this[_0xf2ed('0xd')][_0x15d9e1[_0xf2ed('0x35')]][_0xf2ed('0x55')]),util[_0xf2ed('0x53')](_0xf2ed('0x5a'),_0x1262e3[_0xf2ed('0x57')]['toLowerCase']()),_['merge']({'ProjectId':parseInt(_0x1262e3['data1'],0xa)},_0x15d9e1));break;default:this[_0xf2ed('0x2d')](util['format']('user:%s',this[_0xf2ed('0xd')][_0x15d9e1[_0xf2ed('0x35')]][_0xf2ed('0x55')]),util[_0xf2ed('0x53')]('trigger:%s',_0x1262e3[_0xf2ed('0x57')][_0xf2ed('0x3d')]()),_['merge']({},_0x1262e3,_0x15d9e1));}}switch(_0x1262e3[_0xf2ed('0x57')]['toLowerCase']()){case _0xf2ed('0x3e'):cmContact[_0xf2ed('0x5b')](this[_0xf2ed('0xd')][_0x15d9e1[_0xf2ed('0x35')]],this['triggers'][_0x34c2f9],_0x1262e3,_0x15d9e1);break;case _0xf2ed('0x5c'):integration[_0xf2ed('0x5b')](_0x1262e3,_0x15d9e1,this['accounts'],this[_0xf2ed('0xd')][_0x15d9e1['interface']],this[_0xf2ed('0xe')][_0x15d9e1[_0xf2ed('0x5d')]],this[_0xf2ed('0xf')][_0x15d9e1[_0xf2ed('0x5e')]]);break;case _0xf2ed('0x5f'):urlforward[_0xf2ed('0x5b')](_0x1262e3,_0x15d9e1);break;case'script':script[_0xf2ed('0x5b')](_0x1262e3,_0x15d9e1);break;}}}}}}};Trigger[_0xf2ed('0x30')][_0xf2ed('0x47')]=function(_0x944ab9,_0x5dfdb1,_0x570ceb){var _0x5813f5=url[_0xf2ed('0x60')](Mustache[_0xf2ed('0x44')](_0x944ab9,_0x5dfdb1),!![]);if(_0x570ceb){_[_0xf2ed('0x39')](_0x5813f5[_0xf2ed('0x61')],_0x5dfdb1);}delete _0x5813f5['search'];var _0xa34d8f=url[_0xf2ed('0x53')](_0x5813f5);_0xa34d8f=_0xa34d8f[_0xf2ed('0x62')](/\/\?/g,'?');return _0xa34d8f;};Trigger[_0xf2ed('0x30')][_0xf2ed('0x38')]=function(_0x4dafda,_0x19652a){var _0x1d43bf=!![];var _0x54c3f9=!![];var _0x1c931b=![];if(_0x4dafda[_0xf2ed('0x63')]===_0x19652a[_0xf2ed('0x31')]){_0x1c931b=!![];}if(_0x4dafda[_0xf2ed('0x64')][_0xf2ed('0x3c')]){_0x1d43bf=_[_0xf2ed('0x65')](_0x4dafda[_0xf2ed('0x64')],this[_0xf2ed('0x66')](_0x19652a,_0x4dafda[_0xf2ed('0x63')]));}if(_0x4dafda['AnyConditions'][_0xf2ed('0x3c')]){_0x54c3f9=_[_0xf2ed('0x67')](_0x4dafda[_0xf2ed('0x68')],this[_0xf2ed('0x66')](_0x19652a,_0x4dafda['channel']));}var _0x368543=_0x1c931b&&_0x1d43bf&&_0x54c3f9;logger[_0xf2ed('0x33')](_0xf2ed('0x69'),util[_0xf2ed('0x34')](_0x4dafda,{'showHidden':!![],'depth':null}),_0xf2ed('0x6a'),util['inspect'](_0x19652a,{'showHidden':!![],'depth':null}),_0x1c931b,_0x1d43bf,_0x54c3f9);return _0x368543;};Trigger[_0xf2ed('0x30')][_0xf2ed('0x66')]=function(_0x181b31,_0x3f1189){return function(_0x11d0fd){switch(_0x11d0fd[_0xf2ed('0x6b')]){case'account':if(_0x181b31[_0xf2ed('0x63')]===_0x3f1189){if(_0x3f1189==='chat'){return _0x181b31[_0xf2ed('0x6c')]?operators[_0x11d0fd[_0xf2ed('0x6d')]](Number(_0x11d0fd['value']),_0x181b31[_0xf2ed('0x6c')]):![];}else{return _0x181b31[_['upperFirst'](_0x3f1189)+_0xf2ed('0x6e')]?operators[_0x11d0fd['operator']](Number(_0x11d0fd[_0xf2ed('0x6f')]),_0x181b31[_[_0xf2ed('0x70')](_0x3f1189)+_0xf2ed('0x6e')]):![];}}break;case _0xf2ed('0x71'):case'message':if(_0x181b31[_0xf2ed('0x63')]===_0x3f1189){return _0x181b31[_0xf2ed('0x72')]?operators[_0x11d0fd[_0xf2ed('0x6d')]](_0x11d0fd[_0xf2ed('0x6f')],_0x181b31[_0xf2ed('0x72')]):![];}break;case _0xf2ed('0x5d'):return _0x181b31[_0xf2ed('0x5d')]?operators[_0x11d0fd['operator']](_0x11d0fd['value'],_0x181b31[_0xf2ed('0x5d')]):![];case _0xf2ed('0x73'):return _0x181b31[_0xf2ed('0x74')]?operators[_0x11d0fd['operator']](_0x11d0fd['value'],_0x181b31[_0xf2ed('0x74')]):![];case'lastevent':return _0x181b31[_0xf2ed('0x75')]?operators[_0x11d0fd[_0xf2ed('0x6d')]](_0x11d0fd['value'],_0x181b31[_0xf2ed('0x75')]):![];case _0xf2ed('0x5e'):return _0x181b31['routeId']?operators[_0x11d0fd[_0xf2ed('0x6d')]](_0x11d0fd[_0xf2ed('0x6f')],_0x181b31[_0xf2ed('0x5e')]):![];case _0xf2ed('0x76'):if(_0x11d0fd[_0xf2ed('0x6f')]===_0xf2ed('0x77'))_0x11d0fd['value']=null;return typeof _0x181b31[_0xf2ed('0x78')]!==_0xf2ed('0x79')?operators[_0x11d0fd[_0xf2ed('0x6d')]](_0x11d0fd[_0xf2ed('0x6f')],_0x181b31[_0xf2ed('0x78')]):![];case'offlineMessage':if(_0x181b31[_0xf2ed('0x63')]===_0x3f1189){return _0x11d0fd[_0xf2ed('0x6f')]===_0xf2ed('0x7a')&&_0x11d0fd['operator']==='equals'?!![]:![];}break;}return![];};};module[_0xf2ed('0x7b')]=Trigger;