f294923152bab8ea51e5b006470b8e9abdf79e66
[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 _0xbb14=['rawAttributes','model','query','keys','intersection','fields','attributes','hasOwnProperty','nolimit','order','sort','where','filters','filter','type','VIRTUAL','options','includeAll','include','findAll','rows','catch','params','length','merge','find','create','user','role','Variables','UserProfileSection','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','squel','crypto','jsforce','desk.js','to-csv','papaparse','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','get','UserProfileResource','destroy','then','name','send','index','map','Variable'];(function(_0x18abd2,_0x22d4b3){var _0x1dcad6=function(_0xcc08c4){while(--_0xcc08c4){_0x18abd2['push'](_0x18abd2['shift']());}};_0x1dcad6(++_0x22d4b3);}(_0xbb14,0x71));var _0x4bb1=function(_0x1ac595,_0x5d2081){_0x1ac595=_0x1ac595-0x0;var _0x455fff=_0xbb14[_0x1ac595];return _0x455fff;};'use strict';var emlformat=require(_0x4bb1('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x4bb1('0x1'));var jsonpatch=require(_0x4bb1('0x2'));var rp=require(_0x4bb1('0x3'));var moment=require(_0x4bb1('0x4'));var BPromise=require(_0x4bb1('0x5'));var Mustache=require(_0x4bb1('0x6'));var util=require(_0x4bb1('0x7'));var path=require(_0x4bb1('0x8'));var sox=require(_0x4bb1('0x9'));var csv=require('to-csv');var ejs=require(_0x4bb1('0xa'));var fs=require('fs');var fs_extra=require(_0x4bb1('0xb'));var _=require('lodash');var squel=require(_0x4bb1('0xc'));var crypto=require(_0x4bb1('0xd'));var jsforce=require(_0x4bb1('0xe'));var deskjs=require(_0x4bb1('0xf'));var toCsv=require(_0x4bb1('0x10'));var querystring=require('querystring');var Papa=require(_0x4bb1('0x11'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x4bb1('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x4bb1('0x13'));var logger=require(_0x4bb1('0x14'))('api');var utils=require(_0x4bb1('0x15'));var config=require(_0x4bb1('0x16'));var licenseUtil=require(_0x4bb1('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x4c90bc,_0x4d1a32){_0x4d1a32=_0x4d1a32||0xcc;return function(_0x2f6068){if(_0x2f6068){return _0x4c90bc[_0x4bb1('0x18')](_0x4d1a32);}return _0x4c90bc[_0x4bb1('0x19')](_0x4d1a32)[_0x4bb1('0x1a')]();};}function respondWithResult(_0x3bc739,_0x3b9be3){_0x3b9be3=_0x3b9be3||0xc8;return function(_0x16f890){if(_0x16f890){return _0x3bc739[_0x4bb1('0x19')](_0x3b9be3)[_0x4bb1('0x1b')](_0x16f890);}};}function respondWithFilteredResult(_0x53901b,_0x1e44ce){return function(_0x16aa7b){if(_0x16aa7b){var _0x2ccc85=typeof _0x1e44ce[_0x4bb1('0x1c')]===_0x4bb1('0x1d')&&typeof _0x1e44ce[_0x4bb1('0x1e')]==='undefined';var _0x78ba70=_0x16aa7b[_0x4bb1('0x1f')];var _0x510a40=_0x2ccc85?0x0:_0x1e44ce[_0x4bb1('0x1c')];var _0x35d3c6=_0x2ccc85?_0x16aa7b[_0x4bb1('0x1f')]:_0x1e44ce[_0x4bb1('0x1c')]+_0x1e44ce[_0x4bb1('0x1e')];var _0x17f491;if(_0x35d3c6>=_0x78ba70){_0x35d3c6=_0x78ba70;_0x17f491=0xc8;}else{_0x17f491=0xce;}_0x53901b['status'](_0x17f491);return _0x53901b[_0x4bb1('0x20')](_0x4bb1('0x21'),_0x510a40+'-'+_0x35d3c6+'/'+_0x78ba70)[_0x4bb1('0x1b')](_0x16aa7b);}return null;};}function patchUpdates(_0x5f1bae){return function(_0x2d7195){try{jsonpatch[_0x4bb1('0x22')](_0x2d7195,_0x5f1bae,!![]);}catch(_0x16334a){return BPromise[_0x4bb1('0x23')](_0x16334a);}return _0x2d7195['save']();};}function saveUpdates(_0x25c1ae,_0x14c720){return function(_0x44e860){if(_0x44e860){return _0x44e860['update'](_0x25c1ae)['then'](function(_0x12a59e){return _0x12a59e;});}return null;};}function removeEntity(_0xf574be,_0x4d0e11){return function(_0x3322f6){if(_0x3322f6){return _0x3322f6['destroy']()['then'](function(){var _0x45a6d7=_0x3322f6[_0x4bb1('0x24')]({'plain':!![]});var _0xea63b9='Variables';return db[_0x4bb1('0x25')][_0x4bb1('0x26')]({'where':{'type':_0xea63b9,'resourceId':_0x45a6d7['id']}})['then'](function(){return _0x3322f6;});})[_0x4bb1('0x27')](function(){_0xf574be[_0x4bb1('0x19')](0xcc)[_0x4bb1('0x1a')]();});}};}function handleEntityNotFound(_0x54c365,_0x2204ee){return function(_0x26f52c){if(!_0x26f52c){_0x54c365['sendStatus'](0x194);}return _0x26f52c;};}function handleError(_0x3368d1,_0x542b91){_0x542b91=_0x542b91||0x1f4;return function(_0x583a38){logger['error'](_0x583a38['stack']);if(_0x583a38[_0x4bb1('0x28')]){delete _0x583a38[_0x4bb1('0x28')];}_0x3368d1['status'](_0x542b91)[_0x4bb1('0x29')](_0x583a38);};}exports[_0x4bb1('0x2a')]=function(_0x45871c,_0x4ceed5){var _0x59831f={},_0x41282c={},_0x362c56={'count':0x0,'rows':[]};var _0xe1979=_[_0x4bb1('0x2b')](db[_0x4bb1('0x2c')][_0x4bb1('0x2d')],function(_0x2db0f1){return{'name':_0x2db0f1['fieldName'],'type':_0x2db0f1['type']['key']};});_0x41282c[_0x4bb1('0x2e')]=_[_0x4bb1('0x2b')](_0xe1979,_0x4bb1('0x28'));_0x41282c[_0x4bb1('0x2f')]=_[_0x4bb1('0x30')](_0x45871c[_0x4bb1('0x2f')]);_0x41282c['filters']=_[_0x4bb1('0x31')](_0x41282c['model'],_0x41282c[_0x4bb1('0x2f')]);_0x59831f['attributes']=_['intersection'](_0x41282c[_0x4bb1('0x2e')],qs[_0x4bb1('0x32')](_0x45871c[_0x4bb1('0x2f')]['fields']));_0x59831f[_0x4bb1('0x33')]=_0x59831f[_0x4bb1('0x33')]['length']?_0x59831f['attributes']:_0x41282c[_0x4bb1('0x2e')];if(!_0x45871c[_0x4bb1('0x2f')][_0x4bb1('0x34')](_0x4bb1('0x35'))){_0x59831f[_0x4bb1('0x1e')]=qs[_0x4bb1('0x1e')](_0x45871c[_0x4bb1('0x2f')][_0x4bb1('0x1e')]);_0x59831f[_0x4bb1('0x1c')]=qs['offset'](_0x45871c[_0x4bb1('0x2f')][_0x4bb1('0x1c')]);}_0x59831f[_0x4bb1('0x36')]=qs[_0x4bb1('0x37')](_0x45871c['query'][_0x4bb1('0x37')]);_0x59831f[_0x4bb1('0x38')]=qs[_0x4bb1('0x39')](_['pick'](_0x45871c[_0x4bb1('0x2f')],_0x41282c[_0x4bb1('0x39')]),_0xe1979);if(_0x45871c[_0x4bb1('0x2f')][_0x4bb1('0x3a')]){_0x59831f[_0x4bb1('0x38')]=_['merge'](_0x59831f['where'],{'$or':_['map'](_0xe1979,function(_0xb600f9){if(_0xb600f9[_0x4bb1('0x3b')]!==_0x4bb1('0x3c')){var _0x65547d={};_0x65547d[_0xb600f9[_0x4bb1('0x28')]]={'$like':'%'+_0x45871c[_0x4bb1('0x2f')][_0x4bb1('0x3a')]+'%'};return _0x65547d;}})});}_0x59831f=_['merge']({},_0x59831f,_0x45871c[_0x4bb1('0x3d')]);var _0x3c4623={'where':_0x59831f[_0x4bb1('0x38')]};return db[_0x4bb1('0x2c')][_0x4bb1('0x1f')](_0x3c4623)[_0x4bb1('0x27')](function(_0x5f33cb){_0x362c56[_0x4bb1('0x1f')]=_0x5f33cb;if(_0x45871c['query'][_0x4bb1('0x3e')]){_0x59831f[_0x4bb1('0x3f')]=[{'all':!![]}];}return db[_0x4bb1('0x2c')][_0x4bb1('0x40')](_0x59831f);})[_0x4bb1('0x27')](function(_0x503fee){_0x362c56[_0x4bb1('0x41')]=_0x503fee;return _0x362c56;})[_0x4bb1('0x27')](respondWithFilteredResult(_0x4ceed5,_0x59831f))[_0x4bb1('0x42')](handleError(_0x4ceed5,null));};exports['show']=function(_0x39b0c9,_0x521c61){var _0x1c294c={'raw':!![],'where':{'id':_0x39b0c9[_0x4bb1('0x43')]['id']}},_0x4f00fb={};_0x4f00fb[_0x4bb1('0x2e')]=_[_0x4bb1('0x30')](db[_0x4bb1('0x2c')]['rawAttributes']);_0x4f00fb[_0x4bb1('0x2f')]=_[_0x4bb1('0x30')](_0x39b0c9[_0x4bb1('0x2f')]);_0x4f00fb['filters']=_[_0x4bb1('0x31')](_0x4f00fb[_0x4bb1('0x2e')],_0x4f00fb[_0x4bb1('0x2f')]);_0x1c294c[_0x4bb1('0x33')]=_[_0x4bb1('0x31')](_0x4f00fb[_0x4bb1('0x2e')],qs['fields'](_0x39b0c9[_0x4bb1('0x2f')]['fields']));_0x1c294c['attributes']=_0x1c294c[_0x4bb1('0x33')][_0x4bb1('0x44')]?_0x1c294c[_0x4bb1('0x33')]:_0x4f00fb[_0x4bb1('0x2e')];if(_0x39b0c9[_0x4bb1('0x2f')]['includeAll']){_0x1c294c['include']=[{'all':!![]}];}_0x1c294c=_[_0x4bb1('0x45')]({},_0x1c294c,_0x39b0c9[_0x4bb1('0x3d')]);return db[_0x4bb1('0x2c')][_0x4bb1('0x46')](_0x1c294c)['then'](handleEntityNotFound(_0x521c61,null))['then'](respondWithResult(_0x521c61,null))[_0x4bb1('0x42')](handleError(_0x521c61,null));};exports[_0x4bb1('0x47')]=function(_0x275d8b,_0x375155){return db[_0x4bb1('0x2c')][_0x4bb1('0x47')](_0x275d8b['body'],{})[_0x4bb1('0x27')](function(_0x1478c1){var _0x25e757=_0x275d8b[_0x4bb1('0x48')]['get']({'plain':!![]});if(!_0x25e757)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x25e757[_0x4bb1('0x49')]===_0x4bb1('0x48')){var _0x47db93=_0x1478c1[_0x4bb1('0x24')]({'plain':!![]});var _0x314606=_0x4bb1('0x4a');return db[_0x4bb1('0x4b')]['find']({'where':{'name':_0x314606,'userProfileId':_0x25e757[_0x4bb1('0x4c')]},'raw':!![]})[_0x4bb1('0x27')](function(_0x560ae0){if(_0x560ae0&&_0x560ae0['autoAssociation']===0x0){return db[_0x4bb1('0x25')]['create']({'name':_0x47db93[_0x4bb1('0x28')],'resourceId':_0x47db93['id'],'type':_0x560ae0['name'],'sectionId':_0x560ae0['id']},{})[_0x4bb1('0x27')](function(){return _0x1478c1;});}else{return _0x1478c1;}})[_0x4bb1('0x42')](function(_0x59c05d){logger['error'](_0x4bb1('0x4d'),_0x59c05d);throw _0x59c05d;});}return _0x1478c1;})['then'](respondWithResult(_0x375155,0xc9))[_0x4bb1('0x42')](handleError(_0x375155,null));};exports['update']=function(_0x4befbb,_0x372cb8){if(_0x4befbb[_0x4bb1('0x4e')]['id']){delete _0x4befbb[_0x4bb1('0x4e')]['id'];}return db[_0x4bb1('0x2c')][_0x4bb1('0x46')]({'where':{'id':_0x4befbb[_0x4bb1('0x43')]['id']}})[_0x4bb1('0x27')](handleEntityNotFound(_0x372cb8,null))['then'](saveUpdates(_0x4befbb[_0x4bb1('0x4e')],null))[_0x4bb1('0x27')](respondWithResult(_0x372cb8,null))[_0x4bb1('0x42')](handleError(_0x372cb8,null));};exports['destroy']=function(_0x6c4588,_0x56f94a){return db['Variable'][_0x4bb1('0x46')]({'where':{'id':_0x6c4588[_0x4bb1('0x43')]['id']}})['then'](handleEntityNotFound(_0x56f94a,null))[_0x4bb1('0x27')](removeEntity(_0x56f94a,null))['catch'](handleError(_0x56f94a,null));};