f82ecffd5f84a5023520f9086c87ad01ff15a1ca
[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 _0x7890=['./cmContact','./script','../rpc/cmContact','../../../config/logger','../ami','redis','defaults','socket.io-emitter','popup','url','triggers','agents','voiceQueues','templates','voiceExtensions','zendeskAccounts','freshdeskAccounts','salesforceAccounts','sugarcrmAccounts','zohoAccounts','vtigerAccounts','servicenowAccounts','dynamics365Accounts','freshsalesAccounts','accounts','deskAccounts','syncCustomEvent','bind','custom:agentconnect','custom:agentcomplete','custom:agentnosuchnumber','custom:agentnoanswer','custom:agentbusy','custom:agentcongestion','custom:agentreject','custom:timeout','custom:dialbegin','syncAgentDial','custom:dialend','custom:newinteraction','custom:incomingmessage','custom:outgoingmessage','custom:acceptmessage','custom:rejectmessage','prototype','emit','info','stringify','clone','checkTriggers','hasOwnProperty','status','checkTrigger','variables','Actions','length','interface','action','contactmanager','motionbar','browser','TemplateId','data2','html','render','uri','buildUri','data3','isNil','data4','location','uriLocation','data5','800','windowHeight','data6','600','winapp','winApplication','format','user:%s','trigger:%s:%s','data1','merge','jscripty','name','toLowerCase','integration','exec','routeId','urlforward','script','parse','query','every','checkConditions','channel','AnyConditions','some','field','account','chat','ChatWebsiteId','operator','value','AccountId','upperFirst','interaction','message','event','queue','membername','lastevent','exports','util','lodash','mustache','./urlforward','./integration'];(function(_0x17d0ee,_0x410179){var _0x429bc4=function(_0x1ff923){while(--_0x1ff923){_0x17d0ee['push'](_0x17d0ee['shift']());}};_0x429bc4(++_0x410179);}(_0x7890,0x1d7));var _0x0789=function(_0x489608,_0x1a1529){_0x489608=_0x489608-0x0;var _0x352a2d=_0x7890[_0x489608];return _0x352a2d;};'use strict';var util=require(_0x0789('0x0'));var _=require(_0x0789('0x1'));var url=require('url');var Mustache=require(_0x0789('0x2'));var Redis=require('ioredis');var urlforward=require(_0x0789('0x3'));var integration=require(_0x0789('0x4'));var cmContact=require(_0x0789('0x5'));var script=require(_0x0789('0x6'));var cmContactRpc=require(_0x0789('0x7'));var config=require('../../../config/environment');var logger=require(_0x0789('0x8'))('trigger');var ami=require(_0x0789('0x9'));config[_0x0789('0xa')]=_[_0x0789('0xb')](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0x0789('0xc'))(new Redis(config[_0x0789('0xa')]));var operators={'equals':function(_0x4d23ba,_0x430f30){return _0x4d23ba==_0x430f30;},'not_equals':function(_0x3da542,_0x22db63){return _0x3da542!=_0x22db63;}};var barAndWebActions=[_0x0789('0xd'),_0x0789('0xe'),'winapp'];function Trigger(_0x2a36f3){this[_0x0789('0xf')]=_0x2a36f3['triggers'];this[_0x0789('0x10')]=_0x2a36f3[_0x0789('0x10')];this[_0x0789('0x11')]=_0x2a36f3[_0x0789('0x11')];this['templates']=_0x2a36f3[_0x0789('0x12')];this[_0x0789('0x13')]=_0x2a36f3[_0x0789('0x13')];this[_0x0789('0x14')]=_0x2a36f3[_0x0789('0x14')];this[_0x0789('0x15')]=_0x2a36f3[_0x0789('0x15')];this[_0x0789('0x16')]=_0x2a36f3[_0x0789('0x16')];this[_0x0789('0x17')]=_0x2a36f3[_0x0789('0x17')];this[_0x0789('0x18')]=_0x2a36f3[_0x0789('0x18')];this['deskAccounts']=_0x2a36f3['deskAccounts'];this[_0x0789('0x19')]=_0x2a36f3[_0x0789('0x19')];this[_0x0789('0x1a')]=_0x2a36f3[_0x0789('0x1a')];this[_0x0789('0x1b')]=_0x2a36f3['dynamics365Accounts'];this[_0x0789('0x1c')]=_0x2a36f3[_0x0789('0x1c')];this[_0x0789('0x1d')]={'zendesk':this[_0x0789('0x14')],'salesforce':this['salesforceAccounts'],'freshdesk':this['freshdeskAccounts'],'sugarcrm':this[_0x0789('0x17')],'zoho':this['zohoAccounts'],'desk':this[_0x0789('0x1e')],'vtiger':this[_0x0789('0x19')],'dynamics365':this[_0x0789('0x1b')],'freshsales':this[_0x0789('0x1c')],'servicenow':this[_0x0789('0x1a')]};ami['on']('custom:agentcalled',this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on'](_0x0789('0x21'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on']('custom:agentringnoanswer',this['syncCustomEvent'][_0x0789('0x20')](this));ami['on'](_0x0789('0x22'),this[_0x0789('0x1f')]['bind'](this));ami['on'](_0x0789('0x23'),this['syncCustomEvent'][_0x0789('0x20')](this));ami['on'](_0x0789('0x24'),this['syncCustomEvent']['bind'](this));ami['on'](_0x0789('0x25'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on'](_0x0789('0x26'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on'](_0x0789('0x27'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on']('custom:queuecallerabandon',this[_0x0789('0x1f')]['bind'](this));ami['on'](_0x0789('0x28'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on'](_0x0789('0x29'),this[_0x0789('0x2a')][_0x0789('0x20')](this));ami['on']('custom:dialconnect',this[_0x0789('0x2a')]['bind'](this));ami['on'](_0x0789('0x2b'),this['syncAgentDial'][_0x0789('0x20')](this));ami['on'](_0x0789('0x2c'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on']('custom:closedinteraction',this[_0x0789('0x1f')]['bind'](this));ami['on'](_0x0789('0x2d'),this['syncCustomEvent']['bind'](this));ami['on'](_0x0789('0x2e'),this[_0x0789('0x1f')][_0x0789('0x20')](this));ami['on'](_0x0789('0x2f'),this['syncCustomEvent'][_0x0789('0x20')](this));ami['on'](_0x0789('0x30'),this[_0x0789('0x1f')][_0x0789('0x20')](this));}Trigger[_0x0789('0x31')][_0x0789('0x32')]=function(_0x5ef682,_0xf4b83b,_0x45f5cd){logger[_0x0789('0x33')](_0x5ef682,_0xf4b83b,JSON[_0x0789('0x34')](_0x45f5cd));io['to'](_0x5ef682)[_0x0789('0x32')](_0xf4b83b,_0x45f5cd);};Trigger[_0x0789('0x31')][_0x0789('0x1f')]=function(_0x14eeff){var _0x3b829a=_[_0x0789('0x35')](_0x14eeff);this[_0x0789('0x36')](_0x3b829a);};Trigger['prototype'][_0x0789('0x2a')]=function(_0x23c928){if(this['agents'][_0x23c928['interface']]){this[_0x0789('0x1f')](_0x23c928);}};Trigger[_0x0789('0x31')][_0x0789('0x36')]=function(_0x41ca77){var _0x58cd19;for(var _0x58e6cd in this[_0x0789('0xf')]){if(this['triggers'][_0x0789('0x37')](_0x58e6cd)){if(this['triggers'][_0x58e6cd][_0x0789('0x38')]){_0x58cd19=this[_0x0789('0x39')](this['triggers'][_0x58e6cd],_0x41ca77);if(_0x58cd19){_['merge'](_0x41ca77,_0x41ca77[_0x0789('0x3a')]);for(var _0x5e63fc=0x0,_0xe0788,_0x184288,_0x3f7b6b;_0x5e63fc<this['triggers'][_0x58e6cd][_0x0789('0x3b')][_0x0789('0x3c')];_0x5e63fc+=0x1){_0xe0788=this[_0x0789('0xf')][_0x58e6cd]['Actions'][_0x5e63fc];if(this[_0x0789('0x10')][_0x41ca77[_0x0789('0x3d')]]){switch(_0xe0788[_0x0789('0x3e')]['toLowerCase']()){case _0x0789('0x3f'):break;case _0x0789('0x40'):case _0x0789('0x41'):var _0x16d8bd={};switch(barAndWebActions[_0xe0788['data1']]){case'popup':_0x16d8bd[_0x0789('0x42')]=_0xe0788[_0x0789('0x43')];_0x16d8bd[_0x0789('0x44')]=this[_0x0789('0x12')][_0xe0788['data2']]?Mustache[_0x0789('0x45')](this['templates'][_0xe0788[_0x0789('0x43')]][_0x0789('0x44')],_0x41ca77):'template\x20not\x20found';break;case _0x0789('0xe'):_0x16d8bd[_0x0789('0x46')]=this[_0x0789('0x47')](_0xe0788['data2'],_0x41ca77,_['isNil'](_0xe0788[_0x0789('0x48')])?!![]:parseInt(_0xe0788['data3'],0xa));if(_[_0x0789('0x49')](_0xe0788[_0x0789('0x4a')])){_0x16d8bd[_0x0789('0x4b')]='0';}else{_0x16d8bd[_0x0789('0x4c')]=_0xe0788[_0x0789('0x4a')];_0x16d8bd['windowWidth']=_[_0x0789('0x49')](_0xe0788[_0x0789('0x4d')])?_0x0789('0x4e'):_0xe0788[_0x0789('0x4d')];_0x16d8bd[_0x0789('0x4f')]=_['isNil'](_0xe0788[_0x0789('0x50')])?_0x0789('0x51'):_0xe0788[_0x0789('0x50')];}break;case _0x0789('0x52'):_0x16d8bd[_0x0789('0x53')]=_0xe0788[_0x0789('0x43')]?Mustache['render'](_0xe0788[_0x0789('0x43')],_0x41ca77):'';_0x16d8bd['winArguments']=_0xe0788[_0x0789('0x48')]?Mustache[_0x0789('0x45')](_0xe0788[_0x0789('0x48')],_0x41ca77):'';break;}this[_0x0789('0x32')](util[_0x0789('0x54')](_0x0789('0x55'),this[_0x0789('0x10')][_0x41ca77[_0x0789('0x3d')]]['name']),util[_0x0789('0x54')](_0x0789('0x56'),_0xe0788[_0x0789('0x3e')]['toLowerCase'](),barAndWebActions[_0xe0788[_0x0789('0x57')]]),_[_0x0789('0x58')](_0x16d8bd,_0x41ca77));break;case _0x0789('0x59'):this[_0x0789('0x32')](util[_0x0789('0x54')](_0x0789('0x55'),this[_0x0789('0x10')][_0x41ca77['interface']][_0x0789('0x5a')]),util[_0x0789('0x54')]('trigger:%s',_0xe0788[_0x0789('0x3e')][_0x0789('0x5b')]()),_[_0x0789('0x58')]({'ProjectId':parseInt(_0xe0788[_0x0789('0x57')],0xa)},_0x41ca77));break;default:this[_0x0789('0x32')](util[_0x0789('0x54')]('user:%s',this['agents'][_0x41ca77['interface']]['name']),util[_0x0789('0x54')]('trigger:%s',_0xe0788[_0x0789('0x3e')][_0x0789('0x5b')]()),_[_0x0789('0x58')]({},_0xe0788,_0x41ca77));}}switch(_0xe0788[_0x0789('0x3e')][_0x0789('0x5b')]()){case _0x0789('0x3f'):cmContact['exec'](this[_0x0789('0x10')][_0x41ca77[_0x0789('0x3d')]],_0xe0788,_0x41ca77,cmContactRpc);break;case _0x0789('0x5c'):integration[_0x0789('0x5d')](_0xe0788,_0x41ca77,this['accounts'],this['agents'][_0x41ca77[_0x0789('0x3d')]],this['voiceQueues'][_0x41ca77['queue']],this[_0x0789('0x13')][_0x41ca77[_0x0789('0x5e')]]);break;case _0x0789('0x5f'):urlforward[_0x0789('0x5d')](_0xe0788,_0x41ca77);break;case _0x0789('0x60'):script[_0x0789('0x5d')](_0xe0788,_0x41ca77);break;}}}}}}};Trigger['prototype']['buildUri']=function(_0x5d1f68,_0x208534,_0x253830){var _0xaa241a=url[_0x0789('0x61')](Mustache[_0x0789('0x45')](_0x5d1f68,_0x208534),!![]);if(_0x253830){_[_0x0789('0x58')](_0xaa241a[_0x0789('0x62')],_0x208534);}delete _0xaa241a['search'];var _0x5675e9=url['format'](_0xaa241a);_0x5675e9=_0x5675e9['replace'](/\/\?/g,'?');return _0x5675e9;};Trigger['prototype']['checkTrigger']=function(_0x11bfc3,_0x4915f7){var _0x395ab8=!![],_0xc8799f=!![];if(_0x11bfc3['AllConditions'][_0x0789('0x3c')]){_0x395ab8=_[_0x0789('0x63')](_0x11bfc3['AllConditions'],this[_0x0789('0x64')](_0x4915f7,_0x11bfc3[_0x0789('0x65')]));}if(_0x11bfc3[_0x0789('0x66')][_0x0789('0x3c')]){_0xc8799f=_[_0x0789('0x67')](_0x11bfc3[_0x0789('0x66')],this[_0x0789('0x64')](_0x4915f7,_0x11bfc3['channel']));}return _0x395ab8&&_0xc8799f;};Trigger['prototype'][_0x0789('0x64')]=function(_0x5bb16f,_0x4f82df){return function(_0x52536a){switch(_0x52536a[_0x0789('0x68')]){case _0x0789('0x69'):if(_0x5bb16f[_0x0789('0x65')]===_0x4f82df){if(_0x4f82df===_0x0789('0x6a')){return _0x5bb16f[_0x0789('0x6b')]?operators[_0x52536a[_0x0789('0x6c')]](Number(_0x52536a[_0x0789('0x6d')]),_0x5bb16f[_0x0789('0x6b')]):![];}else{return _0x5bb16f[_['upperFirst'](_0x4f82df)+_0x0789('0x6e')]?operators[_0x52536a[_0x0789('0x6c')]](Number(_0x52536a[_0x0789('0x6d')]),_0x5bb16f[_[_0x0789('0x6f')](_0x4f82df)+'AccountId']):![];}}break;case _0x0789('0x70'):case _0x0789('0x71'):if(_0x5bb16f['channel']===_0x4f82df){return _0x5bb16f[_0x0789('0x72')]?operators[_0x52536a[_0x0789('0x6c')]](_0x52536a['value'],_0x5bb16f[_0x0789('0x72')]):![];}break;case'queue':return _0x5bb16f[_0x0789('0x73')]?operators[_0x52536a['operator']](_0x52536a[_0x0789('0x6d')],_0x5bb16f[_0x0789('0x73')]):![];case'agent':return _0x5bb16f[_0x0789('0x74')]?operators[_0x52536a[_0x0789('0x6c')]](_0x52536a['value'],_0x5bb16f['membername']):![];case _0x0789('0x75'):return _0x5bb16f['lastevent']?operators[_0x52536a[_0x0789('0x6c')]](_0x52536a['value'],_0x5bb16f[_0x0789('0x75')]):![];case _0x0789('0x5e'):return _0x5bb16f[_0x0789('0x5e')]?operators[_0x52536a['operator']](_0x52536a[_0x0789('0x6d')],_0x5bb16f[_0x0789('0x5e')]):![];}return![];};};module[_0x0789('0x76')]=Trigger;