Built motion from commit ddca4d0d.|2.5.47
[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 _0xfaa4=['json','offset','limit','undefined','count','set','Content-Range','apply','reject','save','update','then','Variables','UserProfileResource','destroy','sendStatus','error','stack','name','send','index','map','Variable','fieldName','type','key','model','query','filters','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','options','includeAll','params','keys','rawAttributes','length','include','merge','find','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','catch','zip-dir','request-promise','moment','bluebird','mustache','util','path','sox','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status'];(function(_0x240d67,_0x5006ff){var _0x31b036=function(_0x19dce5){while(--_0x19dce5){_0x240d67['push'](_0x240d67['shift']());}};_0x31b036(++_0x5006ff);}(_0xfaa4,0x8b));var _0x4faa=function(_0x5720e2,_0xd9e048){_0x5720e2=_0x5720e2-0x0;var _0x228e07=_0xfaa4[_0x5720e2];return _0x228e07;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x4faa('0x0'));var jsonpatch=require('fast-json-patch');var rp=require(_0x4faa('0x1'));var moment=require(_0x4faa('0x2'));var BPromise=require(_0x4faa('0x3'));var Mustache=require(_0x4faa('0x4'));var util=require(_0x4faa('0x5'));var path=require(_0x4faa('0x6'));var sox=require(_0x4faa('0x7'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x4faa('0x8'));var _=require(_0x4faa('0x9'));var squel=require(_0x4faa('0xa'));var crypto=require(_0x4faa('0xb'));var jsforce=require(_0x4faa('0xc'));var deskjs=require(_0x4faa('0xd'));var toCsv=require(_0x4faa('0xe'));var querystring=require(_0x4faa('0xf'));var Papa=require(_0x4faa('0x10'));var Redis=require(_0x4faa('0x11'));var authService=require(_0x4faa('0x12'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x4faa('0x13'));var logger=require('../../config/logger')('api');var utils=require(_0x4faa('0x14'));var config=require(_0x4faa('0x15'));var licenseUtil=require(_0x4faa('0x16'));var db=require(_0x4faa('0x17'))['db'];function respondWithStatusCode(_0x36aa28,_0x1e2613){_0x1e2613=_0x1e2613||0xcc;return function(_0x20accb){if(_0x20accb){return _0x36aa28['sendStatus'](_0x1e2613);}return _0x36aa28['status'](_0x1e2613)['end']();};}function respondWithResult(_0xa3b90,_0x371016){_0x371016=_0x371016||0xc8;return function(_0x5e7d75){if(_0x5e7d75){return _0xa3b90[_0x4faa('0x18')](_0x371016)[_0x4faa('0x19')](_0x5e7d75);}};}function respondWithFilteredResult(_0xf11ace,_0x32ac05){return function(_0x3d9de3){if(_0x3d9de3){var _0x151feb=typeof _0x32ac05[_0x4faa('0x1a')]==='undefined'&&typeof _0x32ac05[_0x4faa('0x1b')]===_0x4faa('0x1c');var _0x40f907=_0x3d9de3['count'];var _0x2ac070=_0x151feb?0x0:_0x32ac05[_0x4faa('0x1a')];var _0x33878b=_0x151feb?_0x3d9de3[_0x4faa('0x1d')]:_0x32ac05[_0x4faa('0x1a')]+_0x32ac05[_0x4faa('0x1b')];var _0x225aef;if(_0x33878b>=_0x40f907){_0x33878b=_0x40f907;_0x225aef=0xc8;}else{_0x225aef=0xce;}_0xf11ace[_0x4faa('0x18')](_0x225aef);return _0xf11ace[_0x4faa('0x1e')](_0x4faa('0x1f'),_0x2ac070+'-'+_0x33878b+'/'+_0x40f907)['json'](_0x3d9de3);}return null;};}function patchUpdates(_0x1950c2){return function(_0x43936b){try{jsonpatch[_0x4faa('0x20')](_0x43936b,_0x1950c2,!![]);}catch(_0x204ba8){return BPromise[_0x4faa('0x21')](_0x204ba8);}return _0x43936b[_0x4faa('0x22')]();};}function saveUpdates(_0x42cc93,_0x41f0e7){return function(_0x15e501){if(_0x15e501){return _0x15e501[_0x4faa('0x23')](_0x42cc93)[_0x4faa('0x24')](function(_0x3c5be5){return _0x3c5be5;});}return null;};}function removeEntity(_0x2b5fe7,_0x3d9698){return function(_0x480b78){if(_0x480b78){return _0x480b78['destroy']()[_0x4faa('0x24')](function(){var _0x57199e=_0x480b78['get']({'plain':!![]});var _0x3424c8=_0x4faa('0x25');return db[_0x4faa('0x26')][_0x4faa('0x27')]({'where':{'type':_0x3424c8,'resourceId':_0x57199e['id']}})[_0x4faa('0x24')](function(){return _0x480b78;});})[_0x4faa('0x24')](function(){_0x2b5fe7[_0x4faa('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x1bebd1,_0x2bc31a){return function(_0x19fc76){if(!_0x19fc76){_0x1bebd1[_0x4faa('0x28')](0x194);}return _0x19fc76;};}function handleError(_0x10ed0f,_0xfd34da){_0xfd34da=_0xfd34da||0x1f4;return function(_0x22e0a2){logger[_0x4faa('0x29')](_0x22e0a2[_0x4faa('0x2a')]);if(_0x22e0a2[_0x4faa('0x2b')]){delete _0x22e0a2[_0x4faa('0x2b')];}_0x10ed0f['status'](_0xfd34da)[_0x4faa('0x2c')](_0x22e0a2);};}exports[_0x4faa('0x2d')]=function(_0x2ff9a2,_0x1ff431){var _0x348b3e={},_0x28246f={},_0x2035a8={'count':0x0,'rows':[]};var _0x5e221f=_[_0x4faa('0x2e')](db[_0x4faa('0x2f')]['rawAttributes'],function(_0x4afeaf){return{'name':_0x4afeaf[_0x4faa('0x30')],'type':_0x4afeaf[_0x4faa('0x31')][_0x4faa('0x32')]};});_0x28246f[_0x4faa('0x33')]=_[_0x4faa('0x2e')](_0x5e221f,_0x4faa('0x2b'));_0x28246f[_0x4faa('0x34')]=_['keys'](_0x2ff9a2[_0x4faa('0x34')]);_0x28246f[_0x4faa('0x35')]=_[_0x4faa('0x36')](_0x28246f[_0x4faa('0x33')],_0x28246f[_0x4faa('0x34')]);_0x348b3e[_0x4faa('0x37')]=_['intersection'](_0x28246f['model'],qs[_0x4faa('0x38')](_0x2ff9a2[_0x4faa('0x34')][_0x4faa('0x38')]));_0x348b3e['attributes']=_0x348b3e[_0x4faa('0x37')]['length']?_0x348b3e['attributes']:_0x28246f[_0x4faa('0x33')];if(!_0x2ff9a2['query'][_0x4faa('0x39')](_0x4faa('0x3a'))){_0x348b3e[_0x4faa('0x1b')]=qs[_0x4faa('0x1b')](_0x2ff9a2[_0x4faa('0x34')]['limit']);_0x348b3e[_0x4faa('0x1a')]=qs[_0x4faa('0x1a')](_0x2ff9a2[_0x4faa('0x34')][_0x4faa('0x1a')]);}_0x348b3e[_0x4faa('0x3b')]=qs[_0x4faa('0x3c')](_0x2ff9a2[_0x4faa('0x34')][_0x4faa('0x3c')]);_0x348b3e[_0x4faa('0x3d')]=qs[_0x4faa('0x35')](_[_0x4faa('0x3e')](_0x2ff9a2[_0x4faa('0x34')],_0x28246f['filters']),_0x5e221f);if(_0x2ff9a2[_0x4faa('0x34')][_0x4faa('0x3f')]){_0x348b3e['where']=_['merge'](_0x348b3e[_0x4faa('0x3d')],{'$or':_['map'](_0x5e221f,function(_0x847218){if(_0x847218[_0x4faa('0x31')]!=='VIRTUAL'){var _0x21b192={};_0x21b192[_0x847218[_0x4faa('0x2b')]]={'$like':'%'+_0x2ff9a2[_0x4faa('0x34')][_0x4faa('0x3f')]+'%'};return _0x21b192;}})});}_0x348b3e=_['merge']({},_0x348b3e,_0x2ff9a2[_0x4faa('0x40')]);var _0x4f54c1={'where':_0x348b3e[_0x4faa('0x3d')]};return db['Variable']['count'](_0x4f54c1)['then'](function(_0x4eca78){_0x2035a8[_0x4faa('0x1d')]=_0x4eca78;if(_0x2ff9a2[_0x4faa('0x34')][_0x4faa('0x41')]){_0x348b3e['include']=[{'all':!![]}];}return db[_0x4faa('0x2f')]['findAll'](_0x348b3e);})[_0x4faa('0x24')](function(_0x3a16af){_0x2035a8['rows']=_0x3a16af;return _0x2035a8;})[_0x4faa('0x24')](respondWithFilteredResult(_0x1ff431,_0x348b3e))['catch'](handleError(_0x1ff431,null));};exports['show']=function(_0x1d6c00,_0x36bff2){var _0x25e9ce={'raw':!![],'where':{'id':_0x1d6c00[_0x4faa('0x42')]['id']}},_0x116124={};_0x116124[_0x4faa('0x33')]=_[_0x4faa('0x43')](db['Variable'][_0x4faa('0x44')]);_0x116124[_0x4faa('0x34')]=_[_0x4faa('0x43')](_0x1d6c00[_0x4faa('0x34')]);_0x116124[_0x4faa('0x35')]=_['intersection'](_0x116124[_0x4faa('0x33')],_0x116124['query']);_0x25e9ce[_0x4faa('0x37')]=_[_0x4faa('0x36')](_0x116124[_0x4faa('0x33')],qs[_0x4faa('0x38')](_0x1d6c00['query'][_0x4faa('0x38')]));_0x25e9ce[_0x4faa('0x37')]=_0x25e9ce[_0x4faa('0x37')][_0x4faa('0x45')]?_0x25e9ce['attributes']:_0x116124['model'];if(_0x1d6c00[_0x4faa('0x34')][_0x4faa('0x41')]){_0x25e9ce[_0x4faa('0x46')]=[{'all':!![]}];}_0x25e9ce=_[_0x4faa('0x47')]({},_0x25e9ce,_0x1d6c00['options']);return db['Variable'][_0x4faa('0x48')](_0x25e9ce)[_0x4faa('0x24')](handleEntityNotFound(_0x36bff2,null))[_0x4faa('0x24')](respondWithResult(_0x36bff2,null))['catch'](handleError(_0x36bff2,null));};exports[_0x4faa('0x49')]=function(_0x19eb51,_0x12de50){return db[_0x4faa('0x2f')][_0x4faa('0x49')](_0x19eb51[_0x4faa('0x4a')],{})[_0x4faa('0x24')](function(_0x27eafc){var _0x18ef68=_0x19eb51[_0x4faa('0x4b')][_0x4faa('0x4c')]({'plain':!![]});if(!_0x18ef68)throw new Error(_0x4faa('0x4d'));if(_0x18ef68[_0x4faa('0x4e')]===_0x4faa('0x4b')){var _0x13e522=_0x27eafc[_0x4faa('0x4c')]({'plain':!![]});var _0x2f35c3=_0x4faa('0x25');return db[_0x4faa('0x4f')][_0x4faa('0x48')]({'where':{'name':_0x2f35c3,'userProfileId':_0x18ef68['userProfileId']},'raw':!![]})['then'](function(_0x3dbe63){if(_0x3dbe63&&_0x3dbe63[_0x4faa('0x50')]===0x0){return db['UserProfileResource']['create']({'name':_0x13e522[_0x4faa('0x2b')],'resourceId':_0x13e522['id'],'type':_0x3dbe63['name'],'sectionId':_0x3dbe63['id']},{})['then'](function(){return _0x27eafc;});}else{return _0x27eafc;}})[_0x4faa('0x51')](function(_0x32cf4){logger[_0x4faa('0x29')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x32cf4);throw _0x32cf4;});}return _0x27eafc;})[_0x4faa('0x24')](respondWithResult(_0x12de50,0xc9))[_0x4faa('0x51')](handleError(_0x12de50,null));};exports[_0x4faa('0x23')]=function(_0x1da4a2,_0x570e98){if(_0x1da4a2[_0x4faa('0x4a')]['id']){delete _0x1da4a2[_0x4faa('0x4a')]['id'];}return db[_0x4faa('0x2f')][_0x4faa('0x48')]({'where':{'id':_0x1da4a2[_0x4faa('0x42')]['id']}})[_0x4faa('0x24')](handleEntityNotFound(_0x570e98,null))[_0x4faa('0x24')](saveUpdates(_0x1da4a2[_0x4faa('0x4a')],null))[_0x4faa('0x24')](respondWithResult(_0x570e98,null))[_0x4faa('0x51')](handleError(_0x570e98,null));};exports['destroy']=function(_0x46e490,_0x214699){return db[_0x4faa('0x2f')][_0x4faa('0x48')]({'where':{'id':_0x46e490['params']['id']}})[_0x4faa('0x24')](handleEntityNotFound(_0x214699,null))['then'](removeEntity(_0x214699,null))['catch'](handleError(_0x214699,null));};