c358880d103d1985abebca5b28d1aac0b988c5e9
[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 _0x1f93=['count','limit','Content-Range','apply','save','destroy','then','get','Variables','error','name','send','index','map','Variable','rawAttributes','type','key','model','keys','filters','intersection','attributes','fields','query','length','hasOwnProperty','nolimit','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','findAll','rows','catch','show','params','include','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','userProfileId','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','eml-format','zip-dir','fast-json-patch','mustache','util','path','sox','to-csv','ejs','fs-extra','squel','crypto','jsforce','querystring','papaparse','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','status','json','offset','undefined'];(function(_0x1762b4,_0x33d836){var _0x29beda=function(_0x31c2fa){while(--_0x31c2fa){_0x1762b4['push'](_0x1762b4['shift']());}};_0x29beda(++_0x33d836);}(_0x1f93,0x124));var _0x31f9=function(_0x47d64e,_0x544d7e){_0x47d64e=_0x47d64e-0x0;var _0x1cf2cb=_0x1f93[_0x47d64e];return _0x1cf2cb;};'use strict';var emlformat=require(_0x31f9('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x31f9('0x1'));var jsonpatch=require(_0x31f9('0x2'));var rp=require('request-promise');var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0x31f9('0x3'));var util=require(_0x31f9('0x4'));var path=require(_0x31f9('0x5'));var sox=require(_0x31f9('0x6'));var csv=require(_0x31f9('0x7'));var ejs=require(_0x31f9('0x8'));var fs=require('fs');var fs_extra=require(_0x31f9('0x9'));var _=require('lodash');var squel=require(_0x31f9('0xa'));var crypto=require(_0x31f9('0xb'));var jsforce=require(_0x31f9('0xc'));var deskjs=require('desk.js');var toCsv=require(_0x31f9('0x7'));var querystring=require(_0x31f9('0xd'));var Papa=require(_0x31f9('0xe'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x31f9('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x31f9('0x10'));var logger=require(_0x31f9('0x11'))(_0x31f9('0x12'));var utils=require(_0x31f9('0x13'));var config=require(_0x31f9('0x14'));var licenseUtil=require('../../config/license/util');var db=require(_0x31f9('0x15'))['db'];function respondWithStatusCode(_0x4c331d,_0x2072cd){_0x2072cd=_0x2072cd||0xcc;return function(_0xe78a5){if(_0xe78a5){return _0x4c331d[_0x31f9('0x16')](_0x2072cd);}return _0x4c331d['status'](_0x2072cd)[_0x31f9('0x17')]();};}function respondWithResult(_0x53407e,_0x31a5a6){_0x31a5a6=_0x31a5a6||0xc8;return function(_0x24508e){if(_0x24508e){return _0x53407e[_0x31f9('0x18')](_0x31a5a6)[_0x31f9('0x19')](_0x24508e);}};}function respondWithFilteredResult(_0x3f40eb,_0x306085){return function(_0x18c676){if(_0x18c676){var _0x457c49=typeof _0x306085[_0x31f9('0x1a')]===_0x31f9('0x1b')&&typeof _0x306085['limit']==='undefined';var _0x52f7d4=_0x18c676[_0x31f9('0x1c')];var _0x59bfb8=_0x457c49?0x0:_0x306085[_0x31f9('0x1a')];var _0x36bc51=_0x457c49?_0x18c676[_0x31f9('0x1c')]:_0x306085[_0x31f9('0x1a')]+_0x306085[_0x31f9('0x1d')];var _0x20aea3;if(_0x36bc51>=_0x52f7d4){_0x36bc51=_0x52f7d4;_0x20aea3=0xc8;}else{_0x20aea3=0xce;}_0x3f40eb[_0x31f9('0x18')](_0x20aea3);return _0x3f40eb['set'](_0x31f9('0x1e'),_0x59bfb8+'-'+_0x36bc51+'/'+_0x52f7d4)['json'](_0x18c676);}return null;};}function patchUpdates(_0x51e4b7){return function(_0x2571a2){try{jsonpatch[_0x31f9('0x1f')](_0x2571a2,_0x51e4b7,!![]);}catch(_0x4c16dd){return BPromise['reject'](_0x4c16dd);}return _0x2571a2[_0x31f9('0x20')]();};}function saveUpdates(_0x540042,_0x33eeef){return function(_0x50d819){if(_0x50d819){return _0x50d819['update'](_0x540042)['then'](function(_0x4ec10b){return _0x4ec10b;});}return null;};}function removeEntity(_0x4edc55,_0x3c4253){return function(_0x240d23){if(_0x240d23){return _0x240d23[_0x31f9('0x21')]()[_0x31f9('0x22')](function(){var _0xd57c22=_0x240d23[_0x31f9('0x23')]({'plain':!![]});var _0x193412=_0x31f9('0x24');return db['UserProfileResource'][_0x31f9('0x21')]({'where':{'type':_0x193412,'resourceId':_0xd57c22['id']}})[_0x31f9('0x22')](function(){return _0x240d23;});})['then'](function(){_0x4edc55[_0x31f9('0x18')](0xcc)[_0x31f9('0x17')]();});}};}function handleEntityNotFound(_0x579c38,_0x378e4f){return function(_0x348033){if(!_0x348033){_0x579c38[_0x31f9('0x16')](0x194);}return _0x348033;};}function handleError(_0x3e8508,_0x290b70){_0x290b70=_0x290b70||0x1f4;return function(_0x33e3d2){logger[_0x31f9('0x25')](_0x33e3d2['stack']);if(_0x33e3d2[_0x31f9('0x26')]){delete _0x33e3d2['name'];}_0x3e8508[_0x31f9('0x18')](_0x290b70)[_0x31f9('0x27')](_0x33e3d2);};}exports[_0x31f9('0x28')]=function(_0x26bbfc,_0x1ad1b1){var _0x107a76={},_0x5ef29e={},_0x1c6dd3={'count':0x0,'rows':[]};var _0x5baba4=_[_0x31f9('0x29')](db[_0x31f9('0x2a')][_0x31f9('0x2b')],function(_0x1febc0){return{'name':_0x1febc0['fieldName'],'type':_0x1febc0[_0x31f9('0x2c')][_0x31f9('0x2d')]};});_0x5ef29e[_0x31f9('0x2e')]=_[_0x31f9('0x29')](_0x5baba4,'name');_0x5ef29e['query']=_[_0x31f9('0x2f')](_0x26bbfc['query']);_0x5ef29e[_0x31f9('0x30')]=_[_0x31f9('0x31')](_0x5ef29e[_0x31f9('0x2e')],_0x5ef29e['query']);_0x107a76[_0x31f9('0x32')]=_[_0x31f9('0x31')](_0x5ef29e['model'],qs[_0x31f9('0x33')](_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x33')]));_0x107a76[_0x31f9('0x32')]=_0x107a76['attributes'][_0x31f9('0x35')]?_0x107a76[_0x31f9('0x32')]:_0x5ef29e[_0x31f9('0x2e')];if(!_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x36')](_0x31f9('0x37'))){_0x107a76[_0x31f9('0x1d')]=qs[_0x31f9('0x1d')](_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x1d')]);_0x107a76[_0x31f9('0x1a')]=qs['offset'](_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x1a')]);}_0x107a76['order']=qs['sort'](_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x38')]);_0x107a76[_0x31f9('0x39')]=qs['filters'](_[_0x31f9('0x3a')](_0x26bbfc[_0x31f9('0x34')],_0x5ef29e[_0x31f9('0x30')]),_0x5baba4);if(_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x3b')]){_0x107a76['where']=_[_0x31f9('0x3c')](_0x107a76[_0x31f9('0x39')],{'$or':_['map'](_0x5baba4,function(_0x48a3dd){if(_0x48a3dd[_0x31f9('0x2c')]!==_0x31f9('0x3d')){var _0x16de8e={};_0x16de8e[_0x48a3dd[_0x31f9('0x26')]]={'$like':'%'+_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x3b')]+'%'};return _0x16de8e;}})});}_0x107a76=_[_0x31f9('0x3c')]({},_0x107a76,_0x26bbfc[_0x31f9('0x3e')]);var _0x568fc1={'where':_0x107a76[_0x31f9('0x39')]};return db[_0x31f9('0x2a')][_0x31f9('0x1c')](_0x568fc1)[_0x31f9('0x22')](function(_0x4151d1){_0x1c6dd3[_0x31f9('0x1c')]=_0x4151d1;if(_0x26bbfc[_0x31f9('0x34')][_0x31f9('0x3f')]){_0x107a76['include']=[{'all':!![]}];}return db['Variable'][_0x31f9('0x40')](_0x107a76);})[_0x31f9('0x22')](function(_0x2edbc8){_0x1c6dd3[_0x31f9('0x41')]=_0x2edbc8;return _0x1c6dd3;})[_0x31f9('0x22')](respondWithFilteredResult(_0x1ad1b1,_0x107a76))[_0x31f9('0x42')](handleError(_0x1ad1b1,null));};exports[_0x31f9('0x43')]=function(_0xdbb2a8,_0x2741fe){var _0x3834e5={'raw':!![],'where':{'id':_0xdbb2a8[_0x31f9('0x44')]['id']}},_0x2bab9d={};_0x2bab9d[_0x31f9('0x2e')]=_[_0x31f9('0x2f')](db['Variable']['rawAttributes']);_0x2bab9d[_0x31f9('0x34')]=_[_0x31f9('0x2f')](_0xdbb2a8[_0x31f9('0x34')]);_0x2bab9d[_0x31f9('0x30')]=_[_0x31f9('0x31')](_0x2bab9d['model'],_0x2bab9d[_0x31f9('0x34')]);_0x3834e5[_0x31f9('0x32')]=_['intersection'](_0x2bab9d[_0x31f9('0x2e')],qs[_0x31f9('0x33')](_0xdbb2a8[_0x31f9('0x34')]['fields']));_0x3834e5[_0x31f9('0x32')]=_0x3834e5[_0x31f9('0x32')]['length']?_0x3834e5['attributes']:_0x2bab9d['model'];if(_0xdbb2a8[_0x31f9('0x34')][_0x31f9('0x3f')]){_0x3834e5[_0x31f9('0x45')]=[{'all':!![]}];}_0x3834e5=_[_0x31f9('0x3c')]({},_0x3834e5,_0xdbb2a8['options']);return db[_0x31f9('0x2a')][_0x31f9('0x46')](_0x3834e5)[_0x31f9('0x22')](handleEntityNotFound(_0x2741fe,null))['then'](respondWithResult(_0x2741fe,null))[_0x31f9('0x42')](handleError(_0x2741fe,null));};exports[_0x31f9('0x47')]=function(_0x3d80fc,_0x20d7e9){return db['Variable'][_0x31f9('0x47')](_0x3d80fc[_0x31f9('0x48')],{})[_0x31f9('0x22')](function(_0x4969bf){var _0x2b1762=_0x3d80fc[_0x31f9('0x49')][_0x31f9('0x23')]({'plain':!![]});if(!_0x2b1762)throw new Error(_0x31f9('0x4a'));if(_0x2b1762['role']===_0x31f9('0x49')){var _0x2d36e0=_0x4969bf['get']({'plain':!![]});var _0x3ff651=_0x31f9('0x24');return db[_0x31f9('0x4b')][_0x31f9('0x46')]({'where':{'name':_0x3ff651,'userProfileId':_0x2b1762[_0x31f9('0x4c')]},'raw':!![]})[_0x31f9('0x22')](function(_0x3fc9a3){if(_0x3fc9a3&&_0x3fc9a3['autoAssociation']===0x0){return db[_0x31f9('0x4d')][_0x31f9('0x47')]({'name':_0x2d36e0[_0x31f9('0x26')],'resourceId':_0x2d36e0['id'],'type':_0x3fc9a3[_0x31f9('0x26')],'sectionId':_0x3fc9a3['id']},{})[_0x31f9('0x22')](function(){return _0x4969bf;});}else{return _0x4969bf;}})['catch'](function(_0x10b2d0){logger[_0x31f9('0x25')](_0x31f9('0x4e'),_0x10b2d0);throw _0x10b2d0;});}return _0x4969bf;})[_0x31f9('0x22')](respondWithResult(_0x20d7e9,0xc9))['catch'](handleError(_0x20d7e9,null));};exports[_0x31f9('0x4f')]=function(_0x23679f,_0x4ebd59){if(_0x23679f[_0x31f9('0x48')]['id']){delete _0x23679f[_0x31f9('0x48')]['id'];}return db[_0x31f9('0x2a')][_0x31f9('0x46')]({'where':{'id':_0x23679f[_0x31f9('0x44')]['id']}})[_0x31f9('0x22')](handleEntityNotFound(_0x4ebd59,null))[_0x31f9('0x22')](saveUpdates(_0x23679f['body'],null))[_0x31f9('0x22')](respondWithResult(_0x4ebd59,null))[_0x31f9('0x42')](handleError(_0x4ebd59,null));};exports[_0x31f9('0x21')]=function(_0x88d350,_0x238707){return db['Variable'][_0x31f9('0x46')]({'where':{'id':_0x88d350['params']['id']}})[_0x31f9('0x22')](handleEntityNotFound(_0x238707,null))[_0x31f9('0x22')](removeEntity(_0x238707,null))[_0x31f9('0x42')](handleError(_0x238707,null));};