9efb05a02ecf3e1262b69a2e8bcf9c9ab0b69b10
[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 _0x22fb=['bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/license/util','sendStatus','status','end','offset','undefined','limit','count','Content-Range','json','apply','save','update','then','destroy','get','UserProfileResource','stack','name','send','index','map','rawAttributes','type','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','pick','where','VIRTUAL','filter','merge','options','Tag','includeAll','include','findAll','rows','catch','show','params','keys','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','zip-dir','request-promise','moment'];(function(_0x4df69e,_0x5131d5){var _0x1f9041=function(_0x1e6288){while(--_0x1e6288){_0x4df69e['push'](_0x4df69e['shift']());}};_0x1f9041(++_0x5131d5);}(_0x22fb,0x192));var _0xb22f=function(_0x47fd5d,_0x27ce2f){_0x47fd5d=_0x47fd5d-0x0;var _0x4780f9=_0x22fb[_0x47fd5d];return _0x4780f9;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0xb22f('0x0'));var jsonpatch=require('fast-json-patch');var rp=require(_0xb22f('0x1'));var moment=require(_0xb22f('0x2'));var BPromise=require(_0xb22f('0x3'));var Mustache=require(_0xb22f('0x4'));var util=require(_0xb22f('0x5'));var path=require('path');var sox=require(_0xb22f('0x6'));var csv=require(_0xb22f('0x7'));var ejs=require(_0xb22f('0x8'));var fs=require('fs');var fs_extra=require(_0xb22f('0x9'));var _=require(_0xb22f('0xa'));var squel=require(_0xb22f('0xb'));var crypto=require('crypto');var jsforce=require(_0xb22f('0xc'));var deskjs=require(_0xb22f('0xd'));var toCsv=require(_0xb22f('0x7'));var querystring=require(_0xb22f('0xe'));var Papa=require(_0xb22f('0xf'));var Redis=require(_0xb22f('0x10'));var authService=require(_0xb22f('0x11'));var qs=require(_0xb22f('0x12'));var as=require(_0xb22f('0x13'));var hardwareService=require(_0xb22f('0x14'));var logger=require(_0xb22f('0x15'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xb22f('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x4bb02a,_0x5bf0b9){_0x5bf0b9=_0x5bf0b9||0xcc;return function(_0x45e426){if(_0x45e426){return _0x4bb02a[_0xb22f('0x17')](_0x5bf0b9);}return _0x4bb02a[_0xb22f('0x18')](_0x5bf0b9)[_0xb22f('0x19')]();};}function respondWithResult(_0x390c53,_0x32d796){_0x32d796=_0x32d796||0xc8;return function(_0x5c6ccf){if(_0x5c6ccf){return _0x390c53[_0xb22f('0x18')](_0x32d796)['json'](_0x5c6ccf);}};}function respondWithFilteredResult(_0x36e2a8,_0x4f9108){return function(_0x2187fc){if(_0x2187fc){var _0x514a8f=typeof _0x4f9108[_0xb22f('0x1a')]===_0xb22f('0x1b')&&typeof _0x4f9108[_0xb22f('0x1c')]==='undefined';var _0x1530f7=_0x2187fc['count'];var _0x252742=_0x514a8f?0x0:_0x4f9108[_0xb22f('0x1a')];var _0x2aaec4=_0x514a8f?_0x2187fc[_0xb22f('0x1d')]:_0x4f9108[_0xb22f('0x1a')]+_0x4f9108[_0xb22f('0x1c')];var _0x5c843b;if(_0x2aaec4>=_0x1530f7){_0x2aaec4=_0x1530f7;_0x5c843b=0xc8;}else{_0x5c843b=0xce;}_0x36e2a8[_0xb22f('0x18')](_0x5c843b);return _0x36e2a8['set'](_0xb22f('0x1e'),_0x252742+'-'+_0x2aaec4+'/'+_0x1530f7)[_0xb22f('0x1f')](_0x2187fc);}return null;};}function patchUpdates(_0x316f11){return function(_0x2239b8){try{jsonpatch[_0xb22f('0x20')](_0x2239b8,_0x316f11,!![]);}catch(_0x4bbb12){return BPromise['reject'](_0x4bbb12);}return _0x2239b8[_0xb22f('0x21')]();};}function saveUpdates(_0x19e480,_0xf34e0d){return function(_0x16e465){if(_0x16e465){return _0x16e465[_0xb22f('0x22')](_0x19e480)[_0xb22f('0x23')](function(_0x201764){return _0x201764;});}return null;};}function removeEntity(_0x286465,_0x583fbe){return function(_0x482b3a){if(_0x482b3a){return _0x482b3a[_0xb22f('0x24')]()[_0xb22f('0x23')](function(){var _0x5e1b8c=_0x482b3a[_0xb22f('0x25')]({'plain':!![]});var _0x4364f5='Tags';return db[_0xb22f('0x26')][_0xb22f('0x24')]({'where':{'type':_0x4364f5,'resourceId':_0x5e1b8c['id']}})['then'](function(){return _0x482b3a;});})[_0xb22f('0x23')](function(){_0x286465[_0xb22f('0x18')](0xcc)[_0xb22f('0x19')]();});}};}function handleEntityNotFound(_0xec047c,_0x3bf205){return function(_0x246a0c){if(!_0x246a0c){_0xec047c[_0xb22f('0x17')](0x194);}return _0x246a0c;};}function handleError(_0x2ae6d4,_0x4ce9af){_0x4ce9af=_0x4ce9af||0x1f4;return function(_0x483cc3){logger['error'](_0x483cc3[_0xb22f('0x27')]);if(_0x483cc3['name']){delete _0x483cc3[_0xb22f('0x28')];}_0x2ae6d4[_0xb22f('0x18')](_0x4ce9af)[_0xb22f('0x29')](_0x483cc3);};}exports[_0xb22f('0x2a')]=function(_0x3bf636,_0x5b2a42){var _0x519034={},_0x435690={},_0x12c042={'count':0x0,'rows':[]};var _0x319e56=_[_0xb22f('0x2b')](db['Tag'][_0xb22f('0x2c')],function(_0x29ad43){return{'name':_0x29ad43['fieldName'],'type':_0x29ad43[_0xb22f('0x2d')][_0xb22f('0x2e')]};});_0x435690[_0xb22f('0x2f')]=_[_0xb22f('0x2b')](_0x319e56,_0xb22f('0x28'));_0x435690['query']=_['keys'](_0x3bf636[_0xb22f('0x30')]);_0x435690[_0xb22f('0x31')]=_[_0xb22f('0x32')](_0x435690[_0xb22f('0x2f')],_0x435690[_0xb22f('0x30')]);_0x519034[_0xb22f('0x33')]=_[_0xb22f('0x32')](_0x435690[_0xb22f('0x2f')],qs[_0xb22f('0x34')](_0x3bf636['query'][_0xb22f('0x34')]));_0x519034['attributes']=_0x519034['attributes'][_0xb22f('0x35')]?_0x519034[_0xb22f('0x33')]:_0x435690[_0xb22f('0x2f')];if(!_0x3bf636[_0xb22f('0x30')][_0xb22f('0x36')](_0xb22f('0x37'))){_0x519034[_0xb22f('0x1c')]=qs[_0xb22f('0x1c')](_0x3bf636[_0xb22f('0x30')][_0xb22f('0x1c')]);_0x519034[_0xb22f('0x1a')]=qs[_0xb22f('0x1a')](_0x3bf636[_0xb22f('0x30')]['offset']);}_0x519034['order']=qs[_0xb22f('0x38')](_0x3bf636[_0xb22f('0x30')][_0xb22f('0x38')]);_0x519034['where']=qs[_0xb22f('0x31')](_[_0xb22f('0x39')](_0x3bf636['query'],_0x435690['filters']),_0x319e56);if(_0x3bf636[_0xb22f('0x30')]['filter']){_0x519034[_0xb22f('0x3a')]=_['merge'](_0x519034[_0xb22f('0x3a')],{'$or':_[_0xb22f('0x2b')](_0x319e56,function(_0x27e081){if(_0x27e081['type']!==_0xb22f('0x3b')){var _0x2d265c={};_0x2d265c[_0x27e081[_0xb22f('0x28')]]={'$like':'%'+_0x3bf636['query'][_0xb22f('0x3c')]+'%'};return _0x2d265c;}})});}_0x519034=_[_0xb22f('0x3d')]({},_0x519034,_0x3bf636[_0xb22f('0x3e')]);var _0x476ff6={'where':_0x519034[_0xb22f('0x3a')]};return db[_0xb22f('0x3f')][_0xb22f('0x1d')](_0x476ff6)[_0xb22f('0x23')](function(_0xcb55d3){_0x12c042[_0xb22f('0x1d')]=_0xcb55d3;if(_0x3bf636['query'][_0xb22f('0x40')]){_0x519034[_0xb22f('0x41')]=[{'all':!![]}];}return db['Tag'][_0xb22f('0x42')](_0x519034);})['then'](function(_0x58ba40){_0x12c042[_0xb22f('0x43')]=_0x58ba40;return _0x12c042;})[_0xb22f('0x23')](respondWithFilteredResult(_0x5b2a42,_0x519034))[_0xb22f('0x44')](handleError(_0x5b2a42,null));};exports[_0xb22f('0x45')]=function(_0x563740,_0x1475d1){var _0x572a0c={'raw':!![],'where':{'id':_0x563740[_0xb22f('0x46')]['id']}},_0xddfda6={};_0xddfda6[_0xb22f('0x2f')]=_[_0xb22f('0x47')](db[_0xb22f('0x3f')][_0xb22f('0x2c')]);_0xddfda6['query']=_[_0xb22f('0x47')](_0x563740['query']);_0xddfda6[_0xb22f('0x31')]=_[_0xb22f('0x32')](_0xddfda6['model'],_0xddfda6[_0xb22f('0x30')]);_0x572a0c[_0xb22f('0x33')]=_['intersection'](_0xddfda6['model'],qs[_0xb22f('0x34')](_0x563740['query'][_0xb22f('0x34')]));_0x572a0c[_0xb22f('0x33')]=_0x572a0c[_0xb22f('0x33')][_0xb22f('0x35')]?_0x572a0c['attributes']:_0xddfda6[_0xb22f('0x2f')];if(_0x563740[_0xb22f('0x30')]['includeAll']){_0x572a0c[_0xb22f('0x41')]=[{'all':!![]}];}_0x572a0c=_[_0xb22f('0x3d')]({},_0x572a0c,_0x563740[_0xb22f('0x3e')]);return db[_0xb22f('0x3f')][_0xb22f('0x48')](_0x572a0c)['then'](handleEntityNotFound(_0x1475d1,null))[_0xb22f('0x23')](respondWithResult(_0x1475d1,null))['catch'](handleError(_0x1475d1,null));};exports['create']=function(_0x3fb0ac,_0x90eef9){return db[_0xb22f('0x3f')][_0xb22f('0x49')](_0x3fb0ac[_0xb22f('0x4a')],{})['then'](function(_0x275ca5){var _0x504511=_0x3fb0ac[_0xb22f('0x4b')]['get']({'plain':!![]});if(!_0x504511)throw new Error(_0xb22f('0x4c'));if(_0x504511[_0xb22f('0x4d')]===_0xb22f('0x4b')){var _0x58cd19=_0x275ca5[_0xb22f('0x25')]({'plain':!![]});var _0x436425='Tags';return db[_0xb22f('0x4e')][_0xb22f('0x48')]({'where':{'name':_0x436425,'userProfileId':_0x504511['userProfileId']},'raw':!![]})['then'](function(_0x261bc0){if(_0x261bc0&&_0x261bc0[_0xb22f('0x4f')]===0x0){return db['UserProfileResource'][_0xb22f('0x49')]({'name':_0x58cd19[_0xb22f('0x28')],'resourceId':_0x58cd19['id'],'type':_0x261bc0['name'],'sectionId':_0x261bc0['id']},{})['then'](function(){return _0x275ca5;});}else{return _0x275ca5;}})[_0xb22f('0x44')](function(_0x3759ef){logger['error'](_0xb22f('0x50'),_0x3759ef);throw _0x3759ef;});}return _0x275ca5;})[_0xb22f('0x23')](respondWithResult(_0x90eef9,0xc9))[_0xb22f('0x44')](handleError(_0x90eef9,null));};exports[_0xb22f('0x22')]=function(_0x518e28,_0x30039a){if(_0x518e28[_0xb22f('0x4a')]['id']){delete _0x518e28['body']['id'];}return db['Tag'][_0xb22f('0x48')]({'where':{'id':_0x518e28[_0xb22f('0x46')]['id']}})[_0xb22f('0x23')](handleEntityNotFound(_0x30039a,null))[_0xb22f('0x23')](saveUpdates(_0x518e28['body'],null))[_0xb22f('0x23')](respondWithResult(_0x30039a,null))[_0xb22f('0x44')](handleError(_0x30039a,null));};exports['destroy']=function(_0x1e1390,_0x34e0f0){return db[_0xb22f('0x3f')][_0xb22f('0x48')]({'where':{'id':_0x1e1390[_0xb22f('0x46')]['id']}})[_0xb22f('0x23')](handleEntityNotFound(_0x34e0f0,null))[_0xb22f('0x23')](removeEntity(_0x34e0f0,null))['catch'](handleError(_0x34e0f0,null));};