899c33de6205fde5689392cc626e74d8e83b274f
[motion2.git] / server / api / schedule / schedule.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 _0x1f09=['destroy','then','Scheduler','error','stack','name','send','index','map','Schedule','rawAttributes','fieldName','key','model','keys','intersection','query','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filters','filter','merge','type','VIRTUAL','options','include','findAll','rows','show','find','catch','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','run','../../config/schedule','eml-format','rimraf','zip-dir','request-promise','bluebird','mustache','util','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','json','offset','undefined','count','limit','set','Content-Range','apply','reject','save','update'];(function(_0x1402bc,_0x3755cf){var _0x32bd61=function(_0x243e53){while(--_0x243e53){_0x1402bc['push'](_0x1402bc['shift']());}};_0x32bd61(++_0x3755cf);}(_0x1f09,0x1ea));var _0x91f0=function(_0x5da96d,_0x489ba4){_0x5da96d=_0x5da96d-0x0;var _0x39d319=_0x1f09[_0x5da96d];return _0x39d319;};'use strict';var emlformat=require(_0x91f0('0x0'));var rimraf=require(_0x91f0('0x1'));var zipdir=require(_0x91f0('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x91f0('0x3'));var moment=require('moment');var BPromise=require(_0x91f0('0x4'));var Mustache=require(_0x91f0('0x5'));var util=require(_0x91f0('0x6'));var path=require('path');var sox=require('sox');var csv=require(_0x91f0('0x7'));var ejs=require(_0x91f0('0x8'));var fs=require('fs');var fs_extra=require(_0x91f0('0x9'));var _=require('lodash');var squel=require(_0x91f0('0xa'));var crypto=require(_0x91f0('0xb'));var jsforce=require(_0x91f0('0xc'));var deskjs=require(_0x91f0('0xd'));var toCsv=require(_0x91f0('0x7'));var querystring=require(_0x91f0('0xe'));var Papa=require(_0x91f0('0xf'));var Redis=require(_0x91f0('0x10'));var authService=require('../../components/auth/service');var qs=require(_0x91f0('0x11'));var as=require(_0x91f0('0x12'));var hardwareService=require(_0x91f0('0x13'));var logger=require(_0x91f0('0x14'))(_0x91f0('0x15'));var utils=require(_0x91f0('0x16'));var config=require(_0x91f0('0x17'));var licenseUtil=require(_0x91f0('0x18'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0xad9f26,_0x20e097){_0x20e097=_0x20e097||0xcc;return function(_0x531293){if(_0x531293){return _0xad9f26[_0x91f0('0x19')](_0x20e097);}return _0xad9f26[_0x91f0('0x1a')](_0x20e097)['end']();};}function respondWithResult(_0x65e6cb,_0x40bb61){_0x40bb61=_0x40bb61||0xc8;return function(_0x167c5e){if(_0x167c5e){return _0x65e6cb['status'](_0x40bb61)[_0x91f0('0x1b')](_0x167c5e);}};}function respondWithFilteredResult(_0x83c47c,_0x8e4442){return function(_0x193943){if(_0x193943){var _0x505199=typeof _0x8e4442[_0x91f0('0x1c')]===_0x91f0('0x1d')&&typeof _0x8e4442['limit']===_0x91f0('0x1d');var _0x22727b=_0x193943['count'];var _0x36b8ab=_0x505199?0x0:_0x8e4442[_0x91f0('0x1c')];var _0x35f069=_0x505199?_0x193943[_0x91f0('0x1e')]:_0x8e4442['offset']+_0x8e4442[_0x91f0('0x1f')];var _0x20cd19;if(_0x35f069>=_0x22727b){_0x35f069=_0x22727b;_0x20cd19=0xc8;}else{_0x20cd19=0xce;}_0x83c47c['status'](_0x20cd19);return _0x83c47c[_0x91f0('0x20')](_0x91f0('0x21'),_0x36b8ab+'-'+_0x35f069+'/'+_0x22727b)[_0x91f0('0x1b')](_0x193943);}return null;};}function patchUpdates(_0x49b7c0){return function(_0x538d5a){try{jsonpatch[_0x91f0('0x22')](_0x538d5a,_0x49b7c0,!![]);}catch(_0x58941d){return BPromise[_0x91f0('0x23')](_0x58941d);}return _0x538d5a[_0x91f0('0x24')]();};}function saveUpdates(_0x18e3e8,_0x46f16d){return function(_0x6a68bb){if(_0x6a68bb){return _0x6a68bb[_0x91f0('0x25')](_0x18e3e8)['then'](function(_0x186e74){return _0x186e74;});}return null;};}function removeEntity(_0x1c3022,_0x5b88bc){return function(_0x357739){if(_0x357739){return _0x357739[_0x91f0('0x26')]()[_0x91f0('0x27')](function(){var _0x39ab51=_0x357739['get']({'plain':!![]});var _0x5eb288=_0x91f0('0x28');return db['UserProfileResource'][_0x91f0('0x26')]({'where':{'type':_0x5eb288,'resourceId':_0x39ab51['id']}})[_0x91f0('0x27')](function(){return _0x357739;});})[_0x91f0('0x27')](function(){_0x1c3022[_0x91f0('0x1a')](0xcc)['end']();});}};}function handleEntityNotFound(_0x23091d,_0x261116){return function(_0x47e84d){if(!_0x47e84d){_0x23091d[_0x91f0('0x19')](0x194);}return _0x47e84d;};}function handleError(_0x4cb734,_0x50f7f2){_0x50f7f2=_0x50f7f2||0x1f4;return function(_0x3c7d5b){logger[_0x91f0('0x29')](_0x3c7d5b[_0x91f0('0x2a')]);if(_0x3c7d5b[_0x91f0('0x2b')]){delete _0x3c7d5b[_0x91f0('0x2b')];}_0x4cb734[_0x91f0('0x1a')](_0x50f7f2)[_0x91f0('0x2c')](_0x3c7d5b);};}exports[_0x91f0('0x2d')]=function(_0x13a848,_0x451591){var _0x543cee={},_0x35f92c={},_0x4d7702={'count':0x0,'rows':[]};var _0x1332a3=_[_0x91f0('0x2e')](db[_0x91f0('0x2f')][_0x91f0('0x30')],function(_0x395674){return{'name':_0x395674[_0x91f0('0x31')],'type':_0x395674['type'][_0x91f0('0x32')]};});_0x35f92c[_0x91f0('0x33')]=_[_0x91f0('0x2e')](_0x1332a3,_0x91f0('0x2b'));_0x35f92c['query']=_[_0x91f0('0x34')](_0x13a848['query']);_0x35f92c['filters']=_[_0x91f0('0x35')](_0x35f92c[_0x91f0('0x33')],_0x35f92c[_0x91f0('0x36')]);_0x543cee[_0x91f0('0x37')]=_[_0x91f0('0x35')](_0x35f92c['model'],qs['fields'](_0x13a848[_0x91f0('0x36')][_0x91f0('0x38')]));_0x543cee['attributes']=_0x543cee[_0x91f0('0x37')][_0x91f0('0x39')]?_0x543cee[_0x91f0('0x37')]:_0x35f92c[_0x91f0('0x33')];if(!_0x13a848['query'][_0x91f0('0x3a')](_0x91f0('0x3b'))){_0x543cee[_0x91f0('0x1f')]=qs[_0x91f0('0x1f')](_0x13a848[_0x91f0('0x36')]['limit']);_0x543cee['offset']=qs[_0x91f0('0x1c')](_0x13a848[_0x91f0('0x36')][_0x91f0('0x1c')]);}_0x543cee[_0x91f0('0x3c')]=qs[_0x91f0('0x3d')](_0x13a848['query']['sort']);_0x543cee[_0x91f0('0x3e')]=qs['filters'](_['pick'](_0x13a848[_0x91f0('0x36')],_0x35f92c[_0x91f0('0x3f')]),_0x1332a3);if(_0x13a848[_0x91f0('0x36')][_0x91f0('0x40')]){_0x543cee[_0x91f0('0x3e')]=_[_0x91f0('0x41')](_0x543cee['where'],{'$or':_[_0x91f0('0x2e')](_0x1332a3,function(_0x2b832e){if(_0x2b832e[_0x91f0('0x42')]!==_0x91f0('0x43')){var _0x1cba14={};_0x1cba14[_0x2b832e['name']]={'$like':'%'+_0x13a848[_0x91f0('0x36')][_0x91f0('0x40')]+'%'};return _0x1cba14;}})});}_0x543cee=_['merge']({},_0x543cee,_0x13a848[_0x91f0('0x44')]);var _0x546e66={'where':_0x543cee[_0x91f0('0x3e')]};return db['Schedule']['count'](_0x546e66)[_0x91f0('0x27')](function(_0x56e185){_0x4d7702[_0x91f0('0x1e')]=_0x56e185;if(_0x13a848['query']['includeAll']){_0x543cee[_0x91f0('0x45')]=[{'all':!![]}];}return db[_0x91f0('0x2f')][_0x91f0('0x46')](_0x543cee);})['then'](function(_0x24bb76){_0x4d7702[_0x91f0('0x47')]=_0x24bb76;return _0x4d7702;})['then'](respondWithFilteredResult(_0x451591,_0x543cee))['catch'](handleError(_0x451591,null));};exports[_0x91f0('0x48')]=function(_0x1c4dd7,_0x14510b){var _0x19e9db={'raw':![],'where':{'id':_0x1c4dd7['params']['id']}},_0x51622a={};_0x51622a['model']=_[_0x91f0('0x34')](db[_0x91f0('0x2f')][_0x91f0('0x30')]);_0x51622a[_0x91f0('0x36')]=_[_0x91f0('0x34')](_0x1c4dd7[_0x91f0('0x36')]);_0x51622a[_0x91f0('0x3f')]=_[_0x91f0('0x35')](_0x51622a[_0x91f0('0x33')],_0x51622a[_0x91f0('0x36')]);_0x19e9db[_0x91f0('0x37')]=_[_0x91f0('0x35')](_0x51622a[_0x91f0('0x33')],qs[_0x91f0('0x38')](_0x1c4dd7[_0x91f0('0x36')]['fields']));_0x19e9db[_0x91f0('0x37')]=_0x19e9db[_0x91f0('0x37')][_0x91f0('0x39')]?_0x19e9db['attributes']:_0x51622a[_0x91f0('0x33')];if(_0x1c4dd7[_0x91f0('0x36')]['includeAll']){_0x19e9db['include']=[{'all':!![]}];}_0x19e9db=_[_0x91f0('0x41')]({},_0x19e9db,_0x1c4dd7[_0x91f0('0x44')]);return db[_0x91f0('0x2f')][_0x91f0('0x49')](_0x19e9db)[_0x91f0('0x27')](handleEntityNotFound(_0x14510b,null))[_0x91f0('0x27')](respondWithResult(_0x14510b,null))[_0x91f0('0x4a')](handleError(_0x14510b,null));};exports[_0x91f0('0x4b')]=function(_0x260192,_0x456fb8){return db[_0x91f0('0x2f')][_0x91f0('0x4b')](_0x260192[_0x91f0('0x4c')],{})['then'](function(_0x4e98e5){var _0x59e14a=_0x260192[_0x91f0('0x4d')]['get']({'plain':!![]});if(!_0x59e14a)throw new Error(_0x91f0('0x4e'));if(_0x59e14a[_0x91f0('0x4f')]===_0x91f0('0x4d')){var _0x2ea279=_0x4e98e5['get']({'plain':!![]});var _0x489454=_0x91f0('0x28');return db[_0x91f0('0x50')]['find']({'where':{'name':_0x489454,'userProfileId':_0x59e14a[_0x91f0('0x51')]},'raw':!![]})[_0x91f0('0x27')](function(_0x25489f){if(_0x25489f&&_0x25489f[_0x91f0('0x52')]===0x0){return db[_0x91f0('0x53')]['create']({'name':_0x2ea279['name'],'resourceId':_0x2ea279['id'],'type':_0x25489f[_0x91f0('0x2b')],'sectionId':_0x25489f['id']},{})['then'](function(){return _0x4e98e5;});}else{return _0x4e98e5;}})[_0x91f0('0x4a')](function(_0x12c93c){logger[_0x91f0('0x29')](_0x91f0('0x54'),_0x12c93c);throw _0x12c93c;});}return _0x4e98e5;})[_0x91f0('0x27')](respondWithResult(_0x456fb8,0xc9))[_0x91f0('0x4a')](handleError(_0x456fb8,null));};exports[_0x91f0('0x25')]=function(_0xb08e65,_0x2f0b85){if(_0xb08e65[_0x91f0('0x4c')]['id']){delete _0xb08e65[_0x91f0('0x4c')]['id'];}return db['Schedule'][_0x91f0('0x49')]({'where':{'id':_0xb08e65['params']['id']}})[_0x91f0('0x27')](handleEntityNotFound(_0x2f0b85,null))[_0x91f0('0x27')](saveUpdates(_0xb08e65[_0x91f0('0x4c')],null))[_0x91f0('0x27')](respondWithResult(_0x2f0b85,null))[_0x91f0('0x4a')](handleError(_0x2f0b85,null));};exports[_0x91f0('0x26')]=function(_0xd58c5d,_0x20a2ab){return db[_0x91f0('0x2f')]['find']({'where':{'id':_0xd58c5d[_0x91f0('0x55')]['id']}})[_0x91f0('0x27')](handleEntityNotFound(_0x20a2ab,null))['then'](removeEntity(_0x20a2ab,null))[_0x91f0('0x4a')](handleError(_0x20a2ab,null));};exports[_0x91f0('0x56')]=function(_0x1d8441,_0x1f9993,_0x9d579c){var _0x9408ab=require(_0x91f0('0x57'));return db[_0x91f0('0x2f')][_0x91f0('0x49')]({'where':{'id':_0x1d8441[_0x91f0('0x55')]['id']},'raw':!![]})[_0x91f0('0x27')](handleEntityNotFound(_0x1f9993,null))[_0x91f0('0x27')](function(){_0x9408ab[_0x91f0('0x56')](_0x1d8441[_0x91f0('0x55')]['id']);})[_0x91f0('0x27')](respondWithStatusCode(_0x1f9993,null))['catch'](handleError(_0x1f9993,null));};