41c502428eae0373048a215069f1854f4ea635d8
[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 _0xab39=['userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','eml-format','rimraf','moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','api','../../config/utils','../../mysqldb','sendStatus','status','end','json','undefined','limit','count','offset','Content-Range','apply','reject','update','then','destroy','get','Variables','UserProfileResource','stack','name','send','index','fieldName','type','key','model','map','query','filters','intersection','fields','attributes','length','nolimit','order','sort','pick','filter','where','VIRTUAL','merge','options','includeAll','include','Variable','findAll','catch','params','keys','rawAttributes','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','find'];(function(_0xb0b922,_0x183826){var _0x5dc41e=function(_0x461183){while(--_0x461183){_0xb0b922['push'](_0xb0b922['shift']());}};_0x5dc41e(++_0x183826);}(_0xab39,0xf1));var _0x9ab3=function(_0x5bd233,_0x101d88){_0x5bd233=_0x5bd233-0x0;var _0x2febde=_0xab39[_0x5bd233];return _0x2febde;};'use strict';var emlformat=require(_0x9ab3('0x0'));var rimraf=require(_0x9ab3('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x9ab3('0x2'));var BPromise=require(_0x9ab3('0x3'));var Mustache=require(_0x9ab3('0x4'));var util=require('util');var path=require(_0x9ab3('0x5'));var sox=require(_0x9ab3('0x6'));var csv=require(_0x9ab3('0x7'));var ejs=require(_0x9ab3('0x8'));var fs=require('fs');var fs_extra=require(_0x9ab3('0x9'));var _=require(_0x9ab3('0xa'));var squel=require(_0x9ab3('0xb'));var crypto=require('crypto');var jsforce=require(_0x9ab3('0xc'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x9ab3('0xd'));var Papa=require('papaparse');var Redis=require(_0x9ab3('0xe'));var authService=require(_0x9ab3('0xf'));var qs=require(_0x9ab3('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x9ab3('0x11'));var logger=require('../../config/logger')(_0x9ab3('0x12'));var utils=require(_0x9ab3('0x13'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0x9ab3('0x14'))['db'];function respondWithStatusCode(_0x90d4d5,_0xa4eeae){_0xa4eeae=_0xa4eeae||0xcc;return function(_0x470274){if(_0x470274){return _0x90d4d5[_0x9ab3('0x15')](_0xa4eeae);}return _0x90d4d5[_0x9ab3('0x16')](_0xa4eeae)[_0x9ab3('0x17')]();};}function respondWithResult(_0x879e2a,_0x393d46){_0x393d46=_0x393d46||0xc8;return function(_0x212f01){if(_0x212f01){return _0x879e2a['status'](_0x393d46)[_0x9ab3('0x18')](_0x212f01);}};}function respondWithFilteredResult(_0x1276be,_0xc60689){return function(_0x37e6e1){if(_0x37e6e1){var _0x29b629=typeof _0xc60689['offset']===_0x9ab3('0x19')&&typeof _0xc60689[_0x9ab3('0x1a')]===_0x9ab3('0x19');var _0x51fc75=_0x37e6e1[_0x9ab3('0x1b')];var _0x1363f0=_0x29b629?0x0:_0xc60689[_0x9ab3('0x1c')];var _0x5db12e=_0x29b629?_0x37e6e1['count']:_0xc60689[_0x9ab3('0x1c')]+_0xc60689[_0x9ab3('0x1a')];var _0x520d86;if(_0x5db12e>=_0x51fc75){_0x5db12e=_0x51fc75;_0x520d86=0xc8;}else{_0x520d86=0xce;}_0x1276be[_0x9ab3('0x16')](_0x520d86);return _0x1276be['set'](_0x9ab3('0x1d'),_0x1363f0+'-'+_0x5db12e+'/'+_0x51fc75)[_0x9ab3('0x18')](_0x37e6e1);}return null;};}function patchUpdates(_0x5443fd){return function(_0x567c92){try{jsonpatch[_0x9ab3('0x1e')](_0x567c92,_0x5443fd,!![]);}catch(_0x20fcf2){return BPromise[_0x9ab3('0x1f')](_0x20fcf2);}return _0x567c92['save']();};}function saveUpdates(_0x5125a1,_0x445097){return function(_0x467d24){if(_0x467d24){return _0x467d24[_0x9ab3('0x20')](_0x5125a1)[_0x9ab3('0x21')](function(_0x56161e){return _0x56161e;});}return null;};}function removeEntity(_0x455e70,_0x51e730){return function(_0x4ac5b3){if(_0x4ac5b3){return _0x4ac5b3[_0x9ab3('0x22')]()[_0x9ab3('0x21')](function(){var _0x5d1a3c=_0x4ac5b3[_0x9ab3('0x23')]({'plain':!![]});var _0x3243f4=_0x9ab3('0x24');return db[_0x9ab3('0x25')][_0x9ab3('0x22')]({'where':{'type':_0x3243f4,'resourceId':_0x5d1a3c['id']}})['then'](function(){return _0x4ac5b3;});})[_0x9ab3('0x21')](function(){_0x455e70[_0x9ab3('0x16')](0xcc)[_0x9ab3('0x17')]();});}};}function handleEntityNotFound(_0x2dbd71,_0x4b485a){return function(_0x4ce5a4){if(!_0x4ce5a4){_0x2dbd71[_0x9ab3('0x15')](0x194);}return _0x4ce5a4;};}function handleError(_0x21b707,_0x49b9e3){_0x49b9e3=_0x49b9e3||0x1f4;return function(_0x213e72){logger['error'](_0x213e72[_0x9ab3('0x26')]);if(_0x213e72[_0x9ab3('0x27')]){delete _0x213e72[_0x9ab3('0x27')];}_0x21b707[_0x9ab3('0x16')](_0x49b9e3)[_0x9ab3('0x28')](_0x213e72);};}exports[_0x9ab3('0x29')]=function(_0x5c1734,_0x5322f2){var _0x112c6c={},_0x524981={},_0x5b26f1={'count':0x0,'rows':[]};var _0xb5662c=_['map'](db['Variable']['rawAttributes'],function(_0x36086f){return{'name':_0x36086f[_0x9ab3('0x2a')],'type':_0x36086f[_0x9ab3('0x2b')][_0x9ab3('0x2c')]};});_0x524981[_0x9ab3('0x2d')]=_[_0x9ab3('0x2e')](_0xb5662c,'name');_0x524981[_0x9ab3('0x2f')]=_['keys'](_0x5c1734[_0x9ab3('0x2f')]);_0x524981[_0x9ab3('0x30')]=_[_0x9ab3('0x31')](_0x524981[_0x9ab3('0x2d')],_0x524981[_0x9ab3('0x2f')]);_0x112c6c['attributes']=_[_0x9ab3('0x31')](_0x524981['model'],qs[_0x9ab3('0x32')](_0x5c1734[_0x9ab3('0x2f')][_0x9ab3('0x32')]));_0x112c6c[_0x9ab3('0x33')]=_0x112c6c[_0x9ab3('0x33')][_0x9ab3('0x34')]?_0x112c6c[_0x9ab3('0x33')]:_0x524981[_0x9ab3('0x2d')];if(!_0x5c1734[_0x9ab3('0x2f')]['hasOwnProperty'](_0x9ab3('0x35'))){_0x112c6c[_0x9ab3('0x1a')]=qs[_0x9ab3('0x1a')](_0x5c1734[_0x9ab3('0x2f')][_0x9ab3('0x1a')]);_0x112c6c[_0x9ab3('0x1c')]=qs[_0x9ab3('0x1c')](_0x5c1734['query'][_0x9ab3('0x1c')]);}_0x112c6c[_0x9ab3('0x36')]=qs['sort'](_0x5c1734[_0x9ab3('0x2f')][_0x9ab3('0x37')]);_0x112c6c['where']=qs[_0x9ab3('0x30')](_[_0x9ab3('0x38')](_0x5c1734[_0x9ab3('0x2f')],_0x524981[_0x9ab3('0x30')]),_0xb5662c);if(_0x5c1734[_0x9ab3('0x2f')][_0x9ab3('0x39')]){_0x112c6c[_0x9ab3('0x3a')]=_['merge'](_0x112c6c['where'],{'$or':_[_0x9ab3('0x2e')](_0xb5662c,function(_0x5969d1){if(_0x5969d1[_0x9ab3('0x2b')]!==_0x9ab3('0x3b')){var _0x7f6ab7={};_0x7f6ab7[_0x5969d1[_0x9ab3('0x27')]]={'$like':'%'+_0x5c1734[_0x9ab3('0x2f')][_0x9ab3('0x39')]+'%'};return _0x7f6ab7;}})});}_0x112c6c=_[_0x9ab3('0x3c')]({},_0x112c6c,_0x5c1734[_0x9ab3('0x3d')]);var _0x216db8={'where':_0x112c6c[_0x9ab3('0x3a')]};return db['Variable'][_0x9ab3('0x1b')](_0x216db8)[_0x9ab3('0x21')](function(_0x1334f2){_0x5b26f1['count']=_0x1334f2;if(_0x5c1734['query'][_0x9ab3('0x3e')]){_0x112c6c[_0x9ab3('0x3f')]=[{'all':!![]}];}return db[_0x9ab3('0x40')][_0x9ab3('0x41')](_0x112c6c);})[_0x9ab3('0x21')](function(_0x3ececb){_0x5b26f1['rows']=_0x3ececb;return _0x5b26f1;})[_0x9ab3('0x21')](respondWithFilteredResult(_0x5322f2,_0x112c6c))[_0x9ab3('0x42')](handleError(_0x5322f2,null));};exports['show']=function(_0x17692b,_0x50a03e){var _0xf21bb2={'raw':!![],'where':{'id':_0x17692b[_0x9ab3('0x43')]['id']}},_0x4f557d={};_0x4f557d['model']=_[_0x9ab3('0x44')](db['Variable'][_0x9ab3('0x45')]);_0x4f557d['query']=_['keys'](_0x17692b['query']);_0x4f557d[_0x9ab3('0x30')]=_[_0x9ab3('0x31')](_0x4f557d[_0x9ab3('0x2d')],_0x4f557d['query']);_0xf21bb2[_0x9ab3('0x33')]=_['intersection'](_0x4f557d['model'],qs[_0x9ab3('0x32')](_0x17692b[_0x9ab3('0x2f')][_0x9ab3('0x32')]));_0xf21bb2['attributes']=_0xf21bb2['attributes']['length']?_0xf21bb2[_0x9ab3('0x33')]:_0x4f557d[_0x9ab3('0x2d')];if(_0x17692b[_0x9ab3('0x2f')][_0x9ab3('0x3e')]){_0xf21bb2[_0x9ab3('0x3f')]=[{'all':!![]}];}_0xf21bb2=_[_0x9ab3('0x3c')]({},_0xf21bb2,_0x17692b[_0x9ab3('0x3d')]);return db[_0x9ab3('0x40')]['find'](_0xf21bb2)[_0x9ab3('0x21')](handleEntityNotFound(_0x50a03e,null))[_0x9ab3('0x21')](respondWithResult(_0x50a03e,null))[_0x9ab3('0x42')](handleError(_0x50a03e,null));};exports[_0x9ab3('0x46')]=function(_0x38b3df,_0x12997e){return db[_0x9ab3('0x40')][_0x9ab3('0x46')](_0x38b3df['body'],{})[_0x9ab3('0x21')](function(_0x242f62){var _0x4b2b87=_0x38b3df[_0x9ab3('0x47')][_0x9ab3('0x23')]({'plain':!![]});if(!_0x4b2b87)throw new Error(_0x9ab3('0x48'));if(_0x4b2b87[_0x9ab3('0x49')]===_0x9ab3('0x47')){var _0x32b8c4=_0x242f62['get']({'plain':!![]});var _0x59b5a3='Variables';return db['UserProfileSection'][_0x9ab3('0x4a')]({'where':{'name':_0x59b5a3,'userProfileId':_0x4b2b87[_0x9ab3('0x4b')]},'raw':!![]})[_0x9ab3('0x21')](function(_0x24e4e7){if(_0x24e4e7&&_0x24e4e7[_0x9ab3('0x4c')]===0x0){return db[_0x9ab3('0x25')]['create']({'name':_0x32b8c4['name'],'resourceId':_0x32b8c4['id'],'type':_0x24e4e7[_0x9ab3('0x27')],'sectionId':_0x24e4e7['id']},{})[_0x9ab3('0x21')](function(){return _0x242f62;});}else{return _0x242f62;}})[_0x9ab3('0x42')](function(_0x445ec9){logger['error'](_0x9ab3('0x4d'),_0x445ec9);throw _0x445ec9;});}return _0x242f62;})[_0x9ab3('0x21')](respondWithResult(_0x12997e,0xc9))[_0x9ab3('0x42')](handleError(_0x12997e,null));};exports[_0x9ab3('0x20')]=function(_0x4c6492,_0x5f3d9f){if(_0x4c6492['body']['id']){delete _0x4c6492[_0x9ab3('0x4e')]['id'];}return db['Variable'][_0x9ab3('0x4a')]({'where':{'id':_0x4c6492['params']['id']}})[_0x9ab3('0x21')](handleEntityNotFound(_0x5f3d9f,null))[_0x9ab3('0x21')](saveUpdates(_0x4c6492[_0x9ab3('0x4e')],null))['then'](respondWithResult(_0x5f3d9f,null))[_0x9ab3('0x42')](handleError(_0x5f3d9f,null));};exports[_0x9ab3('0x22')]=function(_0x525a9d,_0x561086){return db[_0x9ab3('0x40')][_0x9ab3('0x4a')]({'where':{'id':_0x525a9d['params']['id']}})['then'](handleEntityNotFound(_0x561086,null))[_0x9ab3('0x21')](removeEntity(_0x561086,null))[_0x9ab3('0x42')](handleError(_0x561086,null));};