Built motion from commit 167bccf5.|2.6.7
[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 _0x88b7=['create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','eml-format','rimraf','fast-json-patch','request-promise','moment','mustache','util','sox','to-csv','lodash','squel','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','json','limit','undefined','count','offset','set','Content-Range','save','update','then','destroy','get','Variables','UserProfileResource','error','stack','name','send','map','Variable','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','include','findAll','rows','catch','params','find'];(function(_0x4001ad,_0x1e22dd){var _0x4c7ec2=function(_0x24cb8a){while(--_0x24cb8a){_0x4001ad['push'](_0x4001ad['shift']());}};_0x4c7ec2(++_0x1e22dd);}(_0x88b7,0x141));var _0x788b=function(_0x4e4416,_0x195668){_0x4e4416=_0x4e4416-0x0;var _0x3e96c4=_0x88b7[_0x4e4416];return _0x3e96c4;};'use strict';var emlformat=require(_0x788b('0x0'));var rimraf=require(_0x788b('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x788b('0x2'));var rp=require(_0x788b('0x3'));var moment=require(_0x788b('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x788b('0x5'));var util=require(_0x788b('0x6'));var path=require('path');var sox=require(_0x788b('0x7'));var csv=require(_0x788b('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x788b('0x9'));var squel=require(_0x788b('0xa'));var crypto=require('crypto');var jsforce=require(_0x788b('0xb'));var deskjs=require(_0x788b('0xc'));var toCsv=require(_0x788b('0x8'));var querystring=require(_0x788b('0xd'));var Papa=require('papaparse');var Redis=require(_0x788b('0xe'));var authService=require(_0x788b('0xf'));var qs=require(_0x788b('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x788b('0x11'));var logger=require(_0x788b('0x12'))('api');var utils=require(_0x788b('0x13'));var config=require('../../config/environment');var licenseUtil=require(_0x788b('0x14'));var db=require(_0x788b('0x15'))['db'];function respondWithStatusCode(_0x4078c5,_0x2c2569){_0x2c2569=_0x2c2569||0xcc;return function(_0x3c8ad5){if(_0x3c8ad5){return _0x4078c5[_0x788b('0x16')](_0x2c2569);}return _0x4078c5[_0x788b('0x17')](_0x2c2569)[_0x788b('0x18')]();};}function respondWithResult(_0x356079,_0x2ebd1d){_0x2ebd1d=_0x2ebd1d||0xc8;return function(_0x58bd92){if(_0x58bd92){return _0x356079['status'](_0x2ebd1d)[_0x788b('0x19')](_0x58bd92);}};}function respondWithFilteredResult(_0x4dadae,_0x486714){return function(_0x6bbfcf){if(_0x6bbfcf){var _0x20caf8=typeof _0x486714['offset']==='undefined'&&typeof _0x486714[_0x788b('0x1a')]===_0x788b('0x1b');var _0x3a1598=_0x6bbfcf[_0x788b('0x1c')];var _0x49e968=_0x20caf8?0x0:_0x486714[_0x788b('0x1d')];var _0xd0b13=_0x20caf8?_0x6bbfcf[_0x788b('0x1c')]:_0x486714['offset']+_0x486714[_0x788b('0x1a')];var _0xc6a04b;if(_0xd0b13>=_0x3a1598){_0xd0b13=_0x3a1598;_0xc6a04b=0xc8;}else{_0xc6a04b=0xce;}_0x4dadae[_0x788b('0x17')](_0xc6a04b);return _0x4dadae[_0x788b('0x1e')](_0x788b('0x1f'),_0x49e968+'-'+_0xd0b13+'/'+_0x3a1598)[_0x788b('0x19')](_0x6bbfcf);}return null;};}function patchUpdates(_0x1c0ec0){return function(_0x5780b5){try{jsonpatch['apply'](_0x5780b5,_0x1c0ec0,!![]);}catch(_0x4c6a7c){return BPromise['reject'](_0x4c6a7c);}return _0x5780b5[_0x788b('0x20')]();};}function saveUpdates(_0x4b6164,_0x115c60){return function(_0x1db810){if(_0x1db810){return _0x1db810[_0x788b('0x21')](_0x4b6164)[_0x788b('0x22')](function(_0x520156){return _0x520156;});}return null;};}function removeEntity(_0x51b741,_0x106f32){return function(_0x2f7a32){if(_0x2f7a32){return _0x2f7a32[_0x788b('0x23')]()[_0x788b('0x22')](function(){var _0x25da1f=_0x2f7a32[_0x788b('0x24')]({'plain':!![]});var _0x180d87=_0x788b('0x25');return db[_0x788b('0x26')][_0x788b('0x23')]({'where':{'type':_0x180d87,'resourceId':_0x25da1f['id']}})[_0x788b('0x22')](function(){return _0x2f7a32;});})[_0x788b('0x22')](function(){_0x51b741[_0x788b('0x17')](0xcc)[_0x788b('0x18')]();});}};}function handleEntityNotFound(_0x4a0e5f,_0x56a204){return function(_0x350ff0){if(!_0x350ff0){_0x4a0e5f[_0x788b('0x16')](0x194);}return _0x350ff0;};}function handleError(_0x54a198,_0x38d32f){_0x38d32f=_0x38d32f||0x1f4;return function(_0x5e32df){logger[_0x788b('0x27')](_0x5e32df[_0x788b('0x28')]);if(_0x5e32df[_0x788b('0x29')]){delete _0x5e32df[_0x788b('0x29')];}_0x54a198['status'](_0x38d32f)[_0x788b('0x2a')](_0x5e32df);};}exports['index']=function(_0x5de27b,_0x5551eb){var _0x33cf86={},_0x56f067={},_0x9dabce={'count':0x0,'rows':[]};var _0x44fbfd=_[_0x788b('0x2b')](db[_0x788b('0x2c')][_0x788b('0x2d')],function(_0x48cc2d){return{'name':_0x48cc2d[_0x788b('0x2e')],'type':_0x48cc2d[_0x788b('0x2f')][_0x788b('0x30')]};});_0x56f067[_0x788b('0x31')]=_['map'](_0x44fbfd,_0x788b('0x29'));_0x56f067[_0x788b('0x32')]=_[_0x788b('0x33')](_0x5de27b[_0x788b('0x32')]);_0x56f067[_0x788b('0x34')]=_['intersection'](_0x56f067[_0x788b('0x31')],_0x56f067['query']);_0x33cf86[_0x788b('0x35')]=_[_0x788b('0x36')](_0x56f067[_0x788b('0x31')],qs[_0x788b('0x37')](_0x5de27b['query'][_0x788b('0x37')]));_0x33cf86[_0x788b('0x35')]=_0x33cf86[_0x788b('0x35')][_0x788b('0x38')]?_0x33cf86[_0x788b('0x35')]:_0x56f067[_0x788b('0x31')];if(!_0x5de27b[_0x788b('0x32')][_0x788b('0x39')](_0x788b('0x3a'))){_0x33cf86[_0x788b('0x1a')]=qs[_0x788b('0x1a')](_0x5de27b['query'][_0x788b('0x1a')]);_0x33cf86[_0x788b('0x1d')]=qs['offset'](_0x5de27b[_0x788b('0x32')][_0x788b('0x1d')]);}_0x33cf86[_0x788b('0x3b')]=qs[_0x788b('0x3c')](_0x5de27b[_0x788b('0x32')][_0x788b('0x3c')]);_0x33cf86[_0x788b('0x3d')]=qs['filters'](_[_0x788b('0x3e')](_0x5de27b[_0x788b('0x32')],_0x56f067[_0x788b('0x34')]),_0x44fbfd);if(_0x5de27b['query'][_0x788b('0x3f')]){_0x33cf86['where']=_['merge'](_0x33cf86['where'],{'$or':_[_0x788b('0x2b')](_0x44fbfd,function(_0x1017a8){if(_0x1017a8[_0x788b('0x2f')]!==_0x788b('0x40')){var _0x39f1c2={};_0x39f1c2[_0x1017a8[_0x788b('0x29')]]={'$like':'%'+_0x5de27b['query'][_0x788b('0x3f')]+'%'};return _0x39f1c2;}})});}_0x33cf86=_[_0x788b('0x41')]({},_0x33cf86,_0x5de27b[_0x788b('0x42')]);var _0x481507={'where':_0x33cf86[_0x788b('0x3d')]};return db['Variable'][_0x788b('0x1c')](_0x481507)[_0x788b('0x22')](function(_0xb66319){_0x9dabce[_0x788b('0x1c')]=_0xb66319;if(_0x5de27b['query'][_0x788b('0x43')]){_0x33cf86[_0x788b('0x44')]=[{'all':!![]}];}return db['Variable'][_0x788b('0x45')](_0x33cf86);})[_0x788b('0x22')](function(_0xfe3464){_0x9dabce[_0x788b('0x46')]=_0xfe3464;return _0x9dabce;})[_0x788b('0x22')](respondWithFilteredResult(_0x5551eb,_0x33cf86))[_0x788b('0x47')](handleError(_0x5551eb,null));};exports['show']=function(_0x3229d7,_0x1c9381){var _0x30b458={'raw':!![],'where':{'id':_0x3229d7[_0x788b('0x48')]['id']}},_0x5c489f={};_0x5c489f['model']=_[_0x788b('0x33')](db[_0x788b('0x2c')]['rawAttributes']);_0x5c489f[_0x788b('0x32')]=_[_0x788b('0x33')](_0x3229d7[_0x788b('0x32')]);_0x5c489f[_0x788b('0x34')]=_['intersection'](_0x5c489f['model'],_0x5c489f[_0x788b('0x32')]);_0x30b458[_0x788b('0x35')]=_['intersection'](_0x5c489f[_0x788b('0x31')],qs[_0x788b('0x37')](_0x3229d7['query'][_0x788b('0x37')]));_0x30b458[_0x788b('0x35')]=_0x30b458['attributes']['length']?_0x30b458['attributes']:_0x5c489f[_0x788b('0x31')];if(_0x3229d7[_0x788b('0x32')][_0x788b('0x43')]){_0x30b458['include']=[{'all':!![]}];}_0x30b458=_[_0x788b('0x41')]({},_0x30b458,_0x3229d7['options']);return db[_0x788b('0x2c')][_0x788b('0x49')](_0x30b458)[_0x788b('0x22')](handleEntityNotFound(_0x1c9381,null))[_0x788b('0x22')](respondWithResult(_0x1c9381,null))['catch'](handleError(_0x1c9381,null));};exports['create']=function(_0x53e3cb,_0xbb9e1b){return db[_0x788b('0x2c')][_0x788b('0x4a')](_0x53e3cb[_0x788b('0x4b')],{})[_0x788b('0x22')](function(_0x34c4cb){var _0x6305c8=_0x53e3cb['user'][_0x788b('0x24')]({'plain':!![]});if(!_0x6305c8)throw new Error(_0x788b('0x4c'));if(_0x6305c8['role']==='user'){var _0x59af47=_0x34c4cb['get']({'plain':!![]});var _0x8b7354='Variables';return db[_0x788b('0x4d')][_0x788b('0x49')]({'where':{'name':_0x8b7354,'userProfileId':_0x6305c8['userProfileId']},'raw':!![]})[_0x788b('0x22')](function(_0x5ca9a8){if(_0x5ca9a8&&_0x5ca9a8['autoAssociation']===0x0){return db['UserProfileResource'][_0x788b('0x4a')]({'name':_0x59af47[_0x788b('0x29')],'resourceId':_0x59af47['id'],'type':_0x5ca9a8[_0x788b('0x29')],'sectionId':_0x5ca9a8['id']},{})['then'](function(){return _0x34c4cb;});}else{return _0x34c4cb;}})[_0x788b('0x47')](function(_0x420a3f){logger['error'](_0x788b('0x4e'),_0x420a3f);throw _0x420a3f;});}return _0x34c4cb;})[_0x788b('0x22')](respondWithResult(_0xbb9e1b,0xc9))[_0x788b('0x47')](handleError(_0xbb9e1b,null));};exports['update']=function(_0x3e54fb,_0x4b4af4){if(_0x3e54fb[_0x788b('0x4b')]['id']){delete _0x3e54fb['body']['id'];}return db[_0x788b('0x2c')]['find']({'where':{'id':_0x3e54fb['params']['id']}})['then'](handleEntityNotFound(_0x4b4af4,null))[_0x788b('0x22')](saveUpdates(_0x3e54fb['body'],null))[_0x788b('0x22')](respondWithResult(_0x4b4af4,null))[_0x788b('0x47')](handleError(_0x4b4af4,null));};exports[_0x788b('0x23')]=function(_0x22e469,_0x43cfa4){return db['Variable'][_0x788b('0x49')]({'where':{'id':_0x22e469[_0x788b('0x48')]['id']}})[_0x788b('0x22')](handleEntityNotFound(_0x43cfa4,null))[_0x788b('0x22')](removeEntity(_0x43cfa4,null))['catch'](handleError(_0x43cfa4,null));};