Built motion from commit d5e4af8c.|2.6.23
[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 _0x05f1=['moment','bluebird','util','path','sox','to-csv','fs-extra','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','json','offset','undefined','count','limit','status','set','apply','reject','update','then','destroy','get','Templates','stack','name','send','index','Template','fieldName','query','keys','intersection','model','fields','attributes','length','hasOwnProperty','order','sort','where','filters','pick','merge','map','type','VIRTUAL','includeAll','include','findAll','rows','show','rawAttributes','options','find','create','body','role','user','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','catch','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','eml-format','rimraf','zip-dir','fast-json-patch'];(function(_0x2ceedb,_0x3d4af3){var _0x1eef5c=function(_0xee38c1){while(--_0xee38c1){_0x2ceedb['push'](_0x2ceedb['shift']());}};_0x1eef5c(++_0x3d4af3);}(_0x05f1,0x1ee));var _0x105f=function(_0x2c91e9,_0x2fb965){_0x2c91e9=_0x2c91e9-0x0;var _0x13f0ff=_0x05f1[_0x2c91e9];return _0x13f0ff;};'use strict';var emlformat=require(_0x105f('0x0'));var rimraf=require(_0x105f('0x1'));var zipdir=require(_0x105f('0x2'));var jsonpatch=require(_0x105f('0x3'));var rp=require('request-promise');var moment=require(_0x105f('0x4'));var BPromise=require(_0x105f('0x5'));var Mustache=require('mustache');var util=require(_0x105f('0x6'));var path=require(_0x105f('0x7'));var sox=require(_0x105f('0x8'));var csv=require(_0x105f('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x105f('0xa'));var _=require('lodash');var squel=require(_0x105f('0xb'));var crypto=require(_0x105f('0xc'));var jsforce=require(_0x105f('0xd'));var deskjs=require(_0x105f('0xe'));var toCsv=require(_0x105f('0x9'));var querystring=require('querystring');var Papa=require(_0x105f('0xf'));var Redis=require(_0x105f('0x10'));var authService=require(_0x105f('0x11'));var qs=require(_0x105f('0x12'));var as=require(_0x105f('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x105f('0x14'))(_0x105f('0x15'));var utils=require(_0x105f('0x16'));var config=require(_0x105f('0x17'));var licenseUtil=require('../../config/license/util');var db=require(_0x105f('0x18'))['db'];function respondWithStatusCode(_0x42a5fb,_0x3bd98a){_0x3bd98a=_0x3bd98a||0xcc;return function(_0x423168){if(_0x423168){return _0x42a5fb[_0x105f('0x19')](_0x3bd98a);}return _0x42a5fb['status'](_0x3bd98a)[_0x105f('0x1a')]();};}function respondWithResult(_0x30d7eb,_0x2505bd){_0x2505bd=_0x2505bd||0xc8;return function(_0x23a4d8){if(_0x23a4d8){return _0x30d7eb['status'](_0x2505bd)[_0x105f('0x1b')](_0x23a4d8);}};}function respondWithFilteredResult(_0x79891e,_0x30549c){return function(_0x269ce1){if(_0x269ce1){var _0x148c0c=typeof _0x30549c[_0x105f('0x1c')]===_0x105f('0x1d')&&typeof _0x30549c['limit']===_0x105f('0x1d');var _0x66881f=_0x269ce1[_0x105f('0x1e')];var _0x4c8304=_0x148c0c?0x0:_0x30549c[_0x105f('0x1c')];var _0x45c7ed=_0x148c0c?_0x269ce1[_0x105f('0x1e')]:_0x30549c[_0x105f('0x1c')]+_0x30549c[_0x105f('0x1f')];var _0x46adb6;if(_0x45c7ed>=_0x66881f){_0x45c7ed=_0x66881f;_0x46adb6=0xc8;}else{_0x46adb6=0xce;}_0x79891e[_0x105f('0x20')](_0x46adb6);return _0x79891e[_0x105f('0x21')]('Content-Range',_0x4c8304+'-'+_0x45c7ed+'/'+_0x66881f)[_0x105f('0x1b')](_0x269ce1);}return null;};}function patchUpdates(_0x187bc7){return function(_0x36a955){try{jsonpatch[_0x105f('0x22')](_0x36a955,_0x187bc7,!![]);}catch(_0x124934){return BPromise[_0x105f('0x23')](_0x124934);}return _0x36a955['save']();};}function saveUpdates(_0x1f6972,_0x251aa0){return function(_0x59ce89){if(_0x59ce89){return _0x59ce89[_0x105f('0x24')](_0x1f6972)[_0x105f('0x25')](function(_0x12d7c0){return _0x12d7c0;});}return null;};}function removeEntity(_0x2d2964,_0x2073b6){return function(_0x591f4){if(_0x591f4){return _0x591f4[_0x105f('0x26')]()[_0x105f('0x25')](function(){var _0x48fb0d=_0x591f4[_0x105f('0x27')]({'plain':!![]});var _0x90d199=_0x105f('0x28');return db['UserProfileResource'][_0x105f('0x26')]({'where':{'type':_0x90d199,'resourceId':_0x48fb0d['id']}})[_0x105f('0x25')](function(){return _0x591f4;});})[_0x105f('0x25')](function(){_0x2d2964[_0x105f('0x20')](0xcc)[_0x105f('0x1a')]();});}};}function handleEntityNotFound(_0x28561f,_0x1b867f){return function(_0x4ad1eb){if(!_0x4ad1eb){_0x28561f[_0x105f('0x19')](0x194);}return _0x4ad1eb;};}function handleError(_0x4686f6,_0x35280f){_0x35280f=_0x35280f||0x1f4;return function(_0x3a8a7e){logger['error'](_0x3a8a7e[_0x105f('0x29')]);if(_0x3a8a7e[_0x105f('0x2a')]){delete _0x3a8a7e[_0x105f('0x2a')];}_0x4686f6[_0x105f('0x20')](_0x35280f)[_0x105f('0x2b')](_0x3a8a7e);};}exports[_0x105f('0x2c')]=function(_0x5827b2,_0x58f0c1){var _0x32e5b9={},_0x4c19b9={},_0x57c53b={'count':0x0,'rows':[]};var _0x54d539=_['map'](db[_0x105f('0x2d')]['rawAttributes'],function(_0x2d396a){return{'name':_0x2d396a[_0x105f('0x2e')],'type':_0x2d396a['type']['key']};});_0x4c19b9['model']=_['map'](_0x54d539,'name');_0x4c19b9[_0x105f('0x2f')]=_[_0x105f('0x30')](_0x5827b2[_0x105f('0x2f')]);_0x4c19b9['filters']=_['intersection'](_0x4c19b9['model'],_0x4c19b9[_0x105f('0x2f')]);_0x32e5b9['attributes']=_[_0x105f('0x31')](_0x4c19b9[_0x105f('0x32')],qs[_0x105f('0x33')](_0x5827b2[_0x105f('0x2f')]['fields']));_0x32e5b9[_0x105f('0x34')]=_0x32e5b9['attributes'][_0x105f('0x35')]?_0x32e5b9[_0x105f('0x34')]:_0x4c19b9[_0x105f('0x32')];if(!_0x5827b2[_0x105f('0x2f')][_0x105f('0x36')]('nolimit')){_0x32e5b9[_0x105f('0x1f')]=qs[_0x105f('0x1f')](_0x5827b2[_0x105f('0x2f')][_0x105f('0x1f')]);_0x32e5b9['offset']=qs[_0x105f('0x1c')](_0x5827b2[_0x105f('0x2f')][_0x105f('0x1c')]);}_0x32e5b9[_0x105f('0x37')]=qs[_0x105f('0x38')](_0x5827b2[_0x105f('0x2f')][_0x105f('0x38')]);_0x32e5b9[_0x105f('0x39')]=qs[_0x105f('0x3a')](_[_0x105f('0x3b')](_0x5827b2[_0x105f('0x2f')],_0x4c19b9[_0x105f('0x3a')]),_0x54d539);if(_0x5827b2[_0x105f('0x2f')]['filter']){_0x32e5b9[_0x105f('0x39')]=_[_0x105f('0x3c')](_0x32e5b9[_0x105f('0x39')],{'$or':_[_0x105f('0x3d')](_0x54d539,function(_0x3ad896){if(_0x3ad896[_0x105f('0x3e')]!==_0x105f('0x3f')){var _0x3923aa={};_0x3923aa[_0x3ad896[_0x105f('0x2a')]]={'$like':'%'+_0x5827b2[_0x105f('0x2f')]['filter']+'%'};return _0x3923aa;}})});}_0x32e5b9=_[_0x105f('0x3c')]({},_0x32e5b9,_0x5827b2['options']);var _0x194e75={'where':_0x32e5b9[_0x105f('0x39')]};return db[_0x105f('0x2d')][_0x105f('0x1e')](_0x194e75)[_0x105f('0x25')](function(_0x504795){_0x57c53b[_0x105f('0x1e')]=_0x504795;if(_0x5827b2['query'][_0x105f('0x40')]){_0x32e5b9[_0x105f('0x41')]=[{'all':!![]}];}return db['Template'][_0x105f('0x42')](_0x32e5b9);})[_0x105f('0x25')](function(_0x17b3a8){_0x57c53b[_0x105f('0x43')]=_0x17b3a8;return _0x57c53b;})[_0x105f('0x25')](respondWithFilteredResult(_0x58f0c1,_0x32e5b9))['catch'](handleError(_0x58f0c1,null));};exports[_0x105f('0x44')]=function(_0x563bb6,_0x3fdb6e){var _0x246e7f={'raw':!![],'where':{'id':_0x563bb6['params']['id']}},_0x2b1a73={};_0x2b1a73[_0x105f('0x32')]=_[_0x105f('0x30')](db[_0x105f('0x2d')][_0x105f('0x45')]);_0x2b1a73[_0x105f('0x2f')]=_[_0x105f('0x30')](_0x563bb6[_0x105f('0x2f')]);_0x2b1a73[_0x105f('0x3a')]=_[_0x105f('0x31')](_0x2b1a73['model'],_0x2b1a73[_0x105f('0x2f')]);_0x246e7f[_0x105f('0x34')]=_[_0x105f('0x31')](_0x2b1a73[_0x105f('0x32')],qs[_0x105f('0x33')](_0x563bb6[_0x105f('0x2f')][_0x105f('0x33')]));_0x246e7f[_0x105f('0x34')]=_0x246e7f['attributes'][_0x105f('0x35')]?_0x246e7f[_0x105f('0x34')]:_0x2b1a73['model'];if(_0x563bb6['query'][_0x105f('0x40')]){_0x246e7f[_0x105f('0x41')]=[{'all':!![]}];}_0x246e7f=_[_0x105f('0x3c')]({},_0x246e7f,_0x563bb6[_0x105f('0x46')]);return db[_0x105f('0x2d')][_0x105f('0x47')](_0x246e7f)['then'](handleEntityNotFound(_0x3fdb6e,null))[_0x105f('0x25')](respondWithResult(_0x3fdb6e,null))['catch'](handleError(_0x3fdb6e,null));};exports[_0x105f('0x48')]=function(_0x5ae00a,_0x59dc2e){return db[_0x105f('0x2d')][_0x105f('0x48')](_0x5ae00a[_0x105f('0x49')],{})[_0x105f('0x25')](function(_0x5e81ff){var _0x5ee792=_0x5ae00a['user'][_0x105f('0x27')]({'plain':!![]});if(!_0x5ee792)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x5ee792[_0x105f('0x4a')]===_0x105f('0x4b')){var _0x32c536=_0x5e81ff[_0x105f('0x27')]({'plain':!![]});var _0x403b89=_0x105f('0x28');return db[_0x105f('0x4c')]['find']({'where':{'name':_0x403b89,'userProfileId':_0x5ee792[_0x105f('0x4d')]},'raw':!![]})[_0x105f('0x25')](function(_0x56524c){if(_0x56524c&&_0x56524c[_0x105f('0x4e')]===0x0){return db[_0x105f('0x4f')][_0x105f('0x48')]({'name':_0x32c536[_0x105f('0x2a')],'resourceId':_0x32c536['id'],'type':_0x56524c[_0x105f('0x2a')],'sectionId':_0x56524c['id']},{})[_0x105f('0x25')](function(){return _0x5e81ff;});}else{return _0x5e81ff;}})[_0x105f('0x50')](function(_0x1a663b){logger['error'](_0x105f('0x51'),_0x1a663b);throw _0x1a663b;});}return _0x5e81ff;})['then'](respondWithResult(_0x59dc2e,0xc9))[_0x105f('0x50')](handleError(_0x59dc2e,null));};exports[_0x105f('0x24')]=function(_0x4deb21,_0x131a63){if(_0x4deb21[_0x105f('0x49')]['id']){delete _0x4deb21[_0x105f('0x49')]['id'];}return db[_0x105f('0x2d')][_0x105f('0x47')]({'where':{'id':_0x4deb21['params']['id']}})[_0x105f('0x25')](handleEntityNotFound(_0x131a63,null))[_0x105f('0x25')](saveUpdates(_0x4deb21['body'],null))[_0x105f('0x25')](respondWithResult(_0x131a63,null))[_0x105f('0x50')](handleError(_0x131a63,null));};exports[_0x105f('0x26')]=function(_0x5cd249,_0x4b343c){return db[_0x105f('0x2d')][_0x105f('0x47')]({'where':{'id':_0x5cd249[_0x105f('0x52')]['id']}})[_0x105f('0x25')](handleEntityNotFound(_0x4b343c,null))['then'](removeEntity(_0x4b343c,null))[_0x105f('0x50')](handleError(_0x4b343c,null));};