139a6326cdc2f4f33731eb2a07fabca6295c7192
[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 _0xb507=['findAll','rows','catch','show','params','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','Variables','UserProfileSection','find','autoAssociation','create','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','eml-format','rimraf','fast-json-patch','request-promise','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','desk.js','querystring','papaparse','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','apply','reject','save','then','destroy','get','UserProfileResource','error','stack','name','send','index','map','Variable','rawAttributes','type','keys','filters','intersection','query','attributes','model','fields','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','VIRTUAL','options','includeAll','include'];(function(_0x20350f,_0x3ff27a){var _0x5e5c9c=function(_0xc06fbd){while(--_0xc06fbd){_0x20350f['push'](_0x20350f['shift']());}};_0x5e5c9c(++_0x3ff27a);}(_0xb507,0x103));var _0x7b50=function(_0x448774,_0x45ddc7){_0x448774=_0x448774-0x0;var _0x46b2f6=_0xb507[_0x448774];return _0x46b2f6;};'use strict';var emlformat=require(_0x7b50('0x0'));var rimraf=require(_0x7b50('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x7b50('0x2'));var rp=require(_0x7b50('0x3'));var moment=require('moment');var BPromise=require(_0x7b50('0x4'));var Mustache=require(_0x7b50('0x5'));var util=require('util');var path=require(_0x7b50('0x6'));var sox=require(_0x7b50('0x7'));var csv=require(_0x7b50('0x8'));var ejs=require(_0x7b50('0x9'));var fs=require('fs');var fs_extra=require(_0x7b50('0xa'));var _=require(_0x7b50('0xb'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x7b50('0xc'));var toCsv=require('to-csv');var querystring=require(_0x7b50('0xd'));var Papa=require(_0x7b50('0xe'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x7b50('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x7b50('0x10'));var logger=require(_0x7b50('0x11'))('api');var utils=require(_0x7b50('0x12'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x4e9151,_0x1acc6d){_0x1acc6d=_0x1acc6d||0xcc;return function(_0x1d93d9){if(_0x1d93d9){return _0x4e9151[_0x7b50('0x13')](_0x1acc6d);}return _0x4e9151[_0x7b50('0x14')](_0x1acc6d)[_0x7b50('0x15')]();};}function respondWithResult(_0x2525d4,_0x51556e){_0x51556e=_0x51556e||0xc8;return function(_0x14ea4c){if(_0x14ea4c){return _0x2525d4[_0x7b50('0x14')](_0x51556e)[_0x7b50('0x16')](_0x14ea4c);}};}function respondWithFilteredResult(_0x285a84,_0xc05353){return function(_0x1d7dc5){if(_0x1d7dc5){var _0x10e03b=typeof _0xc05353[_0x7b50('0x17')]===_0x7b50('0x18')&&typeof _0xc05353['limit']===_0x7b50('0x18');var _0x536ab3=_0x1d7dc5[_0x7b50('0x19')];var _0x2a26df=_0x10e03b?0x0:_0xc05353[_0x7b50('0x17')];var _0x31e8bc=_0x10e03b?_0x1d7dc5[_0x7b50('0x19')]:_0xc05353[_0x7b50('0x17')]+_0xc05353[_0x7b50('0x1a')];var _0x1818fe;if(_0x31e8bc>=_0x536ab3){_0x31e8bc=_0x536ab3;_0x1818fe=0xc8;}else{_0x1818fe=0xce;}_0x285a84[_0x7b50('0x14')](_0x1818fe);return _0x285a84[_0x7b50('0x1b')](_0x7b50('0x1c'),_0x2a26df+'-'+_0x31e8bc+'/'+_0x536ab3)[_0x7b50('0x16')](_0x1d7dc5);}return null;};}function patchUpdates(_0x26c076){return function(_0x5d0579){try{jsonpatch[_0x7b50('0x1d')](_0x5d0579,_0x26c076,!![]);}catch(_0x132b6a){return BPromise[_0x7b50('0x1e')](_0x132b6a);}return _0x5d0579[_0x7b50('0x1f')]();};}function saveUpdates(_0x23404c,_0xccd49c){return function(_0x3ea50e){if(_0x3ea50e){return _0x3ea50e['update'](_0x23404c)[_0x7b50('0x20')](function(_0x4ddc60){return _0x4ddc60;});}return null;};}function removeEntity(_0x4346f3,_0x42b1f6){return function(_0x5ea0c3){if(_0x5ea0c3){return _0x5ea0c3[_0x7b50('0x21')]()[_0x7b50('0x20')](function(){var _0x4bb96a=_0x5ea0c3[_0x7b50('0x22')]({'plain':!![]});var _0x1462fe='Variables';return db[_0x7b50('0x23')]['destroy']({'where':{'type':_0x1462fe,'resourceId':_0x4bb96a['id']}})[_0x7b50('0x20')](function(){return _0x5ea0c3;});})['then'](function(){_0x4346f3[_0x7b50('0x14')](0xcc)[_0x7b50('0x15')]();});}};}function handleEntityNotFound(_0x6c3510,_0x142fe9){return function(_0x2579ec){if(!_0x2579ec){_0x6c3510[_0x7b50('0x13')](0x194);}return _0x2579ec;};}function handleError(_0x3bec01,_0x590321){_0x590321=_0x590321||0x1f4;return function(_0x56813a){logger[_0x7b50('0x24')](_0x56813a[_0x7b50('0x25')]);if(_0x56813a[_0x7b50('0x26')]){delete _0x56813a[_0x7b50('0x26')];}_0x3bec01['status'](_0x590321)[_0x7b50('0x27')](_0x56813a);};}exports[_0x7b50('0x28')]=function(_0x1d3a57,_0x2bd89a){var _0x4919ea={},_0x15b819={},_0x1b03c0={'count':0x0,'rows':[]};var _0x1e8316=_[_0x7b50('0x29')](db[_0x7b50('0x2a')][_0x7b50('0x2b')],function(_0x53c5d9){return{'name':_0x53c5d9['fieldName'],'type':_0x53c5d9[_0x7b50('0x2c')]['key']};});_0x15b819['model']=_[_0x7b50('0x29')](_0x1e8316,'name');_0x15b819['query']=_[_0x7b50('0x2d')](_0x1d3a57['query']);_0x15b819[_0x7b50('0x2e')]=_[_0x7b50('0x2f')](_0x15b819['model'],_0x15b819[_0x7b50('0x30')]);_0x4919ea[_0x7b50('0x31')]=_[_0x7b50('0x2f')](_0x15b819[_0x7b50('0x32')],qs[_0x7b50('0x33')](_0x1d3a57[_0x7b50('0x30')][_0x7b50('0x33')]));_0x4919ea[_0x7b50('0x31')]=_0x4919ea['attributes'][_0x7b50('0x34')]?_0x4919ea[_0x7b50('0x31')]:_0x15b819[_0x7b50('0x32')];if(!_0x1d3a57[_0x7b50('0x30')][_0x7b50('0x35')](_0x7b50('0x36'))){_0x4919ea[_0x7b50('0x1a')]=qs[_0x7b50('0x1a')](_0x1d3a57[_0x7b50('0x30')][_0x7b50('0x1a')]);_0x4919ea['offset']=qs[_0x7b50('0x17')](_0x1d3a57[_0x7b50('0x30')]['offset']);}_0x4919ea[_0x7b50('0x37')]=qs[_0x7b50('0x38')](_0x1d3a57[_0x7b50('0x30')][_0x7b50('0x38')]);_0x4919ea['where']=qs['filters'](_[_0x7b50('0x39')](_0x1d3a57[_0x7b50('0x30')],_0x15b819[_0x7b50('0x2e')]),_0x1e8316);if(_0x1d3a57[_0x7b50('0x30')][_0x7b50('0x3a')]){_0x4919ea[_0x7b50('0x3b')]=_[_0x7b50('0x3c')](_0x4919ea[_0x7b50('0x3b')],{'$or':_[_0x7b50('0x29')](_0x1e8316,function(_0x1fae99){if(_0x1fae99[_0x7b50('0x2c')]!==_0x7b50('0x3d')){var _0xe6ba4f={};_0xe6ba4f[_0x1fae99[_0x7b50('0x26')]]={'$like':'%'+_0x1d3a57['query'][_0x7b50('0x3a')]+'%'};return _0xe6ba4f;}})});}_0x4919ea=_[_0x7b50('0x3c')]({},_0x4919ea,_0x1d3a57[_0x7b50('0x3e')]);var _0x20c8cf={'where':_0x4919ea[_0x7b50('0x3b')]};return db[_0x7b50('0x2a')][_0x7b50('0x19')](_0x20c8cf)['then'](function(_0x588e26){_0x1b03c0['count']=_0x588e26;if(_0x1d3a57[_0x7b50('0x30')][_0x7b50('0x3f')]){_0x4919ea[_0x7b50('0x40')]=[{'all':!![]}];}return db[_0x7b50('0x2a')][_0x7b50('0x41')](_0x4919ea);})[_0x7b50('0x20')](function(_0x895961){_0x1b03c0[_0x7b50('0x42')]=_0x895961;return _0x1b03c0;})['then'](respondWithFilteredResult(_0x2bd89a,_0x4919ea))[_0x7b50('0x43')](handleError(_0x2bd89a,null));};exports[_0x7b50('0x44')]=function(_0x42df60,_0xd20e84){var _0x273f7a={'raw':!![],'where':{'id':_0x42df60[_0x7b50('0x45')]['id']}},_0x29b924={};_0x29b924[_0x7b50('0x32')]=_[_0x7b50('0x2d')](db[_0x7b50('0x2a')][_0x7b50('0x2b')]);_0x29b924['query']=_[_0x7b50('0x2d')](_0x42df60[_0x7b50('0x30')]);_0x29b924[_0x7b50('0x2e')]=_[_0x7b50('0x2f')](_0x29b924[_0x7b50('0x32')],_0x29b924[_0x7b50('0x30')]);_0x273f7a[_0x7b50('0x31')]=_['intersection'](_0x29b924['model'],qs[_0x7b50('0x33')](_0x42df60[_0x7b50('0x30')][_0x7b50('0x33')]));_0x273f7a[_0x7b50('0x31')]=_0x273f7a['attributes']['length']?_0x273f7a[_0x7b50('0x31')]:_0x29b924[_0x7b50('0x32')];if(_0x42df60[_0x7b50('0x30')][_0x7b50('0x3f')]){_0x273f7a['include']=[{'all':!![]}];}_0x273f7a=_[_0x7b50('0x3c')]({},_0x273f7a,_0x42df60[_0x7b50('0x3e')]);return db[_0x7b50('0x2a')]['find'](_0x273f7a)['then'](handleEntityNotFound(_0xd20e84,null))[_0x7b50('0x20')](respondWithResult(_0xd20e84,null))['catch'](handleError(_0xd20e84,null));};exports['create']=function(_0x5477bb,_0x4d8591){return db['Variable']['create'](_0x5477bb[_0x7b50('0x46')],{})['then'](function(_0x3de4cc){var _0xf2ec72=_0x5477bb['user']['get']({'plain':!![]});if(!_0xf2ec72)throw new Error(_0x7b50('0x47'));if(_0xf2ec72[_0x7b50('0x48')]===_0x7b50('0x49')){var _0x414d56=_0x3de4cc[_0x7b50('0x22')]({'plain':!![]});var _0x3f6ea2=_0x7b50('0x4a');return db[_0x7b50('0x4b')][_0x7b50('0x4c')]({'where':{'name':_0x3f6ea2,'userProfileId':_0xf2ec72['userProfileId']},'raw':!![]})[_0x7b50('0x20')](function(_0x5029c1){if(_0x5029c1&&_0x5029c1[_0x7b50('0x4d')]===0x0){return db[_0x7b50('0x23')][_0x7b50('0x4e')]({'name':_0x414d56['name'],'resourceId':_0x414d56['id'],'type':_0x5029c1[_0x7b50('0x26')],'sectionId':_0x5029c1['id']},{})[_0x7b50('0x20')](function(){return _0x3de4cc;});}else{return _0x3de4cc;}})['catch'](function(_0x539a97){logger[_0x7b50('0x24')](_0x7b50('0x4f'),_0x539a97);throw _0x539a97;});}return _0x3de4cc;})['then'](respondWithResult(_0x4d8591,0xc9))[_0x7b50('0x43')](handleError(_0x4d8591,null));};exports[_0x7b50('0x50')]=function(_0x28c364,_0x19bd92){if(_0x28c364['body']['id']){delete _0x28c364[_0x7b50('0x46')]['id'];}return db[_0x7b50('0x2a')][_0x7b50('0x4c')]({'where':{'id':_0x28c364[_0x7b50('0x45')]['id']}})[_0x7b50('0x20')](handleEntityNotFound(_0x19bd92,null))['then'](saveUpdates(_0x28c364[_0x7b50('0x46')],null))[_0x7b50('0x20')](respondWithResult(_0x19bd92,null))[_0x7b50('0x43')](handleError(_0x19bd92,null));};exports['destroy']=function(_0x6feb6f,_0x523647){return db[_0x7b50('0x2a')]['find']({'where':{'id':_0x6feb6f[_0x7b50('0x45')]['id']}})[_0x7b50('0x20')](handleEntityNotFound(_0x523647,null))['then'](removeEntity(_0x523647,null))[_0x7b50('0x43')](handleError(_0x523647,null));};