c448eedc41ec861f0e420cb38f4d1b152b843169
[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 _0x0a12=['catch','params','intersection','includeAll','include','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','run','../../config/schedule','html-pdf','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','sox','lodash','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','json','count','offset','limit','Content-Range','reject','update','then','destroy','get','Scheduler','UserProfileResource','status','stack','name','send','index','map','Schedule','rawAttributes','fieldName','type','model','query','keys','filters','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','findAll'];(function(_0x1526a7,_0x2a94f4){var _0x14d22b=function(_0x3266be){while(--_0x3266be){_0x1526a7['push'](_0x1526a7['shift']());}};_0x14d22b(++_0x2a94f4);}(_0x0a12,0x10a));var _0x20a1=function(_0x341e0f,_0x4a09d0){_0x341e0f=_0x341e0f-0x0;var _0x59555a=_0x0a12[_0x341e0f];return _0x59555a;};'use strict';var pdf=require(_0x20a1('0x0'));var emlformat=require('eml-format');var rimraf=require(_0x20a1('0x1'));var zipdir=require(_0x20a1('0x2'));var jsonpatch=require(_0x20a1('0x3'));var rp=require(_0x20a1('0x4'));var moment=require(_0x20a1('0x5'));var BPromise=require(_0x20a1('0x6'));var Mustache=require(_0x20a1('0x7'));var util=require('util');var path=require('path');var sox=require(_0x20a1('0x8'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0x20a1('0x9'));var squel=require('squel');var crypto=require(_0x20a1('0xa'));var jsforce=require(_0x20a1('0xb'));var deskjs=require(_0x20a1('0xc'));var toCsv=require(_0x20a1('0xd'));var querystring=require(_0x20a1('0xe'));var Papa=require(_0x20a1('0xf'));var Redis=require('ioredis');var authService=require(_0x20a1('0x10'));var qs=require(_0x20a1('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x20a1('0x12'));var logger=require(_0x20a1('0x13'))(_0x20a1('0x14'));var utils=require('../../config/utils');var config=require(_0x20a1('0x15'));var licenseUtil=require(_0x20a1('0x16'));var db=require(_0x20a1('0x17'))['db'];function respondWithStatusCode(_0xba9a11,_0x307c0a){_0x307c0a=_0x307c0a||0xcc;return function(_0x3d0cb2){if(_0x3d0cb2){return _0xba9a11[_0x20a1('0x18')](_0x307c0a);}return _0xba9a11['status'](_0x307c0a)[_0x20a1('0x19')]();};}function respondWithResult(_0x265db2,_0x182003){_0x182003=_0x182003||0xc8;return function(_0x3f046f){if(_0x3f046f){return _0x265db2['status'](_0x182003)[_0x20a1('0x1a')](_0x3f046f);}};}function respondWithFilteredResult(_0x5b28a7,_0x193c6b){return function(_0x4d9649){if(_0x4d9649){var _0x2d14f8=_0x4d9649[_0x20a1('0x1b')],_0x103e16=_0x193c6b[_0x20a1('0x1c')],_0x2e371c=_0x193c6b[_0x20a1('0x1c')]+_0x193c6b[_0x20a1('0x1d')],_0x1b99b4;if(_0x2e371c>=_0x2d14f8){_0x2e371c=_0x2d14f8;_0x1b99b4=0xc8;}else{_0x1b99b4=0xce;}_0x5b28a7['status'](_0x1b99b4);return _0x5b28a7['set'](_0x20a1('0x1e'),_0x103e16+'-'+_0x2e371c+'/'+_0x2d14f8)[_0x20a1('0x1a')](_0x4d9649);}return null;};}function patchUpdates(_0x2222cc){return function(_0x1a896a){try{jsonpatch['apply'](_0x1a896a,_0x2222cc,!![]);}catch(_0x3d5c45){return BPromise[_0x20a1('0x1f')](_0x3d5c45);}return _0x1a896a['save']();};}function saveUpdates(_0x530c5c,_0x1ed3d6){return function(_0x264f2c){if(_0x264f2c){return _0x264f2c[_0x20a1('0x20')](_0x530c5c)[_0x20a1('0x21')](function(_0x28cc2b){return _0x28cc2b;});}return null;};}function removeEntity(_0xf9786e,_0x40999c){return function(_0x392766){if(_0x392766){return _0x392766[_0x20a1('0x22')]()[_0x20a1('0x21')](function(){var _0x3312a3=_0x392766[_0x20a1('0x23')]({'plain':!![]});var _0x4bd64e=_0x20a1('0x24');return db[_0x20a1('0x25')]['destroy']({'where':{'type':_0x4bd64e,'resourceId':_0x3312a3['id']}})[_0x20a1('0x21')](function(){return _0x392766;});})[_0x20a1('0x21')](function(){_0xf9786e[_0x20a1('0x26')](0xcc)[_0x20a1('0x19')]();});}};}function handleEntityNotFound(_0x4fa510,_0x3e4815){return function(_0x4ed4be){if(!_0x4ed4be){_0x4fa510[_0x20a1('0x18')](0x194);}return _0x4ed4be;};}function handleError(_0x2b3ac6,_0x4e5261){_0x4e5261=_0x4e5261||0x1f4;return function(_0x322120){logger['error'](_0x322120[_0x20a1('0x27')]);if(_0x322120[_0x20a1('0x28')]){delete _0x322120['name'];}_0x2b3ac6[_0x20a1('0x26')](_0x4e5261)[_0x20a1('0x29')](_0x322120);};}exports[_0x20a1('0x2a')]=function(_0x3a7326,_0x4073c9){var _0x1deb4b={},_0x4dd992={},_0x1e6531={'count':0x0,'rows':[]};var _0x2843d8=_[_0x20a1('0x2b')](db[_0x20a1('0x2c')][_0x20a1('0x2d')],function(_0x25b467){return{'name':_0x25b467[_0x20a1('0x2e')],'type':_0x25b467[_0x20a1('0x2f')]['key']};});_0x4dd992[_0x20a1('0x30')]=_[_0x20a1('0x2b')](_0x2843d8,'name');_0x4dd992[_0x20a1('0x31')]=_[_0x20a1('0x32')](_0x3a7326[_0x20a1('0x31')]);_0x4dd992[_0x20a1('0x33')]=_['intersection'](_0x4dd992['model'],_0x4dd992['query']);_0x1deb4b[_0x20a1('0x34')]=_['intersection'](_0x4dd992['model'],qs[_0x20a1('0x35')](_0x3a7326[_0x20a1('0x31')][_0x20a1('0x35')]));_0x1deb4b[_0x20a1('0x34')]=_0x1deb4b['attributes'][_0x20a1('0x36')]?_0x1deb4b[_0x20a1('0x34')]:_0x4dd992[_0x20a1('0x30')];if(!_0x3a7326['query'][_0x20a1('0x37')](_0x20a1('0x38'))){_0x1deb4b[_0x20a1('0x1d')]=qs[_0x20a1('0x1d')](_0x3a7326[_0x20a1('0x31')][_0x20a1('0x1d')]);_0x1deb4b[_0x20a1('0x1c')]=qs['offset'](_0x3a7326[_0x20a1('0x31')][_0x20a1('0x1c')]);}_0x1deb4b[_0x20a1('0x39')]=qs[_0x20a1('0x3a')](_0x3a7326['query'][_0x20a1('0x3a')]);_0x1deb4b[_0x20a1('0x3b')]=qs[_0x20a1('0x33')](_[_0x20a1('0x3c')](_0x3a7326['query'],_0x4dd992[_0x20a1('0x33')]),_0x2843d8);if(_0x3a7326['query'][_0x20a1('0x3d')]){_0x1deb4b[_0x20a1('0x3b')]=_[_0x20a1('0x3e')](_0x1deb4b[_0x20a1('0x3b')],{'$or':_[_0x20a1('0x2b')](_0x2843d8,function(_0x36caa0){if(_0x36caa0['type']!==_0x20a1('0x3f')){var _0x4494a2={};_0x4494a2[_0x36caa0[_0x20a1('0x28')]]={'$like':'%'+_0x3a7326['query'][_0x20a1('0x3d')]+'%'};return _0x4494a2;}})});}_0x1deb4b=_['merge']({},_0x1deb4b,_0x3a7326[_0x20a1('0x40')]);var _0x139c64={'where':_0x1deb4b[_0x20a1('0x3b')]};return db[_0x20a1('0x2c')][_0x20a1('0x1b')](_0x139c64)[_0x20a1('0x21')](function(_0x331899){_0x1e6531[_0x20a1('0x1b')]=_0x331899;if(_0x3a7326[_0x20a1('0x31')]['includeAll']){_0x1deb4b['include']=[{'all':!![]}];}return db[_0x20a1('0x2c')][_0x20a1('0x41')](_0x1deb4b);})[_0x20a1('0x21')](function(_0x370741){_0x1e6531['rows']=_0x370741;return _0x1e6531;})[_0x20a1('0x21')](respondWithFilteredResult(_0x4073c9,_0x1deb4b))[_0x20a1('0x42')](handleError(_0x4073c9,null));};exports['show']=function(_0x1d121c,_0x53cb24){var _0xde6d91={'raw':![],'where':{'id':_0x1d121c[_0x20a1('0x43')]['id']}},_0x369df7={};_0x369df7['model']=_[_0x20a1('0x32')](db[_0x20a1('0x2c')]['rawAttributes']);_0x369df7[_0x20a1('0x31')]=_[_0x20a1('0x32')](_0x1d121c[_0x20a1('0x31')]);_0x369df7['filters']=_[_0x20a1('0x44')](_0x369df7[_0x20a1('0x30')],_0x369df7[_0x20a1('0x31')]);_0xde6d91[_0x20a1('0x34')]=_[_0x20a1('0x44')](_0x369df7[_0x20a1('0x30')],qs[_0x20a1('0x35')](_0x1d121c[_0x20a1('0x31')][_0x20a1('0x35')]));_0xde6d91[_0x20a1('0x34')]=_0xde6d91[_0x20a1('0x34')][_0x20a1('0x36')]?_0xde6d91[_0x20a1('0x34')]:_0x369df7[_0x20a1('0x30')];if(_0x1d121c[_0x20a1('0x31')][_0x20a1('0x45')]){_0xde6d91[_0x20a1('0x46')]=[{'all':!![]}];}_0xde6d91=_[_0x20a1('0x3e')]({},_0xde6d91,_0x1d121c['options']);return db[_0x20a1('0x2c')][_0x20a1('0x47')](_0xde6d91)[_0x20a1('0x21')](handleEntityNotFound(_0x53cb24,null))['then'](respondWithResult(_0x53cb24,null))[_0x20a1('0x42')](handleError(_0x53cb24,null));};exports[_0x20a1('0x48')]=function(_0x545cd5,_0x2f0328){return db['Schedule'][_0x20a1('0x48')](_0x545cd5[_0x20a1('0x49')],{})[_0x20a1('0x21')](function(_0x2ce84e){var _0x532863=_0x545cd5[_0x20a1('0x4a')][_0x20a1('0x23')]({'plain':!![]});if(!_0x532863)throw new Error(_0x20a1('0x4b'));if(_0x532863[_0x20a1('0x4c')]===_0x20a1('0x4a')){var _0x4f54f2=_0x2ce84e[_0x20a1('0x23')]({'plain':!![]});var _0x1c1165=_0x20a1('0x24');return db[_0x20a1('0x4d')]['find']({'where':{'name':_0x1c1165,'userProfileId':_0x532863[_0x20a1('0x4e')]},'raw':!![]})[_0x20a1('0x21')](function(_0x2535de){if(_0x2535de&&_0x2535de['autoAssociation']===0x0){return db['UserProfileResource']['create']({'name':_0x4f54f2[_0x20a1('0x28')],'resourceId':_0x4f54f2['id'],'type':_0x2535de['name'],'sectionId':_0x2535de['id']},{})[_0x20a1('0x21')](function(){return _0x2ce84e;});}else{return _0x2ce84e;}})['catch'](function(_0x371357){logger[_0x20a1('0x4f')](_0x20a1('0x50'),_0x371357);throw _0x371357;});}return _0x2ce84e;})[_0x20a1('0x21')](respondWithResult(_0x2f0328,0xc9))[_0x20a1('0x42')](handleError(_0x2f0328,null));};exports[_0x20a1('0x20')]=function(_0x18f48a,_0x46a0fc){if(_0x18f48a[_0x20a1('0x49')]['id']){delete _0x18f48a['body']['id'];}return db[_0x20a1('0x2c')][_0x20a1('0x47')]({'where':{'id':_0x18f48a[_0x20a1('0x43')]['id']}})['then'](handleEntityNotFound(_0x46a0fc,null))['then'](saveUpdates(_0x18f48a[_0x20a1('0x49')],null))[_0x20a1('0x21')](respondWithResult(_0x46a0fc,null))[_0x20a1('0x42')](handleError(_0x46a0fc,null));};exports['destroy']=function(_0x2bf403,_0x2be8b5){return db[_0x20a1('0x2c')][_0x20a1('0x47')]({'where':{'id':_0x2bf403[_0x20a1('0x43')]['id']}})[_0x20a1('0x21')](handleEntityNotFound(_0x2be8b5,null))['then'](removeEntity(_0x2be8b5,null))[_0x20a1('0x42')](handleError(_0x2be8b5,null));};exports[_0x20a1('0x51')]=function(_0x2d1d6e,_0xe9af5e,_0x16c4cf){var _0xc43f28=require(_0x20a1('0x52'));return db['Schedule']['find']({'where':{'id':_0x2d1d6e['params']['id']},'raw':!![]})[_0x20a1('0x21')](handleEntityNotFound(_0xe9af5e,null))['then'](function(){_0xc43f28[_0x20a1('0x51')](_0x2d1d6e[_0x20a1('0x43')]['id']);})[_0x20a1('0x21')](respondWithStatusCode(_0xe9af5e,null))[_0x20a1('0x42')](handleError(_0xe9af5e,null));};