Built motion from commit (unavailable).|2.4.15
[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 _0x782f=['where','pick','VIRTUAL','filter','merge','options','includeAll','findAll','show','params','include','find','catch','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','create','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','html-pdf','rimraf','zip-dir','request-promise','moment','bluebird','mustache','path','ejs','lodash','crypto','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','sendStatus','status','end','json','offset','limit','set','Content-Range','apply','update','then','destroy','Templates','UserProfileResource','error','stack','name','send','index','map','Template','rawAttributes','fieldName','type','key','model','keys','filters','intersection','query','attributes','fields','length','hasOwnProperty','nolimit','order'];(function(_0x5e58b2,_0x1b7ed4){var _0x4facd0=function(_0x58d136){while(--_0x58d136){_0x5e58b2['push'](_0x5e58b2['shift']());}};_0x4facd0(++_0x1b7ed4);}(_0x782f,0x103));var _0xf782=function(_0x55fb03,_0x220d4f){_0x55fb03=_0x55fb03-0x0;var _0x2b0772=_0x782f[_0x55fb03];return _0x2b0772;};'use strict';var pdf=require(_0xf782('0x0'));var emlformat=require('eml-format');var rimraf=require(_0xf782('0x1'));var zipdir=require(_0xf782('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf782('0x3'));var moment=require(_0xf782('0x4'));var BPromise=require(_0xf782('0x5'));var Mustache=require(_0xf782('0x6'));var util=require('util');var path=require(_0xf782('0x7'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xf782('0x8'));var fs=require('fs');var _=require(_0xf782('0x9'));var squel=require('squel');var crypto=require(_0xf782('0xa'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xf782('0xb'));var querystring=require(_0xf782('0xc'));var Papa=require(_0xf782('0xd'));var Redis=require(_0xf782('0xe'));var authService=require('../../components/auth/service');var qs=require(_0xf782('0xf'));var as=require(_0xf782('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xf782('0x11'))(_0xf782('0x12'));var utils=require(_0xf782('0x13'));var config=require(_0xf782('0x14'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2fb63c,_0x574937){_0x574937=_0x574937||0xcc;return function(_0x3e9232){if(_0x3e9232){return _0x2fb63c[_0xf782('0x15')](_0x574937);}return _0x2fb63c[_0xf782('0x16')](_0x574937)[_0xf782('0x17')]();};}function respondWithResult(_0x517979,_0x55fff3){_0x55fff3=_0x55fff3||0xc8;return function(_0x816d32){if(_0x816d32){return _0x517979[_0xf782('0x16')](_0x55fff3)[_0xf782('0x18')](_0x816d32);}};}function respondWithFilteredResult(_0x372f7b,_0x509a0b){return function(_0x60d768){if(_0x60d768){var _0x1a769a=_0x60d768['count'],_0x5e2236=_0x509a0b[_0xf782('0x19')],_0x472ce3=_0x509a0b[_0xf782('0x19')]+_0x509a0b[_0xf782('0x1a')],_0x248612;if(_0x472ce3>=_0x1a769a){_0x472ce3=_0x1a769a;_0x248612=0xc8;}else{_0x248612=0xce;}_0x372f7b[_0xf782('0x16')](_0x248612);return _0x372f7b[_0xf782('0x1b')](_0xf782('0x1c'),_0x5e2236+'-'+_0x472ce3+'/'+_0x1a769a)['json'](_0x60d768);}return null;};}function patchUpdates(_0x2069a7){return function(_0x362af8){try{jsonpatch[_0xf782('0x1d')](_0x362af8,_0x2069a7,!![]);}catch(_0xa05cd7){return BPromise['reject'](_0xa05cd7);}return _0x362af8['save']();};}function saveUpdates(_0x3ebe32,_0x5c8a64){return function(_0xa70819){if(_0xa70819){return _0xa70819[_0xf782('0x1e')](_0x3ebe32)[_0xf782('0x1f')](function(_0x4ba288){return _0x4ba288;});}return null;};}function removeEntity(_0x98ad54,_0x34b64f){return function(_0x4b3c2d){if(_0x4b3c2d){return _0x4b3c2d[_0xf782('0x20')]()[_0xf782('0x1f')](function(){var _0x368e3b=_0x4b3c2d['get']({'plain':!![]});var _0x2cd018=_0xf782('0x21');return db[_0xf782('0x22')][_0xf782('0x20')]({'where':{'type':_0x2cd018,'resourceId':_0x368e3b['id']}})['then'](function(){return _0x4b3c2d;});})[_0xf782('0x1f')](function(){_0x98ad54[_0xf782('0x16')](0xcc)[_0xf782('0x17')]();});}};}function handleEntityNotFound(_0xbf7e3a,_0x2dec7c){return function(_0x2c2ed9){if(!_0x2c2ed9){_0xbf7e3a[_0xf782('0x15')](0x194);}return _0x2c2ed9;};}function handleError(_0x36ab3f,_0x1db98a){_0x1db98a=_0x1db98a||0x1f4;return function(_0x176735){logger[_0xf782('0x23')](_0x176735[_0xf782('0x24')]);if(_0x176735[_0xf782('0x25')]){delete _0x176735[_0xf782('0x25')];}_0x36ab3f['status'](_0x1db98a)[_0xf782('0x26')](_0x176735);};}exports[_0xf782('0x27')]=function(_0x544043,_0x512aad){var _0x2fae3c={},_0x5076cb={},_0x5b05b8={'count':0x0,'rows':[]};var _0x326bea=_[_0xf782('0x28')](db[_0xf782('0x29')][_0xf782('0x2a')],function(_0x507020){return{'name':_0x507020[_0xf782('0x2b')],'type':_0x507020[_0xf782('0x2c')][_0xf782('0x2d')]};});_0x5076cb[_0xf782('0x2e')]=_['map'](_0x326bea,_0xf782('0x25'));_0x5076cb['query']=_[_0xf782('0x2f')](_0x544043['query']);_0x5076cb[_0xf782('0x30')]=_[_0xf782('0x31')](_0x5076cb[_0xf782('0x2e')],_0x5076cb[_0xf782('0x32')]);_0x2fae3c[_0xf782('0x33')]=_[_0xf782('0x31')](_0x5076cb[_0xf782('0x2e')],qs[_0xf782('0x34')](_0x544043[_0xf782('0x32')][_0xf782('0x34')]));_0x2fae3c[_0xf782('0x33')]=_0x2fae3c[_0xf782('0x33')][_0xf782('0x35')]?_0x2fae3c[_0xf782('0x33')]:_0x5076cb[_0xf782('0x2e')];if(!_0x544043[_0xf782('0x32')][_0xf782('0x36')](_0xf782('0x37'))){_0x2fae3c[_0xf782('0x1a')]=qs[_0xf782('0x1a')](_0x544043[_0xf782('0x32')]['limit']);_0x2fae3c[_0xf782('0x19')]=qs[_0xf782('0x19')](_0x544043[_0xf782('0x32')][_0xf782('0x19')]);}_0x2fae3c[_0xf782('0x38')]=qs['sort'](_0x544043[_0xf782('0x32')]['sort']);_0x2fae3c[_0xf782('0x39')]=qs[_0xf782('0x30')](_[_0xf782('0x3a')](_0x544043[_0xf782('0x32')],_0x5076cb['filters']));if(_0x544043[_0xf782('0x32')]['filter']){_0x2fae3c['where']=_['merge'](_0x2fae3c[_0xf782('0x39')],{'$or':_[_0xf782('0x28')](_0x326bea,function(_0x2b300a){if(_0x2b300a[_0xf782('0x2c')]!==_0xf782('0x3b')){var _0x411ef4={};_0x411ef4[_0x2b300a[_0xf782('0x25')]]={'$like':'%'+_0x544043[_0xf782('0x32')][_0xf782('0x3c')]+'%'};return _0x411ef4;}})});}_0x2fae3c=_[_0xf782('0x3d')]({},_0x2fae3c,_0x544043[_0xf782('0x3e')]);var _0x24d369={'where':_0x2fae3c['where']};return db[_0xf782('0x29')]['count'](_0x24d369)[_0xf782('0x1f')](function(_0x343388){_0x5b05b8['count']=_0x343388;if(_0x544043[_0xf782('0x32')][_0xf782('0x3f')]){_0x2fae3c['include']=[{'all':!![]}];}return db[_0xf782('0x29')][_0xf782('0x40')](_0x2fae3c);})[_0xf782('0x1f')](function(_0x31765b){_0x5b05b8['rows']=_0x31765b;return _0x5b05b8;})[_0xf782('0x1f')](respondWithFilteredResult(_0x512aad,_0x2fae3c))['catch'](handleError(_0x512aad,null));};exports[_0xf782('0x41')]=function(_0x5e491b,_0x97964){var _0x3602f2={'raw':!![],'where':{'id':_0x5e491b[_0xf782('0x42')]['id']}},_0x16076a={};_0x16076a[_0xf782('0x2e')]=_[_0xf782('0x2f')](db['Template'][_0xf782('0x2a')]);_0x16076a['query']=_[_0xf782('0x2f')](_0x5e491b[_0xf782('0x32')]);_0x16076a[_0xf782('0x30')]=_[_0xf782('0x31')](_0x16076a[_0xf782('0x2e')],_0x16076a[_0xf782('0x32')]);_0x3602f2[_0xf782('0x33')]=_[_0xf782('0x31')](_0x16076a[_0xf782('0x2e')],qs['fields'](_0x5e491b[_0xf782('0x32')][_0xf782('0x34')]));_0x3602f2[_0xf782('0x33')]=_0x3602f2[_0xf782('0x33')][_0xf782('0x35')]?_0x3602f2[_0xf782('0x33')]:_0x16076a[_0xf782('0x2e')];if(_0x5e491b[_0xf782('0x32')][_0xf782('0x3f')]){_0x3602f2[_0xf782('0x43')]=[{'all':!![]}];}_0x3602f2=_['merge']({},_0x3602f2,_0x5e491b[_0xf782('0x3e')]);return db[_0xf782('0x29')][_0xf782('0x44')](_0x3602f2)['then'](handleEntityNotFound(_0x97964,null))['then'](respondWithResult(_0x97964,null))[_0xf782('0x45')](handleError(_0x97964,null));};exports['create']=function(_0x2917bd,_0x4f2c4b){return db[_0xf782('0x29')]['create'](_0x2917bd[_0xf782('0x46')],{})[_0xf782('0x1f')](function(_0x5754bf){var _0x4d7290=_0x2917bd[_0xf782('0x47')][_0xf782('0x48')]({'plain':!![]});if(!_0x4d7290)throw new Error(_0xf782('0x49'));if(_0x4d7290[_0xf782('0x4a')]===_0xf782('0x47')){var _0x495271=_0x5754bf[_0xf782('0x48')]({'plain':!![]});var _0x3d38b3=_0xf782('0x21');return db[_0xf782('0x4b')]['find']({'where':{'name':_0x3d38b3,'userProfileId':_0x4d7290[_0xf782('0x4c')]},'raw':!![]})[_0xf782('0x1f')](function(_0x1a2b82){if(_0x1a2b82&&_0x1a2b82['autoAssociation']===0x0){return db[_0xf782('0x22')][_0xf782('0x4d')]({'name':_0x495271[_0xf782('0x25')],'resourceId':_0x495271['id'],'type':_0x1a2b82[_0xf782('0x25')],'sectionId':_0x1a2b82['id']},{})[_0xf782('0x1f')](function(){return _0x5754bf;});}else{return _0x5754bf;}})[_0xf782('0x45')](function(_0x36dcdd){logger[_0xf782('0x23')](_0xf782('0x4e'),_0x36dcdd);throw _0x36dcdd;});}return _0x5754bf;})[_0xf782('0x1f')](respondWithResult(_0x4f2c4b,0xc9))[_0xf782('0x45')](handleError(_0x4f2c4b,null));};exports[_0xf782('0x1e')]=function(_0x419d5b,_0x4c7a7b){if(_0x419d5b[_0xf782('0x46')]['id']){delete _0x419d5b['body']['id'];}return db['Template'][_0xf782('0x44')]({'where':{'id':_0x419d5b['params']['id']}})[_0xf782('0x1f')](handleEntityNotFound(_0x4c7a7b,null))[_0xf782('0x1f')](saveUpdates(_0x419d5b['body'],null))[_0xf782('0x1f')](respondWithResult(_0x4c7a7b,null))['catch'](handleError(_0x4c7a7b,null));};exports[_0xf782('0x20')]=function(_0xbe785a,_0x1f5f5a){return db[_0xf782('0x29')][_0xf782('0x44')]({'where':{'id':_0xbe785a['params']['id']}})['then'](handleEntityNotFound(_0x1f5f5a,null))[_0xf782('0x1f')](removeEntity(_0x1f5f5a,null))[_0xf782('0x45')](handleError(_0x1f5f5a,null));};