49db60ca34b33640af774b29a7f45c316ada938d
[motion-next.git] / server / services / ami / trigger / integration / vtiger.js
1 const a1313_0x26d5=['info','GET','../../../../config/logger','Getting\x20agent\x20with\x20email','split','lodash','Fields','accessKey','Ticket\x20created!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','success','./util','serverUrl','toString','resolve','createTicket','then','result','lastname','login','recordingFormat','query','lastevent','POST','getUser','create','Variable','Searching\x20end\x20user\x20with\x20phone','validateEmail','The\x20User\x20ID\x20is:','forEach','Ticket\x20could\x20not\x20be\x20created\x20due\x20to:\x20%s','queue','util','destcalleridname','trigger:browser:url','token','monitor_format','format','type','[vTiger],\x20%s,\x20%s,\x20%s','remoteUri','Recording\x20is\x20enabled!','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','statusCode','Subjects','toLowerCase','[HTTP]','recordingURL','stripTrailingSlash','name','merge','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','variable','md5','SELECT\x20first_name,\x20last_name,\x20email1\x20FROM\x20Users\x20WHERE\x20email1=\x27%s\x27;','admin','idField','uniqueid','%s/index.php?module=HelpDesk&view=Detail&record=%s&app=SUPPORT','Enduser\x20found!','calleridname','phone','Creating\x20new\x20ticket...','email1','Normal','Open','catch','[OUTBOUND]','first_name','email','Contacts','username','../../../../rediscache','error','length','HelpDesk','destcalleridnum','[QUEUE]','calleridnum','content','Account\x20%d\x20is\x20not\x20valid','stringify','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','sessionName','vTiger','userId','firstname','getString','Agent\x20found!','user:%s','string','toUpperCase','variableName','%s/webservice.php'];(function(_0x410999,_0xd36aeb){const _0x26d510=function(_0x3e872c){while(--_0x3e872c){_0x410999['push'](_0x410999['shift']());}};_0x26d510(++_0xd36aeb);}(a1313_0x26d5,0xff));const a1313_0x3e87=function(_0x410999,_0xd36aeb){_0x410999=_0x410999-0x118;let _0x26d510=a1313_0x26d5[_0x410999];return _0x26d510;};const a1313_0x569e3a=a1313_0x3e87;'use strict';const _=require(a1313_0x569e3a(0x141)),rp=require('request-promise-native'),md5=require(a1313_0x569e3a(0x172)),util=require(a1313_0x569e3a(0x15d)),intUtil=require(a1313_0x569e3a(0x147)),logger=require(a1313_0x569e3a(0x13e))(a1313_0x569e3a(0x132)),{getSocket}=require(a1313_0x569e3a(0x124)),io=getSocket();function emit(_0x5774ce,_0x4b5535,_0x28f3c6){io['to'](_0x5774ce)['emit'](_0x4b5535,_0x28f3c6);}function getCustomFields(_0x228cee,_0x366180){const _0xe61975=a1313_0x569e3a,_0x54c6dd={};return _0x228cee[_0xe61975(0x126)]>0x0&&_[_0xe61975(0x15a)](_0x228cee,function(_0x4e91d1){const _0xf9227d=_0xe61975;if(_0x4e91d1[_0xf9227d(0x163)]===_0xf9227d(0x138)&&_0x4e91d1[_0xf9227d(0x175)])_0x54c6dd[_0x4e91d1['idField']]=_0x4e91d1[_0xf9227d(0x12b)][_0xf9227d(0x149)]();else{if(_0x4e91d1[_0xf9227d(0x163)]===_0xf9227d(0x171)&&_0x4e91d1['variableName']&&_0x4e91d1[_0xf9227d(0x175)]&&_0x366180[_0x4e91d1['variableName']])_0x54c6dd[_0x4e91d1[_0xf9227d(0x175)]]=_0x366180[_0x4e91d1[_0xf9227d(0x13a)]][_0xf9227d(0x149)]();else _0x4e91d1[_0xf9227d(0x163)]==='customVariable'&&_0x4e91d1[_0xf9227d(0x156)]&&_0x4e91d1[_0xf9227d(0x156)][_0xf9227d(0x16e)]&&_0x4e91d1[_0xf9227d(0x175)]&&_0x366180[_0x4e91d1[_0xf9227d(0x156)][_0xf9227d(0x16e)][_0xf9227d(0x16a)]()]&&(_0x54c6dd[_0x4e91d1[_0xf9227d(0x175)]]=_0x366180[_0x4e91d1[_0xf9227d(0x156)][_0xf9227d(0x16e)][_0xf9227d(0x16a)]()]['toString']());}}),_0x54c6dd;}function createTicket(_0x1320d9,_0x2deef8,_0x551c1d,_0x7972b6,_0x35838b,_0x5c0aac,_0x5002b8){const _0x186f11=a1313_0x569e3a;logger[_0x186f11(0x13c)](_0x186f11(0x11a));const _0x3477e5=_[_0x186f11(0x16f)]({'parent_id':'','assigned_user_id':_0x2deef8['id'],'ticketpriorities':_0x186f11(0x11c),'ticketstatus':_0x186f11(0x11d),'ticket_title':intUtil[_0x186f11(0x135)](_0x7972b6[_0x186f11(0x169)],_0x35838b,'\x20'),'description':intUtil['getString'](_0x7972b6['Descriptions'],_0x35838b,'\x0a'),'contact_id':_0x551c1d['id']},getCustomFields(_0x5c0aac,_0x35838b)),_0x4cbc74={'method':_0x186f11(0x153),'uri':util[_0x186f11(0x162)](_0x186f11(0x13b),_0x1320d9[_0x186f11(0x165)]),'form':{'operation':_0x186f11(0x155),'username':_0x1320d9[_0x186f11(0x123)],'sessionName':_0x5002b8[_0x186f11(0x131)],'elementType':_0x186f11(0x127),'element':JSON['stringify'](_0x3477e5)},'json':!![]};return request(_0x4cbc74)[_0x186f11(0x14c)](function(_0x11d529){const _0x39c722=_0x186f11;if(_0x11d529['success'])return Promise[_0x39c722(0x14a)]({'id':_0x11d529[_0x39c722(0x14d)]['id']});else throw new Error(util[_0x39c722(0x162)](_0x39c722(0x15b),JSON[_0x39c722(0x12d)](_0x11d529['error'])));})[_0x186f11(0x11e)](function(_0x1de910){const _0x1f6835=_0x186f11,_0x4611b3=_0x1de910[_0x1f6835(0x168)]||0x1f4;logger[_0x1f6835(0x125)](_0x1f6835(0x164),_0x1f6835(0x14b),_0x4611b3,JSON['stringify'](_0x1de910));});}function getUser(_0x39a7cd,_0x59d353,_0x47c2f1){const _0x53f608=a1313_0x569e3a;return intUtil[_0x53f608(0x158)](_0x59d353)?(logger['info'](_0x53f608(0x13f),_0x59d353),request({'method':_0x53f608(0x13d),'uri':util[_0x53f608(0x162)](_0x53f608(0x13b),_0x39a7cd[_0x53f608(0x165)]),'qs':{'operation':_0x53f608(0x151),'username':_0x39a7cd[_0x53f608(0x123)],'sessionName':_0x47c2f1[_0x53f608(0x131)],'query':util[_0x53f608(0x162)](_0x53f608(0x173),_0x59d353)},'json':!![]})['then'](function(_0x10eeb5){const _0x1d9822=_0x53f608;return _0x10eeb5[_0x1d9822(0x146)]&&_0x10eeb5[_0x1d9822(0x14d)][_0x1d9822(0x126)]?(logger[_0x1d9822(0x13c)](_0x1d9822(0x136)),Promise[_0x1d9822(0x14a)]({'id':_0x10eeb5['result'][0x0]['id'],'email':_0x10eeb5['result'][0x0][_0x1d9822(0x11b)],'name':_0x10eeb5[_0x1d9822(0x14d)][0x0][_0x1d9822(0x120)]+'\x20'+_0x10eeb5['result']['last_name']})):(logger[_0x1d9822(0x13c)]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!'),Promise[_0x1d9822(0x14a)]({'id':_0x47c2f1['userId'],'email':_0x39a7cd['username'],'name':_0x1d9822(0x174)}));})[_0x53f608(0x11e)](function(_0x2116bd){const _0x4daf43=_0x53f608,_0x50cdcb=_0x2116bd[_0x4daf43(0x168)]||0x1f4;logger[_0x4daf43(0x125)](_0x4daf43(0x164),'getUser',_0x50cdcb,JSON[_0x4daf43(0x12d)](_0x2116bd));})):(logger[_0x53f608(0x13c)](_0x53f608(0x12e)),Promise[_0x53f608(0x14a)]({'id':_0x47c2f1[_0x53f608(0x133)],'email':_0x39a7cd[_0x53f608(0x123)],'name':'admin'})['catch'](function(_0x404cfa){const _0x5ecf13=_0x53f608,_0x195bdc=_0x404cfa[_0x5ecf13(0x168)]||0x1f4;logger[_0x5ecf13(0x125)](_0x5ecf13(0x164),_0x5ecf13(0x154),_0x195bdc,JSON[_0x5ecf13(0x12d)](_0x404cfa));}));}function getEndUser(_0x938f66,_0xdebaa8,_0x6191e4,_0x6219fb){const _0xfc205f=a1313_0x569e3a;return logger['info'](_0xfc205f(0x157),_0x6191e4),request({'method':_0xfc205f(0x13d),'uri':util['format'](_0xfc205f(0x13b),_0x938f66[_0xfc205f(0x165)]),'qs':{'operation':_0xfc205f(0x151),'username':_0x938f66['username'],'sessionName':_0x6219fb[_0xfc205f(0x131)],'query':util[_0xfc205f(0x162)]('SELECT\x20id,\x20firstname,\x20lastname,\x20email,\x20phone,\x20mobile,\x20homephone,\x20otherphone,\x20assistantphone\x20FROM\x20Contacts\x20WHERE\x20phone\x20=\x20\x27%s\x27\x20OR\x20mobile\x20=\x27%s\x27\x20OR\x20homephone\x20=\x27%s\x27\x20OR\x20otherphone\x20=\x27%s\x27\x20OR\x20assistantphone\x20=\x27%s\x27;',_0x6191e4,_0x6191e4,_0x6191e4,_0x6191e4,_0x6191e4)},'json':!![]})[_0xfc205f(0x14c)](function(_0x5aa118){const _0x14ab3b=_0xfc205f;return _0x5aa118[_0x14ab3b(0x146)]&&_0x5aa118['result']['length']?(logger[_0x14ab3b(0x13c)](_0x14ab3b(0x178)),Promise[_0x14ab3b(0x14a)]({'id':_0x5aa118['result'][0x0]['id'],'name':_0x5aa118['result'][0x0][_0x14ab3b(0x134)]+'\x20'+_0x5aa118[_0x14ab3b(0x14d)][0x0][_0x14ab3b(0x14e)],'phone':_0x6191e4})):(logger['info'](_0x14ab3b(0x145)),request({'method':_0x14ab3b(0x153),'uri':util[_0x14ab3b(0x162)]('%s/webservice.php',_0x938f66[_0x14ab3b(0x165)]),'form':{'operation':_0x14ab3b(0x155),'sessionName':_0x6219fb[_0x14ab3b(0x131)],'elementType':_0x14ab3b(0x122),'element':JSON['stringify']({'lastname':_0xdebaa8,'phone':_0x6191e4,'assigned_user_id':'\x20'})},'json':!![]})[_0x14ab3b(0x14c)](function(_0x4d0b2e){const _0x5de542=_0x14ab3b;return logger[_0x5de542(0x13c)]('Enduser\x20created.'),Promise[_0x5de542(0x14a)]({'id':_0x4d0b2e[_0x5de542(0x14d)]['id'],'name':_0x4d0b2e[_0x5de542(0x14d)][_0x5de542(0x14e)],'phone':_0x4d0b2e[_0x5de542(0x14d)][_0x5de542(0x119)]});})[_0x14ab3b(0x11e)](function(_0x15b067){const _0xe44ec2=_0x14ab3b,_0x5af7e8=_0x15b067[_0xe44ec2(0x168)]||0x1f4;logger[_0xe44ec2(0x125)](_0xe44ec2(0x164),'getEndUser',_0x5af7e8,JSON['stringify'](_0x15b067));}));});}function getConnection(_0x2c10d1){const _0x5bcd97=a1313_0x569e3a,_0x375c15=_0x2c10d1[_0x5bcd97(0x123)],_0x4760db=_0x2c10d1[_0x5bcd97(0x143)];return request({'method':_0x5bcd97(0x13d),'uri':util['format']('%s/webservice.php',_0x2c10d1['remoteUri']),'qs':{'operation':'getchallenge','username':_0x375c15},'json':!![]})['then'](function(_0x593f78){const _0x337dd5=_0x5bcd97;if(_0x593f78[_0x337dd5(0x146)])return request({'method':_0x337dd5(0x153),'uri':util[_0x337dd5(0x162)]('%s/webservice.php',_0x2c10d1['remoteUri']),'form':{'operation':_0x337dd5(0x14f),'username':_0x375c15,'accessKey':md5(_0x593f78[_0x337dd5(0x14d)][_0x337dd5(0x160)]+_0x4760db)},'json':!![]});})[_0x5bcd97(0x14c)](function(_0x22eef6){const _0x4bf4f2=_0x5bcd97;if(_0x22eef6['success'])return Promise[_0x4bf4f2(0x14a)](_0x22eef6[_0x4bf4f2(0x14d)]);else throw new Error(util[_0x4bf4f2(0x162)](_0x4bf4f2(0x12c),_0x2c10d1['id']));})[_0x5bcd97(0x11e)](function(_0x9ca89){const _0x531b4b=_0x5bcd97,_0x388409=_0x9ca89['statusCode']||0x1f4;logger['error']('[vTiger],\x20%s,\x20%s,\x20%s','getConnection',_0x388409,JSON[_0x531b4b(0x12d)](_0x9ca89));});}function request(_0x134cf4){const _0x56ee62=a1313_0x569e3a;return logger[_0x56ee62(0x13c)](_0x56ee62(0x16b),JSON['stringify'](_0x134cf4)),rp(_0x134cf4);}exports[a1313_0x569e3a(0x15c)]=function(_0x1acba4,_0x2c7221,_0xa9f3c6,_0x5aa05b,_0x188420,_0x9ee8f6){const _0x3d6f98=a1313_0x569e3a;let _0x187454,_0x9721a9,_0x1239ab,_0x144174;return _0x9ee8f6[_0x3d6f98(0x165)]=intUtil[_0x3d6f98(0x16d)](_0x9ee8f6['remoteUri']),_0x188420&&_0x188420['monitor_format']?(logger['info'](_0x3d6f98(0x129),_0x3d6f98(0x166)),_0x2c7221[_0x3d6f98(0x16c)]=util[_0x3d6f98(0x162)]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x9ee8f6[_0x3d6f98(0x148)],_0x2c7221[_0x3d6f98(0x176)],md5(_0x2c7221[_0x3d6f98(0x176)]))):_0x2c7221[_0x3d6f98(0x16c)]='',getConnection(_0x9ee8f6)['then'](function(_0x31dc23){const _0x44bf7e=_0x3d6f98;return _0x144174=_0x31dc23,getUser(_0x9ee8f6,_0x5aa05b[_0x44bf7e(0x121)],_0x144174);})[_0x3d6f98(0x14c)](function(_0x2049f8){const _0x411354=_0x3d6f98;return _0x187454=_0x2049f8,logger[_0x411354(0x13c)](_0x411354(0x159),_0x187454['id']),getEndUser(_0x9ee8f6,_0x2c7221[_0x411354(0x118)],_0x2c7221[_0x411354(0x12a)],_0x144174);})[_0x3d6f98(0x14c)](function(_0x1eaaa1){const _0x34a8e4=_0x3d6f98;return _0x9721a9=_0x1eaaa1,logger[_0x34a8e4(0x13c)](_0x34a8e4(0x12f),_0x9721a9['id']),createTicket(_0x9ee8f6,_0x187454,_0x9721a9,_0xa9f3c6,_0x2c7221,_0xa9f3c6['Fields'],_0x144174);})[_0x3d6f98(0x14c)](function(_0x1dcd3b){const _0x284555=_0x3d6f98;_0x1239ab=_0x1dcd3b,logger[_0x284555(0x13c)]('Ticket\x20created!'),logger[_0x284555(0x13c)](_0x284555(0x130),_0x1239ab['id']);const _0x2384d7=util['format'](_0x284555(0x177),_0x9ee8f6['remoteUri'],_[_0x284555(0x140)](_0x1239ab['id'],'x')[0x1]);logger[_0x284555(0x13c)](_0x284555(0x129),_0x284555(0x167)),emit(util['format']('user:%s',_0x5aa05b[_0x284555(0x16e)]),_0x284555(0x15f),{'uri':_0x2384d7});})['catch'](function(_0x32cd1a){const _0x3b682e=_0x3d6f98;logger[_0x3b682e(0x125)](_0x3b682e(0x129),JSON['stringify'](_0x32cd1a));});},exports['outbound']=function(_0x25cbb4,_0x148f97,_0x5b0a84,_0x322e00,_0x7232a2,_0x50c69c,_0x594b32){const _0x408b9f=a1313_0x569e3a;let _0x2ab0c1,_0x110afc,_0x20c1f7,_0x130abd;return _0x50c69c['remoteUri']=intUtil[_0x408b9f(0x16d)](_0x50c69c[_0x408b9f(0x165)]),_0x50c69c[_0x408b9f(0x148)]=intUtil[_0x408b9f(0x16d)](_0x50c69c[_0x408b9f(0x148)]),_0x594b32&&_0x594b32[_0x408b9f(0x150)]&&_0x594b32[_0x408b9f(0x150)]!=='none'&&(logger[_0x408b9f(0x13c)]('[OUTBOUND]','Recording\x20is\x20enabled!'),_0x148f97[_0x408b9f(0x16c)]=util['format'](_0x408b9f(0x170),_0x50c69c[_0x408b9f(0x148)],_0x148f97['uniqueid'],md5(_0x148f97['uniqueid']))),getConnection(_0x50c69c)[_0x408b9f(0x14c)](function(_0xc07a7a){const _0xd239aa=_0x408b9f;return _0x130abd=_0xc07a7a,getUser(_0x50c69c,_0x322e00[_0xd239aa(0x121)],_0x130abd);})[_0x408b9f(0x14c)](function(_0x38f3f0){const _0x4de8ec=_0x408b9f;return _0x2ab0c1=_0x38f3f0,logger[_0x4de8ec(0x13c)](_0x4de8ec(0x159),_0x2ab0c1['id']),getEndUser(_0x50c69c,_0x148f97[_0x4de8ec(0x15e)],_0x148f97[_0x4de8ec(0x128)],_0x130abd);})['then'](function(_0x5e36a3){const _0x1c588e=_0x408b9f;return _0x110afc=_0x5e36a3,logger['info'](_0x1c588e(0x12f),_0x110afc['id']),createTicket(_0x50c69c,_0x2ab0c1,_0x110afc,_0x5b0a84,_0x148f97,_0x5b0a84[_0x1c588e(0x142)],_0x130abd);})[_0x408b9f(0x14c)](function(_0x421829){const _0x2c8ba2=_0x408b9f;_0x20c1f7=_0x421829,logger['info'](_0x2c8ba2(0x144)),logger['info']('The\x20Ticket\x20ID\x20is:',_0x20c1f7['id']);const _0x31d01d=util['format'](_0x2c8ba2(0x177),_0x50c69c[_0x2c8ba2(0x165)],_['split'](_0x20c1f7['id'],'x')[0x1]);logger[_0x2c8ba2(0x13c)](_0x2c8ba2(0x11f),_0x2c8ba2(0x167)),emit(util[_0x2c8ba2(0x162)](_0x2c8ba2(0x137),_0x322e00[_0x2c8ba2(0x16e)]),_0x2c8ba2(0x15f),{'uri':_0x31d01d});})[_0x408b9f(0x11e)](function(_0x265bba){const _0x4c3717=_0x408b9f;logger[_0x4c3717(0x125)]('[OUTBOUND]',JSON[_0x4c3717(0x12d)](_0x265bba));});},exports['unmanaged']=function(_0x381761,_0x650098,_0x251fbc,_0x542e2f,_0x1bf47c,_0x277357){const _0x2945ec=a1313_0x569e3a;let _0x38bc0a,_0x2974b1,_0x1d23b5,_0x374f73;return _0x277357['remoteUri']=intUtil[_0x2945ec(0x16d)](_0x277357['remoteUri']),_0x277357[_0x2945ec(0x148)]=intUtil[_0x2945ec(0x16d)](_0x277357[_0x2945ec(0x148)]),_0x1bf47c&&_0x1bf47c[_0x2945ec(0x161)]?(logger['info']('['+_0x650098[_0x2945ec(0x152)][_0x2945ec(0x139)]()+']',_0x2945ec(0x166)),_0x650098[_0x2945ec(0x16c)]=util[_0x2945ec(0x162)]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x277357[_0x2945ec(0x148)],_0x650098['uniqueid'],md5(_0x650098[_0x2945ec(0x176)]))):_0x650098['recordingURL']='',getConnection(_0x277357)['then'](function(_0x53ba0e){return _0x374f73=_0x53ba0e,getUser(_0x277357,'',_0x374f73);})[_0x2945ec(0x14c)](function(_0x130283){const _0xa81fa2=_0x2945ec;return _0x38bc0a=_0x130283,logger[_0xa81fa2(0x13c)](_0xa81fa2(0x159),_0x38bc0a['id']),getEndUser(_0x277357,_0x650098['calleridname'],_0x650098[_0xa81fa2(0x12a)],_0x374f73);})[_0x2945ec(0x14c)](function(_0x41045c){const _0x2738be=_0x2945ec;return _0x2974b1=_0x41045c,logger[_0x2738be(0x13c)](_0x2738be(0x12f),_0x2974b1['id']),createTicket(_0x277357,_0x38bc0a,_0x2974b1,_0x251fbc,_0x650098,_0x251fbc[_0x2738be(0x142)],_0x374f73);})[_0x2945ec(0x14c)](function(_0x45c6a1){const _0x5049df=_0x2945ec;_0x1d23b5=_0x45c6a1,logger[_0x5049df(0x13c)](_0x5049df(0x144)),logger[_0x5049df(0x13c)](_0x5049df(0x130),_0x1d23b5['id']);})[_0x2945ec(0x11e)](function(_0x36f26d){const _0x3171f9=_0x2945ec;logger[_0x3171f9(0x125)]('['+_0x650098[_0x3171f9(0x152)]['toUpperCase']()+']',JSON[_0x3171f9(0x12d)](_0x36f26d));});};