Built motion from commit db94841.|2.5.3
[motion2.git] / server / api / tag / tag.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 _0xf253=['intersection','model','attributes','fields','length','nolimit','order','sort','where','filters','filter','merge','VIRTUAL','options','Tag','includeAll','include','findAll','rows','show','find','catch','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','UserProfileResource','update','body','params','html-pdf','eml-format','rimraf','zip-dir','request-promise','moment','util','path','sox','ejs','squel','jsforce','desk.js','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','sendStatus','status','end','count','offset','limit','Content-Range','json','apply','reject','save','destroy','get','Tags','then','error','stack','name','send','index','map','rawAttributes','fieldName','type','key','query','keys'];(function(_0x425bf5,_0x54f880){var _0x36cc05=function(_0x51dffe){while(--_0x51dffe){_0x425bf5['push'](_0x425bf5['shift']());}};_0x36cc05(++_0x54f880);}(_0xf253,0xbb));var _0x3f25=function(_0x322ee0,_0x2e2f29){_0x322ee0=_0x322ee0-0x0;var _0x4f519a=_0xf253[_0x322ee0];return _0x4f519a;};'use strict';var pdf=require(_0x3f25('0x0'));var emlformat=require(_0x3f25('0x1'));var rimraf=require(_0x3f25('0x2'));var zipdir=require(_0x3f25('0x3'));var jsonpatch=require('fast-json-patch');var rp=require(_0x3f25('0x4'));var moment=require(_0x3f25('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x3f25('0x6'));var path=require(_0x3f25('0x7'));var sox=require(_0x3f25('0x8'));var csv=require('to-csv');var ejs=require(_0x3f25('0x9'));var fs=require('fs');var _=require('lodash');var squel=require(_0x3f25('0xa'));var crypto=require('crypto');var jsforce=require(_0x3f25('0xb'));var deskjs=require(_0x3f25('0xc'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x3f25('0xd'));var Redis=require('ioredis');var authService=require(_0x3f25('0xe'));var qs=require('../../components/parsers/qs');var as=require(_0x3f25('0xf'));var hardwareService=require(_0x3f25('0x10'));var logger=require(_0x3f25('0x11'))(_0x3f25('0x12'));var utils=require('../../config/utils');var config=require(_0x3f25('0x13'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2af8a7,_0x25d457){_0x25d457=_0x25d457||0xcc;return function(_0x2abb64){if(_0x2abb64){return _0x2af8a7[_0x3f25('0x14')](_0x25d457);}return _0x2af8a7[_0x3f25('0x15')](_0x25d457)[_0x3f25('0x16')]();};}function respondWithResult(_0x35758f,_0x96f7dc){_0x96f7dc=_0x96f7dc||0xc8;return function(_0x24dad0){if(_0x24dad0){return _0x35758f[_0x3f25('0x15')](_0x96f7dc)['json'](_0x24dad0);}};}function respondWithFilteredResult(_0x150138,_0x301208){return function(_0x4be587){if(_0x4be587){var _0x27f0e4=_0x4be587[_0x3f25('0x17')],_0x273571=_0x301208[_0x3f25('0x18')],_0x3c86fc=_0x301208[_0x3f25('0x18')]+_0x301208[_0x3f25('0x19')],_0x30c371;if(_0x3c86fc>=_0x27f0e4){_0x3c86fc=_0x27f0e4;_0x30c371=0xc8;}else{_0x30c371=0xce;}_0x150138[_0x3f25('0x15')](_0x30c371);return _0x150138['set'](_0x3f25('0x1a'),_0x273571+'-'+_0x3c86fc+'/'+_0x27f0e4)[_0x3f25('0x1b')](_0x4be587);}return null;};}function patchUpdates(_0x4e61db){return function(_0x12bcee){try{jsonpatch[_0x3f25('0x1c')](_0x12bcee,_0x4e61db,!![]);}catch(_0x33f666){return BPromise[_0x3f25('0x1d')](_0x33f666);}return _0x12bcee[_0x3f25('0x1e')]();};}function saveUpdates(_0x5be18c,_0x2e8884){return function(_0xa23942){if(_0xa23942){return _0xa23942['update'](_0x5be18c)['then'](function(_0x28eb9e){return _0x28eb9e;});}return null;};}function removeEntity(_0x2be5d4,_0x561468){return function(_0x3b23f4){if(_0x3b23f4){return _0x3b23f4[_0x3f25('0x1f')]()['then'](function(){var _0x2dd190=_0x3b23f4[_0x3f25('0x20')]({'plain':!![]});var _0x2f0713=_0x3f25('0x21');return db['UserProfileResource'][_0x3f25('0x1f')]({'where':{'type':_0x2f0713,'resourceId':_0x2dd190['id']}})[_0x3f25('0x22')](function(){return _0x3b23f4;});})['then'](function(){_0x2be5d4[_0x3f25('0x15')](0xcc)['end']();});}};}function handleEntityNotFound(_0xe57b2b,_0x1dce9e){return function(_0x2f8e32){if(!_0x2f8e32){_0xe57b2b[_0x3f25('0x14')](0x194);}return _0x2f8e32;};}function handleError(_0x814d4b,_0x331659){_0x331659=_0x331659||0x1f4;return function(_0x466cf7){logger[_0x3f25('0x23')](_0x466cf7[_0x3f25('0x24')]);if(_0x466cf7[_0x3f25('0x25')]){delete _0x466cf7[_0x3f25('0x25')];}_0x814d4b[_0x3f25('0x15')](_0x331659)[_0x3f25('0x26')](_0x466cf7);};}exports[_0x3f25('0x27')]=function(_0xf6e476,_0x4f8492){var _0x1b5d13={},_0x45ea75={},_0xad1b55={'count':0x0,'rows':[]};var _0x1746da=_[_0x3f25('0x28')](db['Tag'][_0x3f25('0x29')],function(_0x260682){return{'name':_0x260682[_0x3f25('0x2a')],'type':_0x260682[_0x3f25('0x2b')][_0x3f25('0x2c')]};});_0x45ea75['model']=_[_0x3f25('0x28')](_0x1746da,_0x3f25('0x25'));_0x45ea75[_0x3f25('0x2d')]=_[_0x3f25('0x2e')](_0xf6e476[_0x3f25('0x2d')]);_0x45ea75['filters']=_[_0x3f25('0x2f')](_0x45ea75[_0x3f25('0x30')],_0x45ea75[_0x3f25('0x2d')]);_0x1b5d13[_0x3f25('0x31')]=_['intersection'](_0x45ea75['model'],qs['fields'](_0xf6e476[_0x3f25('0x2d')][_0x3f25('0x32')]));_0x1b5d13['attributes']=_0x1b5d13['attributes'][_0x3f25('0x33')]?_0x1b5d13[_0x3f25('0x31')]:_0x45ea75['model'];if(!_0xf6e476[_0x3f25('0x2d')]['hasOwnProperty'](_0x3f25('0x34'))){_0x1b5d13[_0x3f25('0x19')]=qs[_0x3f25('0x19')](_0xf6e476[_0x3f25('0x2d')]['limit']);_0x1b5d13['offset']=qs['offset'](_0xf6e476[_0x3f25('0x2d')][_0x3f25('0x18')]);}_0x1b5d13[_0x3f25('0x35')]=qs[_0x3f25('0x36')](_0xf6e476[_0x3f25('0x2d')][_0x3f25('0x36')]);_0x1b5d13[_0x3f25('0x37')]=qs[_0x3f25('0x38')](_['pick'](_0xf6e476[_0x3f25('0x2d')],_0x45ea75[_0x3f25('0x38')]),_0x1746da);if(_0xf6e476[_0x3f25('0x2d')][_0x3f25('0x39')]){_0x1b5d13['where']=_[_0x3f25('0x3a')](_0x1b5d13[_0x3f25('0x37')],{'$or':_['map'](_0x1746da,function(_0x9f64c1){if(_0x9f64c1[_0x3f25('0x2b')]!==_0x3f25('0x3b')){var _0x13122e={};_0x13122e[_0x9f64c1[_0x3f25('0x25')]]={'$like':'%'+_0xf6e476[_0x3f25('0x2d')][_0x3f25('0x39')]+'%'};return _0x13122e;}})});}_0x1b5d13=_[_0x3f25('0x3a')]({},_0x1b5d13,_0xf6e476[_0x3f25('0x3c')]);var _0x2b95c3={'where':_0x1b5d13[_0x3f25('0x37')]};return db[_0x3f25('0x3d')]['count'](_0x2b95c3)['then'](function(_0x5471cc){_0xad1b55[_0x3f25('0x17')]=_0x5471cc;if(_0xf6e476[_0x3f25('0x2d')][_0x3f25('0x3e')]){_0x1b5d13[_0x3f25('0x3f')]=[{'all':!![]}];}return db[_0x3f25('0x3d')][_0x3f25('0x40')](_0x1b5d13);})[_0x3f25('0x22')](function(_0x3567b3){_0xad1b55[_0x3f25('0x41')]=_0x3567b3;return _0xad1b55;})[_0x3f25('0x22')](respondWithFilteredResult(_0x4f8492,_0x1b5d13))['catch'](handleError(_0x4f8492,null));};exports[_0x3f25('0x42')]=function(_0x7505bd,_0x1dd699){var _0x2e347e={'raw':!![],'where':{'id':_0x7505bd['params']['id']}},_0x1cbdef={};_0x1cbdef[_0x3f25('0x30')]=_['keys'](db[_0x3f25('0x3d')][_0x3f25('0x29')]);_0x1cbdef[_0x3f25('0x2d')]=_[_0x3f25('0x2e')](_0x7505bd['query']);_0x1cbdef[_0x3f25('0x38')]=_['intersection'](_0x1cbdef['model'],_0x1cbdef[_0x3f25('0x2d')]);_0x2e347e['attributes']=_[_0x3f25('0x2f')](_0x1cbdef[_0x3f25('0x30')],qs[_0x3f25('0x32')](_0x7505bd[_0x3f25('0x2d')][_0x3f25('0x32')]));_0x2e347e[_0x3f25('0x31')]=_0x2e347e[_0x3f25('0x31')]['length']?_0x2e347e['attributes']:_0x1cbdef[_0x3f25('0x30')];if(_0x7505bd[_0x3f25('0x2d')][_0x3f25('0x3e')]){_0x2e347e[_0x3f25('0x3f')]=[{'all':!![]}];}_0x2e347e=_[_0x3f25('0x3a')]({},_0x2e347e,_0x7505bd[_0x3f25('0x3c')]);return db[_0x3f25('0x3d')][_0x3f25('0x43')](_0x2e347e)[_0x3f25('0x22')](handleEntityNotFound(_0x1dd699,null))['then'](respondWithResult(_0x1dd699,null))[_0x3f25('0x44')](handleError(_0x1dd699,null));};exports[_0x3f25('0x45')]=function(_0xb6ef56,_0x48d76c){return db['Tag'][_0x3f25('0x45')](_0xb6ef56['body'],{})[_0x3f25('0x22')](function(_0x2c700c){var _0x3a89f1=_0xb6ef56[_0x3f25('0x46')]['get']({'plain':!![]});if(!_0x3a89f1)throw new Error(_0x3f25('0x47'));if(_0x3a89f1[_0x3f25('0x48')]===_0x3f25('0x46')){var _0x5ba7f2=_0x2c700c[_0x3f25('0x20')]({'plain':!![]});var _0x495c97=_0x3f25('0x21');return db[_0x3f25('0x49')][_0x3f25('0x43')]({'where':{'name':_0x495c97,'userProfileId':_0x3a89f1['userProfileId']},'raw':!![]})[_0x3f25('0x22')](function(_0x58b31a){if(_0x58b31a&&_0x58b31a['autoAssociation']===0x0){return db[_0x3f25('0x4a')][_0x3f25('0x45')]({'name':_0x5ba7f2[_0x3f25('0x25')],'resourceId':_0x5ba7f2['id'],'type':_0x58b31a[_0x3f25('0x25')],'sectionId':_0x58b31a['id']},{})[_0x3f25('0x22')](function(){return _0x2c700c;});}else{return _0x2c700c;}})[_0x3f25('0x44')](function(_0x2fd456){logger[_0x3f25('0x23')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x2fd456);throw _0x2fd456;});}return _0x2c700c;})[_0x3f25('0x22')](respondWithResult(_0x48d76c,0xc9))[_0x3f25('0x44')](handleError(_0x48d76c,null));};exports[_0x3f25('0x4b')]=function(_0x540d2e,_0x113f27){if(_0x540d2e[_0x3f25('0x4c')]['id']){delete _0x540d2e[_0x3f25('0x4c')]['id'];}return db[_0x3f25('0x3d')][_0x3f25('0x43')]({'where':{'id':_0x540d2e[_0x3f25('0x4d')]['id']}})[_0x3f25('0x22')](handleEntityNotFound(_0x113f27,null))[_0x3f25('0x22')](saveUpdates(_0x540d2e[_0x3f25('0x4c')],null))[_0x3f25('0x22')](respondWithResult(_0x113f27,null))[_0x3f25('0x44')](handleError(_0x113f27,null));};exports['destroy']=function(_0x272f8c,_0x1be83d){return db[_0x3f25('0x3d')][_0x3f25('0x43')]({'where':{'id':_0x272f8c[_0x3f25('0x4d')]['id']}})[_0x3f25('0x22')](handleEntityNotFound(_0x1be83d,null))[_0x3f25('0x22')](removeEntity(_0x1be83d,null))['catch'](handleError(_0x1be83d,null));};