97197e2b3c9c588c30119d0cc41d5f819fb1537c
[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 _0xc01b=['rawAttributes','fieldName','type','model','map','keys','query','intersection','attributes','fields','length','nolimit','sort','where','filters','VIRTUAL','filter','merge','Schedule','rows','catch','show','includeAll','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','params','../../config/schedule','run','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','path','to-csv','ejs','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../mysqldb','status','end','json','undefined','limit','offset','count','set','Content-Range','apply','reject','update','then','destroy','get','Scheduler','UserProfileResource','sendStatus','error','stack','name','send','index'];(function(_0x25e7cb,_0x3579b7){var _0x375d92=function(_0xe8cf3b){while(--_0xe8cf3b){_0x25e7cb['push'](_0x25e7cb['shift']());}};_0x375d92(++_0x3579b7);}(_0xc01b,0xcc));var _0xbc01=function(_0x40a45a,_0x1c3551){_0x40a45a=_0x40a45a-0x0;var _0xdea9d8=_0xc01b[_0x40a45a];return _0xdea9d8;};'use strict';var emlformat=require(_0xbc01('0x0'));var rimraf=require(_0xbc01('0x1'));var zipdir=require(_0xbc01('0x2'));var jsonpatch=require(_0xbc01('0x3'));var rp=require(_0xbc01('0x4'));var moment=require('moment');var BPromise=require(_0xbc01('0x5'));var Mustache=require(_0xbc01('0x6'));var util=require('util');var path=require(_0xbc01('0x7'));var sox=require('sox');var csv=require(_0xbc01('0x8'));var ejs=require(_0xbc01('0x9'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xbc01('0xa'));var squel=require(_0xbc01('0xb'));var crypto=require('crypto');var jsforce=require(_0xbc01('0xc'));var deskjs=require(_0xbc01('0xd'));var toCsv=require(_0xbc01('0x8'));var querystring=require(_0xbc01('0xe'));var Papa=require(_0xbc01('0xf'));var Redis=require(_0xbc01('0x10'));var authService=require(_0xbc01('0x11'));var qs=require(_0xbc01('0x12'));var as=require(_0xbc01('0x13'));var hardwareService=require(_0xbc01('0x14'));var logger=require(_0xbc01('0x15'))(_0xbc01('0x16'));var utils=require('../../config/utils');var config=require(_0xbc01('0x17'));var licenseUtil=require('../../config/license/util');var db=require(_0xbc01('0x18'))['db'];function respondWithStatusCode(_0x354819,_0x21f49f){_0x21f49f=_0x21f49f||0xcc;return function(_0x57402b){if(_0x57402b){return _0x354819['sendStatus'](_0x21f49f);}return _0x354819[_0xbc01('0x19')](_0x21f49f)[_0xbc01('0x1a')]();};}function respondWithResult(_0x41e799,_0x40b6c9){_0x40b6c9=_0x40b6c9||0xc8;return function(_0x1fd785){if(_0x1fd785){return _0x41e799[_0xbc01('0x19')](_0x40b6c9)[_0xbc01('0x1b')](_0x1fd785);}};}function respondWithFilteredResult(_0x1b2470,_0x1d959b){return function(_0x5962a9){if(_0x5962a9){var _0x5349d9=typeof _0x1d959b['offset']===_0xbc01('0x1c')&&typeof _0x1d959b[_0xbc01('0x1d')]===_0xbc01('0x1c');var _0x284138=_0x5962a9['count'];var _0x2d0b37=_0x5349d9?0x0:_0x1d959b[_0xbc01('0x1e')];var _0xaaa9ba=_0x5349d9?_0x5962a9[_0xbc01('0x1f')]:_0x1d959b['offset']+_0x1d959b[_0xbc01('0x1d')];var _0x27c264;if(_0xaaa9ba>=_0x284138){_0xaaa9ba=_0x284138;_0x27c264=0xc8;}else{_0x27c264=0xce;}_0x1b2470[_0xbc01('0x19')](_0x27c264);return _0x1b2470[_0xbc01('0x20')](_0xbc01('0x21'),_0x2d0b37+'-'+_0xaaa9ba+'/'+_0x284138)[_0xbc01('0x1b')](_0x5962a9);}return null;};}function patchUpdates(_0x413e91){return function(_0x1a29b2){try{jsonpatch[_0xbc01('0x22')](_0x1a29b2,_0x413e91,!![]);}catch(_0x21ff03){return BPromise[_0xbc01('0x23')](_0x21ff03);}return _0x1a29b2['save']();};}function saveUpdates(_0x4b0bfa,_0x2cd369){return function(_0x5b6d57){if(_0x5b6d57){return _0x5b6d57[_0xbc01('0x24')](_0x4b0bfa)[_0xbc01('0x25')](function(_0x41d0f5){return _0x41d0f5;});}return null;};}function removeEntity(_0x56832b,_0x1c0732){return function(_0x7da9c4){if(_0x7da9c4){return _0x7da9c4[_0xbc01('0x26')]()[_0xbc01('0x25')](function(){var _0x1c2aa0=_0x7da9c4[_0xbc01('0x27')]({'plain':!![]});var _0x21034f=_0xbc01('0x28');return db[_0xbc01('0x29')][_0xbc01('0x26')]({'where':{'type':_0x21034f,'resourceId':_0x1c2aa0['id']}})['then'](function(){return _0x7da9c4;});})[_0xbc01('0x25')](function(){_0x56832b[_0xbc01('0x19')](0xcc)[_0xbc01('0x1a')]();});}};}function handleEntityNotFound(_0x5c2c73,_0x692e21){return function(_0x1786ad){if(!_0x1786ad){_0x5c2c73[_0xbc01('0x2a')](0x194);}return _0x1786ad;};}function handleError(_0x4e0138,_0x24bb9f){_0x24bb9f=_0x24bb9f||0x1f4;return function(_0xb02c3c){logger[_0xbc01('0x2b')](_0xb02c3c[_0xbc01('0x2c')]);if(_0xb02c3c['name']){delete _0xb02c3c[_0xbc01('0x2d')];}_0x4e0138[_0xbc01('0x19')](_0x24bb9f)[_0xbc01('0x2e')](_0xb02c3c);};}exports[_0xbc01('0x2f')]=function(_0x28f0e7,_0x47ed9f){var _0x35659d={},_0x2fe832={},_0x550612={'count':0x0,'rows':[]};var _0x5f373b=_['map'](db['Schedule'][_0xbc01('0x30')],function(_0x226f64){return{'name':_0x226f64[_0xbc01('0x31')],'type':_0x226f64[_0xbc01('0x32')]['key']};});_0x2fe832[_0xbc01('0x33')]=_[_0xbc01('0x34')](_0x5f373b,_0xbc01('0x2d'));_0x2fe832['query']=_[_0xbc01('0x35')](_0x28f0e7[_0xbc01('0x36')]);_0x2fe832['filters']=_[_0xbc01('0x37')](_0x2fe832[_0xbc01('0x33')],_0x2fe832[_0xbc01('0x36')]);_0x35659d[_0xbc01('0x38')]=_[_0xbc01('0x37')](_0x2fe832[_0xbc01('0x33')],qs['fields'](_0x28f0e7['query'][_0xbc01('0x39')]));_0x35659d[_0xbc01('0x38')]=_0x35659d[_0xbc01('0x38')][_0xbc01('0x3a')]?_0x35659d['attributes']:_0x2fe832[_0xbc01('0x33')];if(!_0x28f0e7[_0xbc01('0x36')]['hasOwnProperty'](_0xbc01('0x3b'))){_0x35659d[_0xbc01('0x1d')]=qs[_0xbc01('0x1d')](_0x28f0e7[_0xbc01('0x36')]['limit']);_0x35659d[_0xbc01('0x1e')]=qs[_0xbc01('0x1e')](_0x28f0e7[_0xbc01('0x36')][_0xbc01('0x1e')]);}_0x35659d['order']=qs[_0xbc01('0x3c')](_0x28f0e7[_0xbc01('0x36')]['sort']);_0x35659d[_0xbc01('0x3d')]=qs[_0xbc01('0x3e')](_['pick'](_0x28f0e7[_0xbc01('0x36')],_0x2fe832[_0xbc01('0x3e')]),_0x5f373b);if(_0x28f0e7[_0xbc01('0x36')]['filter']){_0x35659d[_0xbc01('0x3d')]=_['merge'](_0x35659d[_0xbc01('0x3d')],{'$or':_[_0xbc01('0x34')](_0x5f373b,function(_0x53fb55){if(_0x53fb55[_0xbc01('0x32')]!==_0xbc01('0x3f')){var _0x244c70={};_0x244c70[_0x53fb55['name']]={'$like':'%'+_0x28f0e7[_0xbc01('0x36')][_0xbc01('0x40')]+'%'};return _0x244c70;}})});}_0x35659d=_[_0xbc01('0x41')]({},_0x35659d,_0x28f0e7['options']);var _0x165945={'where':_0x35659d[_0xbc01('0x3d')]};return db[_0xbc01('0x42')][_0xbc01('0x1f')](_0x165945)[_0xbc01('0x25')](function(_0x289b0d){_0x550612[_0xbc01('0x1f')]=_0x289b0d;if(_0x28f0e7[_0xbc01('0x36')]['includeAll']){_0x35659d['include']=[{'all':!![]}];}return db['Schedule']['findAll'](_0x35659d);})[_0xbc01('0x25')](function(_0x15da42){_0x550612[_0xbc01('0x43')]=_0x15da42;return _0x550612;})[_0xbc01('0x25')](respondWithFilteredResult(_0x47ed9f,_0x35659d))[_0xbc01('0x44')](handleError(_0x47ed9f,null));};exports[_0xbc01('0x45')]=function(_0x4a7604,_0x15deaa){var _0x444928={'raw':![],'where':{'id':_0x4a7604['params']['id']}},_0x3366a2={};_0x3366a2[_0xbc01('0x33')]=_[_0xbc01('0x35')](db[_0xbc01('0x42')][_0xbc01('0x30')]);_0x3366a2['query']=_['keys'](_0x4a7604[_0xbc01('0x36')]);_0x3366a2[_0xbc01('0x3e')]=_[_0xbc01('0x37')](_0x3366a2[_0xbc01('0x33')],_0x3366a2[_0xbc01('0x36')]);_0x444928[_0xbc01('0x38')]=_[_0xbc01('0x37')](_0x3366a2[_0xbc01('0x33')],qs[_0xbc01('0x39')](_0x4a7604[_0xbc01('0x36')][_0xbc01('0x39')]));_0x444928['attributes']=_0x444928[_0xbc01('0x38')]['length']?_0x444928[_0xbc01('0x38')]:_0x3366a2[_0xbc01('0x33')];if(_0x4a7604[_0xbc01('0x36')][_0xbc01('0x46')]){_0x444928['include']=[{'all':!![]}];}_0x444928=_[_0xbc01('0x41')]({},_0x444928,_0x4a7604[_0xbc01('0x47')]);return db[_0xbc01('0x42')][_0xbc01('0x48')](_0x444928)['then'](handleEntityNotFound(_0x15deaa,null))['then'](respondWithResult(_0x15deaa,null))[_0xbc01('0x44')](handleError(_0x15deaa,null));};exports[_0xbc01('0x49')]=function(_0x3bf33e,_0x2b580f){return db[_0xbc01('0x42')]['create'](_0x3bf33e[_0xbc01('0x4a')],{})['then'](function(_0x2b3bcf){var _0xe139ff=_0x3bf33e[_0xbc01('0x4b')][_0xbc01('0x27')]({'plain':!![]});if(!_0xe139ff)throw new Error(_0xbc01('0x4c'));if(_0xe139ff[_0xbc01('0x4d')]===_0xbc01('0x4b')){var _0x5014ff=_0x2b3bcf[_0xbc01('0x27')]({'plain':!![]});var _0x4158ac=_0xbc01('0x28');return db[_0xbc01('0x4e')][_0xbc01('0x48')]({'where':{'name':_0x4158ac,'userProfileId':_0xe139ff[_0xbc01('0x4f')]},'raw':!![]})['then'](function(_0x1e8a2c){if(_0x1e8a2c&&_0x1e8a2c[_0xbc01('0x50')]===0x0){return db[_0xbc01('0x29')][_0xbc01('0x49')]({'name':_0x5014ff[_0xbc01('0x2d')],'resourceId':_0x5014ff['id'],'type':_0x1e8a2c[_0xbc01('0x2d')],'sectionId':_0x1e8a2c['id']},{})['then'](function(){return _0x2b3bcf;});}else{return _0x2b3bcf;}})['catch'](function(_0x5af608){logger[_0xbc01('0x2b')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x5af608);throw _0x5af608;});}return _0x2b3bcf;})['then'](respondWithResult(_0x2b580f,0xc9))[_0xbc01('0x44')](handleError(_0x2b580f,null));};exports['update']=function(_0x3d5e1c,_0x1c4d0b){if(_0x3d5e1c[_0xbc01('0x4a')]['id']){delete _0x3d5e1c[_0xbc01('0x4a')]['id'];}return db[_0xbc01('0x42')]['find']({'where':{'id':_0x3d5e1c[_0xbc01('0x51')]['id']}})[_0xbc01('0x25')](handleEntityNotFound(_0x1c4d0b,null))['then'](saveUpdates(_0x3d5e1c['body'],null))[_0xbc01('0x25')](respondWithResult(_0x1c4d0b,null))[_0xbc01('0x44')](handleError(_0x1c4d0b,null));};exports['destroy']=function(_0x4bf1d3,_0x5195f8){return db[_0xbc01('0x42')][_0xbc01('0x48')]({'where':{'id':_0x4bf1d3['params']['id']}})['then'](handleEntityNotFound(_0x5195f8,null))[_0xbc01('0x25')](removeEntity(_0x5195f8,null))[_0xbc01('0x44')](handleError(_0x5195f8,null));};exports['run']=function(_0x6e4652,_0x481fde,_0x142b0f){var _0x236f7d=require(_0xbc01('0x52'));return db[_0xbc01('0x42')][_0xbc01('0x48')]({'where':{'id':_0x6e4652[_0xbc01('0x51')]['id']},'raw':!![]})[_0xbc01('0x25')](handleEntityNotFound(_0x481fde,null))['then'](function(){_0x236f7d[_0xbc01('0x53')](_0x6e4652['params']['id']);})['then'](respondWithStatusCode(_0x481fde,null))[_0xbc01('0x44')](handleError(_0x481fde,null));};