Built motion from commit 00bf23f6.|2.6.16
[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 _0x956f=['apply','reject','update','then','destroy','get','Templates','UserProfileResource','error','stack','name','map','Template','rawAttributes','type','key','query','keys','filters','intersection','attributes','model','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','filter','options','include','findAll','show','params','includeAll','find','catch','create','body','user','role','UserProfileSection','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','eml-format','zip-dir','fast-json-patch','moment','bluebird','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../mysqldb','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range'];(function(_0x5ddb1c,_0x5ea4df){var _0x354adc=function(_0x2dfb1f){while(--_0x2dfb1f){_0x5ddb1c['push'](_0x5ddb1c['shift']());}};_0x354adc(++_0x5ea4df);}(_0x956f,0xcf));var _0xf956=function(_0x3d1187,_0x2a8e79){_0x3d1187=_0x3d1187-0x0;var _0x3ceb4d=_0x956f[_0x3d1187];return _0x3ceb4d;};'use strict';var emlformat=require(_0xf956('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xf956('0x1'));var jsonpatch=require(_0xf956('0x2'));var rp=require('request-promise');var moment=require(_0xf956('0x3'));var BPromise=require(_0xf956('0x4'));var Mustache=require('mustache');var util=require(_0xf956('0x5'));var path=require('path');var sox=require(_0xf956('0x6'));var csv=require(_0xf956('0x7'));var ejs=require(_0xf956('0x8'));var fs=require('fs');var fs_extra=require(_0xf956('0x9'));var _=require(_0xf956('0xa'));var squel=require(_0xf956('0xb'));var crypto=require(_0xf956('0xc'));var jsforce=require(_0xf956('0xd'));var deskjs=require(_0xf956('0xe'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0xf956('0xf'));var Redis=require(_0xf956('0x10'));var authService=require(_0xf956('0x11'));var qs=require(_0xf956('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf956('0x13'));var logger=require(_0xf956('0x14'))('api');var utils=require(_0xf956('0x15'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xf956('0x16'))['db'];function respondWithStatusCode(_0x5762ec,_0x5369ae){_0x5369ae=_0x5369ae||0xcc;return function(_0x54f4a9){if(_0x54f4a9){return _0x5762ec[_0xf956('0x17')](_0x5369ae);}return _0x5762ec[_0xf956('0x18')](_0x5369ae)[_0xf956('0x19')]();};}function respondWithResult(_0x46f1c7,_0x1d340b){_0x1d340b=_0x1d340b||0xc8;return function(_0x289950){if(_0x289950){return _0x46f1c7[_0xf956('0x18')](_0x1d340b)[_0xf956('0x1a')](_0x289950);}};}function respondWithFilteredResult(_0x275e12,_0x1c0381){return function(_0x3c423e){if(_0x3c423e){var _0x53a701=typeof _0x1c0381[_0xf956('0x1b')]==='undefined'&&typeof _0x1c0381[_0xf956('0x1c')]===_0xf956('0x1d');var _0x599ead=_0x3c423e[_0xf956('0x1e')];var _0x24245f=_0x53a701?0x0:_0x1c0381[_0xf956('0x1b')];var _0x2a534a=_0x53a701?_0x3c423e[_0xf956('0x1e')]:_0x1c0381[_0xf956('0x1b')]+_0x1c0381[_0xf956('0x1c')];var _0x45e8ff;if(_0x2a534a>=_0x599ead){_0x2a534a=_0x599ead;_0x45e8ff=0xc8;}else{_0x45e8ff=0xce;}_0x275e12[_0xf956('0x18')](_0x45e8ff);return _0x275e12[_0xf956('0x1f')](_0xf956('0x20'),_0x24245f+'-'+_0x2a534a+'/'+_0x599ead)['json'](_0x3c423e);}return null;};}function patchUpdates(_0x41193d){return function(_0x198a66){try{jsonpatch[_0xf956('0x21')](_0x198a66,_0x41193d,!![]);}catch(_0x377f74){return BPromise[_0xf956('0x22')](_0x377f74);}return _0x198a66['save']();};}function saveUpdates(_0x2d6634,_0x5482e3){return function(_0x3a275b){if(_0x3a275b){return _0x3a275b[_0xf956('0x23')](_0x2d6634)[_0xf956('0x24')](function(_0x922baa){return _0x922baa;});}return null;};}function removeEntity(_0x2376fc,_0x541603){return function(_0x53cd90){if(_0x53cd90){return _0x53cd90[_0xf956('0x25')]()[_0xf956('0x24')](function(){var _0x18dab9=_0x53cd90[_0xf956('0x26')]({'plain':!![]});var _0x40ea9e=_0xf956('0x27');return db[_0xf956('0x28')][_0xf956('0x25')]({'where':{'type':_0x40ea9e,'resourceId':_0x18dab9['id']}})[_0xf956('0x24')](function(){return _0x53cd90;});})[_0xf956('0x24')](function(){_0x2376fc['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x338fc1,_0x47cd50){return function(_0x47fd4c){if(!_0x47fd4c){_0x338fc1[_0xf956('0x17')](0x194);}return _0x47fd4c;};}function handleError(_0x51a4c5,_0x4e62f8){_0x4e62f8=_0x4e62f8||0x1f4;return function(_0x2c66c0){logger[_0xf956('0x29')](_0x2c66c0[_0xf956('0x2a')]);if(_0x2c66c0[_0xf956('0x2b')]){delete _0x2c66c0[_0xf956('0x2b')];}_0x51a4c5[_0xf956('0x18')](_0x4e62f8)['send'](_0x2c66c0);};}exports['index']=function(_0x5276f4,_0x3111b6){var _0x348e61={},_0x471f7f={},_0x2266de={'count':0x0,'rows':[]};var _0x150e13=_[_0xf956('0x2c')](db[_0xf956('0x2d')][_0xf956('0x2e')],function(_0x1d38b2){return{'name':_0x1d38b2['fieldName'],'type':_0x1d38b2[_0xf956('0x2f')][_0xf956('0x30')]};});_0x471f7f['model']=_['map'](_0x150e13,_0xf956('0x2b'));_0x471f7f[_0xf956('0x31')]=_[_0xf956('0x32')](_0x5276f4['query']);_0x471f7f[_0xf956('0x33')]=_[_0xf956('0x34')](_0x471f7f['model'],_0x471f7f[_0xf956('0x31')]);_0x348e61[_0xf956('0x35')]=_[_0xf956('0x34')](_0x471f7f[_0xf956('0x36')],qs[_0xf956('0x37')](_0x5276f4[_0xf956('0x31')]['fields']));_0x348e61[_0xf956('0x35')]=_0x348e61['attributes'][_0xf956('0x38')]?_0x348e61['attributes']:_0x471f7f['model'];if(!_0x5276f4[_0xf956('0x31')][_0xf956('0x39')](_0xf956('0x3a'))){_0x348e61[_0xf956('0x1c')]=qs[_0xf956('0x1c')](_0x5276f4[_0xf956('0x31')][_0xf956('0x1c')]);_0x348e61[_0xf956('0x1b')]=qs[_0xf956('0x1b')](_0x5276f4[_0xf956('0x31')][_0xf956('0x1b')]);}_0x348e61[_0xf956('0x3b')]=qs[_0xf956('0x3c')](_0x5276f4[_0xf956('0x31')][_0xf956('0x3c')]);_0x348e61[_0xf956('0x3d')]=qs[_0xf956('0x33')](_[_0xf956('0x3e')](_0x5276f4['query'],_0x471f7f[_0xf956('0x33')]),_0x150e13);if(_0x5276f4[_0xf956('0x31')]['filter']){_0x348e61[_0xf956('0x3d')]=_[_0xf956('0x3f')](_0x348e61[_0xf956('0x3d')],{'$or':_['map'](_0x150e13,function(_0x59bf81){if(_0x59bf81[_0xf956('0x2f')]!==_0xf956('0x40')){var _0x3b676a={};_0x3b676a[_0x59bf81[_0xf956('0x2b')]]={'$like':'%'+_0x5276f4[_0xf956('0x31')][_0xf956('0x41')]+'%'};return _0x3b676a;}})});}_0x348e61=_['merge']({},_0x348e61,_0x5276f4[_0xf956('0x42')]);var _0x2e7ab5={'where':_0x348e61['where']};return db[_0xf956('0x2d')][_0xf956('0x1e')](_0x2e7ab5)['then'](function(_0xf5f31e){_0x2266de[_0xf956('0x1e')]=_0xf5f31e;if(_0x5276f4[_0xf956('0x31')]['includeAll']){_0x348e61[_0xf956('0x43')]=[{'all':!![]}];}return db[_0xf956('0x2d')][_0xf956('0x44')](_0x348e61);})[_0xf956('0x24')](function(_0xd3a616){_0x2266de['rows']=_0xd3a616;return _0x2266de;})['then'](respondWithFilteredResult(_0x3111b6,_0x348e61))['catch'](handleError(_0x3111b6,null));};exports[_0xf956('0x45')]=function(_0x18e39d,_0x48c2d6){var _0x318a0d={'raw':!![],'where':{'id':_0x18e39d[_0xf956('0x46')]['id']}},_0x27e98f={};_0x27e98f[_0xf956('0x36')]=_[_0xf956('0x32')](db['Template'][_0xf956('0x2e')]);_0x27e98f[_0xf956('0x31')]=_[_0xf956('0x32')](_0x18e39d[_0xf956('0x31')]);_0x27e98f[_0xf956('0x33')]=_[_0xf956('0x34')](_0x27e98f[_0xf956('0x36')],_0x27e98f[_0xf956('0x31')]);_0x318a0d[_0xf956('0x35')]=_['intersection'](_0x27e98f['model'],qs[_0xf956('0x37')](_0x18e39d['query']['fields']));_0x318a0d['attributes']=_0x318a0d['attributes']['length']?_0x318a0d[_0xf956('0x35')]:_0x27e98f[_0xf956('0x36')];if(_0x18e39d[_0xf956('0x31')][_0xf956('0x47')]){_0x318a0d['include']=[{'all':!![]}];}_0x318a0d=_['merge']({},_0x318a0d,_0x18e39d['options']);return db[_0xf956('0x2d')][_0xf956('0x48')](_0x318a0d)[_0xf956('0x24')](handleEntityNotFound(_0x48c2d6,null))[_0xf956('0x24')](respondWithResult(_0x48c2d6,null))[_0xf956('0x49')](handleError(_0x48c2d6,null));};exports['create']=function(_0x1d29f9,_0x547e6d){return db[_0xf956('0x2d')][_0xf956('0x4a')](_0x1d29f9[_0xf956('0x4b')],{})[_0xf956('0x24')](function(_0x1cf38c){var _0x8078f6=_0x1d29f9[_0xf956('0x4c')][_0xf956('0x26')]({'plain':!![]});if(!_0x8078f6)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x8078f6[_0xf956('0x4d')]==='user'){var _0x28d03d=_0x1cf38c[_0xf956('0x26')]({'plain':!![]});var _0x2fe844=_0xf956('0x27');return db[_0xf956('0x4e')][_0xf956('0x48')]({'where':{'name':_0x2fe844,'userProfileId':_0x8078f6['userProfileId']},'raw':!![]})[_0xf956('0x24')](function(_0x2f07ce){if(_0x2f07ce&&_0x2f07ce['autoAssociation']===0x0){return db[_0xf956('0x28')]['create']({'name':_0x28d03d[_0xf956('0x2b')],'resourceId':_0x28d03d['id'],'type':_0x2f07ce[_0xf956('0x2b')],'sectionId':_0x2f07ce['id']},{})['then'](function(){return _0x1cf38c;});}else{return _0x1cf38c;}})[_0xf956('0x49')](function(_0x4105aa){logger['error'](_0xf956('0x4f'),_0x4105aa);throw _0x4105aa;});}return _0x1cf38c;})[_0xf956('0x24')](respondWithResult(_0x547e6d,0xc9))['catch'](handleError(_0x547e6d,null));};exports[_0xf956('0x23')]=function(_0x29428b,_0x22833e){if(_0x29428b[_0xf956('0x4b')]['id']){delete _0x29428b['body']['id'];}return db[_0xf956('0x2d')]['find']({'where':{'id':_0x29428b['params']['id']}})[_0xf956('0x24')](handleEntityNotFound(_0x22833e,null))[_0xf956('0x24')](saveUpdates(_0x29428b[_0xf956('0x4b')],null))[_0xf956('0x24')](respondWithResult(_0x22833e,null))[_0xf956('0x49')](handleError(_0x22833e,null));};exports[_0xf956('0x25')]=function(_0x4c0216,_0x92236b){return db[_0xf956('0x2d')]['find']({'where':{'id':_0x4c0216[_0xf956('0x46')]['id']}})['then'](handleEntityNotFound(_0x92236b,null))[_0xf956('0x24')](removeEntity(_0x92236b,null))[_0xf956('0x49')](handleError(_0x92236b,null));};