Built motion from commit b5413ace.|2.6.9
[motion2.git] / server / api / template / template.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 _0x08fc=['squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','status','end','offset','undefined','limit','count','json','reject','save','then','destroy','get','Templates','UserProfileResource','sendStatus','error','stack','name','index','map','rawAttributes','fieldName','key','model','filters','intersection','attributes','fields','query','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','options','Template','includeAll','findAll','rows','catch','show','params','keys','length','create','body','user','role','UserProfileSection','find','userProfileId','update','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','to-csv','fs-extra','lodash'];(function(_0x42b456,_0x39e8de){var _0xa0fbc4=function(_0x4896d7){while(--_0x4896d7){_0x42b456['push'](_0x42b456['shift']());}};_0xa0fbc4(++_0x39e8de);}(_0x08fc,0x1ce));var _0xc08f=function(_0x19dd7a,_0x51de21){_0x19dd7a=_0x19dd7a-0x0;var _0x3feb6a=_0x08fc[_0x19dd7a];return _0x3feb6a;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xc08f('0x0'));var zipdir=require(_0xc08f('0x1'));var jsonpatch=require(_0xc08f('0x2'));var rp=require(_0xc08f('0x3'));var moment=require(_0xc08f('0x4'));var BPromise=require(_0xc08f('0x5'));var Mustache=require('mustache');var util=require(_0xc08f('0x6'));var path=require(_0xc08f('0x7'));var sox=require(_0xc08f('0x8'));var csv=require(_0xc08f('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xc08f('0xa'));var _=require(_0xc08f('0xb'));var squel=require(_0xc08f('0xc'));var crypto=require(_0xc08f('0xd'));var jsforce=require(_0xc08f('0xe'));var deskjs=require(_0xc08f('0xf'));var toCsv=require(_0xc08f('0x9'));var querystring=require(_0xc08f('0x10'));var Papa=require(_0xc08f('0x11'));var Redis=require(_0xc08f('0x12'));var authService=require('../../components/auth/service');var qs=require(_0xc08f('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0xc08f('0x14'))(_0xc08f('0x15'));var utils=require('../../config/utils');var config=require(_0xc08f('0x16'));var licenseUtil=require('../../config/license/util');var db=require(_0xc08f('0x17'))['db'];function respondWithStatusCode(_0x523da3,_0x23758e){_0x23758e=_0x23758e||0xcc;return function(_0x29f770){if(_0x29f770){return _0x523da3['sendStatus'](_0x23758e);}return _0x523da3[_0xc08f('0x18')](_0x23758e)[_0xc08f('0x19')]();};}function respondWithResult(_0x567d5d,_0x32d3b2){_0x32d3b2=_0x32d3b2||0xc8;return function(_0x3633e9){if(_0x3633e9){return _0x567d5d['status'](_0x32d3b2)['json'](_0x3633e9);}};}function respondWithFilteredResult(_0x15a64c,_0x237167){return function(_0x39f411){if(_0x39f411){var _0x993965=typeof _0x237167[_0xc08f('0x1a')]===_0xc08f('0x1b')&&typeof _0x237167[_0xc08f('0x1c')]===_0xc08f('0x1b');var _0x5dfe8f=_0x39f411['count'];var _0x4f651b=_0x993965?0x0:_0x237167[_0xc08f('0x1a')];var _0x5e1410=_0x993965?_0x39f411[_0xc08f('0x1d')]:_0x237167[_0xc08f('0x1a')]+_0x237167[_0xc08f('0x1c')];var _0x48d851;if(_0x5e1410>=_0x5dfe8f){_0x5e1410=_0x5dfe8f;_0x48d851=0xc8;}else{_0x48d851=0xce;}_0x15a64c[_0xc08f('0x18')](_0x48d851);return _0x15a64c['set']('Content-Range',_0x4f651b+'-'+_0x5e1410+'/'+_0x5dfe8f)[_0xc08f('0x1e')](_0x39f411);}return null;};}function patchUpdates(_0x3532b8){return function(_0x20542c){try{jsonpatch['apply'](_0x20542c,_0x3532b8,!![]);}catch(_0x4eb904){return BPromise[_0xc08f('0x1f')](_0x4eb904);}return _0x20542c[_0xc08f('0x20')]();};}function saveUpdates(_0x4c5cad,_0x4a80f1){return function(_0xecd255){if(_0xecd255){return _0xecd255['update'](_0x4c5cad)[_0xc08f('0x21')](function(_0x46c801){return _0x46c801;});}return null;};}function removeEntity(_0x4c4d1a,_0x6df8bd){return function(_0x7081d1){if(_0x7081d1){return _0x7081d1[_0xc08f('0x22')]()[_0xc08f('0x21')](function(){var _0x2b0e0a=_0x7081d1[_0xc08f('0x23')]({'plain':!![]});var _0x3fd813=_0xc08f('0x24');return db[_0xc08f('0x25')]['destroy']({'where':{'type':_0x3fd813,'resourceId':_0x2b0e0a['id']}})[_0xc08f('0x21')](function(){return _0x7081d1;});})['then'](function(){_0x4c4d1a[_0xc08f('0x18')](0xcc)[_0xc08f('0x19')]();});}};}function handleEntityNotFound(_0x5bc3f1,_0x2e7143){return function(_0x328586){if(!_0x328586){_0x5bc3f1[_0xc08f('0x26')](0x194);}return _0x328586;};}function handleError(_0x2f7f2e,_0x3b0403){_0x3b0403=_0x3b0403||0x1f4;return function(_0x31b486){logger[_0xc08f('0x27')](_0x31b486[_0xc08f('0x28')]);if(_0x31b486[_0xc08f('0x29')]){delete _0x31b486[_0xc08f('0x29')];}_0x2f7f2e[_0xc08f('0x18')](_0x3b0403)['send'](_0x31b486);};}exports[_0xc08f('0x2a')]=function(_0x4ccd1d,_0x361f33){var _0x9fa7fe={},_0x34a3c9={},_0x21bab0={'count':0x0,'rows':[]};var _0x34f27e=_[_0xc08f('0x2b')](db['Template'][_0xc08f('0x2c')],function(_0x12a8fc){return{'name':_0x12a8fc[_0xc08f('0x2d')],'type':_0x12a8fc['type'][_0xc08f('0x2e')]};});_0x34a3c9[_0xc08f('0x2f')]=_[_0xc08f('0x2b')](_0x34f27e,_0xc08f('0x29'));_0x34a3c9['query']=_['keys'](_0x4ccd1d['query']);_0x34a3c9[_0xc08f('0x30')]=_[_0xc08f('0x31')](_0x34a3c9[_0xc08f('0x2f')],_0x34a3c9['query']);_0x9fa7fe[_0xc08f('0x32')]=_[_0xc08f('0x31')](_0x34a3c9[_0xc08f('0x2f')],qs[_0xc08f('0x33')](_0x4ccd1d['query'][_0xc08f('0x33')]));_0x9fa7fe[_0xc08f('0x32')]=_0x9fa7fe[_0xc08f('0x32')]['length']?_0x9fa7fe[_0xc08f('0x32')]:_0x34a3c9['model'];if(!_0x4ccd1d[_0xc08f('0x34')][_0xc08f('0x35')](_0xc08f('0x36'))){_0x9fa7fe[_0xc08f('0x1c')]=qs[_0xc08f('0x1c')](_0x4ccd1d[_0xc08f('0x34')][_0xc08f('0x1c')]);_0x9fa7fe[_0xc08f('0x1a')]=qs['offset'](_0x4ccd1d['query'][_0xc08f('0x1a')]);}_0x9fa7fe[_0xc08f('0x37')]=qs[_0xc08f('0x38')](_0x4ccd1d[_0xc08f('0x34')][_0xc08f('0x38')]);_0x9fa7fe[_0xc08f('0x39')]=qs[_0xc08f('0x30')](_[_0xc08f('0x3a')](_0x4ccd1d[_0xc08f('0x34')],_0x34a3c9[_0xc08f('0x30')]),_0x34f27e);if(_0x4ccd1d['query']['filter']){_0x9fa7fe[_0xc08f('0x39')]=_[_0xc08f('0x3b')](_0x9fa7fe[_0xc08f('0x39')],{'$or':_[_0xc08f('0x2b')](_0x34f27e,function(_0x3e6244){if(_0x3e6244['type']!==_0xc08f('0x3c')){var _0x1f7529={};_0x1f7529[_0x3e6244[_0xc08f('0x29')]]={'$like':'%'+_0x4ccd1d[_0xc08f('0x34')]['filter']+'%'};return _0x1f7529;}})});}_0x9fa7fe=_[_0xc08f('0x3b')]({},_0x9fa7fe,_0x4ccd1d[_0xc08f('0x3d')]);var _0x1af5ac={'where':_0x9fa7fe[_0xc08f('0x39')]};return db[_0xc08f('0x3e')][_0xc08f('0x1d')](_0x1af5ac)[_0xc08f('0x21')](function(_0x9f55ad){_0x21bab0[_0xc08f('0x1d')]=_0x9f55ad;if(_0x4ccd1d[_0xc08f('0x34')][_0xc08f('0x3f')]){_0x9fa7fe['include']=[{'all':!![]}];}return db[_0xc08f('0x3e')][_0xc08f('0x40')](_0x9fa7fe);})[_0xc08f('0x21')](function(_0x1f7546){_0x21bab0[_0xc08f('0x41')]=_0x1f7546;return _0x21bab0;})['then'](respondWithFilteredResult(_0x361f33,_0x9fa7fe))[_0xc08f('0x42')](handleError(_0x361f33,null));};exports[_0xc08f('0x43')]=function(_0x198b9f,_0x4c7798){var _0x33ae26={'raw':!![],'where':{'id':_0x198b9f[_0xc08f('0x44')]['id']}},_0x319dfa={};_0x319dfa[_0xc08f('0x2f')]=_[_0xc08f('0x45')](db[_0xc08f('0x3e')][_0xc08f('0x2c')]);_0x319dfa[_0xc08f('0x34')]=_[_0xc08f('0x45')](_0x198b9f[_0xc08f('0x34')]);_0x319dfa[_0xc08f('0x30')]=_['intersection'](_0x319dfa[_0xc08f('0x2f')],_0x319dfa['query']);_0x33ae26[_0xc08f('0x32')]=_[_0xc08f('0x31')](_0x319dfa[_0xc08f('0x2f')],qs['fields'](_0x198b9f[_0xc08f('0x34')][_0xc08f('0x33')]));_0x33ae26['attributes']=_0x33ae26[_0xc08f('0x32')][_0xc08f('0x46')]?_0x33ae26[_0xc08f('0x32')]:_0x319dfa[_0xc08f('0x2f')];if(_0x198b9f['query'][_0xc08f('0x3f')]){_0x33ae26['include']=[{'all':!![]}];}_0x33ae26=_[_0xc08f('0x3b')]({},_0x33ae26,_0x198b9f['options']);return db['Template']['find'](_0x33ae26)['then'](handleEntityNotFound(_0x4c7798,null))['then'](respondWithResult(_0x4c7798,null))['catch'](handleError(_0x4c7798,null));};exports[_0xc08f('0x47')]=function(_0x3050f2,_0xaf90c8){return db[_0xc08f('0x3e')][_0xc08f('0x47')](_0x3050f2[_0xc08f('0x48')],{})[_0xc08f('0x21')](function(_0x521c15){var _0x1f8ada=_0x3050f2[_0xc08f('0x49')][_0xc08f('0x23')]({'plain':!![]});if(!_0x1f8ada)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x1f8ada[_0xc08f('0x4a')]===_0xc08f('0x49')){var _0x2578b1=_0x521c15[_0xc08f('0x23')]({'plain':!![]});var _0x2db0ad='Templates';return db[_0xc08f('0x4b')][_0xc08f('0x4c')]({'where':{'name':_0x2db0ad,'userProfileId':_0x1f8ada[_0xc08f('0x4d')]},'raw':!![]})['then'](function(_0x5e4341){if(_0x5e4341&&_0x5e4341['autoAssociation']===0x0){return db[_0xc08f('0x25')]['create']({'name':_0x2578b1[_0xc08f('0x29')],'resourceId':_0x2578b1['id'],'type':_0x5e4341[_0xc08f('0x29')],'sectionId':_0x5e4341['id']},{})[_0xc08f('0x21')](function(){return _0x521c15;});}else{return _0x521c15;}})[_0xc08f('0x42')](function(_0x116d29){logger[_0xc08f('0x27')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x116d29);throw _0x116d29;});}return _0x521c15;})[_0xc08f('0x21')](respondWithResult(_0xaf90c8,0xc9))[_0xc08f('0x42')](handleError(_0xaf90c8,null));};exports[_0xc08f('0x4e')]=function(_0x5c46b7,_0x33b4dc){if(_0x5c46b7['body']['id']){delete _0x5c46b7[_0xc08f('0x48')]['id'];}return db[_0xc08f('0x3e')]['find']({'where':{'id':_0x5c46b7[_0xc08f('0x44')]['id']}})[_0xc08f('0x21')](handleEntityNotFound(_0x33b4dc,null))[_0xc08f('0x21')](saveUpdates(_0x5c46b7['body'],null))['then'](respondWithResult(_0x33b4dc,null))[_0xc08f('0x42')](handleError(_0x33b4dc,null));};exports['destroy']=function(_0xad058f,_0x24451c){return db[_0xc08f('0x3e')][_0xc08f('0x4c')]({'where':{'id':_0xad058f[_0xc08f('0x44')]['id']}})['then'](handleEntityNotFound(_0x24451c,null))[_0xc08f('0x21')](removeEntity(_0x24451c,null))[_0xc08f('0x42')](handleError(_0x24451c,null));};