Built motion from commit 52c8a02d.|2.6.29
[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 _0xfb19=['querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status','offset','limit','undefined','count','Content-Range','apply','reject','save','update','then','destroy','Templates','UserProfileResource','end','sendStatus','error','stack','name','send','index','map','Template','rawAttributes','fieldName','type','key','model','query','keys','filters','fields','attributes','length','hasOwnProperty','nolimit','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','include','rows','catch','show','params','intersection','find','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js'];(function(_0x3f9e8c,_0x4f6caf){var _0x590945=function(_0x566887){while(--_0x566887){_0x3f9e8c['push'](_0x3f9e8c['shift']());}};_0x590945(++_0x4f6caf);}(_0xfb19,0x1b1));var _0x9fb1=function(_0x321bb2,_0x202794){_0x321bb2=_0x321bb2-0x0;var _0x9c12ed=_0xfb19[_0x321bb2];return _0x9c12ed;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x9fb1('0x0'));var zipdir=require(_0x9fb1('0x1'));var jsonpatch=require(_0x9fb1('0x2'));var rp=require(_0x9fb1('0x3'));var moment=require(_0x9fb1('0x4'));var BPromise=require(_0x9fb1('0x5'));var Mustache=require(_0x9fb1('0x6'));var util=require(_0x9fb1('0x7'));var path=require(_0x9fb1('0x8'));var sox=require(_0x9fb1('0x9'));var csv=require('to-csv');var ejs=require(_0x9fb1('0xa'));var fs=require('fs');var fs_extra=require(_0x9fb1('0xb'));var _=require(_0x9fb1('0xc'));var squel=require(_0x9fb1('0xd'));var crypto=require(_0x9fb1('0xe'));var jsforce=require(_0x9fb1('0xf'));var deskjs=require(_0x9fb1('0x10'));var toCsv=require('to-csv');var querystring=require(_0x9fb1('0x11'));var Papa=require(_0x9fb1('0x12'));var Redis=require(_0x9fb1('0x13'));var authService=require(_0x9fb1('0x14'));var qs=require(_0x9fb1('0x15'));var as=require(_0x9fb1('0x16'));var hardwareService=require(_0x9fb1('0x17'));var logger=require(_0x9fb1('0x18'))(_0x9fb1('0x19'));var utils=require(_0x9fb1('0x1a'));var config=require(_0x9fb1('0x1b'));var licenseUtil=require(_0x9fb1('0x1c'));var db=require(_0x9fb1('0x1d'))['db'];function respondWithStatusCode(_0x44b71,_0x4dea84){_0x4dea84=_0x4dea84||0xcc;return function(_0xcb6616){if(_0xcb6616){return _0x44b71['sendStatus'](_0x4dea84);}return _0x44b71[_0x9fb1('0x1e')](_0x4dea84)['end']();};}function respondWithResult(_0x138c67,_0x2be4f9){_0x2be4f9=_0x2be4f9||0xc8;return function(_0x4b1631){if(_0x4b1631){return _0x138c67[_0x9fb1('0x1e')](_0x2be4f9)['json'](_0x4b1631);}};}function respondWithFilteredResult(_0x3a37b1,_0x4b8ec5){return function(_0x1c388c){if(_0x1c388c){var _0x1a96fa=typeof _0x4b8ec5[_0x9fb1('0x1f')]==='undefined'&&typeof _0x4b8ec5[_0x9fb1('0x20')]===_0x9fb1('0x21');var _0x1e0c7a=_0x1c388c['count'];var _0x135bc1=_0x1a96fa?0x0:_0x4b8ec5[_0x9fb1('0x1f')];var _0x4debcc=_0x1a96fa?_0x1c388c[_0x9fb1('0x22')]:_0x4b8ec5[_0x9fb1('0x1f')]+_0x4b8ec5[_0x9fb1('0x20')];var _0x2e0889;if(_0x4debcc>=_0x1e0c7a){_0x4debcc=_0x1e0c7a;_0x2e0889=0xc8;}else{_0x2e0889=0xce;}_0x3a37b1[_0x9fb1('0x1e')](_0x2e0889);return _0x3a37b1['set'](_0x9fb1('0x23'),_0x135bc1+'-'+_0x4debcc+'/'+_0x1e0c7a)['json'](_0x1c388c);}return null;};}function patchUpdates(_0x14d7f2){return function(_0x4eb833){try{jsonpatch[_0x9fb1('0x24')](_0x4eb833,_0x14d7f2,!![]);}catch(_0x37dd1f){return BPromise[_0x9fb1('0x25')](_0x37dd1f);}return _0x4eb833[_0x9fb1('0x26')]();};}function saveUpdates(_0x42ef27,_0x197a5c){return function(_0x565bd6){if(_0x565bd6){return _0x565bd6[_0x9fb1('0x27')](_0x42ef27)[_0x9fb1('0x28')](function(_0x47c6ab){return _0x47c6ab;});}return null;};}function removeEntity(_0x4888ac,_0x351e56){return function(_0x5c3582){if(_0x5c3582){return _0x5c3582[_0x9fb1('0x29')]()[_0x9fb1('0x28')](function(){var _0x48db91=_0x5c3582['get']({'plain':!![]});var _0x29c641=_0x9fb1('0x2a');return db[_0x9fb1('0x2b')][_0x9fb1('0x29')]({'where':{'type':_0x29c641,'resourceId':_0x48db91['id']}})[_0x9fb1('0x28')](function(){return _0x5c3582;});})[_0x9fb1('0x28')](function(){_0x4888ac[_0x9fb1('0x1e')](0xcc)[_0x9fb1('0x2c')]();});}};}function handleEntityNotFound(_0x4569e3,_0x10573e){return function(_0x523f17){if(!_0x523f17){_0x4569e3[_0x9fb1('0x2d')](0x194);}return _0x523f17;};}function handleError(_0x2cd8b2,_0x102385){_0x102385=_0x102385||0x1f4;return function(_0x1564a9){logger[_0x9fb1('0x2e')](_0x1564a9[_0x9fb1('0x2f')]);if(_0x1564a9[_0x9fb1('0x30')]){delete _0x1564a9[_0x9fb1('0x30')];}_0x2cd8b2[_0x9fb1('0x1e')](_0x102385)[_0x9fb1('0x31')](_0x1564a9);};}exports[_0x9fb1('0x32')]=function(_0x5500ce,_0x178530){var _0x4f4624={},_0x5b0bd7={},_0x1268ad={'count':0x0,'rows':[]};var _0x316f7b=_[_0x9fb1('0x33')](db[_0x9fb1('0x34')][_0x9fb1('0x35')],function(_0x3a9156){return{'name':_0x3a9156[_0x9fb1('0x36')],'type':_0x3a9156[_0x9fb1('0x37')][_0x9fb1('0x38')]};});_0x5b0bd7[_0x9fb1('0x39')]=_[_0x9fb1('0x33')](_0x316f7b,'name');_0x5b0bd7[_0x9fb1('0x3a')]=_[_0x9fb1('0x3b')](_0x5500ce[_0x9fb1('0x3a')]);_0x5b0bd7[_0x9fb1('0x3c')]=_['intersection'](_0x5b0bd7['model'],_0x5b0bd7[_0x9fb1('0x3a')]);_0x4f4624['attributes']=_['intersection'](_0x5b0bd7['model'],qs[_0x9fb1('0x3d')](_0x5500ce[_0x9fb1('0x3a')][_0x9fb1('0x3d')]));_0x4f4624[_0x9fb1('0x3e')]=_0x4f4624[_0x9fb1('0x3e')][_0x9fb1('0x3f')]?_0x4f4624['attributes']:_0x5b0bd7[_0x9fb1('0x39')];if(!_0x5500ce['query'][_0x9fb1('0x40')](_0x9fb1('0x41'))){_0x4f4624['limit']=qs[_0x9fb1('0x20')](_0x5500ce[_0x9fb1('0x3a')][_0x9fb1('0x20')]);_0x4f4624[_0x9fb1('0x1f')]=qs['offset'](_0x5500ce['query'][_0x9fb1('0x1f')]);}_0x4f4624['order']=qs[_0x9fb1('0x42')](_0x5500ce['query'][_0x9fb1('0x42')]);_0x4f4624[_0x9fb1('0x43')]=qs[_0x9fb1('0x3c')](_[_0x9fb1('0x44')](_0x5500ce[_0x9fb1('0x3a')],_0x5b0bd7[_0x9fb1('0x3c')]),_0x316f7b);if(_0x5500ce[_0x9fb1('0x3a')][_0x9fb1('0x45')]){_0x4f4624[_0x9fb1('0x43')]=_[_0x9fb1('0x46')](_0x4f4624['where'],{'$or':_['map'](_0x316f7b,function(_0x23e1dd){if(_0x23e1dd['type']!==_0x9fb1('0x47')){var _0x5d70b4={};_0x5d70b4[_0x23e1dd['name']]={'$like':'%'+_0x5500ce[_0x9fb1('0x3a')][_0x9fb1('0x45')]+'%'};return _0x5d70b4;}})});}_0x4f4624=_[_0x9fb1('0x46')]({},_0x4f4624,_0x5500ce[_0x9fb1('0x48')]);var _0x395849={'where':_0x4f4624[_0x9fb1('0x43')]};return db[_0x9fb1('0x34')][_0x9fb1('0x22')](_0x395849)['then'](function(_0x1078f1){_0x1268ad['count']=_0x1078f1;if(_0x5500ce[_0x9fb1('0x3a')][_0x9fb1('0x49')]){_0x4f4624[_0x9fb1('0x4a')]=[{'all':!![]}];}return db[_0x9fb1('0x34')]['findAll'](_0x4f4624);})[_0x9fb1('0x28')](function(_0x153013){_0x1268ad[_0x9fb1('0x4b')]=_0x153013;return _0x1268ad;})[_0x9fb1('0x28')](respondWithFilteredResult(_0x178530,_0x4f4624))[_0x9fb1('0x4c')](handleError(_0x178530,null));};exports[_0x9fb1('0x4d')]=function(_0x2cd4ed,_0x2c8660){var _0xa9f74={'raw':!![],'where':{'id':_0x2cd4ed[_0x9fb1('0x4e')]['id']}},_0x10f642={};_0x10f642[_0x9fb1('0x39')]=_[_0x9fb1('0x3b')](db[_0x9fb1('0x34')][_0x9fb1('0x35')]);_0x10f642[_0x9fb1('0x3a')]=_[_0x9fb1('0x3b')](_0x2cd4ed[_0x9fb1('0x3a')]);_0x10f642[_0x9fb1('0x3c')]=_['intersection'](_0x10f642[_0x9fb1('0x39')],_0x10f642[_0x9fb1('0x3a')]);_0xa9f74[_0x9fb1('0x3e')]=_[_0x9fb1('0x4f')](_0x10f642[_0x9fb1('0x39')],qs[_0x9fb1('0x3d')](_0x2cd4ed[_0x9fb1('0x3a')][_0x9fb1('0x3d')]));_0xa9f74['attributes']=_0xa9f74[_0x9fb1('0x3e')][_0x9fb1('0x3f')]?_0xa9f74[_0x9fb1('0x3e')]:_0x10f642[_0x9fb1('0x39')];if(_0x2cd4ed['query'][_0x9fb1('0x49')]){_0xa9f74[_0x9fb1('0x4a')]=[{'all':!![]}];}_0xa9f74=_[_0x9fb1('0x46')]({},_0xa9f74,_0x2cd4ed[_0x9fb1('0x48')]);return db[_0x9fb1('0x34')][_0x9fb1('0x50')](_0xa9f74)[_0x9fb1('0x28')](handleEntityNotFound(_0x2c8660,null))[_0x9fb1('0x28')](respondWithResult(_0x2c8660,null))['catch'](handleError(_0x2c8660,null));};exports[_0x9fb1('0x51')]=function(_0x1d84ea,_0x4f218f){return db[_0x9fb1('0x34')][_0x9fb1('0x51')](_0x1d84ea[_0x9fb1('0x52')],{})['then'](function(_0x3915df){var _0x346e88=_0x1d84ea[_0x9fb1('0x53')][_0x9fb1('0x54')]({'plain':!![]});if(!_0x346e88)throw new Error(_0x9fb1('0x55'));if(_0x346e88[_0x9fb1('0x56')]===_0x9fb1('0x53')){var _0x5306a1=_0x3915df[_0x9fb1('0x54')]({'plain':!![]});var _0x55104d=_0x9fb1('0x2a');return db['UserProfileSection'][_0x9fb1('0x50')]({'where':{'name':_0x55104d,'userProfileId':_0x346e88[_0x9fb1('0x57')]},'raw':!![]})[_0x9fb1('0x28')](function(_0x3040ed){if(_0x3040ed&&_0x3040ed[_0x9fb1('0x58')]===0x0){return db[_0x9fb1('0x2b')][_0x9fb1('0x51')]({'name':_0x5306a1['name'],'resourceId':_0x5306a1['id'],'type':_0x3040ed[_0x9fb1('0x30')],'sectionId':_0x3040ed['id']},{})[_0x9fb1('0x28')](function(){return _0x3915df;});}else{return _0x3915df;}})[_0x9fb1('0x4c')](function(_0x3bf36f){logger[_0x9fb1('0x2e')](_0x9fb1('0x59'),_0x3bf36f);throw _0x3bf36f;});}return _0x3915df;})[_0x9fb1('0x28')](respondWithResult(_0x4f218f,0xc9))[_0x9fb1('0x4c')](handleError(_0x4f218f,null));};exports[_0x9fb1('0x27')]=function(_0xb47c5a,_0x1886f5){if(_0xb47c5a[_0x9fb1('0x52')]['id']){delete _0xb47c5a['body']['id'];}return db[_0x9fb1('0x34')][_0x9fb1('0x50')]({'where':{'id':_0xb47c5a[_0x9fb1('0x4e')]['id']}})['then'](handleEntityNotFound(_0x1886f5,null))[_0x9fb1('0x28')](saveUpdates(_0xb47c5a['body'],null))[_0x9fb1('0x28')](respondWithResult(_0x1886f5,null))[_0x9fb1('0x4c')](handleError(_0x1886f5,null));};exports[_0x9fb1('0x29')]=function(_0x429548,_0x1a544b){return db[_0x9fb1('0x34')]['find']({'where':{'id':_0x429548[_0x9fb1('0x4e')]['id']}})['then'](handleEntityNotFound(_0x1a544b,null))[_0x9fb1('0x28')](removeEntity(_0x1a544b,null))['catch'](handleError(_0x1a544b,null));};