1a02f4d95f460bfd5ce0fdbc064a8ac3c09b355f
[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 _0x761f=['json','limit','undefined','offset','Content-Range','apply','reject','save','update','then','get','Templates','destroy','end','name','send','index','map','Template','rawAttributes','fieldName','type','model','query','keys','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','merge','VIRTUAL','options','findAll','rows','catch','show','params','find','create','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','eml-format','rimraf','zip-dir','bluebird','mustache','sox','to-csv','ejs','fs-extra','lodash','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status'];(function(_0x4e8401,_0x51b328){var _0x17269f=function(_0x35ac65){while(--_0x35ac65){_0x4e8401['push'](_0x4e8401['shift']());}};_0x17269f(++_0x51b328);}(_0x761f,0x1ce));var _0xf761=function(_0xa45395,_0x5579e6){_0xa45395=_0xa45395-0x0;var _0x3c83d2=_0x761f[_0xa45395];return _0x3c83d2;};'use strict';var emlformat=require(_0xf761('0x0'));var rimraf=require(_0xf761('0x1'));var zipdir=require(_0xf761('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xf761('0x3'));var Mustache=require(_0xf761('0x4'));var util=require('util');var path=require('path');var sox=require(_0xf761('0x5'));var csv=require(_0xf761('0x6'));var ejs=require(_0xf761('0x7'));var fs=require('fs');var fs_extra=require(_0xf761('0x8'));var _=require(_0xf761('0x9'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xf761('0xa'));var toCsv=require('to-csv');var querystring=require(_0xf761('0xb'));var Papa=require(_0xf761('0xc'));var Redis=require(_0xf761('0xd'));var authService=require(_0xf761('0xe'));var qs=require('../../components/parsers/qs');var as=require(_0xf761('0xf'));var hardwareService=require(_0xf761('0x10'));var logger=require(_0xf761('0x11'))('api');var utils=require(_0xf761('0x12'));var config=require(_0xf761('0x13'));var licenseUtil=require(_0xf761('0x14'));var db=require(_0xf761('0x15'))['db'];function respondWithStatusCode(_0x25b59c,_0x1ad781){_0x1ad781=_0x1ad781||0xcc;return function(_0x4951c6){if(_0x4951c6){return _0x25b59c[_0xf761('0x16')](_0x1ad781);}return _0x25b59c[_0xf761('0x17')](_0x1ad781)['end']();};}function respondWithResult(_0x384585,_0x469efa){_0x469efa=_0x469efa||0xc8;return function(_0x4b73fe){if(_0x4b73fe){return _0x384585[_0xf761('0x17')](_0x469efa)[_0xf761('0x18')](_0x4b73fe);}};}function respondWithFilteredResult(_0x4d75f2,_0x48bcf1){return function(_0x5b1f50){if(_0x5b1f50){var _0x1cb7b9=typeof _0x48bcf1['offset']==='undefined'&&typeof _0x48bcf1[_0xf761('0x19')]===_0xf761('0x1a');var _0xc0ad52=_0x5b1f50['count'];var _0x5535e3=_0x1cb7b9?0x0:_0x48bcf1[_0xf761('0x1b')];var _0x42331c=_0x1cb7b9?_0x5b1f50['count']:_0x48bcf1['offset']+_0x48bcf1['limit'];var _0x1a4793;if(_0x42331c>=_0xc0ad52){_0x42331c=_0xc0ad52;_0x1a4793=0xc8;}else{_0x1a4793=0xce;}_0x4d75f2[_0xf761('0x17')](_0x1a4793);return _0x4d75f2['set'](_0xf761('0x1c'),_0x5535e3+'-'+_0x42331c+'/'+_0xc0ad52)[_0xf761('0x18')](_0x5b1f50);}return null;};}function patchUpdates(_0x5c6aa6){return function(_0x3cb8da){try{jsonpatch[_0xf761('0x1d')](_0x3cb8da,_0x5c6aa6,!![]);}catch(_0x5702d6){return BPromise[_0xf761('0x1e')](_0x5702d6);}return _0x3cb8da[_0xf761('0x1f')]();};}function saveUpdates(_0x42d082,_0x4254cc){return function(_0x7a61c4){if(_0x7a61c4){return _0x7a61c4[_0xf761('0x20')](_0x42d082)[_0xf761('0x21')](function(_0xee88e6){return _0xee88e6;});}return null;};}function removeEntity(_0xb1ab3e,_0x1ec814){return function(_0x2ad54f){if(_0x2ad54f){return _0x2ad54f['destroy']()[_0xf761('0x21')](function(){var _0x135f32=_0x2ad54f[_0xf761('0x22')]({'plain':!![]});var _0x43d359=_0xf761('0x23');return db['UserProfileResource'][_0xf761('0x24')]({'where':{'type':_0x43d359,'resourceId':_0x135f32['id']}})['then'](function(){return _0x2ad54f;});})[_0xf761('0x21')](function(){_0xb1ab3e[_0xf761('0x17')](0xcc)[_0xf761('0x25')]();});}};}function handleEntityNotFound(_0x42eed5,_0x5d1354){return function(_0x2230d5){if(!_0x2230d5){_0x42eed5[_0xf761('0x16')](0x194);}return _0x2230d5;};}function handleError(_0x3a35b5,_0x3608f3){_0x3608f3=_0x3608f3||0x1f4;return function(_0x3894b9){logger['error'](_0x3894b9['stack']);if(_0x3894b9[_0xf761('0x26')]){delete _0x3894b9[_0xf761('0x26')];}_0x3a35b5[_0xf761('0x17')](_0x3608f3)[_0xf761('0x27')](_0x3894b9);};}exports[_0xf761('0x28')]=function(_0x1266b8,_0x3a50d3){var _0x2a0cf3={},_0x54522f={},_0x5acaf2={'count':0x0,'rows':[]};var _0x3720eb=_[_0xf761('0x29')](db[_0xf761('0x2a')][_0xf761('0x2b')],function(_0x179b78){return{'name':_0x179b78[_0xf761('0x2c')],'type':_0x179b78[_0xf761('0x2d')]['key']};});_0x54522f[_0xf761('0x2e')]=_[_0xf761('0x29')](_0x3720eb,_0xf761('0x26'));_0x54522f[_0xf761('0x2f')]=_[_0xf761('0x30')](_0x1266b8[_0xf761('0x2f')]);_0x54522f['filters']=_[_0xf761('0x31')](_0x54522f['model'],_0x54522f[_0xf761('0x2f')]);_0x2a0cf3['attributes']=_[_0xf761('0x31')](_0x54522f[_0xf761('0x2e')],qs[_0xf761('0x32')](_0x1266b8[_0xf761('0x2f')][_0xf761('0x32')]));_0x2a0cf3[_0xf761('0x33')]=_0x2a0cf3[_0xf761('0x33')][_0xf761('0x34')]?_0x2a0cf3[_0xf761('0x33')]:_0x54522f[_0xf761('0x2e')];if(!_0x1266b8[_0xf761('0x2f')][_0xf761('0x35')](_0xf761('0x36'))){_0x2a0cf3['limit']=qs['limit'](_0x1266b8['query']['limit']);_0x2a0cf3['offset']=qs[_0xf761('0x1b')](_0x1266b8['query'][_0xf761('0x1b')]);}_0x2a0cf3[_0xf761('0x37')]=qs[_0xf761('0x38')](_0x1266b8[_0xf761('0x2f')][_0xf761('0x38')]);_0x2a0cf3[_0xf761('0x39')]=qs[_0xf761('0x3a')](_[_0xf761('0x3b')](_0x1266b8[_0xf761('0x2f')],_0x54522f[_0xf761('0x3a')]),_0x3720eb);if(_0x1266b8[_0xf761('0x2f')][_0xf761('0x3c')]){_0x2a0cf3['where']=_[_0xf761('0x3d')](_0x2a0cf3[_0xf761('0x39')],{'$or':_[_0xf761('0x29')](_0x3720eb,function(_0x65d1fd){if(_0x65d1fd[_0xf761('0x2d')]!==_0xf761('0x3e')){var _0x4a6997={};_0x4a6997[_0x65d1fd['name']]={'$like':'%'+_0x1266b8[_0xf761('0x2f')][_0xf761('0x3c')]+'%'};return _0x4a6997;}})});}_0x2a0cf3=_['merge']({},_0x2a0cf3,_0x1266b8[_0xf761('0x3f')]);var _0x54179f={'where':_0x2a0cf3['where']};return db[_0xf761('0x2a')]['count'](_0x54179f)['then'](function(_0x3b282d){_0x5acaf2['count']=_0x3b282d;if(_0x1266b8['query']['includeAll']){_0x2a0cf3['include']=[{'all':!![]}];}return db['Template'][_0xf761('0x40')](_0x2a0cf3);})[_0xf761('0x21')](function(_0x49d971){_0x5acaf2[_0xf761('0x41')]=_0x49d971;return _0x5acaf2;})['then'](respondWithFilteredResult(_0x3a50d3,_0x2a0cf3))[_0xf761('0x42')](handleError(_0x3a50d3,null));};exports[_0xf761('0x43')]=function(_0x11b15e,_0x322461){var _0x3dbf67={'raw':!![],'where':{'id':_0x11b15e[_0xf761('0x44')]['id']}},_0x1dc34d={};_0x1dc34d['model']=_[_0xf761('0x30')](db[_0xf761('0x2a')][_0xf761('0x2b')]);_0x1dc34d['query']=_['keys'](_0x11b15e[_0xf761('0x2f')]);_0x1dc34d['filters']=_[_0xf761('0x31')](_0x1dc34d['model'],_0x1dc34d[_0xf761('0x2f')]);_0x3dbf67['attributes']=_[_0xf761('0x31')](_0x1dc34d[_0xf761('0x2e')],qs[_0xf761('0x32')](_0x11b15e['query'][_0xf761('0x32')]));_0x3dbf67['attributes']=_0x3dbf67['attributes']['length']?_0x3dbf67['attributes']:_0x1dc34d['model'];if(_0x11b15e[_0xf761('0x2f')]['includeAll']){_0x3dbf67['include']=[{'all':!![]}];}_0x3dbf67=_[_0xf761('0x3d')]({},_0x3dbf67,_0x11b15e[_0xf761('0x3f')]);return db[_0xf761('0x2a')][_0xf761('0x45')](_0x3dbf67)[_0xf761('0x21')](handleEntityNotFound(_0x322461,null))[_0xf761('0x21')](respondWithResult(_0x322461,null))[_0xf761('0x42')](handleError(_0x322461,null));};exports[_0xf761('0x46')]=function(_0xb3dfd4,_0x55d496){return db['Template'][_0xf761('0x46')](_0xb3dfd4['body'],{})[_0xf761('0x21')](function(_0x4bd887){var _0x87d466=_0xb3dfd4['user'][_0xf761('0x22')]({'plain':!![]});if(!_0x87d466)throw new Error(_0xf761('0x47'));if(_0x87d466[_0xf761('0x48')]===_0xf761('0x49')){var _0x9397c6=_0x4bd887[_0xf761('0x22')]({'plain':!![]});var _0x535bbc=_0xf761('0x23');return db[_0xf761('0x4a')][_0xf761('0x45')]({'where':{'name':_0x535bbc,'userProfileId':_0x87d466[_0xf761('0x4b')]},'raw':!![]})[_0xf761('0x21')](function(_0x56ef4f){if(_0x56ef4f&&_0x56ef4f[_0xf761('0x4c')]===0x0){return db[_0xf761('0x4d')][_0xf761('0x46')]({'name':_0x9397c6[_0xf761('0x26')],'resourceId':_0x9397c6['id'],'type':_0x56ef4f[_0xf761('0x26')],'sectionId':_0x56ef4f['id']},{})[_0xf761('0x21')](function(){return _0x4bd887;});}else{return _0x4bd887;}})[_0xf761('0x42')](function(_0x293bb5){logger[_0xf761('0x4e')](_0xf761('0x4f'),_0x293bb5);throw _0x293bb5;});}return _0x4bd887;})[_0xf761('0x21')](respondWithResult(_0x55d496,0xc9))[_0xf761('0x42')](handleError(_0x55d496,null));};exports[_0xf761('0x20')]=function(_0x1d14dd,_0x1e90da){if(_0x1d14dd[_0xf761('0x50')]['id']){delete _0x1d14dd[_0xf761('0x50')]['id'];}return db[_0xf761('0x2a')][_0xf761('0x45')]({'where':{'id':_0x1d14dd[_0xf761('0x44')]['id']}})[_0xf761('0x21')](handleEntityNotFound(_0x1e90da,null))['then'](saveUpdates(_0x1d14dd[_0xf761('0x50')],null))[_0xf761('0x21')](respondWithResult(_0x1e90da,null))[_0xf761('0x42')](handleError(_0x1e90da,null));};exports[_0xf761('0x24')]=function(_0x263970,_0x29ef1d){return db[_0xf761('0x2a')][_0xf761('0x45')]({'where':{'id':_0x263970[_0xf761('0x44')]['id']}})[_0xf761('0x21')](handleEntityNotFound(_0x29ef1d,null))[_0xf761('0x21')](removeEntity(_0x29ef1d,null))[_0xf761('0x42')](handleError(_0x29ef1d,null));};