Built motion from commit (unavailable).|2.5.31
[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 _0xa2c8=['crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','json','offset','undefined','limit','count','set','apply','reject','save','update','then','destroy','get','error','stack','name','merge','body','message','agent','interaction','index','map','Variable','rawAttributes','fieldName','key','model','query','keys','filters','intersection','attributes','fields','nolimit','sort','pick','filter','where','options','includeAll','include','rows','catch','show','params','length','find','create','user','role','Variables','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','to-csv','ejs','fs-extra','lodash'];(function(_0x3642e7,_0x16358e){var _0x31e3dd=function(_0x188ec6){while(--_0x188ec6){_0x3642e7['push'](_0x3642e7['shift']());}};_0x31e3dd(++_0x16358e);}(_0xa2c8,0x99));var _0x8a2c=function(_0x20a584,_0xe384a5){_0x20a584=_0x20a584-0x0;var _0x4b41a8=_0xa2c8[_0x20a584];return _0x4b41a8;};'use strict';var emlformat=require(_0x8a2c('0x0'));var rimraf=require(_0x8a2c('0x1'));var zipdir=require(_0x8a2c('0x2'));var jsonpatch=require(_0x8a2c('0x3'));var rp=require(_0x8a2c('0x4'));var moment=require(_0x8a2c('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x8a2c('0x6'));var util=require('util');var path=require('path');var sox=require('sox');var csv=require(_0x8a2c('0x7'));var ejs=require(_0x8a2c('0x8'));var fs=require('fs');var fs_extra=require(_0x8a2c('0x9'));var _=require(_0x8a2c('0xa'));var squel=require('squel');var crypto=require(_0x8a2c('0xb'));var jsforce=require(_0x8a2c('0xc'));var deskjs=require(_0x8a2c('0xd'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x8a2c('0xe'));var authService=require(_0x8a2c('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x8a2c('0x10'));var hardwareService=require(_0x8a2c('0x11'));var logger=require(_0x8a2c('0x12'))(_0x8a2c('0x13'));var utils=require(_0x8a2c('0x14'));var config=require(_0x8a2c('0x15'));var licenseUtil=require('../../config/license/util');var db=require(_0x8a2c('0x16'))['db'];function respondWithStatusCode(_0x27f040,_0x4b18eb){_0x4b18eb=_0x4b18eb||0xcc;return function(_0xec4883){if(_0xec4883){return _0x27f040[_0x8a2c('0x17')](_0x4b18eb);}return _0x27f040['status'](_0x4b18eb)['end']();};}function respondWithResult(_0x545fb0,_0x4c7eac){_0x4c7eac=_0x4c7eac||0xc8;return function(_0x16bff7){if(_0x16bff7){return _0x545fb0[_0x8a2c('0x18')](_0x4c7eac)[_0x8a2c('0x19')](_0x16bff7);}};}function respondWithFilteredResult(_0x4f02e1,_0x4e8d08){return function(_0xc9d4c7){if(_0xc9d4c7){var _0x8594b4=typeof _0x4e8d08[_0x8a2c('0x1a')]===_0x8a2c('0x1b')&&typeof _0x4e8d08[_0x8a2c('0x1c')]===_0x8a2c('0x1b');var _0x3d82a5=_0xc9d4c7[_0x8a2c('0x1d')];var _0x48d922=_0x8594b4?0x0:_0x4e8d08['offset'];var _0x41226b=_0x8594b4?_0xc9d4c7[_0x8a2c('0x1d')]:_0x4e8d08[_0x8a2c('0x1a')]+_0x4e8d08[_0x8a2c('0x1c')];var _0x2e7e0b;if(_0x41226b>=_0x3d82a5){_0x41226b=_0x3d82a5;_0x2e7e0b=0xc8;}else{_0x2e7e0b=0xce;}_0x4f02e1[_0x8a2c('0x18')](_0x2e7e0b);return _0x4f02e1[_0x8a2c('0x1e')]('Content-Range',_0x48d922+'-'+_0x41226b+'/'+_0x3d82a5)[_0x8a2c('0x19')](_0xc9d4c7);}return null;};}function patchUpdates(_0x4a8d1e){return function(_0x29efe9){try{jsonpatch[_0x8a2c('0x1f')](_0x29efe9,_0x4a8d1e,!![]);}catch(_0x17a645){return BPromise[_0x8a2c('0x20')](_0x17a645);}return _0x29efe9[_0x8a2c('0x21')]();};}function saveUpdates(_0x292c02,_0xa77550){return function(_0x10c951){if(_0x10c951){return _0x10c951[_0x8a2c('0x22')](_0x292c02)[_0x8a2c('0x23')](function(_0x49454f){return _0x49454f;});}return null;};}function removeEntity(_0x2d89cb,_0x5acc49){return function(_0x26c80c){if(_0x26c80c){return _0x26c80c[_0x8a2c('0x24')]()[_0x8a2c('0x23')](function(){var _0xebdcf9=_0x26c80c[_0x8a2c('0x25')]({'plain':!![]});var _0x305949='Variables';return db['UserProfileResource'][_0x8a2c('0x24')]({'where':{'type':_0x305949,'resourceId':_0xebdcf9['id']}})[_0x8a2c('0x23')](function(){return _0x26c80c;});})[_0x8a2c('0x23')](function(){_0x2d89cb[_0x8a2c('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x3334b8,_0x418102){return function(_0x2fc23a){if(!_0x2fc23a){_0x3334b8[_0x8a2c('0x17')](0x194);}return _0x2fc23a;};}function handleError(_0x3494cb,_0x126d3f){_0x126d3f=_0x126d3f||0x1f4;return function(_0x57c59e){logger[_0x8a2c('0x26')](_0x57c59e[_0x8a2c('0x27')]);if(_0x57c59e[_0x8a2c('0x28')]){delete _0x57c59e['name'];}_0x3494cb[_0x8a2c('0x18')](_0x126d3f)['send'](_0x57c59e);};}function getMustacheRender(_0x328013,_0x525b15){return Mustache['render'](_0x328013,_[_0x8a2c('0x29')](_0x525b15[_0x8a2c('0x2a')],{'contact':_0x525b15['contact'],'message':_0x525b15[_0x8a2c('0x2b')],'agent':_0x525b15[_0x8a2c('0x2c')],'interaction':_0x525b15[_0x8a2c('0x2d')],'account':_0x525b15['account']}))||'';}exports[_0x8a2c('0x2e')]=function(_0x16de6d,_0x380aac){var _0x37b1f4={},_0x21cb4a={},_0x56dfc3={'count':0x0,'rows':[]};var _0x240c0c=_[_0x8a2c('0x2f')](db[_0x8a2c('0x30')][_0x8a2c('0x31')],function(_0x90b694){return{'name':_0x90b694[_0x8a2c('0x32')],'type':_0x90b694['type'][_0x8a2c('0x33')]};});_0x21cb4a[_0x8a2c('0x34')]=_['map'](_0x240c0c,_0x8a2c('0x28'));_0x21cb4a[_0x8a2c('0x35')]=_[_0x8a2c('0x36')](_0x16de6d[_0x8a2c('0x35')]);_0x21cb4a[_0x8a2c('0x37')]=_[_0x8a2c('0x38')](_0x21cb4a[_0x8a2c('0x34')],_0x21cb4a[_0x8a2c('0x35')]);_0x37b1f4[_0x8a2c('0x39')]=_['intersection'](_0x21cb4a[_0x8a2c('0x34')],qs[_0x8a2c('0x3a')](_0x16de6d[_0x8a2c('0x35')][_0x8a2c('0x3a')]));_0x37b1f4[_0x8a2c('0x39')]=_0x37b1f4[_0x8a2c('0x39')]['length']?_0x37b1f4[_0x8a2c('0x39')]:_0x21cb4a[_0x8a2c('0x34')];if(!_0x16de6d[_0x8a2c('0x35')]['hasOwnProperty'](_0x8a2c('0x3b'))){_0x37b1f4[_0x8a2c('0x1c')]=qs[_0x8a2c('0x1c')](_0x16de6d[_0x8a2c('0x35')][_0x8a2c('0x1c')]);_0x37b1f4['offset']=qs['offset'](_0x16de6d[_0x8a2c('0x35')][_0x8a2c('0x1a')]);}_0x37b1f4['order']=qs[_0x8a2c('0x3c')](_0x16de6d[_0x8a2c('0x35')]['sort']);_0x37b1f4['where']=qs[_0x8a2c('0x37')](_[_0x8a2c('0x3d')](_0x16de6d['query'],_0x21cb4a[_0x8a2c('0x37')]),_0x240c0c);if(_0x16de6d[_0x8a2c('0x35')][_0x8a2c('0x3e')]){_0x37b1f4[_0x8a2c('0x3f')]=_[_0x8a2c('0x29')](_0x37b1f4['where'],{'$or':_[_0x8a2c('0x2f')](_0x240c0c,function(_0x24ee1c){if(_0x24ee1c['type']!=='VIRTUAL'){var _0x282769={};_0x282769[_0x24ee1c[_0x8a2c('0x28')]]={'$like':'%'+_0x16de6d['query'][_0x8a2c('0x3e')]+'%'};return _0x282769;}})});}_0x37b1f4=_[_0x8a2c('0x29')]({},_0x37b1f4,_0x16de6d[_0x8a2c('0x40')]);var _0x1fd19c={'where':_0x37b1f4[_0x8a2c('0x3f')]};return db['Variable'][_0x8a2c('0x1d')](_0x1fd19c)[_0x8a2c('0x23')](function(_0x32a4c7){_0x56dfc3['count']=_0x32a4c7;if(_0x16de6d[_0x8a2c('0x35')][_0x8a2c('0x41')]){_0x37b1f4[_0x8a2c('0x42')]=[{'all':!![]}];}return db[_0x8a2c('0x30')]['findAll'](_0x37b1f4);})[_0x8a2c('0x23')](function(_0x22006f){_0x56dfc3[_0x8a2c('0x43')]=_0x22006f;return _0x56dfc3;})[_0x8a2c('0x23')](respondWithFilteredResult(_0x380aac,_0x37b1f4))[_0x8a2c('0x44')](handleError(_0x380aac,null));};exports[_0x8a2c('0x45')]=function(_0x330271,_0x278f25){var _0x3ec26b={'raw':!![],'where':{'id':_0x330271[_0x8a2c('0x46')]['id']}},_0x13167c={};_0x13167c[_0x8a2c('0x34')]=_[_0x8a2c('0x36')](db['Variable'][_0x8a2c('0x31')]);_0x13167c['query']=_['keys'](_0x330271[_0x8a2c('0x35')]);_0x13167c[_0x8a2c('0x37')]=_[_0x8a2c('0x38')](_0x13167c['model'],_0x13167c[_0x8a2c('0x35')]);_0x3ec26b[_0x8a2c('0x39')]=_[_0x8a2c('0x38')](_0x13167c[_0x8a2c('0x34')],qs[_0x8a2c('0x3a')](_0x330271['query'][_0x8a2c('0x3a')]));_0x3ec26b[_0x8a2c('0x39')]=_0x3ec26b[_0x8a2c('0x39')][_0x8a2c('0x47')]?_0x3ec26b['attributes']:_0x13167c[_0x8a2c('0x34')];if(_0x330271['query'][_0x8a2c('0x41')]){_0x3ec26b[_0x8a2c('0x42')]=[{'all':!![]}];}_0x3ec26b=_[_0x8a2c('0x29')]({},_0x3ec26b,_0x330271[_0x8a2c('0x40')]);return db[_0x8a2c('0x30')][_0x8a2c('0x48')](_0x3ec26b)[_0x8a2c('0x23')](handleEntityNotFound(_0x278f25,null))['then'](respondWithResult(_0x278f25,null))[_0x8a2c('0x44')](handleError(_0x278f25,null));};exports[_0x8a2c('0x49')]=function(_0x410194,_0x4047ac){return db['Variable'][_0x8a2c('0x49')](_0x410194['body'],{})[_0x8a2c('0x23')](function(_0x38dd75){var _0x2bb40c=_0x410194[_0x8a2c('0x4a')][_0x8a2c('0x25')]({'plain':!![]});if(!_0x2bb40c)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x2bb40c[_0x8a2c('0x4b')]===_0x8a2c('0x4a')){var _0x41526c=_0x38dd75[_0x8a2c('0x25')]({'plain':!![]});var _0x502511=_0x8a2c('0x4c');return db[_0x8a2c('0x4d')][_0x8a2c('0x48')]({'where':{'name':_0x502511,'userProfileId':_0x2bb40c[_0x8a2c('0x4e')]},'raw':!![]})[_0x8a2c('0x23')](function(_0x1edb49){if(_0x1edb49&&_0x1edb49[_0x8a2c('0x4f')]===0x0){return db[_0x8a2c('0x50')][_0x8a2c('0x49')]({'name':_0x41526c[_0x8a2c('0x28')],'resourceId':_0x41526c['id'],'type':_0x1edb49[_0x8a2c('0x28')],'sectionId':_0x1edb49['id']},{})[_0x8a2c('0x23')](function(){return _0x38dd75;});}else{return _0x38dd75;}})[_0x8a2c('0x44')](function(_0x515a24){logger['error'](_0x8a2c('0x51'),_0x515a24);throw _0x515a24;});}return _0x38dd75;})['then'](respondWithResult(_0x4047ac,0xc9))[_0x8a2c('0x44')](handleError(_0x4047ac,null));};exports[_0x8a2c('0x22')]=function(_0x37e89c,_0x5d519d){if(_0x37e89c['body']['id']){delete _0x37e89c['body']['id'];}return db[_0x8a2c('0x30')][_0x8a2c('0x48')]({'where':{'id':_0x37e89c[_0x8a2c('0x46')]['id']}})[_0x8a2c('0x23')](handleEntityNotFound(_0x5d519d,null))[_0x8a2c('0x23')](saveUpdates(_0x37e89c[_0x8a2c('0x2a')],null))['then'](respondWithResult(_0x5d519d,null))[_0x8a2c('0x44')](handleError(_0x5d519d,null));};exports[_0x8a2c('0x24')]=function(_0xa31049,_0x14055e){return db[_0x8a2c('0x30')]['find']({'where':{'id':_0xa31049[_0x8a2c('0x46')]['id']}})[_0x8a2c('0x23')](handleEntityNotFound(_0x14055e,null))[_0x8a2c('0x23')](removeEntity(_0x14055e,null))[_0x8a2c('0x44')](handleError(_0x14055e,null));};