4547328ccd21740f0eb668c97ad222d1108869e2
[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 _0x27ff=['keys','filters','intersection','fields','attributes','length','nolimit','order','sort','pick','where','merge','filter','options','includeAll','include','findAll','rows','params','find','create','body','user','UserProfileSection','userProfileId','catch','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','eml-format','rimraf','zip-dir','moment','mustache','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','to-csv','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','undefined','limit','count','Content-Range','apply','save','then','destroy','get','Variables','UserProfileResource','error','stack','name','send','index','map','Variable','rawAttributes','fieldName','type','key','model','query'];(function(_0x1789c3,_0x29b7d4){var _0x16f236=function(_0x5e1006){while(--_0x5e1006){_0x1789c3['push'](_0x1789c3['shift']());}};_0x16f236(++_0x29b7d4);}(_0x27ff,0x158));var _0xf27f=function(_0x3b5f66,_0x1dbfc5){_0x3b5f66=_0x3b5f66-0x0;var _0x4aa1a8=_0x27ff[_0x3b5f66];return _0x4aa1a8;};'use strict';var emlformat=require(_0xf27f('0x0'));var rimraf=require(_0xf27f('0x1'));var zipdir=require(_0xf27f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xf27f('0x3'));var BPromise=require('bluebird');var Mustache=require(_0xf27f('0x4'));var util=require('util');var path=require(_0xf27f('0x5'));var sox=require(_0xf27f('0x6'));var csv=require('to-csv');var ejs=require(_0xf27f('0x7'));var fs=require('fs');var fs_extra=require(_0xf27f('0x8'));var _=require(_0xf27f('0x9'));var squel=require(_0xf27f('0xa'));var crypto=require(_0xf27f('0xb'));var jsforce=require(_0xf27f('0xc'));var deskjs=require('desk.js');var toCsv=require(_0xf27f('0xd'));var querystring=require(_0xf27f('0xe'));var Papa=require('papaparse');var Redis=require(_0xf27f('0xf'));var authService=require(_0xf27f('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0xf27f('0x11'));var hardwareService=require(_0xf27f('0x12'));var logger=require(_0xf27f('0x13'))('api');var utils=require('../../config/utils');var config=require(_0xf27f('0x14'));var licenseUtil=require(_0xf27f('0x15'));var db=require(_0xf27f('0x16'))['db'];function respondWithStatusCode(_0x47ddfa,_0x3f0b48){_0x3f0b48=_0x3f0b48||0xcc;return function(_0xfa50e7){if(_0xfa50e7){return _0x47ddfa[_0xf27f('0x17')](_0x3f0b48);}return _0x47ddfa[_0xf27f('0x18')](_0x3f0b48)['end']();};}function respondWithResult(_0x4a9fdb,_0x3f2b5e){_0x3f2b5e=_0x3f2b5e||0xc8;return function(_0xbe0d81){if(_0xbe0d81){return _0x4a9fdb[_0xf27f('0x18')](_0x3f2b5e)[_0xf27f('0x19')](_0xbe0d81);}};}function respondWithFilteredResult(_0x4a5b08,_0x49d836){return function(_0x1dc9f1){if(_0x1dc9f1){var _0x90ad4c=typeof _0x49d836[_0xf27f('0x1a')]===_0xf27f('0x1b')&&typeof _0x49d836[_0xf27f('0x1c')]===_0xf27f('0x1b');var _0x21538d=_0x1dc9f1[_0xf27f('0x1d')];var _0x1f40fa=_0x90ad4c?0x0:_0x49d836[_0xf27f('0x1a')];var _0x28e4b6=_0x90ad4c?_0x1dc9f1[_0xf27f('0x1d')]:_0x49d836[_0xf27f('0x1a')]+_0x49d836[_0xf27f('0x1c')];var _0xa383e9;if(_0x28e4b6>=_0x21538d){_0x28e4b6=_0x21538d;_0xa383e9=0xc8;}else{_0xa383e9=0xce;}_0x4a5b08['status'](_0xa383e9);return _0x4a5b08['set'](_0xf27f('0x1e'),_0x1f40fa+'-'+_0x28e4b6+'/'+_0x21538d)[_0xf27f('0x19')](_0x1dc9f1);}return null;};}function patchUpdates(_0x359816){return function(_0x3957ad){try{jsonpatch[_0xf27f('0x1f')](_0x3957ad,_0x359816,!![]);}catch(_0x56c2aa){return BPromise['reject'](_0x56c2aa);}return _0x3957ad[_0xf27f('0x20')]();};}function saveUpdates(_0x15130d,_0x2a7217){return function(_0x1b815c){if(_0x1b815c){return _0x1b815c['update'](_0x15130d)[_0xf27f('0x21')](function(_0x38214a){return _0x38214a;});}return null;};}function removeEntity(_0x2ac8c3,_0x2d9087){return function(_0x5bcf09){if(_0x5bcf09){return _0x5bcf09[_0xf27f('0x22')]()['then'](function(){var _0x1e0d17=_0x5bcf09[_0xf27f('0x23')]({'plain':!![]});var _0x18bea0=_0xf27f('0x24');return db[_0xf27f('0x25')][_0xf27f('0x22')]({'where':{'type':_0x18bea0,'resourceId':_0x1e0d17['id']}})[_0xf27f('0x21')](function(){return _0x5bcf09;});})[_0xf27f('0x21')](function(){_0x2ac8c3[_0xf27f('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4a07cb,_0x5aea24){return function(_0x40d7e1){if(!_0x40d7e1){_0x4a07cb[_0xf27f('0x17')](0x194);}return _0x40d7e1;};}function handleError(_0x313b21,_0x2d7256){_0x2d7256=_0x2d7256||0x1f4;return function(_0x36803e){logger[_0xf27f('0x26')](_0x36803e[_0xf27f('0x27')]);if(_0x36803e['name']){delete _0x36803e[_0xf27f('0x28')];}_0x313b21['status'](_0x2d7256)[_0xf27f('0x29')](_0x36803e);};}exports[_0xf27f('0x2a')]=function(_0x347555,_0x79ac22){var _0x5d47b8={},_0x2353a3={},_0x2f20e9={'count':0x0,'rows':[]};var _0x2bf4bd=_[_0xf27f('0x2b')](db[_0xf27f('0x2c')][_0xf27f('0x2d')],function(_0x5e89f3){return{'name':_0x5e89f3[_0xf27f('0x2e')],'type':_0x5e89f3[_0xf27f('0x2f')][_0xf27f('0x30')]};});_0x2353a3[_0xf27f('0x31')]=_['map'](_0x2bf4bd,'name');_0x2353a3[_0xf27f('0x32')]=_[_0xf27f('0x33')](_0x347555['query']);_0x2353a3[_0xf27f('0x34')]=_[_0xf27f('0x35')](_0x2353a3['model'],_0x2353a3['query']);_0x5d47b8['attributes']=_[_0xf27f('0x35')](_0x2353a3[_0xf27f('0x31')],qs[_0xf27f('0x36')](_0x347555[_0xf27f('0x32')][_0xf27f('0x36')]));_0x5d47b8[_0xf27f('0x37')]=_0x5d47b8[_0xf27f('0x37')][_0xf27f('0x38')]?_0x5d47b8[_0xf27f('0x37')]:_0x2353a3['model'];if(!_0x347555['query']['hasOwnProperty'](_0xf27f('0x39'))){_0x5d47b8[_0xf27f('0x1c')]=qs['limit'](_0x347555['query'][_0xf27f('0x1c')]);_0x5d47b8['offset']=qs[_0xf27f('0x1a')](_0x347555['query'][_0xf27f('0x1a')]);}_0x5d47b8[_0xf27f('0x3a')]=qs[_0xf27f('0x3b')](_0x347555[_0xf27f('0x32')]['sort']);_0x5d47b8['where']=qs['filters'](_[_0xf27f('0x3c')](_0x347555['query'],_0x2353a3[_0xf27f('0x34')]),_0x2bf4bd);if(_0x347555[_0xf27f('0x32')]['filter']){_0x5d47b8[_0xf27f('0x3d')]=_[_0xf27f('0x3e')](_0x5d47b8[_0xf27f('0x3d')],{'$or':_[_0xf27f('0x2b')](_0x2bf4bd,function(_0x5098ae){if(_0x5098ae[_0xf27f('0x2f')]!=='VIRTUAL'){var _0x24e550={};_0x24e550[_0x5098ae[_0xf27f('0x28')]]={'$like':'%'+_0x347555[_0xf27f('0x32')][_0xf27f('0x3f')]+'%'};return _0x24e550;}})});}_0x5d47b8=_[_0xf27f('0x3e')]({},_0x5d47b8,_0x347555[_0xf27f('0x40')]);var _0x1245c6={'where':_0x5d47b8['where']};return db['Variable'][_0xf27f('0x1d')](_0x1245c6)[_0xf27f('0x21')](function(_0x2c2484){_0x2f20e9[_0xf27f('0x1d')]=_0x2c2484;if(_0x347555['query'][_0xf27f('0x41')]){_0x5d47b8[_0xf27f('0x42')]=[{'all':!![]}];}return db[_0xf27f('0x2c')][_0xf27f('0x43')](_0x5d47b8);})['then'](function(_0x4e4750){_0x2f20e9[_0xf27f('0x44')]=_0x4e4750;return _0x2f20e9;})[_0xf27f('0x21')](respondWithFilteredResult(_0x79ac22,_0x5d47b8))['catch'](handleError(_0x79ac22,null));};exports['show']=function(_0x12c119,_0x3ae450){var _0x2e673a={'raw':!![],'where':{'id':_0x12c119[_0xf27f('0x45')]['id']}},_0x15b558={};_0x15b558[_0xf27f('0x31')]=_[_0xf27f('0x33')](db['Variable'][_0xf27f('0x2d')]);_0x15b558[_0xf27f('0x32')]=_['keys'](_0x12c119[_0xf27f('0x32')]);_0x15b558[_0xf27f('0x34')]=_[_0xf27f('0x35')](_0x15b558[_0xf27f('0x31')],_0x15b558[_0xf27f('0x32')]);_0x2e673a[_0xf27f('0x37')]=_['intersection'](_0x15b558[_0xf27f('0x31')],qs[_0xf27f('0x36')](_0x12c119[_0xf27f('0x32')][_0xf27f('0x36')]));_0x2e673a[_0xf27f('0x37')]=_0x2e673a[_0xf27f('0x37')]['length']?_0x2e673a[_0xf27f('0x37')]:_0x15b558['model'];if(_0x12c119[_0xf27f('0x32')][_0xf27f('0x41')]){_0x2e673a[_0xf27f('0x42')]=[{'all':!![]}];}_0x2e673a=_[_0xf27f('0x3e')]({},_0x2e673a,_0x12c119[_0xf27f('0x40')]);return db[_0xf27f('0x2c')][_0xf27f('0x46')](_0x2e673a)['then'](handleEntityNotFound(_0x3ae450,null))[_0xf27f('0x21')](respondWithResult(_0x3ae450,null))['catch'](handleError(_0x3ae450,null));};exports[_0xf27f('0x47')]=function(_0x2637a4,_0x2f2613){return db['Variable'][_0xf27f('0x47')](_0x2637a4[_0xf27f('0x48')],{})[_0xf27f('0x21')](function(_0x1310de){var _0x56a58c=_0x2637a4[_0xf27f('0x49')]['get']({'plain':!![]});if(!_0x56a58c)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x56a58c['role']===_0xf27f('0x49')){var _0x391a80=_0x1310de['get']({'plain':!![]});var _0x1b4d72=_0xf27f('0x24');return db[_0xf27f('0x4a')][_0xf27f('0x46')]({'where':{'name':_0x1b4d72,'userProfileId':_0x56a58c[_0xf27f('0x4b')]},'raw':!![]})[_0xf27f('0x21')](function(_0x402408){if(_0x402408&&_0x402408['autoAssociation']===0x0){return db['UserProfileResource'][_0xf27f('0x47')]({'name':_0x391a80[_0xf27f('0x28')],'resourceId':_0x391a80['id'],'type':_0x402408[_0xf27f('0x28')],'sectionId':_0x402408['id']},{})[_0xf27f('0x21')](function(){return _0x1310de;});}else{return _0x1310de;}})[_0xf27f('0x4c')](function(_0x1e5483){logger[_0xf27f('0x26')](_0xf27f('0x4d'),_0x1e5483);throw _0x1e5483;});}return _0x1310de;})[_0xf27f('0x21')](respondWithResult(_0x2f2613,0xc9))[_0xf27f('0x4c')](handleError(_0x2f2613,null));};exports[_0xf27f('0x4e')]=function(_0x573e61,_0x2ef2f1){if(_0x573e61['body']['id']){delete _0x573e61[_0xf27f('0x48')]['id'];}return db[_0xf27f('0x2c')]['find']({'where':{'id':_0x573e61[_0xf27f('0x45')]['id']}})[_0xf27f('0x21')](handleEntityNotFound(_0x2ef2f1,null))['then'](saveUpdates(_0x573e61[_0xf27f('0x48')],null))[_0xf27f('0x21')](respondWithResult(_0x2ef2f1,null))['catch'](handleError(_0x2ef2f1,null));};exports[_0xf27f('0x22')]=function(_0x92cff9,_0x4cd33c){return db[_0xf27f('0x2c')]['find']({'where':{'id':_0x92cff9['params']['id']}})[_0xf27f('0x21')](handleEntityNotFound(_0x4cd33c,null))['then'](removeEntity(_0x4cd33c,null))[_0xf27f('0x4c')](handleError(_0x4cd33c,null));};