Built motion from commit 10af8726.|2.6.34
[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 _0xf02b=['query','filters','intersection','attributes','fields','nolimit','order','sort','pick','filter','where','type','VIRTUAL','merge','options','includeAll','include','findAll','rows','catch','show','keys','length','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','squel','jsforce','desk.js','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','update','then','destroy','get','Templates','UserProfileResource','status','error','name','send','index','Template','rawAttributes','fieldName','key','model','map'];(function(_0x4c5c69,_0x356630){var _0x35a82c=function(_0x2ef5f2){while(--_0x2ef5f2){_0x4c5c69['push'](_0x4c5c69['shift']());}};_0x35a82c(++_0x356630);}(_0xf02b,0x77));var _0xbf02=function(_0x283a81,_0x42f033){_0x283a81=_0x283a81-0x0;var _0x57533f=_0xf02b[_0x283a81];return _0x57533f;};'use strict';var emlformat=require(_0xbf02('0x0'));var rimraf=require(_0xbf02('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xbf02('0x2'));var rp=require('request-promise');var moment=require(_0xbf02('0x3'));var BPromise=require(_0xbf02('0x4'));var Mustache=require(_0xbf02('0x5'));var util=require(_0xbf02('0x6'));var path=require(_0xbf02('0x7'));var sox=require(_0xbf02('0x8'));var csv=require(_0xbf02('0x9'));var ejs=require(_0xbf02('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xbf02('0xb'));var squel=require(_0xbf02('0xc'));var crypto=require('crypto');var jsforce=require(_0xbf02('0xd'));var deskjs=require(_0xbf02('0xe'));var toCsv=require(_0xbf02('0x9'));var querystring=require('querystring');var Papa=require(_0xbf02('0xf'));var Redis=require('ioredis');var authService=require(_0xbf02('0x10'));var qs=require(_0xbf02('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xbf02('0x12'));var logger=require('../../config/logger')(_0xbf02('0x13'));var utils=require(_0xbf02('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0xbf02('0x15'));var db=require(_0xbf02('0x16'))['db'];function respondWithStatusCode(_0x41a407,_0x2a9de9){_0x2a9de9=_0x2a9de9||0xcc;return function(_0x3a49cc){if(_0x3a49cc){return _0x41a407[_0xbf02('0x17')](_0x2a9de9);}return _0x41a407['status'](_0x2a9de9)[_0xbf02('0x18')]();};}function respondWithResult(_0x2ad661,_0x290a1d){_0x290a1d=_0x290a1d||0xc8;return function(_0x232086){if(_0x232086){return _0x2ad661['status'](_0x290a1d)[_0xbf02('0x19')](_0x232086);}};}function respondWithFilteredResult(_0x2bb33b,_0x1c0c1d){return function(_0x1e31f9){if(_0x1e31f9){var _0x2a0525=typeof _0x1c0c1d[_0xbf02('0x1a')]===_0xbf02('0x1b')&&typeof _0x1c0c1d[_0xbf02('0x1c')]===_0xbf02('0x1b');var _0x4952e1=_0x1e31f9[_0xbf02('0x1d')];var _0x25b7ad=_0x2a0525?0x0:_0x1c0c1d[_0xbf02('0x1a')];var _0x27fc44=_0x2a0525?_0x1e31f9['count']:_0x1c0c1d[_0xbf02('0x1a')]+_0x1c0c1d[_0xbf02('0x1c')];var _0x3d32e9;if(_0x27fc44>=_0x4952e1){_0x27fc44=_0x4952e1;_0x3d32e9=0xc8;}else{_0x3d32e9=0xce;}_0x2bb33b['status'](_0x3d32e9);return _0x2bb33b[_0xbf02('0x1e')](_0xbf02('0x1f'),_0x25b7ad+'-'+_0x27fc44+'/'+_0x4952e1)[_0xbf02('0x19')](_0x1e31f9);}return null;};}function patchUpdates(_0x14098b){return function(_0x27c67b){try{jsonpatch[_0xbf02('0x20')](_0x27c67b,_0x14098b,!![]);}catch(_0x150ead){return BPromise[_0xbf02('0x21')](_0x150ead);}return _0x27c67b['save']();};}function saveUpdates(_0x2885c6,_0x3963f6){return function(_0x4bfdd6){if(_0x4bfdd6){return _0x4bfdd6[_0xbf02('0x22')](_0x2885c6)[_0xbf02('0x23')](function(_0x9b684e){return _0x9b684e;});}return null;};}function removeEntity(_0x40a367,_0x15e8e2){return function(_0x7ad728){if(_0x7ad728){return _0x7ad728[_0xbf02('0x24')]()[_0xbf02('0x23')](function(){var _0xfe11fb=_0x7ad728[_0xbf02('0x25')]({'plain':!![]});var _0x1b32c7=_0xbf02('0x26');return db[_0xbf02('0x27')][_0xbf02('0x24')]({'where':{'type':_0x1b32c7,'resourceId':_0xfe11fb['id']}})[_0xbf02('0x23')](function(){return _0x7ad728;});})['then'](function(){_0x40a367[_0xbf02('0x28')](0xcc)['end']();});}};}function handleEntityNotFound(_0xbab84c,_0x6b8a32){return function(_0x31b8de){if(!_0x31b8de){_0xbab84c[_0xbf02('0x17')](0x194);}return _0x31b8de;};}function handleError(_0x432b6d,_0x1eb06a){_0x1eb06a=_0x1eb06a||0x1f4;return function(_0x2b8603){logger[_0xbf02('0x29')](_0x2b8603['stack']);if(_0x2b8603['name']){delete _0x2b8603[_0xbf02('0x2a')];}_0x432b6d[_0xbf02('0x28')](_0x1eb06a)[_0xbf02('0x2b')](_0x2b8603);};}exports[_0xbf02('0x2c')]=function(_0x5b9195,_0x96ee09){var _0x271dc6={},_0x470f71={},_0x48573d={'count':0x0,'rows':[]};var _0x30bfe1=_['map'](db[_0xbf02('0x2d')][_0xbf02('0x2e')],function(_0x5d7863){return{'name':_0x5d7863[_0xbf02('0x2f')],'type':_0x5d7863['type'][_0xbf02('0x30')]};});_0x470f71[_0xbf02('0x31')]=_[_0xbf02('0x32')](_0x30bfe1,_0xbf02('0x2a'));_0x470f71[_0xbf02('0x33')]=_['keys'](_0x5b9195[_0xbf02('0x33')]);_0x470f71[_0xbf02('0x34')]=_[_0xbf02('0x35')](_0x470f71['model'],_0x470f71[_0xbf02('0x33')]);_0x271dc6[_0xbf02('0x36')]=_[_0xbf02('0x35')](_0x470f71['model'],qs[_0xbf02('0x37')](_0x5b9195['query']['fields']));_0x271dc6[_0xbf02('0x36')]=_0x271dc6[_0xbf02('0x36')]['length']?_0x271dc6[_0xbf02('0x36')]:_0x470f71[_0xbf02('0x31')];if(!_0x5b9195[_0xbf02('0x33')]['hasOwnProperty'](_0xbf02('0x38'))){_0x271dc6[_0xbf02('0x1c')]=qs['limit'](_0x5b9195[_0xbf02('0x33')][_0xbf02('0x1c')]);_0x271dc6[_0xbf02('0x1a')]=qs[_0xbf02('0x1a')](_0x5b9195['query'][_0xbf02('0x1a')]);}_0x271dc6[_0xbf02('0x39')]=qs[_0xbf02('0x3a')](_0x5b9195[_0xbf02('0x33')][_0xbf02('0x3a')]);_0x271dc6['where']=qs[_0xbf02('0x34')](_[_0xbf02('0x3b')](_0x5b9195['query'],_0x470f71['filters']),_0x30bfe1);if(_0x5b9195[_0xbf02('0x33')][_0xbf02('0x3c')]){_0x271dc6[_0xbf02('0x3d')]=_['merge'](_0x271dc6[_0xbf02('0x3d')],{'$or':_['map'](_0x30bfe1,function(_0x40ae65){if(_0x40ae65[_0xbf02('0x3e')]!==_0xbf02('0x3f')){var _0x5ed356={};_0x5ed356[_0x40ae65['name']]={'$like':'%'+_0x5b9195[_0xbf02('0x33')][_0xbf02('0x3c')]+'%'};return _0x5ed356;}})});}_0x271dc6=_[_0xbf02('0x40')]({},_0x271dc6,_0x5b9195[_0xbf02('0x41')]);var _0x596b28={'where':_0x271dc6['where']};return db[_0xbf02('0x2d')][_0xbf02('0x1d')](_0x596b28)['then'](function(_0x5f7a28){_0x48573d[_0xbf02('0x1d')]=_0x5f7a28;if(_0x5b9195['query'][_0xbf02('0x42')]){_0x271dc6[_0xbf02('0x43')]=[{'all':!![]}];}return db[_0xbf02('0x2d')][_0xbf02('0x44')](_0x271dc6);})[_0xbf02('0x23')](function(_0x376deb){_0x48573d[_0xbf02('0x45')]=_0x376deb;return _0x48573d;})['then'](respondWithFilteredResult(_0x96ee09,_0x271dc6))[_0xbf02('0x46')](handleError(_0x96ee09,null));};exports[_0xbf02('0x47')]=function(_0x3e2ffa,_0xb870d1){var _0x5ccf0a={'raw':!![],'where':{'id':_0x3e2ffa['params']['id']}},_0x115ca8={};_0x115ca8[_0xbf02('0x31')]=_[_0xbf02('0x48')](db[_0xbf02('0x2d')][_0xbf02('0x2e')]);_0x115ca8['query']=_[_0xbf02('0x48')](_0x3e2ffa[_0xbf02('0x33')]);_0x115ca8['filters']=_['intersection'](_0x115ca8[_0xbf02('0x31')],_0x115ca8[_0xbf02('0x33')]);_0x5ccf0a[_0xbf02('0x36')]=_['intersection'](_0x115ca8[_0xbf02('0x31')],qs['fields'](_0x3e2ffa['query']['fields']));_0x5ccf0a[_0xbf02('0x36')]=_0x5ccf0a[_0xbf02('0x36')][_0xbf02('0x49')]?_0x5ccf0a['attributes']:_0x115ca8[_0xbf02('0x31')];if(_0x3e2ffa['query']['includeAll']){_0x5ccf0a[_0xbf02('0x43')]=[{'all':!![]}];}_0x5ccf0a=_['merge']({},_0x5ccf0a,_0x3e2ffa['options']);return db[_0xbf02('0x2d')][_0xbf02('0x4a')](_0x5ccf0a)[_0xbf02('0x23')](handleEntityNotFound(_0xb870d1,null))[_0xbf02('0x23')](respondWithResult(_0xb870d1,null))[_0xbf02('0x46')](handleError(_0xb870d1,null));};exports[_0xbf02('0x4b')]=function(_0x5200fb,_0x41a02b){return db['Template'][_0xbf02('0x4b')](_0x5200fb[_0xbf02('0x4c')],{})['then'](function(_0x5ebf00){var _0x29b264=_0x5200fb[_0xbf02('0x4d')][_0xbf02('0x25')]({'plain':!![]});if(!_0x29b264)throw new Error(_0xbf02('0x4e'));if(_0x29b264[_0xbf02('0x4f')]==='user'){var _0x5e98e4=_0x5ebf00['get']({'plain':!![]});var _0x287612=_0xbf02('0x26');return db[_0xbf02('0x50')]['find']({'where':{'name':_0x287612,'userProfileId':_0x29b264[_0xbf02('0x51')]},'raw':!![]})[_0xbf02('0x23')](function(_0x398c13){if(_0x398c13&&_0x398c13[_0xbf02('0x52')]===0x0){return db[_0xbf02('0x27')][_0xbf02('0x4b')]({'name':_0x5e98e4[_0xbf02('0x2a')],'resourceId':_0x5e98e4['id'],'type':_0x398c13[_0xbf02('0x2a')],'sectionId':_0x398c13['id']},{})[_0xbf02('0x23')](function(){return _0x5ebf00;});}else{return _0x5ebf00;}})['catch'](function(_0x29f21c){logger[_0xbf02('0x29')](_0xbf02('0x53'),_0x29f21c);throw _0x29f21c;});}return _0x5ebf00;})[_0xbf02('0x23')](respondWithResult(_0x41a02b,0xc9))[_0xbf02('0x46')](handleError(_0x41a02b,null));};exports[_0xbf02('0x22')]=function(_0x499ba5,_0x5c480b){if(_0x499ba5[_0xbf02('0x4c')]['id']){delete _0x499ba5['body']['id'];}return db[_0xbf02('0x2d')][_0xbf02('0x4a')]({'where':{'id':_0x499ba5[_0xbf02('0x54')]['id']}})[_0xbf02('0x23')](handleEntityNotFound(_0x5c480b,null))[_0xbf02('0x23')](saveUpdates(_0x499ba5['body'],null))[_0xbf02('0x23')](respondWithResult(_0x5c480b,null))[_0xbf02('0x46')](handleError(_0x5c480b,null));};exports[_0xbf02('0x24')]=function(_0x189b77,_0x18893e){return db[_0xbf02('0x2d')][_0xbf02('0x4a')]({'where':{'id':_0x189b77['params']['id']}})[_0xbf02('0x23')](handleEntityNotFound(_0x18893e,null))['then'](removeEntity(_0x18893e,null))[_0xbf02('0x46')](handleError(_0x18893e,null));};