5814137acbec465376332c4e986f66f1f1a5e29d
[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 _0x281f=['keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','includeAll','include','findAll','rows','catch','show','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','Templates','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','params','eml-format','zip-dir','moment','bluebird','mustache','util','sox','to-csv','ejs','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','sendStatus','status','offset','undefined','count','limit','Content-Range','json','reject','update','then','get','destroy','end','error','name','send','map','Template','rawAttributes','fieldName','type','key','model','query'];(function(_0x503a57,_0x3353f6){var _0xc4373b=function(_0x30c47d){while(--_0x30c47d){_0x503a57['push'](_0x503a57['shift']());}};_0xc4373b(++_0x3353f6);}(_0x281f,0x15c));var _0xf281=function(_0x1780f8,_0x11ba93){_0x1780f8=_0x1780f8-0x0;var _0x33a410=_0x281f[_0x1780f8];return _0x33a410;};'use strict';var emlformat=require(_0xf281('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xf281('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xf281('0x2'));var BPromise=require(_0xf281('0x3'));var Mustache=require(_0xf281('0x4'));var util=require(_0xf281('0x5'));var path=require('path');var sox=require(_0xf281('0x6'));var csv=require(_0xf281('0x7'));var ejs=require(_0xf281('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xf281('0x9'));var squel=require(_0xf281('0xa'));var crypto=require(_0xf281('0xb'));var jsforce=require('jsforce');var deskjs=require(_0xf281('0xc'));var toCsv=require('to-csv');var querystring=require(_0xf281('0xd'));var Papa=require(_0xf281('0xe'));var Redis=require(_0xf281('0xf'));var authService=require(_0xf281('0x10'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf281('0x11'));var logger=require(_0xf281('0x12'))(_0xf281('0x13'));var utils=require(_0xf281('0x14'));var config=require(_0xf281('0x15'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2657df,_0x21cea8){_0x21cea8=_0x21cea8||0xcc;return function(_0x464348){if(_0x464348){return _0x2657df[_0xf281('0x16')](_0x21cea8);}return _0x2657df['status'](_0x21cea8)['end']();};}function respondWithResult(_0x20be3f,_0x4fe738){_0x4fe738=_0x4fe738||0xc8;return function(_0x1548a3){if(_0x1548a3){return _0x20be3f[_0xf281('0x17')](_0x4fe738)['json'](_0x1548a3);}};}function respondWithFilteredResult(_0x53e17c,_0x11f0cd){return function(_0x193635){if(_0x193635){var _0xa28355=typeof _0x11f0cd[_0xf281('0x18')]==='undefined'&&typeof _0x11f0cd['limit']===_0xf281('0x19');var _0x4faf90=_0x193635[_0xf281('0x1a')];var _0x1fae50=_0xa28355?0x0:_0x11f0cd[_0xf281('0x18')];var _0x37a6ab=_0xa28355?_0x193635[_0xf281('0x1a')]:_0x11f0cd[_0xf281('0x18')]+_0x11f0cd[_0xf281('0x1b')];var _0xe92e30;if(_0x37a6ab>=_0x4faf90){_0x37a6ab=_0x4faf90;_0xe92e30=0xc8;}else{_0xe92e30=0xce;}_0x53e17c[_0xf281('0x17')](_0xe92e30);return _0x53e17c['set'](_0xf281('0x1c'),_0x1fae50+'-'+_0x37a6ab+'/'+_0x4faf90)[_0xf281('0x1d')](_0x193635);}return null;};}function patchUpdates(_0x2627bb){return function(_0x115e47){try{jsonpatch['apply'](_0x115e47,_0x2627bb,!![]);}catch(_0x461a19){return BPromise[_0xf281('0x1e')](_0x461a19);}return _0x115e47['save']();};}function saveUpdates(_0xaf7ec5,_0x782ab5){return function(_0x32e922){if(_0x32e922){return _0x32e922[_0xf281('0x1f')](_0xaf7ec5)[_0xf281('0x20')](function(_0x2dde38){return _0x2dde38;});}return null;};}function removeEntity(_0xf22075,_0x303c9c){return function(_0xbe4fa){if(_0xbe4fa){return _0xbe4fa['destroy']()[_0xf281('0x20')](function(){var _0x26f01e=_0xbe4fa[_0xf281('0x21')]({'plain':!![]});var _0x282761='Templates';return db['UserProfileResource'][_0xf281('0x22')]({'where':{'type':_0x282761,'resourceId':_0x26f01e['id']}})[_0xf281('0x20')](function(){return _0xbe4fa;});})[_0xf281('0x20')](function(){_0xf22075['status'](0xcc)[_0xf281('0x23')]();});}};}function handleEntityNotFound(_0x31200c,_0x2b76d1){return function(_0x3b8fb5){if(!_0x3b8fb5){_0x31200c[_0xf281('0x16')](0x194);}return _0x3b8fb5;};}function handleError(_0x38f093,_0x34b959){_0x34b959=_0x34b959||0x1f4;return function(_0x5d600d){logger[_0xf281('0x24')](_0x5d600d['stack']);if(_0x5d600d[_0xf281('0x25')]){delete _0x5d600d[_0xf281('0x25')];}_0x38f093['status'](_0x34b959)[_0xf281('0x26')](_0x5d600d);};}exports['index']=function(_0x2ba6f3,_0x5d831b){var _0x11525e={},_0x33b96b={},_0x310da8={'count':0x0,'rows':[]};var _0x2ae0a6=_[_0xf281('0x27')](db[_0xf281('0x28')][_0xf281('0x29')],function(_0x37db21){return{'name':_0x37db21[_0xf281('0x2a')],'type':_0x37db21[_0xf281('0x2b')][_0xf281('0x2c')]};});_0x33b96b[_0xf281('0x2d')]=_['map'](_0x2ae0a6,_0xf281('0x25'));_0x33b96b[_0xf281('0x2e')]=_[_0xf281('0x2f')](_0x2ba6f3['query']);_0x33b96b[_0xf281('0x30')]=_['intersection'](_0x33b96b[_0xf281('0x2d')],_0x33b96b[_0xf281('0x2e')]);_0x11525e[_0xf281('0x31')]=_[_0xf281('0x32')](_0x33b96b['model'],qs[_0xf281('0x33')](_0x2ba6f3[_0xf281('0x2e')][_0xf281('0x33')]));_0x11525e['attributes']=_0x11525e[_0xf281('0x31')][_0xf281('0x34')]?_0x11525e[_0xf281('0x31')]:_0x33b96b[_0xf281('0x2d')];if(!_0x2ba6f3['query'][_0xf281('0x35')](_0xf281('0x36'))){_0x11525e[_0xf281('0x1b')]=qs[_0xf281('0x1b')](_0x2ba6f3['query']['limit']);_0x11525e[_0xf281('0x18')]=qs[_0xf281('0x18')](_0x2ba6f3[_0xf281('0x2e')][_0xf281('0x18')]);}_0x11525e[_0xf281('0x37')]=qs[_0xf281('0x38')](_0x2ba6f3['query'][_0xf281('0x38')]);_0x11525e[_0xf281('0x39')]=qs[_0xf281('0x30')](_[_0xf281('0x3a')](_0x2ba6f3['query'],_0x33b96b[_0xf281('0x30')]),_0x2ae0a6);if(_0x2ba6f3[_0xf281('0x2e')][_0xf281('0x3b')]){_0x11525e[_0xf281('0x39')]=_['merge'](_0x11525e[_0xf281('0x39')],{'$or':_['map'](_0x2ae0a6,function(_0x1b3e85){if(_0x1b3e85[_0xf281('0x2b')]!==_0xf281('0x3c')){var _0x5637db={};_0x5637db[_0x1b3e85['name']]={'$like':'%'+_0x2ba6f3['query'][_0xf281('0x3b')]+'%'};return _0x5637db;}})});}_0x11525e=_[_0xf281('0x3d')]({},_0x11525e,_0x2ba6f3['options']);var _0x314b62={'where':_0x11525e['where']};return db['Template'][_0xf281('0x1a')](_0x314b62)['then'](function(_0x54869c){_0x310da8[_0xf281('0x1a')]=_0x54869c;if(_0x2ba6f3[_0xf281('0x2e')][_0xf281('0x3e')]){_0x11525e[_0xf281('0x3f')]=[{'all':!![]}];}return db[_0xf281('0x28')][_0xf281('0x40')](_0x11525e);})['then'](function(_0x299513){_0x310da8[_0xf281('0x41')]=_0x299513;return _0x310da8;})[_0xf281('0x20')](respondWithFilteredResult(_0x5d831b,_0x11525e))[_0xf281('0x42')](handleError(_0x5d831b,null));};exports[_0xf281('0x43')]=function(_0x2ca43b,_0x57c825){var _0x2961c0={'raw':!![],'where':{'id':_0x2ca43b['params']['id']}},_0x487cd9={};_0x487cd9[_0xf281('0x2d')]=_[_0xf281('0x2f')](db['Template'][_0xf281('0x29')]);_0x487cd9['query']=_[_0xf281('0x2f')](_0x2ca43b[_0xf281('0x2e')]);_0x487cd9[_0xf281('0x30')]=_[_0xf281('0x32')](_0x487cd9[_0xf281('0x2d')],_0x487cd9['query']);_0x2961c0['attributes']=_['intersection'](_0x487cd9[_0xf281('0x2d')],qs[_0xf281('0x33')](_0x2ca43b['query'][_0xf281('0x33')]));_0x2961c0['attributes']=_0x2961c0[_0xf281('0x31')][_0xf281('0x34')]?_0x2961c0['attributes']:_0x487cd9[_0xf281('0x2d')];if(_0x2ca43b['query'][_0xf281('0x3e')]){_0x2961c0[_0xf281('0x3f')]=[{'all':!![]}];}_0x2961c0=_[_0xf281('0x3d')]({},_0x2961c0,_0x2ca43b['options']);return db[_0xf281('0x28')][_0xf281('0x44')](_0x2961c0)[_0xf281('0x20')](handleEntityNotFound(_0x57c825,null))[_0xf281('0x20')](respondWithResult(_0x57c825,null))[_0xf281('0x42')](handleError(_0x57c825,null));};exports['create']=function(_0x44c827,_0x3fd12d){return db[_0xf281('0x28')][_0xf281('0x45')](_0x44c827[_0xf281('0x46')],{})[_0xf281('0x20')](function(_0x180911){var _0x55a972=_0x44c827[_0xf281('0x47')][_0xf281('0x21')]({'plain':!![]});if(!_0x55a972)throw new Error(_0xf281('0x48'));if(_0x55a972['role']==='user'){var _0x2fe53b=_0x180911['get']({'plain':!![]});var _0x4fa850=_0xf281('0x49');return db[_0xf281('0x4a')][_0xf281('0x44')]({'where':{'name':_0x4fa850,'userProfileId':_0x55a972[_0xf281('0x4b')]},'raw':!![]})[_0xf281('0x20')](function(_0x18bfe8){if(_0x18bfe8&&_0x18bfe8[_0xf281('0x4c')]===0x0){return db[_0xf281('0x4d')][_0xf281('0x45')]({'name':_0x2fe53b[_0xf281('0x25')],'resourceId':_0x2fe53b['id'],'type':_0x18bfe8[_0xf281('0x25')],'sectionId':_0x18bfe8['id']},{})[_0xf281('0x20')](function(){return _0x180911;});}else{return _0x180911;}})['catch'](function(_0x33f617){logger[_0xf281('0x24')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x33f617);throw _0x33f617;});}return _0x180911;})[_0xf281('0x20')](respondWithResult(_0x3fd12d,0xc9))[_0xf281('0x42')](handleError(_0x3fd12d,null));};exports[_0xf281('0x1f')]=function(_0x2589ea,_0x4d1be1){if(_0x2589ea['body']['id']){delete _0x2589ea[_0xf281('0x46')]['id'];}return db[_0xf281('0x28')]['find']({'where':{'id':_0x2589ea[_0xf281('0x4e')]['id']}})[_0xf281('0x20')](handleEntityNotFound(_0x4d1be1,null))[_0xf281('0x20')](saveUpdates(_0x2589ea[_0xf281('0x46')],null))[_0xf281('0x20')](respondWithResult(_0x4d1be1,null))[_0xf281('0x42')](handleError(_0x4d1be1,null));};exports[_0xf281('0x22')]=function(_0x133fcc,_0x85198f){return db[_0xf281('0x28')]['find']({'where':{'id':_0x133fcc[_0xf281('0x4e')]['id']}})[_0xf281('0x20')](handleEntityNotFound(_0x85198f,null))[_0xf281('0x20')](removeEntity(_0x85198f,null))[_0xf281('0x42')](handleError(_0x85198f,null));};