Built motion from commit 497d3d92.|2.5.43
[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 _0x5b3b=['to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','update','then','destroy','get','Variables','UserProfileResource','name','send','index','map','Variable','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','includeAll','rows','catch','show','params','include','find','create','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','sox'];(function(_0x57223b,_0x209a3c){var _0x57d2cf=function(_0x299b1c){while(--_0x299b1c){_0x57223b['push'](_0x57223b['shift']());}};_0x57d2cf(++_0x209a3c);}(_0x5b3b,0x1d1));var _0xb5b3=function(_0x4898d7,_0x16375d){_0x4898d7=_0x4898d7-0x0;var _0x29e76a=_0x5b3b[_0x4898d7];return _0x29e76a;};'use strict';var emlformat=require(_0xb5b3('0x0'));var rimraf=require(_0xb5b3('0x1'));var zipdir=require(_0xb5b3('0x2'));var jsonpatch=require(_0xb5b3('0x3'));var rp=require(_0xb5b3('0x4'));var moment=require(_0xb5b3('0x5'));var BPromise=require(_0xb5b3('0x6'));var Mustache=require('mustache');var util=require(_0xb5b3('0x7'));var path=require('path');var sox=require(_0xb5b3('0x8'));var csv=require(_0xb5b3('0x9'));var ejs=require(_0xb5b3('0xa'));var fs=require('fs');var fs_extra=require(_0xb5b3('0xb'));var _=require('lodash');var squel=require(_0xb5b3('0xc'));var crypto=require(_0xb5b3('0xd'));var jsforce=require(_0xb5b3('0xe'));var deskjs=require(_0xb5b3('0xf'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0xb5b3('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0xb5b3('0x11'));var hardwareService=require(_0xb5b3('0x12'));var logger=require('../../config/logger')(_0xb5b3('0x13'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xb5b3('0x14'));var db=require(_0xb5b3('0x15'))['db'];function respondWithStatusCode(_0x5dfb4d,_0x30b86f){_0x30b86f=_0x30b86f||0xcc;return function(_0x3504d3){if(_0x3504d3){return _0x5dfb4d[_0xb5b3('0x16')](_0x30b86f);}return _0x5dfb4d[_0xb5b3('0x17')](_0x30b86f)[_0xb5b3('0x18')]();};}function respondWithResult(_0xc4aa4c,_0x16196b){_0x16196b=_0x16196b||0xc8;return function(_0x1c6590){if(_0x1c6590){return _0xc4aa4c[_0xb5b3('0x17')](_0x16196b)[_0xb5b3('0x19')](_0x1c6590);}};}function respondWithFilteredResult(_0x34b70f,_0x2bb324){return function(_0x2c56db){if(_0x2c56db){var _0x282d71=typeof _0x2bb324[_0xb5b3('0x1a')]===_0xb5b3('0x1b')&&typeof _0x2bb324[_0xb5b3('0x1c')]===_0xb5b3('0x1b');var _0x48a082=_0x2c56db['count'];var _0x20c1d3=_0x282d71?0x0:_0x2bb324[_0xb5b3('0x1a')];var _0x39200f=_0x282d71?_0x2c56db[_0xb5b3('0x1d')]:_0x2bb324[_0xb5b3('0x1a')]+_0x2bb324[_0xb5b3('0x1c')];var _0x20ebef;if(_0x39200f>=_0x48a082){_0x39200f=_0x48a082;_0x20ebef=0xc8;}else{_0x20ebef=0xce;}_0x34b70f[_0xb5b3('0x17')](_0x20ebef);return _0x34b70f[_0xb5b3('0x1e')](_0xb5b3('0x1f'),_0x20c1d3+'-'+_0x39200f+'/'+_0x48a082)[_0xb5b3('0x19')](_0x2c56db);}return null;};}function patchUpdates(_0x5e713f){return function(_0x49f9df){try{jsonpatch[_0xb5b3('0x20')](_0x49f9df,_0x5e713f,!![]);}catch(_0x32af72){return BPromise[_0xb5b3('0x21')](_0x32af72);}return _0x49f9df['save']();};}function saveUpdates(_0x3b4a96,_0xa1706a){return function(_0x5cc9dc){if(_0x5cc9dc){return _0x5cc9dc[_0xb5b3('0x22')](_0x3b4a96)[_0xb5b3('0x23')](function(_0x11a320){return _0x11a320;});}return null;};}function removeEntity(_0x212f0a,_0x37fbb5){return function(_0x1fe449){if(_0x1fe449){return _0x1fe449[_0xb5b3('0x24')]()[_0xb5b3('0x23')](function(){var _0xad389=_0x1fe449[_0xb5b3('0x25')]({'plain':!![]});var _0x37e934=_0xb5b3('0x26');return db[_0xb5b3('0x27')][_0xb5b3('0x24')]({'where':{'type':_0x37e934,'resourceId':_0xad389['id']}})[_0xb5b3('0x23')](function(){return _0x1fe449;});})[_0xb5b3('0x23')](function(){_0x212f0a[_0xb5b3('0x17')](0xcc)[_0xb5b3('0x18')]();});}};}function handleEntityNotFound(_0x9f629c,_0x1e5349){return function(_0x380b58){if(!_0x380b58){_0x9f629c[_0xb5b3('0x16')](0x194);}return _0x380b58;};}function handleError(_0x51cec5,_0x4a354e){_0x4a354e=_0x4a354e||0x1f4;return function(_0x5b78d6){logger['error'](_0x5b78d6['stack']);if(_0x5b78d6['name']){delete _0x5b78d6[_0xb5b3('0x28')];}_0x51cec5[_0xb5b3('0x17')](_0x4a354e)[_0xb5b3('0x29')](_0x5b78d6);};}exports[_0xb5b3('0x2a')]=function(_0x2584c4,_0x4b1900){var _0x4b138e={},_0x17e501={},_0x4bcd78={'count':0x0,'rows':[]};var _0x1d5f92=_[_0xb5b3('0x2b')](db[_0xb5b3('0x2c')][_0xb5b3('0x2d')],function(_0x2e6c11){return{'name':_0x2e6c11[_0xb5b3('0x2e')],'type':_0x2e6c11[_0xb5b3('0x2f')][_0xb5b3('0x30')]};});_0x17e501[_0xb5b3('0x31')]=_[_0xb5b3('0x2b')](_0x1d5f92,_0xb5b3('0x28'));_0x17e501[_0xb5b3('0x32')]=_[_0xb5b3('0x33')](_0x2584c4[_0xb5b3('0x32')]);_0x17e501[_0xb5b3('0x34')]=_[_0xb5b3('0x35')](_0x17e501[_0xb5b3('0x31')],_0x17e501[_0xb5b3('0x32')]);_0x4b138e['attributes']=_[_0xb5b3('0x35')](_0x17e501[_0xb5b3('0x31')],qs[_0xb5b3('0x36')](_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x36')]));_0x4b138e['attributes']=_0x4b138e[_0xb5b3('0x37')][_0xb5b3('0x38')]?_0x4b138e['attributes']:_0x17e501[_0xb5b3('0x31')];if(!_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x39')](_0xb5b3('0x3a'))){_0x4b138e[_0xb5b3('0x1c')]=qs['limit'](_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x1c')]);_0x4b138e[_0xb5b3('0x1a')]=qs[_0xb5b3('0x1a')](_0x2584c4[_0xb5b3('0x32')]['offset']);}_0x4b138e[_0xb5b3('0x3b')]=qs['sort'](_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x3c')]);_0x4b138e[_0xb5b3('0x3d')]=qs[_0xb5b3('0x34')](_[_0xb5b3('0x3e')](_0x2584c4[_0xb5b3('0x32')],_0x17e501[_0xb5b3('0x34')]),_0x1d5f92);if(_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x3f')]){_0x4b138e['where']=_[_0xb5b3('0x40')](_0x4b138e[_0xb5b3('0x3d')],{'$or':_[_0xb5b3('0x2b')](_0x1d5f92,function(_0x28f0ab){if(_0x28f0ab[_0xb5b3('0x2f')]!==_0xb5b3('0x41')){var _0x41fa3b={};_0x41fa3b[_0x28f0ab[_0xb5b3('0x28')]]={'$like':'%'+_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x3f')]+'%'};return _0x41fa3b;}})});}_0x4b138e=_['merge']({},_0x4b138e,_0x2584c4['options']);var _0x462094={'where':_0x4b138e['where']};return db['Variable'][_0xb5b3('0x1d')](_0x462094)[_0xb5b3('0x23')](function(_0x38ee34){_0x4bcd78[_0xb5b3('0x1d')]=_0x38ee34;if(_0x2584c4[_0xb5b3('0x32')][_0xb5b3('0x42')]){_0x4b138e['include']=[{'all':!![]}];}return db[_0xb5b3('0x2c')]['findAll'](_0x4b138e);})[_0xb5b3('0x23')](function(_0x157465){_0x4bcd78[_0xb5b3('0x43')]=_0x157465;return _0x4bcd78;})[_0xb5b3('0x23')](respondWithFilteredResult(_0x4b1900,_0x4b138e))[_0xb5b3('0x44')](handleError(_0x4b1900,null));};exports[_0xb5b3('0x45')]=function(_0x36ecec,_0x442cc7){var _0xe5657={'raw':!![],'where':{'id':_0x36ecec[_0xb5b3('0x46')]['id']}},_0x59a154={};_0x59a154[_0xb5b3('0x31')]=_[_0xb5b3('0x33')](db[_0xb5b3('0x2c')]['rawAttributes']);_0x59a154[_0xb5b3('0x32')]=_[_0xb5b3('0x33')](_0x36ecec['query']);_0x59a154[_0xb5b3('0x34')]=_[_0xb5b3('0x35')](_0x59a154[_0xb5b3('0x31')],_0x59a154[_0xb5b3('0x32')]);_0xe5657[_0xb5b3('0x37')]=_[_0xb5b3('0x35')](_0x59a154[_0xb5b3('0x31')],qs['fields'](_0x36ecec[_0xb5b3('0x32')][_0xb5b3('0x36')]));_0xe5657[_0xb5b3('0x37')]=_0xe5657[_0xb5b3('0x37')][_0xb5b3('0x38')]?_0xe5657[_0xb5b3('0x37')]:_0x59a154[_0xb5b3('0x31')];if(_0x36ecec['query']['includeAll']){_0xe5657[_0xb5b3('0x47')]=[{'all':!![]}];}_0xe5657=_['merge']({},_0xe5657,_0x36ecec['options']);return db['Variable'][_0xb5b3('0x48')](_0xe5657)[_0xb5b3('0x23')](handleEntityNotFound(_0x442cc7,null))[_0xb5b3('0x23')](respondWithResult(_0x442cc7,null))[_0xb5b3('0x44')](handleError(_0x442cc7,null));};exports[_0xb5b3('0x49')]=function(_0x4c97d2,_0x53b280){return db[_0xb5b3('0x2c')][_0xb5b3('0x49')](_0x4c97d2['body'],{})['then'](function(_0x25c440){var _0x4293e1=_0x4c97d2['user'][_0xb5b3('0x25')]({'plain':!![]});if(!_0x4293e1)throw new Error(_0xb5b3('0x4a'));if(_0x4293e1['role']==='user'){var _0x344a2c=_0x25c440[_0xb5b3('0x25')]({'plain':!![]});var _0x5cd660='Variables';return db[_0xb5b3('0x4b')][_0xb5b3('0x48')]({'where':{'name':_0x5cd660,'userProfileId':_0x4293e1['userProfileId']},'raw':!![]})[_0xb5b3('0x23')](function(_0x448d7a){if(_0x448d7a&&_0x448d7a[_0xb5b3('0x4c')]===0x0){return db[_0xb5b3('0x27')][_0xb5b3('0x49')]({'name':_0x344a2c[_0xb5b3('0x28')],'resourceId':_0x344a2c['id'],'type':_0x448d7a[_0xb5b3('0x28')],'sectionId':_0x448d7a['id']},{})[_0xb5b3('0x23')](function(){return _0x25c440;});}else{return _0x25c440;}})[_0xb5b3('0x44')](function(_0x14edfa){logger[_0xb5b3('0x4d')](_0xb5b3('0x4e'),_0x14edfa);throw _0x14edfa;});}return _0x25c440;})['then'](respondWithResult(_0x53b280,0xc9))[_0xb5b3('0x44')](handleError(_0x53b280,null));};exports[_0xb5b3('0x22')]=function(_0x575274,_0x232c92){if(_0x575274['body']['id']){delete _0x575274['body']['id'];}return db[_0xb5b3('0x2c')][_0xb5b3('0x48')]({'where':{'id':_0x575274['params']['id']}})[_0xb5b3('0x23')](handleEntityNotFound(_0x232c92,null))[_0xb5b3('0x23')](saveUpdates(_0x575274['body'],null))[_0xb5b3('0x23')](respondWithResult(_0x232c92,null))[_0xb5b3('0x44')](handleError(_0x232c92,null));};exports[_0xb5b3('0x24')]=function(_0x92147a,_0x52e5ae){return db[_0xb5b3('0x2c')]['find']({'where':{'id':_0x92147a[_0xb5b3('0x46')]['id']}})['then'](handleEntityNotFound(_0x52e5ae,null))[_0xb5b3('0x23')](removeEntity(_0x52e5ae,null))[_0xb5b3('0x44')](handleError(_0x52e5ae,null));};