f59d592c452cbb914dc5bea137f2032d3e70922e
[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 _0x1796=['VIRTUAL','filter','options','includeAll','include','findAll','rows','catch','show','params','keys','Template','find','create','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','body','eml-format','rimraf','zip-dir','bluebird','mustache','ejs','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','then','Templates','UserProfileResource','destroy','error','stack','name','send','rawAttributes','type','key','model','map','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge'];(function(_0x2d7ead,_0x48cb08){var _0x4ecf0e=function(_0xca9c69){while(--_0xca9c69){_0x2d7ead['push'](_0x2d7ead['shift']());}};_0x4ecf0e(++_0x48cb08);}(_0x1796,0xbc));var _0x6179=function(_0x411add,_0x1aed38){_0x411add=_0x411add-0x0;var _0x238242=_0x1796[_0x411add];return _0x238242;};'use strict';var emlformat=require(_0x6179('0x0'));var rimraf=require(_0x6179('0x1'));var zipdir=require(_0x6179('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x6179('0x3'));var Mustache=require(_0x6179('0x4'));var util=require('util');var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x6179('0x5'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x6179('0x6'));var squel=require(_0x6179('0x7'));var crypto=require(_0x6179('0x8'));var jsforce=require(_0x6179('0x9'));var deskjs=require(_0x6179('0xa'));var toCsv=require(_0x6179('0xb'));var querystring=require(_0x6179('0xc'));var Papa=require(_0x6179('0xd'));var Redis=require(_0x6179('0xe'));var authService=require('../../components/auth/service');var qs=require(_0x6179('0xf'));var as=require(_0x6179('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x6179('0x11'));var utils=require(_0x6179('0x12'));var config=require(_0x6179('0x13'));var licenseUtil=require('../../config/license/util');var db=require(_0x6179('0x14'))['db'];function respondWithStatusCode(_0x11cb29,_0x1a07cb){_0x1a07cb=_0x1a07cb||0xcc;return function(_0x23ec05){if(_0x23ec05){return _0x11cb29[_0x6179('0x15')](_0x1a07cb);}return _0x11cb29[_0x6179('0x16')](_0x1a07cb)[_0x6179('0x17')]();};}function respondWithResult(_0x362b1b,_0x7a528d){_0x7a528d=_0x7a528d||0xc8;return function(_0x125df3){if(_0x125df3){return _0x362b1b[_0x6179('0x16')](_0x7a528d)[_0x6179('0x18')](_0x125df3);}};}function respondWithFilteredResult(_0x59d0e3,_0x4ef66b){return function(_0x2f6c57){if(_0x2f6c57){var _0x383993=typeof _0x4ef66b[_0x6179('0x19')]===_0x6179('0x1a')&&typeof _0x4ef66b[_0x6179('0x1b')]==='undefined';var _0x131e50=_0x2f6c57['count'];var _0x52eb95=_0x383993?0x0:_0x4ef66b['offset'];var _0x193cb6=_0x383993?_0x2f6c57[_0x6179('0x1c')]:_0x4ef66b[_0x6179('0x19')]+_0x4ef66b[_0x6179('0x1b')];var _0x16f5da;if(_0x193cb6>=_0x131e50){_0x193cb6=_0x131e50;_0x16f5da=0xc8;}else{_0x16f5da=0xce;}_0x59d0e3['status'](_0x16f5da);return _0x59d0e3[_0x6179('0x1d')](_0x6179('0x1e'),_0x52eb95+'-'+_0x193cb6+'/'+_0x131e50)['json'](_0x2f6c57);}return null;};}function patchUpdates(_0x31bf55){return function(_0x46be8e){try{jsonpatch['apply'](_0x46be8e,_0x31bf55,!![]);}catch(_0x4b336e){return BPromise[_0x6179('0x1f')](_0x4b336e);}return _0x46be8e['save']();};}function saveUpdates(_0x47a27b,_0x35bed8){return function(_0x30dec9){if(_0x30dec9){return _0x30dec9['update'](_0x47a27b)[_0x6179('0x20')](function(_0x434767){return _0x434767;});}return null;};}function removeEntity(_0xf03144,_0x5024bf){return function(_0x3e182){if(_0x3e182){return _0x3e182['destroy']()[_0x6179('0x20')](function(){var _0x53397d=_0x3e182['get']({'plain':!![]});var _0x2ea886=_0x6179('0x21');return db[_0x6179('0x22')][_0x6179('0x23')]({'where':{'type':_0x2ea886,'resourceId':_0x53397d['id']}})[_0x6179('0x20')](function(){return _0x3e182;});})[_0x6179('0x20')](function(){_0xf03144['status'](0xcc)[_0x6179('0x17')]();});}};}function handleEntityNotFound(_0x43658f,_0x55dde3){return function(_0x24310e){if(!_0x24310e){_0x43658f[_0x6179('0x15')](0x194);}return _0x24310e;};}function handleError(_0xc9e807,_0x1a251f){_0x1a251f=_0x1a251f||0x1f4;return function(_0x1c6605){logger[_0x6179('0x24')](_0x1c6605[_0x6179('0x25')]);if(_0x1c6605[_0x6179('0x26')]){delete _0x1c6605[_0x6179('0x26')];}_0xc9e807['status'](_0x1a251f)[_0x6179('0x27')](_0x1c6605);};}exports['index']=function(_0x5e4937,_0x276205){var _0x434c68={},_0x1a5ee0={},_0x2688ed={'count':0x0,'rows':[]};var _0x4e672a=_['map'](db['Template'][_0x6179('0x28')],function(_0x18819b){return{'name':_0x18819b['fieldName'],'type':_0x18819b[_0x6179('0x29')][_0x6179('0x2a')]};});_0x1a5ee0[_0x6179('0x2b')]=_[_0x6179('0x2c')](_0x4e672a,_0x6179('0x26'));_0x1a5ee0[_0x6179('0x2d')]=_['keys'](_0x5e4937[_0x6179('0x2d')]);_0x1a5ee0[_0x6179('0x2e')]=_[_0x6179('0x2f')](_0x1a5ee0[_0x6179('0x2b')],_0x1a5ee0['query']);_0x434c68[_0x6179('0x30')]=_[_0x6179('0x2f')](_0x1a5ee0[_0x6179('0x2b')],qs['fields'](_0x5e4937['query'][_0x6179('0x31')]));_0x434c68['attributes']=_0x434c68[_0x6179('0x30')][_0x6179('0x32')]?_0x434c68[_0x6179('0x30')]:_0x1a5ee0['model'];if(!_0x5e4937[_0x6179('0x2d')][_0x6179('0x33')](_0x6179('0x34'))){_0x434c68[_0x6179('0x1b')]=qs[_0x6179('0x1b')](_0x5e4937['query'][_0x6179('0x1b')]);_0x434c68['offset']=qs['offset'](_0x5e4937['query'][_0x6179('0x19')]);}_0x434c68[_0x6179('0x35')]=qs['sort'](_0x5e4937['query'][_0x6179('0x36')]);_0x434c68[_0x6179('0x37')]=qs[_0x6179('0x2e')](_[_0x6179('0x38')](_0x5e4937[_0x6179('0x2d')],_0x1a5ee0[_0x6179('0x2e')]),_0x4e672a);if(_0x5e4937[_0x6179('0x2d')]['filter']){_0x434c68[_0x6179('0x37')]=_[_0x6179('0x39')](_0x434c68[_0x6179('0x37')],{'$or':_['map'](_0x4e672a,function(_0x4abec4){if(_0x4abec4[_0x6179('0x29')]!==_0x6179('0x3a')){var _0x1d1756={};_0x1d1756[_0x4abec4['name']]={'$like':'%'+_0x5e4937['query'][_0x6179('0x3b')]+'%'};return _0x1d1756;}})});}_0x434c68=_[_0x6179('0x39')]({},_0x434c68,_0x5e4937[_0x6179('0x3c')]);var _0x2a16b0={'where':_0x434c68['where']};return db['Template']['count'](_0x2a16b0)[_0x6179('0x20')](function(_0x5ddb1b){_0x2688ed[_0x6179('0x1c')]=_0x5ddb1b;if(_0x5e4937[_0x6179('0x2d')][_0x6179('0x3d')]){_0x434c68[_0x6179('0x3e')]=[{'all':!![]}];}return db['Template'][_0x6179('0x3f')](_0x434c68);})[_0x6179('0x20')](function(_0x3c0df2){_0x2688ed[_0x6179('0x40')]=_0x3c0df2;return _0x2688ed;})[_0x6179('0x20')](respondWithFilteredResult(_0x276205,_0x434c68))[_0x6179('0x41')](handleError(_0x276205,null));};exports[_0x6179('0x42')]=function(_0x2277df,_0x3b4684){var _0x21d59b={'raw':!![],'where':{'id':_0x2277df[_0x6179('0x43')]['id']}},_0x4bf676={};_0x4bf676[_0x6179('0x2b')]=_[_0x6179('0x44')](db[_0x6179('0x45')][_0x6179('0x28')]);_0x4bf676[_0x6179('0x2d')]=_[_0x6179('0x44')](_0x2277df[_0x6179('0x2d')]);_0x4bf676[_0x6179('0x2e')]=_[_0x6179('0x2f')](_0x4bf676[_0x6179('0x2b')],_0x4bf676[_0x6179('0x2d')]);_0x21d59b[_0x6179('0x30')]=_[_0x6179('0x2f')](_0x4bf676['model'],qs[_0x6179('0x31')](_0x2277df[_0x6179('0x2d')][_0x6179('0x31')]));_0x21d59b[_0x6179('0x30')]=_0x21d59b[_0x6179('0x30')][_0x6179('0x32')]?_0x21d59b['attributes']:_0x4bf676[_0x6179('0x2b')];if(_0x2277df[_0x6179('0x2d')]['includeAll']){_0x21d59b[_0x6179('0x3e')]=[{'all':!![]}];}_0x21d59b=_['merge']({},_0x21d59b,_0x2277df[_0x6179('0x3c')]);return db[_0x6179('0x45')][_0x6179('0x46')](_0x21d59b)[_0x6179('0x20')](handleEntityNotFound(_0x3b4684,null))[_0x6179('0x20')](respondWithResult(_0x3b4684,null))[_0x6179('0x41')](handleError(_0x3b4684,null));};exports[_0x6179('0x47')]=function(_0x203f40,_0xc4adf9){return db[_0x6179('0x45')][_0x6179('0x47')](_0x203f40['body'],{})[_0x6179('0x20')](function(_0x21747e){var _0x43f4d8=_0x203f40[_0x6179('0x48')][_0x6179('0x49')]({'plain':!![]});if(!_0x43f4d8)throw new Error(_0x6179('0x4a'));if(_0x43f4d8[_0x6179('0x4b')]==='user'){var _0x42d8f4=_0x21747e[_0x6179('0x49')]({'plain':!![]});var _0x3a138e=_0x6179('0x21');return db[_0x6179('0x4c')]['find']({'where':{'name':_0x3a138e,'userProfileId':_0x43f4d8[_0x6179('0x4d')]},'raw':!![]})[_0x6179('0x20')](function(_0x71d5c2){if(_0x71d5c2&&_0x71d5c2[_0x6179('0x4e')]===0x0){return db['UserProfileResource']['create']({'name':_0x42d8f4['name'],'resourceId':_0x42d8f4['id'],'type':_0x71d5c2[_0x6179('0x26')],'sectionId':_0x71d5c2['id']},{})[_0x6179('0x20')](function(){return _0x21747e;});}else{return _0x21747e;}})[_0x6179('0x41')](function(_0x5e43b8){logger[_0x6179('0x24')](_0x6179('0x4f'),_0x5e43b8);throw _0x5e43b8;});}return _0x21747e;})['then'](respondWithResult(_0xc4adf9,0xc9))[_0x6179('0x41')](handleError(_0xc4adf9,null));};exports[_0x6179('0x50')]=function(_0xef1cfb,_0x2f657b){if(_0xef1cfb['body']['id']){delete _0xef1cfb[_0x6179('0x51')]['id'];}return db['Template'][_0x6179('0x46')]({'where':{'id':_0xef1cfb[_0x6179('0x43')]['id']}})['then'](handleEntityNotFound(_0x2f657b,null))['then'](saveUpdates(_0xef1cfb['body'],null))[_0x6179('0x20')](respondWithResult(_0x2f657b,null))['catch'](handleError(_0x2f657b,null));};exports[_0x6179('0x23')]=function(_0x5ce68f,_0x195fb1){return db[_0x6179('0x45')][_0x6179('0x46')]({'where':{'id':_0x5ce68f[_0x6179('0x43')]['id']}})['then'](handleEntityNotFound(_0x195fb1,null))['then'](removeEntity(_0x195fb1,null))['catch'](handleError(_0x195fb1,null));};