8e852fcd3b41822fb3067afb9974bca85494b9c0
[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 _0x149f=['includeAll','include','show','keys','find','catch','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','Scheduler','UserProfileSection','userProfileId','autoAssociation','params','run','../../config/schedule','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','sox','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','offset','undefined','limit','set','json','apply','reject','update','then','destroy','get','UserProfileResource','end','error','stack','name','index','map','Schedule','rawAttributes','fieldName','type','key','model','filters','intersection','query','fields','length','attributes','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','count'];(function(_0x569d11,_0x217bc7){var _0x4160c5=function(_0x5b8980){while(--_0x5b8980){_0x569d11['push'](_0x569d11['shift']());}};_0x4160c5(++_0x217bc7);}(_0x149f,0x1ca));var _0xf149=function(_0x5d4607,_0x6b5e7b){_0x5d4607=_0x5d4607-0x0;var _0x5e4a86=_0x149f[_0x5d4607];return _0x5e4a86;};'use strict';var emlformat=require(_0xf149('0x0'));var rimraf=require(_0xf149('0x1'));var zipdir=require(_0xf149('0x2'));var jsonpatch=require(_0xf149('0x3'));var rp=require(_0xf149('0x4'));var moment=require(_0xf149('0x5'));var BPromise=require(_0xf149('0x6'));var Mustache=require(_0xf149('0x7'));var util=require('util');var path=require('path');var sox=require(_0xf149('0x8'));var csv=require('to-csv');var ejs=require(_0xf149('0x9'));var fs=require('fs');var fs_extra=require(_0xf149('0xa'));var _=require(_0xf149('0xb'));var squel=require(_0xf149('0xc'));var crypto=require(_0xf149('0xd'));var jsforce=require('jsforce');var deskjs=require(_0xf149('0xe'));var toCsv=require('to-csv');var querystring=require(_0xf149('0xf'));var Papa=require(_0xf149('0x10'));var Redis=require('ioredis');var authService=require(_0xf149('0x11'));var qs=require(_0xf149('0x12'));var as=require(_0xf149('0x13'));var hardwareService=require(_0xf149('0x14'));var logger=require(_0xf149('0x15'))(_0xf149('0x16'));var utils=require(_0xf149('0x17'));var config=require(_0xf149('0x18'));var licenseUtil=require(_0xf149('0x19'));var db=require(_0xf149('0x1a'))['db'];function respondWithStatusCode(_0x2e3a49,_0x1387ce){_0x1387ce=_0x1387ce||0xcc;return function(_0x4cd848){if(_0x4cd848){return _0x2e3a49[_0xf149('0x1b')](_0x1387ce);}return _0x2e3a49[_0xf149('0x1c')](_0x1387ce)['end']();};}function respondWithResult(_0x1a43c9,_0x1816ab){_0x1816ab=_0x1816ab||0xc8;return function(_0x4ef640){if(_0x4ef640){return _0x1a43c9[_0xf149('0x1c')](_0x1816ab)['json'](_0x4ef640);}};}function respondWithFilteredResult(_0x3c0643,_0x4246a5){return function(_0x3c8e60){if(_0x3c8e60){var _0xf39ac6=typeof _0x4246a5[_0xf149('0x1d')]===_0xf149('0x1e')&&typeof _0x4246a5[_0xf149('0x1f')]==='undefined';var _0x441645=_0x3c8e60['count'];var _0x52db33=_0xf39ac6?0x0:_0x4246a5['offset'];var _0x290d52=_0xf39ac6?_0x3c8e60['count']:_0x4246a5['offset']+_0x4246a5[_0xf149('0x1f')];var _0x3f25b3;if(_0x290d52>=_0x441645){_0x290d52=_0x441645;_0x3f25b3=0xc8;}else{_0x3f25b3=0xce;}_0x3c0643[_0xf149('0x1c')](_0x3f25b3);return _0x3c0643[_0xf149('0x20')]('Content-Range',_0x52db33+'-'+_0x290d52+'/'+_0x441645)[_0xf149('0x21')](_0x3c8e60);}return null;};}function patchUpdates(_0x380d3c){return function(_0x3b1b9d){try{jsonpatch[_0xf149('0x22')](_0x3b1b9d,_0x380d3c,!![]);}catch(_0x262ebe){return BPromise[_0xf149('0x23')](_0x262ebe);}return _0x3b1b9d['save']();};}function saveUpdates(_0x32d2aa,_0x32a1f6){return function(_0x2ec479){if(_0x2ec479){return _0x2ec479[_0xf149('0x24')](_0x32d2aa)[_0xf149('0x25')](function(_0x5125a0){return _0x5125a0;});}return null;};}function removeEntity(_0x25d6c7,_0xaf95ea){return function(_0x41e626){if(_0x41e626){return _0x41e626[_0xf149('0x26')]()['then'](function(){var _0x4ac432=_0x41e626[_0xf149('0x27')]({'plain':!![]});var _0x32505b='Scheduler';return db[_0xf149('0x28')][_0xf149('0x26')]({'where':{'type':_0x32505b,'resourceId':_0x4ac432['id']}})[_0xf149('0x25')](function(){return _0x41e626;});})[_0xf149('0x25')](function(){_0x25d6c7['status'](0xcc)[_0xf149('0x29')]();});}};}function handleEntityNotFound(_0x52e7f2,_0x2d3414){return function(_0x3f9a38){if(!_0x3f9a38){_0x52e7f2[_0xf149('0x1b')](0x194);}return _0x3f9a38;};}function handleError(_0x17722b,_0x33e85c){_0x33e85c=_0x33e85c||0x1f4;return function(_0x5c6ba1){logger[_0xf149('0x2a')](_0x5c6ba1[_0xf149('0x2b')]);if(_0x5c6ba1[_0xf149('0x2c')]){delete _0x5c6ba1[_0xf149('0x2c')];}_0x17722b[_0xf149('0x1c')](_0x33e85c)['send'](_0x5c6ba1);};}exports[_0xf149('0x2d')]=function(_0x2f116a,_0xf76370){var _0x28b9aa={},_0x333fe1={},_0x4519db={'count':0x0,'rows':[]};var _0x564c80=_[_0xf149('0x2e')](db[_0xf149('0x2f')][_0xf149('0x30')],function(_0x413c5e){return{'name':_0x413c5e[_0xf149('0x31')],'type':_0x413c5e[_0xf149('0x32')][_0xf149('0x33')]};});_0x333fe1[_0xf149('0x34')]=_[_0xf149('0x2e')](_0x564c80,_0xf149('0x2c'));_0x333fe1['query']=_['keys'](_0x2f116a['query']);_0x333fe1[_0xf149('0x35')]=_[_0xf149('0x36')](_0x333fe1['model'],_0x333fe1[_0xf149('0x37')]);_0x28b9aa['attributes']=_[_0xf149('0x36')](_0x333fe1['model'],qs['fields'](_0x2f116a[_0xf149('0x37')][_0xf149('0x38')]));_0x28b9aa['attributes']=_0x28b9aa['attributes'][_0xf149('0x39')]?_0x28b9aa[_0xf149('0x3a')]:_0x333fe1[_0xf149('0x34')];if(!_0x2f116a['query'][_0xf149('0x3b')](_0xf149('0x3c'))){_0x28b9aa['limit']=qs[_0xf149('0x1f')](_0x2f116a['query'][_0xf149('0x1f')]);_0x28b9aa['offset']=qs[_0xf149('0x1d')](_0x2f116a['query'][_0xf149('0x1d')]);}_0x28b9aa[_0xf149('0x3d')]=qs[_0xf149('0x3e')](_0x2f116a[_0xf149('0x37')][_0xf149('0x3e')]);_0x28b9aa[_0xf149('0x3f')]=qs['filters'](_[_0xf149('0x40')](_0x2f116a[_0xf149('0x37')],_0x333fe1['filters']),_0x564c80);if(_0x2f116a[_0xf149('0x37')][_0xf149('0x41')]){_0x28b9aa[_0xf149('0x3f')]=_[_0xf149('0x42')](_0x28b9aa[_0xf149('0x3f')],{'$or':_[_0xf149('0x2e')](_0x564c80,function(_0x441aee){if(_0x441aee[_0xf149('0x32')]!==_0xf149('0x43')){var _0x2f7c45={};_0x2f7c45[_0x441aee[_0xf149('0x2c')]]={'$like':'%'+_0x2f116a[_0xf149('0x37')]['filter']+'%'};return _0x2f7c45;}})});}_0x28b9aa=_['merge']({},_0x28b9aa,_0x2f116a[_0xf149('0x44')]);var _0x407a12={'where':_0x28b9aa[_0xf149('0x3f')]};return db['Schedule'][_0xf149('0x45')](_0x407a12)[_0xf149('0x25')](function(_0x26ba89){_0x4519db[_0xf149('0x45')]=_0x26ba89;if(_0x2f116a[_0xf149('0x37')][_0xf149('0x46')]){_0x28b9aa[_0xf149('0x47')]=[{'all':!![]}];}return db['Schedule']['findAll'](_0x28b9aa);})[_0xf149('0x25')](function(_0x20bcd3){_0x4519db['rows']=_0x20bcd3;return _0x4519db;})[_0xf149('0x25')](respondWithFilteredResult(_0xf76370,_0x28b9aa))['catch'](handleError(_0xf76370,null));};exports[_0xf149('0x48')]=function(_0x21e246,_0x10958a){var _0x277b33={'raw':![],'where':{'id':_0x21e246['params']['id']}},_0x239897={};_0x239897[_0xf149('0x34')]=_[_0xf149('0x49')](db[_0xf149('0x2f')][_0xf149('0x30')]);_0x239897['query']=_[_0xf149('0x49')](_0x21e246[_0xf149('0x37')]);_0x239897['filters']=_[_0xf149('0x36')](_0x239897['model'],_0x239897['query']);_0x277b33[_0xf149('0x3a')]=_['intersection'](_0x239897['model'],qs[_0xf149('0x38')](_0x21e246[_0xf149('0x37')]['fields']));_0x277b33[_0xf149('0x3a')]=_0x277b33[_0xf149('0x3a')]['length']?_0x277b33[_0xf149('0x3a')]:_0x239897[_0xf149('0x34')];if(_0x21e246[_0xf149('0x37')][_0xf149('0x46')]){_0x277b33[_0xf149('0x47')]=[{'all':!![]}];}_0x277b33=_[_0xf149('0x42')]({},_0x277b33,_0x21e246['options']);return db[_0xf149('0x2f')][_0xf149('0x4a')](_0x277b33)[_0xf149('0x25')](handleEntityNotFound(_0x10958a,null))['then'](respondWithResult(_0x10958a,null))[_0xf149('0x4b')](handleError(_0x10958a,null));};exports['create']=function(_0x54601b,_0x59c0e0){return db[_0xf149('0x2f')][_0xf149('0x4c')](_0x54601b[_0xf149('0x4d')],{})[_0xf149('0x25')](function(_0x41167c){var _0x36cab4=_0x54601b['user']['get']({'plain':!![]});if(!_0x36cab4)throw new Error(_0xf149('0x4e'));if(_0x36cab4[_0xf149('0x4f')]===_0xf149('0x50')){var _0x176eb2=_0x41167c[_0xf149('0x27')]({'plain':!![]});var _0x53c9ac=_0xf149('0x51');return db[_0xf149('0x52')][_0xf149('0x4a')]({'where':{'name':_0x53c9ac,'userProfileId':_0x36cab4[_0xf149('0x53')]},'raw':!![]})['then'](function(_0x32889a){if(_0x32889a&&_0x32889a[_0xf149('0x54')]===0x0){return db[_0xf149('0x28')]['create']({'name':_0x176eb2[_0xf149('0x2c')],'resourceId':_0x176eb2['id'],'type':_0x32889a[_0xf149('0x2c')],'sectionId':_0x32889a['id']},{})['then'](function(){return _0x41167c;});}else{return _0x41167c;}})[_0xf149('0x4b')](function(_0x597449){logger[_0xf149('0x2a')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x597449);throw _0x597449;});}return _0x41167c;})[_0xf149('0x25')](respondWithResult(_0x59c0e0,0xc9))[_0xf149('0x4b')](handleError(_0x59c0e0,null));};exports[_0xf149('0x24')]=function(_0x246bec,_0x1dae6d){if(_0x246bec[_0xf149('0x4d')]['id']){delete _0x246bec[_0xf149('0x4d')]['id'];}return db[_0xf149('0x2f')]['find']({'where':{'id':_0x246bec[_0xf149('0x55')]['id']}})['then'](handleEntityNotFound(_0x1dae6d,null))[_0xf149('0x25')](saveUpdates(_0x246bec[_0xf149('0x4d')],null))[_0xf149('0x25')](respondWithResult(_0x1dae6d,null))['catch'](handleError(_0x1dae6d,null));};exports[_0xf149('0x26')]=function(_0x4086bd,_0x5bb6dd){return db['Schedule'][_0xf149('0x4a')]({'where':{'id':_0x4086bd['params']['id']}})[_0xf149('0x25')](handleEntityNotFound(_0x5bb6dd,null))[_0xf149('0x25')](removeEntity(_0x5bb6dd,null))[_0xf149('0x4b')](handleError(_0x5bb6dd,null));};exports[_0xf149('0x56')]=function(_0x2d2dcb,_0x55bcbc,_0x325d75){var _0x2e55ba=require(_0xf149('0x57'));return db['Schedule'][_0xf149('0x4a')]({'where':{'id':_0x2d2dcb[_0xf149('0x55')]['id']},'raw':!![]})['then'](handleEntityNotFound(_0x55bcbc,null))['then'](function(){_0x2e55ba['run'](_0x2d2dcb[_0xf149('0x55')]['id']);})[_0xf149('0x25')](respondWithStatusCode(_0x55bcbc,null))[_0xf149('0x4b')](handleError(_0x55bcbc,null));};