Built motion from commit d5e4af8c.|2.6.23
[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 _0x958b=['keys','filters','intersection','attributes','fields','sort','pick','where','merge','map','VIRTUAL','filter','options','includeAll','findAll','rows','catch','show','params','length','include','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','get','find','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','run','../../config/schedule','eml-format','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','Content-Range','reject','then','destroy','sendStatus','error','stack','name','send','index','Schedule','rawAttributes','fieldName','type','key','model','query'];(function(_0x4fd109,_0x33c503){var _0x30f0a6=function(_0xc11c0d){while(--_0xc11c0d){_0x4fd109['push'](_0x4fd109['shift']());}};_0x30f0a6(++_0x33c503);}(_0x958b,0x11b));var _0xb958=function(_0x8dee8d,_0x2a17fb){_0x8dee8d=_0x8dee8d-0x0;var _0x40d498=_0x958b[_0x8dee8d];return _0x40d498;};'use strict';var emlformat=require(_0xb958('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0xb958('0x1'));var rp=require(_0xb958('0x2'));var moment=require(_0xb958('0x3'));var BPromise=require(_0xb958('0x4'));var Mustache=require(_0xb958('0x5'));var util=require(_0xb958('0x6'));var path=require(_0xb958('0x7'));var sox=require(_0xb958('0x8'));var csv=require(_0xb958('0x9'));var ejs=require(_0xb958('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xb958('0xb'));var squel=require(_0xb958('0xc'));var crypto=require('crypto');var jsforce=require(_0xb958('0xd'));var deskjs=require(_0xb958('0xe'));var toCsv=require(_0xb958('0x9'));var querystring=require(_0xb958('0xf'));var Papa=require(_0xb958('0x10'));var Redis=require(_0xb958('0x11'));var authService=require(_0xb958('0x12'));var qs=require('../../components/parsers/qs');var as=require(_0xb958('0x13'));var hardwareService=require(_0xb958('0x14'));var logger=require('../../config/logger')('api');var utils=require(_0xb958('0x15'));var config=require(_0xb958('0x16'));var licenseUtil=require(_0xb958('0x17'));var db=require(_0xb958('0x18'))['db'];function respondWithStatusCode(_0x2bfee2,_0x4597c5){_0x4597c5=_0x4597c5||0xcc;return function(_0x3f0486){if(_0x3f0486){return _0x2bfee2['sendStatus'](_0x4597c5);}return _0x2bfee2[_0xb958('0x19')](_0x4597c5)[_0xb958('0x1a')]();};}function respondWithResult(_0x58e337,_0x56db3f){_0x56db3f=_0x56db3f||0xc8;return function(_0x27909e){if(_0x27909e){return _0x58e337[_0xb958('0x19')](_0x56db3f)[_0xb958('0x1b')](_0x27909e);}};}function respondWithFilteredResult(_0x4d39ce,_0x2bfe25){return function(_0x2498a9){if(_0x2498a9){var _0x58c248=typeof _0x2bfe25[_0xb958('0x1c')]===_0xb958('0x1d')&&typeof _0x2bfe25[_0xb958('0x1e')]===_0xb958('0x1d');var _0x42a69a=_0x2498a9[_0xb958('0x1f')];var _0x176b51=_0x58c248?0x0:_0x2bfe25[_0xb958('0x1c')];var _0xff51e4=_0x58c248?_0x2498a9[_0xb958('0x1f')]:_0x2bfe25['offset']+_0x2bfe25['limit'];var _0x5e5375;if(_0xff51e4>=_0x42a69a){_0xff51e4=_0x42a69a;_0x5e5375=0xc8;}else{_0x5e5375=0xce;}_0x4d39ce['status'](_0x5e5375);return _0x4d39ce['set'](_0xb958('0x20'),_0x176b51+'-'+_0xff51e4+'/'+_0x42a69a)['json'](_0x2498a9);}return null;};}function patchUpdates(_0x451f6d){return function(_0x5dd3a9){try{jsonpatch['apply'](_0x5dd3a9,_0x451f6d,!![]);}catch(_0x460f73){return BPromise[_0xb958('0x21')](_0x460f73);}return _0x5dd3a9['save']();};}function saveUpdates(_0x18432f,_0x59578f){return function(_0x478c4b){if(_0x478c4b){return _0x478c4b['update'](_0x18432f)[_0xb958('0x22')](function(_0x29d399){return _0x29d399;});}return null;};}function removeEntity(_0x1c32cb,_0x445582){return function(_0x4396dd){if(_0x4396dd){return _0x4396dd[_0xb958('0x23')]()[_0xb958('0x22')](function(){var _0x4997e1=_0x4396dd['get']({'plain':!![]});var _0xa7852c='Scheduler';return db['UserProfileResource'][_0xb958('0x23')]({'where':{'type':_0xa7852c,'resourceId':_0x4997e1['id']}})['then'](function(){return _0x4396dd;});})[_0xb958('0x22')](function(){_0x1c32cb[_0xb958('0x19')](0xcc)[_0xb958('0x1a')]();});}};}function handleEntityNotFound(_0x39a6e8,_0x569579){return function(_0x1c393e){if(!_0x1c393e){_0x39a6e8[_0xb958('0x24')](0x194);}return _0x1c393e;};}function handleError(_0xa86662,_0x45d86b){_0x45d86b=_0x45d86b||0x1f4;return function(_0x14d7b7){logger[_0xb958('0x25')](_0x14d7b7[_0xb958('0x26')]);if(_0x14d7b7[_0xb958('0x27')]){delete _0x14d7b7[_0xb958('0x27')];}_0xa86662['status'](_0x45d86b)[_0xb958('0x28')](_0x14d7b7);};}exports[_0xb958('0x29')]=function(_0x2d6244,_0x5ef0e8){var _0x5899d1={},_0x325d31={},_0x544d6e={'count':0x0,'rows':[]};var _0x17f859=_['map'](db[_0xb958('0x2a')][_0xb958('0x2b')],function(_0x1ffaf2){return{'name':_0x1ffaf2[_0xb958('0x2c')],'type':_0x1ffaf2[_0xb958('0x2d')][_0xb958('0x2e')]};});_0x325d31[_0xb958('0x2f')]=_['map'](_0x17f859,_0xb958('0x27'));_0x325d31[_0xb958('0x30')]=_[_0xb958('0x31')](_0x2d6244[_0xb958('0x30')]);_0x325d31[_0xb958('0x32')]=_[_0xb958('0x33')](_0x325d31[_0xb958('0x2f')],_0x325d31['query']);_0x5899d1[_0xb958('0x34')]=_[_0xb958('0x33')](_0x325d31[_0xb958('0x2f')],qs[_0xb958('0x35')](_0x2d6244[_0xb958('0x30')]['fields']));_0x5899d1['attributes']=_0x5899d1[_0xb958('0x34')]['length']?_0x5899d1[_0xb958('0x34')]:_0x325d31[_0xb958('0x2f')];if(!_0x2d6244[_0xb958('0x30')]['hasOwnProperty']('nolimit')){_0x5899d1['limit']=qs['limit'](_0x2d6244[_0xb958('0x30')][_0xb958('0x1e')]);_0x5899d1['offset']=qs[_0xb958('0x1c')](_0x2d6244[_0xb958('0x30')]['offset']);}_0x5899d1['order']=qs[_0xb958('0x36')](_0x2d6244[_0xb958('0x30')]['sort']);_0x5899d1['where']=qs[_0xb958('0x32')](_[_0xb958('0x37')](_0x2d6244[_0xb958('0x30')],_0x325d31[_0xb958('0x32')]),_0x17f859);if(_0x2d6244[_0xb958('0x30')]['filter']){_0x5899d1[_0xb958('0x38')]=_[_0xb958('0x39')](_0x5899d1['where'],{'$or':_[_0xb958('0x3a')](_0x17f859,function(_0xe8777a){if(_0xe8777a[_0xb958('0x2d')]!==_0xb958('0x3b')){var _0x228081={};_0x228081[_0xe8777a[_0xb958('0x27')]]={'$like':'%'+_0x2d6244[_0xb958('0x30')][_0xb958('0x3c')]+'%'};return _0x228081;}})});}_0x5899d1=_[_0xb958('0x39')]({},_0x5899d1,_0x2d6244[_0xb958('0x3d')]);var _0x69bcb0={'where':_0x5899d1['where']};return db[_0xb958('0x2a')]['count'](_0x69bcb0)[_0xb958('0x22')](function(_0x1d6ab2){_0x544d6e[_0xb958('0x1f')]=_0x1d6ab2;if(_0x2d6244[_0xb958('0x30')][_0xb958('0x3e')]){_0x5899d1['include']=[{'all':!![]}];}return db['Schedule'][_0xb958('0x3f')](_0x5899d1);})['then'](function(_0x5cea34){_0x544d6e[_0xb958('0x40')]=_0x5cea34;return _0x544d6e;})[_0xb958('0x22')](respondWithFilteredResult(_0x5ef0e8,_0x5899d1))[_0xb958('0x41')](handleError(_0x5ef0e8,null));};exports[_0xb958('0x42')]=function(_0x39af28,_0x38b0b7){var _0x43986f={'raw':![],'where':{'id':_0x39af28[_0xb958('0x43')]['id']}},_0x5dfa79={};_0x5dfa79[_0xb958('0x2f')]=_[_0xb958('0x31')](db['Schedule'][_0xb958('0x2b')]);_0x5dfa79['query']=_[_0xb958('0x31')](_0x39af28[_0xb958('0x30')]);_0x5dfa79[_0xb958('0x32')]=_[_0xb958('0x33')](_0x5dfa79[_0xb958('0x2f')],_0x5dfa79[_0xb958('0x30')]);_0x43986f[_0xb958('0x34')]=_[_0xb958('0x33')](_0x5dfa79[_0xb958('0x2f')],qs[_0xb958('0x35')](_0x39af28[_0xb958('0x30')]['fields']));_0x43986f[_0xb958('0x34')]=_0x43986f[_0xb958('0x34')][_0xb958('0x44')]?_0x43986f['attributes']:_0x5dfa79['model'];if(_0x39af28[_0xb958('0x30')][_0xb958('0x3e')]){_0x43986f[_0xb958('0x45')]=[{'all':!![]}];}_0x43986f=_[_0xb958('0x39')]({},_0x43986f,_0x39af28['options']);return db['Schedule']['find'](_0x43986f)[_0xb958('0x22')](handleEntityNotFound(_0x38b0b7,null))['then'](respondWithResult(_0x38b0b7,null))[_0xb958('0x41')](handleError(_0x38b0b7,null));};exports['create']=function(_0xd83bae,_0x18060f){return db[_0xb958('0x2a')][_0xb958('0x46')](_0xd83bae[_0xb958('0x47')],{})[_0xb958('0x22')](function(_0x216a45){var _0x2e0858=_0xd83bae[_0xb958('0x48')]['get']({'plain':!![]});if(!_0x2e0858)throw new Error(_0xb958('0x49'));if(_0x2e0858[_0xb958('0x4a')]===_0xb958('0x48')){var _0x2f9996=_0x216a45[_0xb958('0x4b')]({'plain':!![]});var _0x46cbe6='Scheduler';return db['UserProfileSection'][_0xb958('0x4c')]({'where':{'name':_0x46cbe6,'userProfileId':_0x2e0858[_0xb958('0x4d')]},'raw':!![]})[_0xb958('0x22')](function(_0x192376){if(_0x192376&&_0x192376[_0xb958('0x4e')]===0x0){return db['UserProfileResource'][_0xb958('0x46')]({'name':_0x2f9996[_0xb958('0x27')],'resourceId':_0x2f9996['id'],'type':_0x192376[_0xb958('0x27')],'sectionId':_0x192376['id']},{})[_0xb958('0x22')](function(){return _0x216a45;});}else{return _0x216a45;}})['catch'](function(_0x4b07de){logger[_0xb958('0x25')](_0xb958('0x4f'),_0x4b07de);throw _0x4b07de;});}return _0x216a45;})['then'](respondWithResult(_0x18060f,0xc9))['catch'](handleError(_0x18060f,null));};exports[_0xb958('0x50')]=function(_0x116c20,_0x919540){if(_0x116c20[_0xb958('0x47')]['id']){delete _0x116c20[_0xb958('0x47')]['id'];}return db['Schedule'][_0xb958('0x4c')]({'where':{'id':_0x116c20[_0xb958('0x43')]['id']}})[_0xb958('0x22')](handleEntityNotFound(_0x919540,null))[_0xb958('0x22')](saveUpdates(_0x116c20[_0xb958('0x47')],null))[_0xb958('0x22')](respondWithResult(_0x919540,null))['catch'](handleError(_0x919540,null));};exports[_0xb958('0x23')]=function(_0x34f597,_0x21acc1){return db[_0xb958('0x2a')]['find']({'where':{'id':_0x34f597[_0xb958('0x43')]['id']}})[_0xb958('0x22')](handleEntityNotFound(_0x21acc1,null))[_0xb958('0x22')](removeEntity(_0x21acc1,null))['catch'](handleError(_0x21acc1,null));};exports[_0xb958('0x51')]=function(_0x366bae,_0x48ea30,_0x592b27){var _0x9844b6=require(_0xb958('0x52'));return db['Schedule'][_0xb958('0x4c')]({'where':{'id':_0x366bae['params']['id']},'raw':!![]})['then'](handleEntityNotFound(_0x48ea30,null))['then'](function(){_0x9844b6[_0xb958('0x51')](_0x366bae[_0xb958('0x43')]['id']);})[_0xb958('0x22')](respondWithStatusCode(_0x48ea30,null))[_0xb958('0x41')](handleError(_0x48ea30,null));};