4d4f8e0a66199435a7b4152ebe4b743c7792052f
[motion2.git] / server / api / plugin / plugin.controller.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 _0x0bf4=['sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','extract-zip','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','socket.io-emitter','./plugin.socket','register','sendStatus','status','json','undefined','count','limit','set','Content-Range','apply','reject','save','update','then','destroy','get','Plugins','UserProfileResource','end','error','stack','name','send','index','map','Plugin','fieldName','model','query','keys','filters','attributes','intersection','fields','length','offset','order','sort','pick','filter','where','merge','type','findAll','rows','catch','params','includeAll','include','options','download','find','join','root','format','%s.zip','basename','createWriteStream','zip','warning','code','ENOENT','Error\x20while\x20generating\x20the\x20zip\x20archive','Sequelize','Error','pipe','glob','**/*','server/files/plugins','node_modules/**/*','attachments/**/*','finalize','Plugin\x20not\x20found','unlinkSync','info','Download\x20completed','Temporary\x20file\x20removed','file','filename','Plugin\x20upload\x20filepath:\x20%s','Plugin\x20upload\x20destination:\x20%s','chmodSync','0777','readdirSync','Invalid\x20plugin\x20structure','lstatSync','isFile','isDirectory','manifest.json','Plugin\x20manifest\x20missing','readFileSync','parse','Invalid\x20manifest:\x20type\x20missing','version','Invalid\x20manifest:\x20version\x20not\x20specified','parameters','Invalid\x20manifest:\x20parameters\x20missing','html','views','Invalid\x20manifest:\x20views\x20missing','admin','agent','Invalid\x20manifest:\x20script\x20name\x20missing','scriptPath','Invalid\x20manifest:\x20script\x20path\x20missing','Invalid\x20manifest:\x20invalid\x20type','description','sidebar','always','icon','icon-apps','test','plugins','Warning:\x20agent\x20view\x20not\x20specified','script','body','role','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','userProfileId','autoAssociation','create','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','Zip\x20archive\x20removed','Extracted\x20folder\x20removed','delete','dirname','webhookGet','\x27PATH\x27\x20parameter\x20not\x20specified','\x27PORT\x27\x20parameter\x20not\x20specified','hostname','port','http://localhost:','encoding','method','%s%s','headers','content-length','content-type','endsWith','statusCode','resolve','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','path'];(function(_0xc4001c,_0x1bf8f0){var _0x49f7bc=function(_0x1059b8){while(--_0x1059b8){_0xc4001c['push'](_0xc4001c['shift']());}};_0x49f7bc(++_0x1bf8f0);}(_0x0bf4,0x156));var _0x40bf=function(_0x490c02,_0x2faa1b){_0x490c02=_0x490c02-0x0;var _0x4604ab=_0x0bf4[_0x490c02];return _0x4604ab;};'use strict';var emlformat=require(_0x40bf('0x0'));var rimraf=require(_0x40bf('0x1'));var zipdir=require(_0x40bf('0x2'));var jsonpatch=require(_0x40bf('0x3'));var rp=require('request-promise');var moment=require(_0x40bf('0x4'));var BPromise=require(_0x40bf('0x5'));var Mustache=require(_0x40bf('0x6'));var util=require('util');var path=require(_0x40bf('0x7'));var sox=require(_0x40bf('0x8'));var csv=require('to-csv');var ejs=require(_0x40bf('0x9'));var fs=require('fs');var fs_extra=require(_0x40bf('0xa'));var _=require(_0x40bf('0xb'));var squel=require(_0x40bf('0xc'));var crypto=require(_0x40bf('0xd'));var jsforce=require(_0x40bf('0xe'));var deskjs=require(_0x40bf('0xf'));var toCsv=require(_0x40bf('0x10'));var querystring=require(_0x40bf('0x11'));var Papa=require(_0x40bf('0x12'));var extract=require(_0x40bf('0x13'));var rimraf=require(_0x40bf('0x1'));var archiver=require('archiver');var Redis=require(_0x40bf('0x14'));var authService=require(_0x40bf('0x15'));var qs=require(_0x40bf('0x16'));var as=require(_0x40bf('0x17'));var hardwareService=require(_0x40bf('0x18'));var logger=require('../../config/logger')('api');var utils=require(_0x40bf('0x19'));var config=require(_0x40bf('0x1a'));var licenseUtil=require(_0x40bf('0x1b'));var db=require(_0x40bf('0x1c'))['db'];config[_0x40bf('0x1d')]=_['defaults'](config[_0x40bf('0x1d')],{'host':'localhost','port':0x18eb});var socket=require(_0x40bf('0x1e'))(new Redis(config['redis']));require(_0x40bf('0x1f'))[_0x40bf('0x20')](socket);function respondWithStatusCode(_0x36eddf,_0x49e389){_0x49e389=_0x49e389||0xcc;return function(_0x39a63b){if(_0x39a63b){return _0x36eddf[_0x40bf('0x21')](_0x49e389);}return _0x36eddf[_0x40bf('0x22')](_0x49e389)['end']();};}function respondWithResult(_0x20c02b,_0x345f87){_0x345f87=_0x345f87||0xc8;return function(_0x55a2aa){if(_0x55a2aa){return _0x20c02b[_0x40bf('0x22')](_0x345f87)[_0x40bf('0x23')](_0x55a2aa);}};}function respondWithFilteredResult(_0x31847b,_0x1613ec){return function(_0x4c21e8){if(_0x4c21e8){var _0x30670e=typeof _0x1613ec['offset']==='undefined'&&typeof _0x1613ec['limit']===_0x40bf('0x24');var _0x2963bd=_0x4c21e8[_0x40bf('0x25')];var _0x3daac4=_0x30670e?0x0:_0x1613ec['offset'];var _0x32ca12=_0x30670e?_0x4c21e8[_0x40bf('0x25')]:_0x1613ec['offset']+_0x1613ec[_0x40bf('0x26')];var _0x9577f9;if(_0x32ca12>=_0x2963bd){_0x32ca12=_0x2963bd;_0x9577f9=0xc8;}else{_0x9577f9=0xce;}_0x31847b['status'](_0x9577f9);return _0x31847b[_0x40bf('0x27')](_0x40bf('0x28'),_0x3daac4+'-'+_0x32ca12+'/'+_0x2963bd)['json'](_0x4c21e8);}return null;};}function patchUpdates(_0x41247d){return function(_0x260b61){try{jsonpatch[_0x40bf('0x29')](_0x260b61,_0x41247d,!![]);}catch(_0x2dc950){return BPromise[_0x40bf('0x2a')](_0x2dc950);}return _0x260b61[_0x40bf('0x2b')]();};}function saveUpdates(_0x584344,_0x1c1153){return function(_0x1c1952){if(_0x1c1952){return _0x1c1952[_0x40bf('0x2c')](_0x584344)[_0x40bf('0x2d')](function(_0x12ce2b){return _0x12ce2b;});}return null;};}function removeEntity(_0x20b072,_0xc1e702){return function(_0xe96f1f){if(_0xe96f1f){return _0xe96f1f[_0x40bf('0x2e')]()[_0x40bf('0x2d')](function(){var _0x6587cd=_0xe96f1f[_0x40bf('0x2f')]({'plain':!![]});var _0x3f2d1b=_0x40bf('0x30');return db[_0x40bf('0x31')][_0x40bf('0x2e')]({'where':{'type':_0x3f2d1b,'resourceId':_0x6587cd['id']}})[_0x40bf('0x2d')](function(){return _0xe96f1f;});})[_0x40bf('0x2d')](function(){_0x20b072[_0x40bf('0x22')](0xcc)[_0x40bf('0x32')]();});}};}function handleEntityNotFound(_0x31385b,_0x298d1f){return function(_0x275152){if(!_0x275152){_0x31385b['sendStatus'](0x194);}return _0x275152;};}function handleError(_0xa2bf44,_0x5ede97){_0x5ede97=_0x5ede97||0x1f4;return function(_0x5c4995){logger[_0x40bf('0x33')](_0x5c4995[_0x40bf('0x34')]);if(_0x5c4995[_0x40bf('0x35')]){delete _0x5c4995[_0x40bf('0x35')];}_0xa2bf44[_0x40bf('0x22')](_0x5ede97)[_0x40bf('0x36')](_0x5c4995);};}exports[_0x40bf('0x37')]=function(_0x595c30,_0xd30b88){var _0x3f2989={},_0x4e8d1f={},_0x15571c={'count':0x0,'rows':[]};var _0x4a1748=_[_0x40bf('0x38')](db[_0x40bf('0x39')]['rawAttributes'],function(_0x19b129){return{'name':_0x19b129[_0x40bf('0x3a')],'type':_0x19b129['type']['key']};});_0x4e8d1f[_0x40bf('0x3b')]=_[_0x40bf('0x38')](_0x4a1748,'name');_0x4e8d1f[_0x40bf('0x3c')]=_[_0x40bf('0x3d')](_0x595c30[_0x40bf('0x3c')]);_0x4e8d1f[_0x40bf('0x3e')]=_['intersection'](_0x4e8d1f['model'],_0x4e8d1f['query']);_0x3f2989[_0x40bf('0x3f')]=_[_0x40bf('0x40')](_0x4e8d1f[_0x40bf('0x3b')],qs[_0x40bf('0x41')](_0x595c30[_0x40bf('0x3c')]['fields']));_0x3f2989['attributes']=_0x3f2989[_0x40bf('0x3f')][_0x40bf('0x42')]?_0x3f2989[_0x40bf('0x3f')]:_0x4e8d1f[_0x40bf('0x3b')];if(!_0x595c30[_0x40bf('0x3c')]['hasOwnProperty']('nolimit')){_0x3f2989['limit']=qs[_0x40bf('0x26')](_0x595c30[_0x40bf('0x3c')][_0x40bf('0x26')]);_0x3f2989[_0x40bf('0x43')]=qs[_0x40bf('0x43')](_0x595c30['query'][_0x40bf('0x43')]);}_0x3f2989[_0x40bf('0x44')]=qs[_0x40bf('0x45')](_0x595c30['query'][_0x40bf('0x45')]);_0x3f2989['where']=qs[_0x40bf('0x3e')](_[_0x40bf('0x46')](_0x595c30['query'],_0x4e8d1f[_0x40bf('0x3e')]),_0x4a1748);if(_0x595c30[_0x40bf('0x3c')][_0x40bf('0x47')]){_0x3f2989[_0x40bf('0x48')]=_[_0x40bf('0x49')](_0x3f2989['where'],{'$or':_['map'](_0x4a1748,function(_0xa72176){if(_0xa72176[_0x40bf('0x4a')]!=='VIRTUAL'){var _0x932092={};_0x932092[_0xa72176['name']]={'$like':'%'+_0x595c30[_0x40bf('0x3c')][_0x40bf('0x47')]+'%'};return _0x932092;}})});}_0x3f2989=_[_0x40bf('0x49')]({},_0x3f2989,_0x595c30['options']);var _0x5d6aac={'where':_0x3f2989['where']};return db['Plugin'][_0x40bf('0x25')](_0x5d6aac)[_0x40bf('0x2d')](function(_0x368ebf){_0x15571c[_0x40bf('0x25')]=_0x368ebf;if(_0x595c30[_0x40bf('0x3c')]['includeAll']){_0x3f2989['include']=[{'all':!![]}];}return db[_0x40bf('0x39')][_0x40bf('0x4b')](_0x3f2989);})[_0x40bf('0x2d')](function(_0x371846){_0x15571c[_0x40bf('0x4c')]=_0x371846;return _0x15571c;})[_0x40bf('0x2d')](respondWithFilteredResult(_0xd30b88,_0x3f2989))[_0x40bf('0x4d')](handleError(_0xd30b88,null));};exports['show']=function(_0x24d2ba,_0x5e8354){var _0x5dd3e6={'raw':!![],'where':{'id':_0x24d2ba[_0x40bf('0x4e')]['id']}},_0x20fbe0={};_0x20fbe0['model']=_[_0x40bf('0x3d')](db[_0x40bf('0x39')]['rawAttributes']);_0x20fbe0[_0x40bf('0x3c')]=_['keys'](_0x24d2ba['query']);_0x20fbe0[_0x40bf('0x3e')]=_[_0x40bf('0x40')](_0x20fbe0[_0x40bf('0x3b')],_0x20fbe0[_0x40bf('0x3c')]);_0x5dd3e6[_0x40bf('0x3f')]=_['intersection'](_0x20fbe0[_0x40bf('0x3b')],qs[_0x40bf('0x41')](_0x24d2ba[_0x40bf('0x3c')][_0x40bf('0x41')]));_0x5dd3e6[_0x40bf('0x3f')]=_0x5dd3e6[_0x40bf('0x3f')][_0x40bf('0x42')]?_0x5dd3e6['attributes']:_0x20fbe0[_0x40bf('0x3b')];if(_0x24d2ba[_0x40bf('0x3c')][_0x40bf('0x4f')]){_0x5dd3e6[_0x40bf('0x50')]=[{'all':!![]}];}_0x5dd3e6=_[_0x40bf('0x49')]({},_0x5dd3e6,_0x24d2ba[_0x40bf('0x51')]);return db[_0x40bf('0x39')]['find'](_0x5dd3e6)[_0x40bf('0x2d')](handleEntityNotFound(_0x5e8354,null))['then'](respondWithResult(_0x5e8354,null))['catch'](handleError(_0x5e8354,null));};exports[_0x40bf('0x52')]=function(_0x40ab6d,_0x556016,_0x292cab){return db[_0x40bf('0x39')][_0x40bf('0x53')]({'where':{'id':_0x40ab6d[_0x40bf('0x4e')]['id']},'attributes':[_0x40bf('0x35'),_0x40bf('0x7')],'raw':!![]})[_0x40bf('0x2d')](handleEntityNotFound(_0x556016,null))[_0x40bf('0x2d')](function(_0x4323d7){if(_0x4323d7){var _0x25430e=path[_0x40bf('0x54')](config[_0x40bf('0x55')],'server/files/plugins',util[_0x40bf('0x56')](_0x40bf('0x57'),path[_0x40bf('0x58')](_0x4323d7['path'])));var _0x31863d=fs[_0x40bf('0x59')](_0x25430e);var _0x240edb=archiver(_0x40bf('0x5a'),{'zlib':{'level':0x9}});_0x240edb['on'](_0x40bf('0x5b'),function(_0x3439b9){if(_0x3439b9[_0x40bf('0x5c')]===_0x40bf('0x5d')){logger['error'](_0x3439b9);}else{throw new db['Sequelize']['Error'](_0x40bf('0x5e'));}});_0x240edb['on'](_0x40bf('0x33'),function(_0x20367b){throw new db[(_0x40bf('0x5f'))][(_0x40bf('0x60'))]('Error\x20while\x20generating\x20the\x20zip\x20archive');});_0x240edb[_0x40bf('0x61')](_0x31863d);_0x240edb[_0x40bf('0x62')](_0x40bf('0x63'),{'cwd':path[_0x40bf('0x54')](config['root'],_0x40bf('0x64'),path['dirname'](_0x4323d7[_0x40bf('0x7')])),'ignore':[path[_0x40bf('0x54')](path['basename'](_0x4323d7['path']),_0x40bf('0x65')),path[_0x40bf('0x54')](path[_0x40bf('0x58')](_0x4323d7[_0x40bf('0x7')]),_0x40bf('0x66'))],'nodir':!![]})[_0x40bf('0x67')]();_0x31863d['on']('close',function(){if(!fs['existsSync'](_0x25430e)){throw new db[(_0x40bf('0x5f'))]['Error'](_0x40bf('0x68'));}return _0x556016['download'](_0x25430e,util[_0x40bf('0x56')](_0x40bf('0x57'),path[_0x40bf('0x58')](_0x4323d7['path'])),function(_0x3cdd61){if(_0x3cdd61){throw new db[(_0x40bf('0x5f'))][(_0x40bf('0x60'))]('Error\x20while\x20downloading\x20the\x20plugin');}fs[_0x40bf('0x69')](_0x25430e);logger[_0x40bf('0x6a')](_0x40bf('0x6b'));logger['info'](_0x40bf('0x6c'));});});}return null;})[_0x40bf('0x4d')](handleError(_0x556016,null));};exports['upload']=function(_0xda55c7,_0x184fac,_0x261bc7){var _0x218e84=path[_0x40bf('0x58')](_0xda55c7[_0x40bf('0x6d')][_0x40bf('0x6e')],path['extname'](_0xda55c7[_0x40bf('0x6d')][_0x40bf('0x6e')]));var _0x1b798c=path[_0x40bf('0x54')](config[_0x40bf('0x55')],'server/files/plugins',_0xda55c7[_0x40bf('0x6d')][_0x40bf('0x6e')]);var _0x2c1700=path[_0x40bf('0x54')](config[_0x40bf('0x55')],_0x40bf('0x64'),_0x218e84);logger[_0x40bf('0x6a')](_0x40bf('0x6f'),_0x1b798c);logger[_0x40bf('0x6a')](_0x40bf('0x70'),_0x2c1700);try{fs[_0x40bf('0x71')](_0x1b798c,parseInt(_0x40bf('0x72'),0x8));extract(_0x1b798c,{'dir':_0x2c1700},function(_0x46ea9d){if(_0x46ea9d){logger[_0x40bf('0x33')](_0x46ea9d);handleError(_0x46ea9d,!![]);};var _0xfcb5c0=fs[_0x40bf('0x73')](_0x2c1700);if(_0xfcb5c0[_0x40bf('0x42')]>0x1){removeReferences(_0x1b798c,_0x2c1700);logger[_0x40bf('0x33')](_0x40bf('0x74'));return _0x261bc7(new Error(_0x40bf('0x74')));}var _0x3b9aa4=fs[_0x40bf('0x75')](path[_0x40bf('0x54')](_0x2c1700,_0xfcb5c0[0x0]));if(_0x3b9aa4[_0x40bf('0x76')]()){removeReferences(_0x1b798c,_0x2c1700);logger[_0x40bf('0x33')]('Invalid\x20plugin\x20structure');return _0x261bc7(new Error(_0x40bf('0x74')));}if(_0x3b9aa4[_0x40bf('0x77')]()){var _0x4b0959=fs[_0x40bf('0x73')](path[_0x40bf('0x54')](_0x2c1700,_0xfcb5c0[0x0]));if(_0x4b0959[_0x40bf('0x42')]===0x0){removeReferences(_0x1b798c,_0x2c1700);logger[_0x40bf('0x33')](_0x40bf('0x74'));return _0x261bc7(new Error('Invalid\x20plugin\x20structure'));}var _0x5b4c49=_0x4b0959['some'](function(_0x3a7eae){return _0x3a7eae===_0x40bf('0x78');});if(!_0x5b4c49){return _0x261bc7(new Error(_0x40bf('0x79')));}var _0x3fb473=fs[_0x40bf('0x7a')](path[_0x40bf('0x54')](_0x2c1700,_0xfcb5c0[0x0],_0x40bf('0x78')));var _0x4110f4=JSON[_0x40bf('0x7b')](_0x3fb473);if('undefined'==typeof _0x4110f4['name']){return _0x261bc7(new Error('Invalid\x20manifest:\x20name\x20missing'));}if(_0x40bf('0x24')==typeof _0x4110f4[_0x40bf('0x4a')]){return _0x261bc7(new Error(_0x40bf('0x7c')));}if('undefined'==typeof _0x4110f4[_0x40bf('0x7d')]){return _0x261bc7(new Error(_0x40bf('0x7e')));}if(_0x40bf('0x24')==typeof _0x4110f4[_0x40bf('0x7f')]){return _0x261bc7(new Error(_0x40bf('0x80')));}if(_0x4110f4[_0x40bf('0x4a')]===_0x40bf('0x81')){if(_0x40bf('0x24')==typeof _0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')]){return _0x261bc7(new Error(_0x40bf('0x83')));}if(_0x40bf('0x24')==typeof _0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')][_0x40bf('0x84')]&&_0x40bf('0x24')==typeof _0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')][_0x40bf('0x85')]){return _0x261bc7(new Error('Invalid\x20manifest:\x20no\x20view\x20specified'));}}else if(_0x4110f4[_0x40bf('0x4a')]==='script'){if(_0x40bf('0x24')==typeof _0x4110f4[_0x40bf('0x7f')]['scriptName']){return _0x261bc7(new Error(_0x40bf('0x86')));}if(_0x40bf('0x24')==typeof _0x4110f4['parameters'][_0x40bf('0x87')]){return _0x261bc7(new Error(_0x40bf('0x88')));}}else{return _0x261bc7(new Error(_0x40bf('0x89')));}var _0x1b466b=_0x4110f4[_0x40bf('0x35')];var _0x15c42f=_0x4110f4[_0x40bf('0x8a')];var _0x479a88=_0x4110f4[_0x40bf('0x4a')];var _0x378200=_0x4110f4['sidebar']?_0x4110f4[_0x40bf('0x8b')]:_0x40bf('0x8c');var _0xed06a4=_0x4110f4[_0x40bf('0x8d')]?_0x4110f4[_0x40bf('0x8d')]:_0x40bf('0x8e');var _0x2aafc4='';var _0x5df43c='';var _0x5e1f6c='';var _0x48185f='';var _0x2ed1a0=_0x4110f4[_0x40bf('0x7d')];if(_0x479a88===_0x40bf('0x81')){if('undefined'!=typeof _0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')][_0x40bf('0x84')]){var _0x25f6a4=_0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')][_0x40bf('0x84')];if(!/^(f|ht)tps?:\/\//i[_0x40bf('0x8f')](_0x25f6a4)){_0x2aafc4=path[_0x40bf('0x54')](_0x40bf('0x90'),_0x218e84,_0xfcb5c0[0x0],_0x25f6a4);}else{_0x2aafc4=_0x25f6a4;}}else{logger['info']('Warning:\x20admin\x20view\x20not\x20specified');}if('undefined'!=typeof _0x4110f4['parameters'][_0x40bf('0x82')][_0x40bf('0x85')]){var _0x2b302b=_0x4110f4['parameters'][_0x40bf('0x82')]['agent'];if(!/^(f|ht)tps?:\/\//i[_0x40bf('0x8f')](_0x2b302b)){_0x5df43c=path[_0x40bf('0x54')]('plugins',_0x218e84,_0xfcb5c0[0x0],_0x2b302b);}else{_0x5df43c=_0x2b302b;}}else{logger[_0x40bf('0x6a')](_0x40bf('0x91'));}}else if(_0x479a88===_0x40bf('0x92')){_0x5e1f6c=_0x4110f4[_0x40bf('0x7f')]['scriptName'];_0x48185f=_0x4110f4['parameters'][_0x40bf('0x87')];if('undefined'!=typeof _0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')]){if(_0x40bf('0x24')!=typeof _0x4110f4[_0x40bf('0x7f')]['views'][_0x40bf('0x84')]){var _0x25f6a4=_0x4110f4['parameters'][_0x40bf('0x82')][_0x40bf('0x84')];if(!/^(f|ht)tps?:\/\//i[_0x40bf('0x8f')](_0x25f6a4)){_0x2aafc4=path['join']('plugins',_0x218e84,_0xfcb5c0[0x0],_0x25f6a4);}else{_0x2aafc4=_0x25f6a4;}}if('undefined'!=typeof _0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')][_0x40bf('0x85')]){var _0x2b302b=_0x4110f4[_0x40bf('0x7f')][_0x40bf('0x82')]['agent'];if(!/^(f|ht)tps?:\/\//i['test'](_0x2b302b)){_0x5df43c=path[_0x40bf('0x54')](_0x40bf('0x90'),_0x218e84,_0xfcb5c0[0x0],_0x2b302b);}else{_0x5df43c=_0x2b302b;}}}}var _0x528411={'name':_0x1b466b,'description':_0x15c42f,'path':path['join'](_0x218e84,_0xfcb5c0[0x0]),'type':_0x479a88,'sidebar':_0x378200,'icon':_0xed06a4,'adminUrl':_0x2aafc4,'agentUrl':_0x5df43c,'scriptName':_0x5e1f6c,'scriptPath':_0x48185f,'version':_0x2ed1a0};removeReferences(_0x1b798c,null);return db[_0x40bf('0x39')]['create'](_0x528411)['then'](function(_0x278338){var _0x590ec6={'role':_0xda55c7[_0x40bf('0x93')][_0x40bf('0x94')],'userProfileId':_0xda55c7[_0x40bf('0x93')]['userProfileId']};if(!_0x590ec6)throw new Error(_0x40bf('0x95'));if(_0x590ec6[_0x40bf('0x94')]===_0x40bf('0x96')){var _0x5d8417=_0x278338['get']({'plain':!![]});return db[_0x40bf('0x97')][_0x40bf('0x53')]({'where':{'name':_0x40bf('0x30'),'userProfileId':_0x590ec6[_0x40bf('0x98')]},'raw':!![]})['then'](function(_0x56daf1){if(_0x56daf1&&_0x56daf1[_0x40bf('0x99')]===0x0){return db[_0x40bf('0x31')][_0x40bf('0x9a')]({'name':_0x5d8417['name'],'resourceId':_0x5d8417['id'],'type':_0x56daf1['name'],'sectionId':_0x56daf1['id']},{})[_0x40bf('0x2d')](function(){return _0x278338;});}else{return _0x278338;}})[_0x40bf('0x4d')](function(_0x1ff180){logger['error'](_0x40bf('0x9b'),_0x1ff180);throw _0x1ff180;});}return _0x278338;})[_0x40bf('0x2d')](respondWithResult(_0x184fac,null))['catch'](handleError(_0x184fac,null));}});}catch(_0x585f12){_0x184fac[_0x40bf('0x22')](0x1f4)[_0x40bf('0x36')](_0x585f12);}};function removeReferences(_0x2e592a,_0x4e61fb){try{if(_0x2e592a){fs[_0x40bf('0x69')](_0x2e592a);logger[_0x40bf('0x6a')](_0x40bf('0x9c'));}if(_0x4e61fb){rimraf(_0x4e61fb,function(){});logger['info'](_0x40bf('0x9d'));}}catch(_0x136287){logger[_0x40bf('0x33')](_0x136287);}}exports[_0x40bf('0x2c')]=function(_0x389ba7,_0x4d7668,_0x4a8f74){return db[_0x40bf('0x39')][_0x40bf('0x53')]({'where':{'id':_0x389ba7[_0x40bf('0x4e')]['id']}})[_0x40bf('0x2d')](handleEntityNotFound(_0x4d7668,null))['then'](saveUpdates(_0x389ba7[_0x40bf('0x93')],null))[_0x40bf('0x2d')](respondWithResult(_0x4d7668,null))[_0x40bf('0x4d')](handleError(_0x4d7668,null));};exports[_0x40bf('0x9e')]=function(_0x3001f1,_0x1d474c,_0x2fd5b5){return db[_0x40bf('0x39')]['find']({'where':{'id':_0x3001f1[_0x40bf('0x4e')]['id']}})[_0x40bf('0x2d')](function(_0x315254){if(_0x315254){return _0x315254[_0x40bf('0x2e')]();}})[_0x40bf('0x2d')](function(_0x58e0e8){rimraf(path['join'](config[_0x40bf('0x55')],_0x40bf('0x64'),path[_0x40bf('0x9f')](_0x58e0e8[_0x40bf('0x7')])),function(){});return _0x58e0e8;})['then'](handleEntityNotFound(_0x1d474c,null))[_0x40bf('0x2d')](respondWithStatusCode(_0x1d474c,null))['catch'](handleError(_0x1d474c,null));};exports[_0x40bf('0xa0')]=function(_0x4f6468,_0x2aa33c,_0x2d8ba4){return BPromise['resolve']()['then'](function(){if(!_0x4f6468[_0x40bf('0x3c')][_0x40bf('0x7')]){throw new Error(_0x40bf('0xa1'));}if(!_0x4f6468[_0x40bf('0x3c')]['port']){throw new Error(_0x40bf('0xa2'));}var _0xf4b79c=_0x4f6468[_0x40bf('0x3c')]['hostname']?_0x4f6468[_0x40bf('0x3c')][_0x40bf('0xa3')]+':'+_0x4f6468[_0x40bf('0x3c')][_0x40bf('0xa4')]:_0x40bf('0xa5')+_0x4f6468['query'][_0x40bf('0xa4')];var _0x4a5f3e=_0x4f6468[_0x40bf('0x3c')][_0x40bf('0x23')]?_0x4f6468[_0x40bf('0x3c')][_0x40bf('0x23')]:!![];var _0x3637e5=_0x4f6468[_0x40bf('0x3c')][_0x40bf('0xa6')]?_0x4f6468[_0x40bf('0x3c')]['encoding']:null;var _0x3b0075={'method':_0x4f6468[_0x40bf('0xa7')],'uri':util[_0x40bf('0x56')](_0x40bf('0xa8'),_0xf4b79c,_0x4f6468[_0x40bf('0x3c')][_0x40bf('0x7')]),'json':_0x4a5f3e,'resolveWithFullResponse':!![],'encoding':_0x3637e5,'qs':_0x4f6468[_0x40bf('0x3c')]};if(_0x4f6468['headers']){_0x3b0075['headers']=_0x4f6468[_0x40bf('0xa9')];if(_0x3b0075['headers']['content-length']){delete _0x3b0075[_0x40bf('0xa9')][_0x40bf('0xaa')];}}if(_0x3b0075[_0x40bf('0xa9')][_0x40bf('0xab')]){delete _0x3b0075['headers'][_0x40bf('0xab')];}return rp(_0x3b0075);})[_0x40bf('0x2d')](function(_0x33dd6c){if(_0x33dd6c){_0x2aa33c[_0x40bf('0x27')](_0x33dd6c[_0x40bf('0xa9')]);logger[_0x40bf('0x6a')](_0x33dd6c['body']);if(_[_0x40bf('0xac')](_0x4f6468['query'][_0x40bf('0x7')],_0x40bf('0x52'))&&_0x33dd6c['statusCode']==0x1f4){return _0x2aa33c[_0x40bf('0x21')](_0x33dd6c[_0x40bf('0xad')]);}else{return _0x2aa33c[_0x40bf('0x22')](_0x33dd6c[_0x40bf('0xad')])[_0x40bf('0x36')](_0x33dd6c[_0x40bf('0x93')]);}}else{return _0x2aa33c[_0x40bf('0x21')](0xc8);}})[_0x40bf('0x4d')](handleError(_0x2aa33c,null));};exports['webhookPost']=function(_0x340712,_0x86893,_0x3b624d){return BPromise[_0x40bf('0xae')]()['then'](function(){if(!_0x340712[_0x40bf('0x3c')]['path']){throw new Error('\x27PATH\x27\x20parameter\x20not\x20specified');}if(!_0x340712[_0x40bf('0x3c')][_0x40bf('0xa4')]){throw new Error(_0x40bf('0xa2'));}var _0x5f4ec3=_0x340712['query'][_0x40bf('0xa3')]?_0x340712[_0x40bf('0x3c')][_0x40bf('0xa3')]+':'+_0x340712[_0x40bf('0x3c')][_0x40bf('0xa4')]:'http://localhost:'+_0x340712['query']['port'];var _0xa49ab3=_0x340712[_0x40bf('0x3c')]['json']?_0x340712[_0x40bf('0x3c')][_0x40bf('0x23')]:!![];var _0x36275a=_0x340712[_0x40bf('0x3c')][_0x40bf('0xa6')]?_0x340712[_0x40bf('0x3c')][_0x40bf('0xa6')]:null;var _0x46d19c={'method':_0x340712['method'],'uri':util[_0x40bf('0x56')](_0x40bf('0xa8'),_0x5f4ec3,_0x340712[_0x40bf('0x3c')]['path']),'json':_0xa49ab3,'resolveWithFullResponse':!![],'encoding':_0x36275a,'body':_0x340712[_0x40bf('0x93')]};if(_0x340712['headers']){_0x46d19c['headers']=_0x340712[_0x40bf('0xa9')];if(_0x46d19c['headers'][_0x40bf('0xaa')]){delete _0x46d19c[_0x40bf('0xa9')][_0x40bf('0xaa')];}}if(_0x46d19c['headers']['content-type']){delete _0x46d19c[_0x40bf('0xa9')]['content-type'];}return rp(_0x46d19c);})[_0x40bf('0x2d')](function(_0x25d074){if(_0x25d074){_0x86893[_0x40bf('0x27')](_0x25d074[_0x40bf('0xa9')]);logger[_0x40bf('0x6a')](_0x25d074[_0x40bf('0x93')]);return _0x86893[_0x40bf('0x22')](_0x25d074[_0x40bf('0xad')])['send'](_0x25d074[_0x40bf('0x93')]);}else{return _0x86893[_0x40bf('0x21')](0xc8);}})['catch'](handleError(_0x86893,null));};