Built motion from commit b5413ace.|2.6.9
[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 _0x5f5f=['index','map','Variable','rawAttributes','fieldName','type','model','query','fields','attributes','hasOwnProperty','nolimit','sort','where','filters','merge','VIRTUAL','filter','options','includeAll','include','findAll','rows','catch','show','params','keys','intersection','length','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','find','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','eml-format','rimraf','fast-json-patch','moment','bluebird','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range','apply','save','update','destroy','then','get','Variables','UserProfileResource','stack','name','send'];(function(_0x5c67c6,_0x355392){var _0x5208b6=function(_0x26a747){while(--_0x26a747){_0x5c67c6['push'](_0x5c67c6['shift']());}};_0x5208b6(++_0x355392);}(_0x5f5f,0x124));var _0xf5f5=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x5f5f[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0xf5f5('0x0'));var rimraf=require(_0xf5f5('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xf5f5('0x2'));var rp=require('request-promise');var moment=require(_0xf5f5('0x3'));var BPromise=require(_0xf5f5('0x4'));var Mustache=require('mustache');var util=require(_0xf5f5('0x5'));var path=require('path');var sox=require(_0xf5f5('0x6'));var csv=require(_0xf5f5('0x7'));var ejs=require(_0xf5f5('0x8'));var fs=require('fs');var fs_extra=require(_0xf5f5('0x9'));var _=require(_0xf5f5('0xa'));var squel=require(_0xf5f5('0xb'));var crypto=require(_0xf5f5('0xc'));var jsforce=require('jsforce');var deskjs=require(_0xf5f5('0xd'));var toCsv=require(_0xf5f5('0x7'));var querystring=require(_0xf5f5('0xe'));var Papa=require('papaparse');var Redis=require(_0xf5f5('0xf'));var authService=require('../../components/auth/service');var qs=require(_0xf5f5('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf5f5('0x11'));var logger=require(_0xf5f5('0x12'))(_0xf5f5('0x13'));var utils=require('../../config/utils');var config=require(_0xf5f5('0x14'));var licenseUtil=require(_0xf5f5('0x15'));var db=require(_0xf5f5('0x16'))['db'];function respondWithStatusCode(_0x32f867,_0x4f106a){_0x4f106a=_0x4f106a||0xcc;return function(_0x1b31a1){if(_0x1b31a1){return _0x32f867[_0xf5f5('0x17')](_0x4f106a);}return _0x32f867[_0xf5f5('0x18')](_0x4f106a)[_0xf5f5('0x19')]();};}function respondWithResult(_0x28dd3f,_0x40920e){_0x40920e=_0x40920e||0xc8;return function(_0x5cddc3){if(_0x5cddc3){return _0x28dd3f[_0xf5f5('0x18')](_0x40920e)[_0xf5f5('0x1a')](_0x5cddc3);}};}function respondWithFilteredResult(_0x12a413,_0xaf47f7){return function(_0x2ea671){if(_0x2ea671){var _0x3a92e9=typeof _0xaf47f7[_0xf5f5('0x1b')]==='undefined'&&typeof _0xaf47f7[_0xf5f5('0x1c')]===_0xf5f5('0x1d');var _0x4dad88=_0x2ea671[_0xf5f5('0x1e')];var _0x535dbd=_0x3a92e9?0x0:_0xaf47f7['offset'];var _0x263194=_0x3a92e9?_0x2ea671[_0xf5f5('0x1e')]:_0xaf47f7[_0xf5f5('0x1b')]+_0xaf47f7[_0xf5f5('0x1c')];var _0x46955b;if(_0x263194>=_0x4dad88){_0x263194=_0x4dad88;_0x46955b=0xc8;}else{_0x46955b=0xce;}_0x12a413['status'](_0x46955b);return _0x12a413[_0xf5f5('0x1f')](_0xf5f5('0x20'),_0x535dbd+'-'+_0x263194+'/'+_0x4dad88)[_0xf5f5('0x1a')](_0x2ea671);}return null;};}function patchUpdates(_0x4ff563){return function(_0x56b811){try{jsonpatch[_0xf5f5('0x21')](_0x56b811,_0x4ff563,!![]);}catch(_0xcd8c6d){return BPromise['reject'](_0xcd8c6d);}return _0x56b811[_0xf5f5('0x22')]();};}function saveUpdates(_0x3ef36b,_0x1138ad){return function(_0x4abab6){if(_0x4abab6){return _0x4abab6[_0xf5f5('0x23')](_0x3ef36b)['then'](function(_0x2e2b66){return _0x2e2b66;});}return null;};}function removeEntity(_0x3ae4b6,_0x17a94c){return function(_0x20f9a1){if(_0x20f9a1){return _0x20f9a1[_0xf5f5('0x24')]()[_0xf5f5('0x25')](function(){var _0x1a32b7=_0x20f9a1[_0xf5f5('0x26')]({'plain':!![]});var _0xd53e79=_0xf5f5('0x27');return db[_0xf5f5('0x28')][_0xf5f5('0x24')]({'where':{'type':_0xd53e79,'resourceId':_0x1a32b7['id']}})[_0xf5f5('0x25')](function(){return _0x20f9a1;});})[_0xf5f5('0x25')](function(){_0x3ae4b6[_0xf5f5('0x18')](0xcc)[_0xf5f5('0x19')]();});}};}function handleEntityNotFound(_0x510ff3,_0x169311){return function(_0x171650){if(!_0x171650){_0x510ff3[_0xf5f5('0x17')](0x194);}return _0x171650;};}function handleError(_0xb502b1,_0x370994){_0x370994=_0x370994||0x1f4;return function(_0x5432f4){logger['error'](_0x5432f4[_0xf5f5('0x29')]);if(_0x5432f4[_0xf5f5('0x2a')]){delete _0x5432f4[_0xf5f5('0x2a')];}_0xb502b1['status'](_0x370994)[_0xf5f5('0x2b')](_0x5432f4);};}exports[_0xf5f5('0x2c')]=function(_0x167929,_0xcbc33d){var _0x4f049b={},_0x1d9a10={},_0x424c3a={'count':0x0,'rows':[]};var _0x8936e3=_[_0xf5f5('0x2d')](db[_0xf5f5('0x2e')][_0xf5f5('0x2f')],function(_0x4e405d){return{'name':_0x4e405d[_0xf5f5('0x30')],'type':_0x4e405d[_0xf5f5('0x31')]['key']};});_0x1d9a10[_0xf5f5('0x32')]=_[_0xf5f5('0x2d')](_0x8936e3,_0xf5f5('0x2a'));_0x1d9a10[_0xf5f5('0x33')]=_['keys'](_0x167929[_0xf5f5('0x33')]);_0x1d9a10['filters']=_['intersection'](_0x1d9a10[_0xf5f5('0x32')],_0x1d9a10[_0xf5f5('0x33')]);_0x4f049b['attributes']=_['intersection'](_0x1d9a10[_0xf5f5('0x32')],qs['fields'](_0x167929[_0xf5f5('0x33')][_0xf5f5('0x34')]));_0x4f049b['attributes']=_0x4f049b[_0xf5f5('0x35')]['length']?_0x4f049b[_0xf5f5('0x35')]:_0x1d9a10[_0xf5f5('0x32')];if(!_0x167929[_0xf5f5('0x33')][_0xf5f5('0x36')](_0xf5f5('0x37'))){_0x4f049b[_0xf5f5('0x1c')]=qs[_0xf5f5('0x1c')](_0x167929[_0xf5f5('0x33')][_0xf5f5('0x1c')]);_0x4f049b['offset']=qs[_0xf5f5('0x1b')](_0x167929[_0xf5f5('0x33')][_0xf5f5('0x1b')]);}_0x4f049b['order']=qs[_0xf5f5('0x38')](_0x167929[_0xf5f5('0x33')]['sort']);_0x4f049b[_0xf5f5('0x39')]=qs[_0xf5f5('0x3a')](_['pick'](_0x167929['query'],_0x1d9a10[_0xf5f5('0x3a')]),_0x8936e3);if(_0x167929[_0xf5f5('0x33')]['filter']){_0x4f049b[_0xf5f5('0x39')]=_[_0xf5f5('0x3b')](_0x4f049b[_0xf5f5('0x39')],{'$or':_[_0xf5f5('0x2d')](_0x8936e3,function(_0x27c8b7){if(_0x27c8b7['type']!==_0xf5f5('0x3c')){var _0x5b6cc8={};_0x5b6cc8[_0x27c8b7['name']]={'$like':'%'+_0x167929['query'][_0xf5f5('0x3d')]+'%'};return _0x5b6cc8;}})});}_0x4f049b=_[_0xf5f5('0x3b')]({},_0x4f049b,_0x167929[_0xf5f5('0x3e')]);var _0x9594e0={'where':_0x4f049b['where']};return db[_0xf5f5('0x2e')][_0xf5f5('0x1e')](_0x9594e0)[_0xf5f5('0x25')](function(_0x52daa6){_0x424c3a[_0xf5f5('0x1e')]=_0x52daa6;if(_0x167929[_0xf5f5('0x33')][_0xf5f5('0x3f')]){_0x4f049b[_0xf5f5('0x40')]=[{'all':!![]}];}return db[_0xf5f5('0x2e')][_0xf5f5('0x41')](_0x4f049b);})[_0xf5f5('0x25')](function(_0x3fcc0b){_0x424c3a[_0xf5f5('0x42')]=_0x3fcc0b;return _0x424c3a;})[_0xf5f5('0x25')](respondWithFilteredResult(_0xcbc33d,_0x4f049b))[_0xf5f5('0x43')](handleError(_0xcbc33d,null));};exports[_0xf5f5('0x44')]=function(_0x3c7a36,_0x42cc99){var _0x478a47={'raw':!![],'where':{'id':_0x3c7a36[_0xf5f5('0x45')]['id']}},_0x2bc9dc={};_0x2bc9dc['model']=_['keys'](db[_0xf5f5('0x2e')]['rawAttributes']);_0x2bc9dc[_0xf5f5('0x33')]=_[_0xf5f5('0x46')](_0x3c7a36[_0xf5f5('0x33')]);_0x2bc9dc['filters']=_[_0xf5f5('0x47')](_0x2bc9dc[_0xf5f5('0x32')],_0x2bc9dc[_0xf5f5('0x33')]);_0x478a47[_0xf5f5('0x35')]=_[_0xf5f5('0x47')](_0x2bc9dc[_0xf5f5('0x32')],qs['fields'](_0x3c7a36[_0xf5f5('0x33')]['fields']));_0x478a47['attributes']=_0x478a47[_0xf5f5('0x35')][_0xf5f5('0x48')]?_0x478a47['attributes']:_0x2bc9dc[_0xf5f5('0x32')];if(_0x3c7a36[_0xf5f5('0x33')][_0xf5f5('0x3f')]){_0x478a47['include']=[{'all':!![]}];}_0x478a47=_[_0xf5f5('0x3b')]({},_0x478a47,_0x3c7a36['options']);return db[_0xf5f5('0x2e')]['find'](_0x478a47)[_0xf5f5('0x25')](handleEntityNotFound(_0x42cc99,null))[_0xf5f5('0x25')](respondWithResult(_0x42cc99,null))[_0xf5f5('0x43')](handleError(_0x42cc99,null));};exports['create']=function(_0x36b863,_0x560395){return db[_0xf5f5('0x2e')][_0xf5f5('0x49')](_0x36b863['body'],{})[_0xf5f5('0x25')](function(_0x111bed){var _0x3e1873=_0x36b863[_0xf5f5('0x4a')][_0xf5f5('0x26')]({'plain':!![]});if(!_0x3e1873)throw new Error(_0xf5f5('0x4b'));if(_0x3e1873[_0xf5f5('0x4c')]===_0xf5f5('0x4a')){var _0xeb65a4=_0x111bed['get']({'plain':!![]});var _0x4b30db='Variables';return db[_0xf5f5('0x4d')][_0xf5f5('0x4e')]({'where':{'name':_0x4b30db,'userProfileId':_0x3e1873[_0xf5f5('0x4f')]},'raw':!![]})['then'](function(_0x37573a){if(_0x37573a&&_0x37573a[_0xf5f5('0x50')]===0x0){return db[_0xf5f5('0x28')]['create']({'name':_0xeb65a4[_0xf5f5('0x2a')],'resourceId':_0xeb65a4['id'],'type':_0x37573a[_0xf5f5('0x2a')],'sectionId':_0x37573a['id']},{})['then'](function(){return _0x111bed;});}else{return _0x111bed;}})[_0xf5f5('0x43')](function(_0x4564b7){logger[_0xf5f5('0x51')](_0xf5f5('0x52'),_0x4564b7);throw _0x4564b7;});}return _0x111bed;})[_0xf5f5('0x25')](respondWithResult(_0x560395,0xc9))[_0xf5f5('0x43')](handleError(_0x560395,null));};exports['update']=function(_0x57e95b,_0x26487e){if(_0x57e95b['body']['id']){delete _0x57e95b['body']['id'];}return db['Variable'][_0xf5f5('0x4e')]({'where':{'id':_0x57e95b[_0xf5f5('0x45')]['id']}})[_0xf5f5('0x25')](handleEntityNotFound(_0x26487e,null))[_0xf5f5('0x25')](saveUpdates(_0x57e95b[_0xf5f5('0x53')],null))[_0xf5f5('0x25')](respondWithResult(_0x26487e,null))[_0xf5f5('0x43')](handleError(_0x26487e,null));};exports['destroy']=function(_0x192abc,_0x4621a7){return db['Variable'][_0xf5f5('0x4e')]({'where':{'id':_0x192abc[_0xf5f5('0x45')]['id']}})[_0xf5f5('0x25')](handleEntityNotFound(_0x4621a7,null))['then'](removeEntity(_0x4621a7,null))[_0xf5f5('0x43')](handleError(_0x4621a7,null));};