8422f209a2002bfc6d23c68fd74ecf9ed501679a
[motion2.git] / server / api / variable / variable.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 _0x5f63=['error','name','send','index','map','Variable','rawAttributes','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','type','VIRTUAL','includeAll','include','findAll','rows','catch','params','options','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','update','find','eml-format','rimraf','moment','util','path','sox','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','json','limit','count','offset','set','Content-Range','destroy','get','Variables','then'];(function(_0x465a0c,_0x19d4d5){var _0x1e202e=function(_0x27bd8a){while(--_0x27bd8a){_0x465a0c['push'](_0x465a0c['shift']());}};_0x1e202e(++_0x19d4d5);}(_0x5f63,0x7a));var _0x35f6=function(_0x1cf06d,_0x4bed75){_0x1cf06d=_0x1cf06d-0x0;var _0x139682=_0x5f63[_0x1cf06d];return _0x139682;};'use strict';var emlformat=require(_0x35f6('0x0'));var rimraf=require(_0x35f6('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x35f6('0x2'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x35f6('0x3'));var path=require(_0x35f6('0x4'));var sox=require(_0x35f6('0x5'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x35f6('0x6'));var _=require(_0x35f6('0x7'));var squel=require(_0x35f6('0x8'));var crypto=require(_0x35f6('0x9'));var jsforce=require(_0x35f6('0xa'));var deskjs=require(_0x35f6('0xb'));var toCsv=require('to-csv');var querystring=require(_0x35f6('0xc'));var Papa=require('papaparse');var Redis=require(_0x35f6('0xd'));var authService=require(_0x35f6('0xe'));var qs=require(_0x35f6('0xf'));var as=require(_0x35f6('0x10'));var hardwareService=require(_0x35f6('0x11'));var logger=require(_0x35f6('0x12'))(_0x35f6('0x13'));var utils=require(_0x35f6('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0x35f6('0x15'));var db=require(_0x35f6('0x16'))['db'];function respondWithStatusCode(_0x5b5350,_0x285de2){_0x285de2=_0x285de2||0xcc;return function(_0x2bbe05){if(_0x2bbe05){return _0x5b5350[_0x35f6('0x17')](_0x285de2);}return _0x5b5350[_0x35f6('0x18')](_0x285de2)[_0x35f6('0x19')]();};}function respondWithResult(_0x393aca,_0x14780d){_0x14780d=_0x14780d||0xc8;return function(_0x59ca92){if(_0x59ca92){return _0x393aca[_0x35f6('0x18')](_0x14780d)[_0x35f6('0x1a')](_0x59ca92);}};}function respondWithFilteredResult(_0x240940,_0x40fd16){return function(_0x53843d){if(_0x53843d){var _0x1b3a7b=typeof _0x40fd16['offset']==='undefined'&&typeof _0x40fd16[_0x35f6('0x1b')]==='undefined';var _0x36a17e=_0x53843d[_0x35f6('0x1c')];var _0x21979d=_0x1b3a7b?0x0:_0x40fd16[_0x35f6('0x1d')];var _0x4a4dc2=_0x1b3a7b?_0x53843d[_0x35f6('0x1c')]:_0x40fd16[_0x35f6('0x1d')]+_0x40fd16[_0x35f6('0x1b')];var _0x370880;if(_0x4a4dc2>=_0x36a17e){_0x4a4dc2=_0x36a17e;_0x370880=0xc8;}else{_0x370880=0xce;}_0x240940['status'](_0x370880);return _0x240940[_0x35f6('0x1e')](_0x35f6('0x1f'),_0x21979d+'-'+_0x4a4dc2+'/'+_0x36a17e)[_0x35f6('0x1a')](_0x53843d);}return null;};}function patchUpdates(_0x3a6f58){return function(_0x2e25d1){try{jsonpatch['apply'](_0x2e25d1,_0x3a6f58,!![]);}catch(_0x5baede){return BPromise['reject'](_0x5baede);}return _0x2e25d1['save']();};}function saveUpdates(_0x39c53a,_0x2e9f29){return function(_0x2a64b6){if(_0x2a64b6){return _0x2a64b6['update'](_0x39c53a)['then'](function(_0x361dd6){return _0x361dd6;});}return null;};}function removeEntity(_0x4622ed,_0x1aef11){return function(_0x4d6d73){if(_0x4d6d73){return _0x4d6d73[_0x35f6('0x20')]()['then'](function(){var _0x41c524=_0x4d6d73[_0x35f6('0x21')]({'plain':!![]});var _0x499cb7=_0x35f6('0x22');return db['UserProfileResource']['destroy']({'where':{'type':_0x499cb7,'resourceId':_0x41c524['id']}})[_0x35f6('0x23')](function(){return _0x4d6d73;});})[_0x35f6('0x23')](function(){_0x4622ed[_0x35f6('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x2b7ae3,_0x3b93f2){return function(_0x29aec6){if(!_0x29aec6){_0x2b7ae3[_0x35f6('0x17')](0x194);}return _0x29aec6;};}function handleError(_0x46a784,_0x1cf900){_0x1cf900=_0x1cf900||0x1f4;return function(_0x3953bf){logger[_0x35f6('0x24')](_0x3953bf['stack']);if(_0x3953bf['name']){delete _0x3953bf[_0x35f6('0x25')];}_0x46a784[_0x35f6('0x18')](_0x1cf900)[_0x35f6('0x26')](_0x3953bf);};}exports[_0x35f6('0x27')]=function(_0x5a6a0b,_0x4b8dc7){var _0x3669e2={},_0x17088a={},_0x2f1e41={'count':0x0,'rows':[]};var _0x5cbf41=_[_0x35f6('0x28')](db[_0x35f6('0x29')][_0x35f6('0x2a')],function(_0x5e9d50){return{'name':_0x5e9d50['fieldName'],'type':_0x5e9d50['type'][_0x35f6('0x2b')]};});_0x17088a[_0x35f6('0x2c')]=_[_0x35f6('0x28')](_0x5cbf41,_0x35f6('0x25'));_0x17088a[_0x35f6('0x2d')]=_[_0x35f6('0x2e')](_0x5a6a0b[_0x35f6('0x2d')]);_0x17088a[_0x35f6('0x2f')]=_[_0x35f6('0x30')](_0x17088a[_0x35f6('0x2c')],_0x17088a['query']);_0x3669e2[_0x35f6('0x31')]=_[_0x35f6('0x30')](_0x17088a['model'],qs[_0x35f6('0x32')](_0x5a6a0b[_0x35f6('0x2d')][_0x35f6('0x32')]));_0x3669e2['attributes']=_0x3669e2[_0x35f6('0x31')][_0x35f6('0x33')]?_0x3669e2[_0x35f6('0x31')]:_0x17088a[_0x35f6('0x2c')];if(!_0x5a6a0b[_0x35f6('0x2d')][_0x35f6('0x34')](_0x35f6('0x35'))){_0x3669e2[_0x35f6('0x1b')]=qs[_0x35f6('0x1b')](_0x5a6a0b['query'][_0x35f6('0x1b')]);_0x3669e2[_0x35f6('0x1d')]=qs['offset'](_0x5a6a0b['query'][_0x35f6('0x1d')]);}_0x3669e2[_0x35f6('0x36')]=qs[_0x35f6('0x37')](_0x5a6a0b['query'][_0x35f6('0x37')]);_0x3669e2[_0x35f6('0x38')]=qs[_0x35f6('0x2f')](_[_0x35f6('0x39')](_0x5a6a0b[_0x35f6('0x2d')],_0x17088a['filters']),_0x5cbf41);if(_0x5a6a0b[_0x35f6('0x2d')][_0x35f6('0x3a')]){_0x3669e2[_0x35f6('0x38')]=_[_0x35f6('0x3b')](_0x3669e2['where'],{'$or':_[_0x35f6('0x28')](_0x5cbf41,function(_0x5021f8){if(_0x5021f8[_0x35f6('0x3c')]!==_0x35f6('0x3d')){var _0x3cf150={};_0x3cf150[_0x5021f8[_0x35f6('0x25')]]={'$like':'%'+_0x5a6a0b[_0x35f6('0x2d')]['filter']+'%'};return _0x3cf150;}})});}_0x3669e2=_[_0x35f6('0x3b')]({},_0x3669e2,_0x5a6a0b['options']);var _0x2fdfb8={'where':_0x3669e2['where']};return db[_0x35f6('0x29')][_0x35f6('0x1c')](_0x2fdfb8)[_0x35f6('0x23')](function(_0x3b1372){_0x2f1e41[_0x35f6('0x1c')]=_0x3b1372;if(_0x5a6a0b[_0x35f6('0x2d')][_0x35f6('0x3e')]){_0x3669e2[_0x35f6('0x3f')]=[{'all':!![]}];}return db[_0x35f6('0x29')][_0x35f6('0x40')](_0x3669e2);})[_0x35f6('0x23')](function(_0x5f053a){_0x2f1e41[_0x35f6('0x41')]=_0x5f053a;return _0x2f1e41;})['then'](respondWithFilteredResult(_0x4b8dc7,_0x3669e2))[_0x35f6('0x42')](handleError(_0x4b8dc7,null));};exports['show']=function(_0x1ad920,_0x5081d0){var _0x2ae91f={'raw':!![],'where':{'id':_0x1ad920[_0x35f6('0x43')]['id']}},_0x596a4c={};_0x596a4c[_0x35f6('0x2c')]=_['keys'](db[_0x35f6('0x29')][_0x35f6('0x2a')]);_0x596a4c[_0x35f6('0x2d')]=_['keys'](_0x1ad920['query']);_0x596a4c[_0x35f6('0x2f')]=_[_0x35f6('0x30')](_0x596a4c[_0x35f6('0x2c')],_0x596a4c[_0x35f6('0x2d')]);_0x2ae91f[_0x35f6('0x31')]=_[_0x35f6('0x30')](_0x596a4c[_0x35f6('0x2c')],qs[_0x35f6('0x32')](_0x1ad920[_0x35f6('0x2d')][_0x35f6('0x32')]));_0x2ae91f[_0x35f6('0x31')]=_0x2ae91f[_0x35f6('0x31')][_0x35f6('0x33')]?_0x2ae91f[_0x35f6('0x31')]:_0x596a4c[_0x35f6('0x2c')];if(_0x1ad920[_0x35f6('0x2d')][_0x35f6('0x3e')]){_0x2ae91f['include']=[{'all':!![]}];}_0x2ae91f=_['merge']({},_0x2ae91f,_0x1ad920[_0x35f6('0x44')]);return db[_0x35f6('0x29')]['find'](_0x2ae91f)[_0x35f6('0x23')](handleEntityNotFound(_0x5081d0,null))['then'](respondWithResult(_0x5081d0,null))[_0x35f6('0x42')](handleError(_0x5081d0,null));};exports['create']=function(_0x14bda0,_0x1b5b84){return db[_0x35f6('0x29')][_0x35f6('0x45')](_0x14bda0[_0x35f6('0x46')],{})[_0x35f6('0x23')](function(_0x371972){var _0x39c8b5=_0x14bda0[_0x35f6('0x47')]['get']({'plain':!![]});if(!_0x39c8b5)throw new Error(_0x35f6('0x48'));if(_0x39c8b5[_0x35f6('0x49')]===_0x35f6('0x47')){var _0x52f002=_0x371972['get']({'plain':!![]});var _0x33614e=_0x35f6('0x22');return db[_0x35f6('0x4a')]['find']({'where':{'name':_0x33614e,'userProfileId':_0x39c8b5[_0x35f6('0x4b')]},'raw':!![]})[_0x35f6('0x23')](function(_0x2e1dbf){if(_0x2e1dbf&&_0x2e1dbf[_0x35f6('0x4c')]===0x0){return db['UserProfileResource'][_0x35f6('0x45')]({'name':_0x52f002[_0x35f6('0x25')],'resourceId':_0x52f002['id'],'type':_0x2e1dbf[_0x35f6('0x25')],'sectionId':_0x2e1dbf['id']},{})['then'](function(){return _0x371972;});}else{return _0x371972;}})[_0x35f6('0x42')](function(_0x5b5d97){logger[_0x35f6('0x24')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x5b5d97);throw _0x5b5d97;});}return _0x371972;})[_0x35f6('0x23')](respondWithResult(_0x1b5b84,0xc9))['catch'](handleError(_0x1b5b84,null));};exports[_0x35f6('0x4d')]=function(_0x3229d3,_0x3c0fce){if(_0x3229d3[_0x35f6('0x46')]['id']){delete _0x3229d3[_0x35f6('0x46')]['id'];}return db[_0x35f6('0x29')][_0x35f6('0x4e')]({'where':{'id':_0x3229d3[_0x35f6('0x43')]['id']}})[_0x35f6('0x23')](handleEntityNotFound(_0x3c0fce,null))[_0x35f6('0x23')](saveUpdates(_0x3229d3[_0x35f6('0x46')],null))[_0x35f6('0x23')](respondWithResult(_0x3c0fce,null))[_0x35f6('0x42')](handleError(_0x3c0fce,null));};exports['destroy']=function(_0x173b5a,_0x2bb071){return db['Variable'][_0x35f6('0x4e')]({'where':{'id':_0x173b5a[_0x35f6('0x43')]['id']}})['then'](handleEntityNotFound(_0x2bb071,null))[_0x35f6('0x23')](removeEntity(_0x2bb071,null))[_0x35f6('0x42')](handleError(_0x2bb071,null));};