14f4fe25d25a876e959f1f7b2811e9329c18caa0
[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 _0x9ea3=['action','toLowerCase','contactmanager','motionbar','browser','data1','TemplateId','data2','html','render','template\x20not\x20found','uri','isNil','data3','data4','windowWidth','data5','800','windowHeight','600','data6','winapp','winApplication','name','format','user:%s','trigger:%s','exec','integration','queue','routeId','urlforward','script','parse','query','replace','channel','AllConditions','length','every','checkConditions','AnyConditions','some','field','account','operator','value','ChatWebsiteId','upperFirst','AccountId','message','membername','lastevent','substatus','offlineMessage','created','exports','util','lodash','url','mustache','ioredis','./urlforward','./integration','./script','../../../config/environment','../../../config/logger','../ami','redis','localhost','socket.io-emitter','popup','triggers','agents','voiceQueues','templates','voiceExtensions','zendeskAccounts','freshdeskAccounts','salesforceAccounts','sugarcrmAccounts','zohoAccounts','deskAccounts','vtigerAccounts','servicenowAccounts','freshsalesAccounts','accounts','dynamics365Accounts','custom:agentcalled','syncCustomEvent','bind','custom:agentconnect','custom:agentnosuchnumber','custom:agentbusy','custom:agentreject','custom:amd','custom:queuecallerabandon','syncAgentDial','custom:dialconnect','custom:dialend','custom:closedinteraction','custom:incomingmessage','custom:outgoingmessage','custom:acceptmessage','custom:rejectmessage','custom:offlineMessage','prototype','emit','info','stringify','motionChannel','undefined','voice','checkTriggers','interface','hasOwnProperty','status','checkTrigger','merge','variables','Actions'];(function(_0x12fc8d,_0x19ee33){var _0x3f9d04=function(_0x52e51c){while(--_0x52e51c){_0x12fc8d['push'](_0x12fc8d['shift']());}};_0x3f9d04(++_0x19ee33);}(_0x9ea3,0x12b));var _0x39ea=function(_0x271060,_0x3f86b1){_0x271060=_0x271060-0x0;var _0x40540b=_0x9ea3[_0x271060];return _0x40540b;};'use strict';var util=require(_0x39ea('0x0'));var _=require(_0x39ea('0x1'));var url=require(_0x39ea('0x2'));var Mustache=require(_0x39ea('0x3'));var Redis=require(_0x39ea('0x4'));var urlforward=require(_0x39ea('0x5'));var integration=require(_0x39ea('0x6'));var cmContact=require('./cmContact');var script=require(_0x39ea('0x7'));var config=require(_0x39ea('0x8'));var logger=require(_0x39ea('0x9'))('trigger');var ami=require(_0x39ea('0xa'));config['redis']=_['defaults'](config[_0x39ea('0xb')],{'host':_0x39ea('0xc'),'port':0x18eb});var io=require(_0x39ea('0xd'))(new Redis(config[_0x39ea('0xb')]));var operators={'equals':function(_0x2b0b86,_0xbeaf08){return _0x2b0b86==_0xbeaf08;},'not_equals':function(_0x43caef,_0x5e1d31){return _0x43caef!=_0x5e1d31;}};var barAndWebActions=[_0x39ea('0xe'),'url','winapp'];function Trigger(_0x4452c8){this[_0x39ea('0xf')]=_0x4452c8[_0x39ea('0xf')];this[_0x39ea('0x10')]=_0x4452c8[_0x39ea('0x10')];this[_0x39ea('0x11')]=_0x4452c8[_0x39ea('0x11')];this[_0x39ea('0x12')]=_0x4452c8['templates'];this[_0x39ea('0x13')]=_0x4452c8[_0x39ea('0x13')];this[_0x39ea('0x14')]=_0x4452c8[_0x39ea('0x14')];this[_0x39ea('0x15')]=_0x4452c8[_0x39ea('0x15')];this[_0x39ea('0x16')]=_0x4452c8[_0x39ea('0x16')];this[_0x39ea('0x17')]=_0x4452c8[_0x39ea('0x17')];this[_0x39ea('0x18')]=_0x4452c8[_0x39ea('0x18')];this[_0x39ea('0x19')]=_0x4452c8['deskAccounts'];this['vtigerAccounts']=_0x4452c8[_0x39ea('0x1a')];this['servicenowAccounts']=_0x4452c8[_0x39ea('0x1b')];this['dynamics365Accounts']=_0x4452c8['dynamics365Accounts'];this[_0x39ea('0x1c')]=_0x4452c8[_0x39ea('0x1c')];this[_0x39ea('0x1d')]={'zendesk':this[_0x39ea('0x14')],'salesforce':this[_0x39ea('0x16')],'freshdesk':this['freshdeskAccounts'],'sugarcrm':this['sugarcrmAccounts'],'zoho':this[_0x39ea('0x18')],'desk':this['deskAccounts'],'vtiger':this[_0x39ea('0x1a')],'dynamics365':this[_0x39ea('0x1e')],'freshsales':this[_0x39ea('0x1c')],'servicenow':this[_0x39ea('0x1b')]};ami['on'](_0x39ea('0x1f'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x22'),this[_0x39ea('0x20')]['bind'](this));ami['on']('custom:agentringnoanswer',this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on']('custom:agentcomplete',this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x23'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on']('custom:agentnoanswer',this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x24'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on']('custom:agentcongestion',this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x25'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x26'),this['syncCustomEvent']['bind'](this));ami['on'](_0x39ea('0x27'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on']('custom:timeout',this['syncCustomEvent'][_0x39ea('0x21')](this));ami['on']('custom:dialbegin',this[_0x39ea('0x28')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x29'),this[_0x39ea('0x28')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x2a'),this[_0x39ea('0x28')][_0x39ea('0x21')](this));ami['on']('custom:newinteraction',this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x2b'),this['syncCustomEvent']['bind'](this));ami['on']('custom:substatus',this['syncCustomEvent']['bind'](this));ami['on'](_0x39ea('0x2c'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x2d'),this[_0x39ea('0x20')][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x2e'),this['syncCustomEvent'][_0x39ea('0x21')](this));ami['on'](_0x39ea('0x2f'),this[_0x39ea('0x20')]['bind'](this));ami['on'](_0x39ea('0x30'),this['syncCustomEvent'][_0x39ea('0x21')](this));}Trigger[_0x39ea('0x31')][_0x39ea('0x32')]=function(_0x2e5694,_0x17728d,_0x1d9c8b){logger[_0x39ea('0x33')](_0x2e5694,_0x17728d,JSON[_0x39ea('0x34')](_0x1d9c8b));io['to'](_0x2e5694)[_0x39ea('0x32')](_0x17728d,_0x1d9c8b);};Trigger['prototype'][_0x39ea('0x20')]=function(_0x5203b9){var _0x2f519e=_['clone'](_0x5203b9);if(typeof _0x2f519e[_0x39ea('0x35')]===_0x39ea('0x36')){_0x2f519e[_0x39ea('0x35')]=_0x39ea('0x37');}this[_0x39ea('0x38')](_0x2f519e);};Trigger[_0x39ea('0x31')]['syncAgentDial']=function(_0x1212fe){if(this[_0x39ea('0x10')][_0x1212fe[_0x39ea('0x39')]]){this['syncCustomEvent'](_0x1212fe);}};Trigger[_0x39ea('0x31')][_0x39ea('0x38')]=function(_0x2e3fd3){var _0x2b93a9;for(var _0x143018 in this[_0x39ea('0xf')]){if(this[_0x39ea('0xf')][_0x39ea('0x3a')](_0x143018)){if(this[_0x39ea('0xf')][_0x143018][_0x39ea('0x3b')]){_0x2b93a9=this[_0x39ea('0x3c')](this[_0x39ea('0xf')][_0x143018],_0x2e3fd3);if(_0x2b93a9){_[_0x39ea('0x3d')](_0x2e3fd3,_0x2e3fd3[_0x39ea('0x3e')]);for(var _0x35397d=0x0,_0x680824,_0x3c8d87,_0x5b29bc;_0x35397d<this[_0x39ea('0xf')][_0x143018]['Actions']['length'];_0x35397d+=0x1){_0x680824=this[_0x39ea('0xf')][_0x143018][_0x39ea('0x3f')][_0x35397d];if(this[_0x39ea('0x10')][_0x2e3fd3[_0x39ea('0x39')]]){switch(_0x680824[_0x39ea('0x40')][_0x39ea('0x41')]()){case _0x39ea('0x42'):break;case _0x39ea('0x43'):case _0x39ea('0x44'):var _0xcab6ce={};switch(barAndWebActions[_0x680824[_0x39ea('0x45')]]){case _0x39ea('0xe'):_0xcab6ce[_0x39ea('0x46')]=_0x680824[_0x39ea('0x47')];_0xcab6ce[_0x39ea('0x48')]=this[_0x39ea('0x12')][_0x680824[_0x39ea('0x47')]]?Mustache[_0x39ea('0x49')](this[_0x39ea('0x12')][_0x680824['data2']]['html'],_0x2e3fd3):_0x39ea('0x4a');break;case _0x39ea('0x2'):_0xcab6ce[_0x39ea('0x4b')]=this['buildUri'](_0x680824['data2'],_0x2e3fd3,_[_0x39ea('0x4c')](_0x680824[_0x39ea('0x4d')])?!![]:parseInt(_0x680824[_0x39ea('0x4d')],0xa));if(_[_0x39ea('0x4c')](_0x680824['data4'])){_0xcab6ce['location']='0';}else{_0xcab6ce['uriLocation']=_0x680824[_0x39ea('0x4e')];_0xcab6ce[_0x39ea('0x4f')]=_[_0x39ea('0x4c')](_0x680824[_0x39ea('0x50')])?_0x39ea('0x51'):_0x680824[_0x39ea('0x50')];_0xcab6ce[_0x39ea('0x52')]=_[_0x39ea('0x4c')](_0x680824['data6'])?_0x39ea('0x53'):_0x680824[_0x39ea('0x54')];}break;case _0x39ea('0x55'):_0xcab6ce[_0x39ea('0x56')]=_0x680824[_0x39ea('0x47')]?Mustache['render'](_0x680824['data2'],_0x2e3fd3):'';_0xcab6ce['winArguments']=_0x680824[_0x39ea('0x4d')]?Mustache['render'](_0x680824['data3'],_0x2e3fd3):'';break;}this[_0x39ea('0x32')](util['format']('user:%s',this[_0x39ea('0x10')][_0x2e3fd3['interface']][_0x39ea('0x57')]),util['format']('trigger:%s:%s',_0x680824['action']['toLowerCase'](),barAndWebActions[_0x680824['data1']]),_['merge'](_0xcab6ce,_0x2e3fd3));break;case'jscripty':this[_0x39ea('0x32')](util[_0x39ea('0x58')](_0x39ea('0x59'),this[_0x39ea('0x10')][_0x2e3fd3['interface']][_0x39ea('0x57')]),util[_0x39ea('0x58')](_0x39ea('0x5a'),_0x680824[_0x39ea('0x40')]['toLowerCase']()),_[_0x39ea('0x3d')]({'ProjectId':parseInt(_0x680824['data1'],0xa)},_0x2e3fd3));break;default:this[_0x39ea('0x32')](util[_0x39ea('0x58')]('user:%s',this[_0x39ea('0x10')][_0x2e3fd3['interface']][_0x39ea('0x57')]),util[_0x39ea('0x58')](_0x39ea('0x5a'),_0x680824['action']['toLowerCase']()),_[_0x39ea('0x3d')]({},_0x680824,_0x2e3fd3));}}switch(_0x680824[_0x39ea('0x40')][_0x39ea('0x41')]()){case _0x39ea('0x42'):cmContact[_0x39ea('0x5b')](this['agents'][_0x2e3fd3[_0x39ea('0x39')]],this['triggers'][_0x143018],_0x680824,_0x2e3fd3);break;case _0x39ea('0x5c'):integration[_0x39ea('0x5b')](_0x680824,_0x2e3fd3,this[_0x39ea('0x1d')],this[_0x39ea('0x10')][_0x2e3fd3[_0x39ea('0x39')]],this['voiceQueues'][_0x2e3fd3[_0x39ea('0x5d')]],this[_0x39ea('0x13')][_0x2e3fd3[_0x39ea('0x5e')]]);break;case _0x39ea('0x5f'):urlforward[_0x39ea('0x5b')](_0x680824,_0x2e3fd3);break;case _0x39ea('0x60'):script[_0x39ea('0x5b')](_0x680824,_0x2e3fd3);break;}}}}}}};Trigger['prototype']['buildUri']=function(_0xaf9ac,_0x47f484,_0x42ab96){var _0x52fe11=url[_0x39ea('0x61')](Mustache[_0x39ea('0x49')](_0xaf9ac,_0x47f484),!![]);if(_0x42ab96){_['merge'](_0x52fe11[_0x39ea('0x62')],_0x47f484);}delete _0x52fe11['search'];var _0x4f2008=url['format'](_0x52fe11);_0x4f2008=_0x4f2008[_0x39ea('0x63')](/\/\?/g,'?');return _0x4f2008;};Trigger[_0x39ea('0x31')]['checkTrigger']=function(_0x38e98f,_0x3c8b42){var _0x4e2600=!![];var _0xb89016=!![];var _0x542259=![];if(_0x38e98f[_0x39ea('0x64')]===_0x3c8b42[_0x39ea('0x35')]){_0x542259=!![];}if(_0x38e98f[_0x39ea('0x65')][_0x39ea('0x66')]){_0x4e2600=_[_0x39ea('0x67')](_0x38e98f[_0x39ea('0x65')],this[_0x39ea('0x68')](_0x3c8b42,_0x38e98f[_0x39ea('0x64')]));}if(_0x38e98f[_0x39ea('0x69')][_0x39ea('0x66')]){_0xb89016=_[_0x39ea('0x6a')](_0x38e98f[_0x39ea('0x69')],this['checkConditions'](_0x3c8b42,_0x38e98f[_0x39ea('0x64')]));}return _0x542259&&_0x4e2600&&_0xb89016;};Trigger[_0x39ea('0x31')]['checkConditions']=function(_0x4a975c,_0x2bd72d){return function(_0x16a34e){switch(_0x16a34e[_0x39ea('0x6b')]){case _0x39ea('0x6c'):if(_0x4a975c[_0x39ea('0x64')]===_0x2bd72d){if(_0x2bd72d==='chat'){return _0x4a975c['ChatWebsiteId']?operators[_0x16a34e[_0x39ea('0x6d')]](Number(_0x16a34e[_0x39ea('0x6e')]),_0x4a975c[_0x39ea('0x6f')]):![];}else{return _0x4a975c[_[_0x39ea('0x70')](_0x2bd72d)+_0x39ea('0x71')]?operators[_0x16a34e[_0x39ea('0x6d')]](Number(_0x16a34e['value']),_0x4a975c[_[_0x39ea('0x70')](_0x2bd72d)+_0x39ea('0x71')]):![];}}break;case'interaction':case _0x39ea('0x72'):if(_0x4a975c['channel']===_0x2bd72d){return _0x4a975c['event']?operators[_0x16a34e['operator']](_0x16a34e[_0x39ea('0x6e')],_0x4a975c['event']):![];}break;case _0x39ea('0x5d'):return _0x4a975c[_0x39ea('0x5d')]?operators[_0x16a34e[_0x39ea('0x6d')]](_0x16a34e[_0x39ea('0x6e')],_0x4a975c[_0x39ea('0x5d')]):![];case'agent':return _0x4a975c[_0x39ea('0x73')]?operators[_0x16a34e[_0x39ea('0x6d')]](_0x16a34e[_0x39ea('0x6e')],_0x4a975c[_0x39ea('0x73')]):![];case _0x39ea('0x74'):return _0x4a975c[_0x39ea('0x74')]?operators[_0x16a34e[_0x39ea('0x6d')]](_0x16a34e[_0x39ea('0x6e')],_0x4a975c['lastevent']):![];case _0x39ea('0x5e'):return _0x4a975c[_0x39ea('0x5e')]?operators[_0x16a34e[_0x39ea('0x6d')]](_0x16a34e['value'],_0x4a975c[_0x39ea('0x5e')]):![];case'mailSubstatus':if(_0x16a34e[_0x39ea('0x6e')]==='null')_0x16a34e[_0x39ea('0x6e')]=null;return typeof _0x4a975c[_0x39ea('0x75')]!==_0x39ea('0x36')?operators[_0x16a34e['operator']](_0x16a34e[_0x39ea('0x6e')],_0x4a975c[_0x39ea('0x75')]):![];case _0x39ea('0x76'):if(_0x4a975c[_0x39ea('0x64')]===_0x2bd72d){return _0x16a34e['value']===_0x39ea('0x77')&&_0x16a34e['operator']==='equals'?!![]:![];}break;}return![];};};module[_0x39ea('0x78')]=Trigger;